From b8134798baa634e91ce9ce2a9bea7234ca46b7d1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 21 May 2017 01:10:38 +0200 Subject: [PATCH] More rewrites: - imported missing classes/interfaces - socket error 95 (operation not supported) is now handled and new exception thrown - EXCEPTION_INVALID_SOCKET is no longer located in BaseListener - updated core framework MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../classes/client/http/class_HttpClient.p | 2 +- .../classes/client/http/class_HttpClient.ph | 2 +- .../socket/class_SocketContainer.php | 416 +++++++++++++++++- .../class_PeerStateLookupDatabaseWrapper.php | 2 +- .../factories/socket/class_SocketFactory.php | 2 +- .../network/class_BaseRawDataHandler.php | 18 - .../ipv4/class_BaseIpV4ConnectionHelper.php | 157 ------- .../ipv4/tcp/class_TcpConnectionHelper.php | 6 +- .../classes/listener/class_BaseListener.php | 121 ----- .../listener/tcp/class_TcpListener.php | 12 +- .../listener/udp/class_UdpListener.php | 2 +- .../assembler/class_PackageAssembler.php | 3 +- .../classes/package/class_NetworkPackage.php | 4 +- .../pools/peer/class_DefaultPeerPool.php | 2 +- .../state/peer/class_PeerStateResolver.php | 2 +- application/hub/classes/states/peer/class_ | 4 +- .../connected/class_ConnectedPeerState.php | 5 +- .../class_ConnectionRefusedPeerState.php | 4 +- .../class_ConnectionTimedOutPeerState.php | 4 +- .../errors/class_NoRouteToHostPeerState.php | 4 +- ...lass_OperationAlreadyProgressPeerState.php | 4 +- .../peer/errors/class_ProblemPeerState.php | 4 +- .../class_TransportEndpointGonePeerState.php | 4 +- .../container/socket/class_StorableSocket.php | 96 +++- core | 2 +- 25 files changed, 514 insertions(+), 368 deletions(-) diff --git a/application/hub/classes/client/http/class_HttpClient.p b/application/hub/classes/client/http/class_HttpClient.p index 98e50d015..bfc535d33 100644 --- a/application/hub/classes/client/http/class_HttpClient.p +++ b/application/hub/classes/client/http/class_HttpClient.p @@ -19,5 +19,5 @@ // Check if there was an error else if ($errorNo > 0) { // Then throw again - throw new InvalidSocketException(array($helperInstance, $socketResource, $errorNo, $errorStr), BaseListener::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException(array($helperInstance, $socketResource, $errorNo, $errorStr), self::EXCEPTION_INVALID_SOCKET); } // END - if diff --git a/application/hub/classes/client/http/class_HttpClient.ph b/application/hub/classes/client/http/class_HttpClient.ph index 49990704c..cdd80f719 100644 --- a/application/hub/classes/client/http/class_HttpClient.ph +++ b/application/hub/classes/client/http/class_HttpClient.ph @@ -38,7 +38,7 @@ // Check if there was an error else if ($errorNo > 0) { // Then throw again - throw new InvalidSocketException(array($helperInstance, $socketResource, $errorNo, $errorStr), BaseListener::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException(array($helperInstance, $socketResource, $errorNo, $errorStr), self::EXCEPTION_INVALID_SOCKET); } // END - if // Prepare the GET request diff --git a/application/hub/classes/container/socket/class_SocketContainer.php b/application/hub/classes/container/socket/class_SocketContainer.php index a49a2c809..db6f3d0d3 100644 --- a/application/hub/classes/container/socket/class_SocketContainer.php +++ b/application/hub/classes/container/socket/class_SocketContainer.php @@ -3,16 +3,23 @@ namespace Hub\Container\Socket; // Import application-specific stuff +use Hub\Handler\RawData\BaseRawDataHandler; +use Hub\Helper\Connection\BaseConnectionHelper; use Hub\Information\ShareableInfo; use Hub\Listener\BaseListener; use Hub\Network\Package\NetworkPackage; // Import framework stuff use CoreFramework\Container\BaseContainer; +use CoreFramework\Factory\ObjectFactory; use CoreFramework\Listener\Listenable; use CoreFramework\Registry\Registerable; use CoreFramework\Socket\InvalidSocketException; use CoreFramework\Socket\NoSocketErrorDetectedException; +use CoreFramework\Socket\SocketBindingException; +use CoreFramework\Socket\SocketConnectionException; +use CoreFramework\Socket\SocketOperationException; +use CoreFramework\Socket\UnsupportedSocketErrorHandlerException; use CoreFramework\Visitor\Visitable; use CoreFramework\Visitor\Visitor; @@ -55,11 +62,11 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable * @param $socketResource A valid socket resource * @param $infoInstance An instance of a ShareableInfo class * @param $packageData Raw package data - * @return $containerInstance An instance of this Container class + * @return $socketInstance An instance of this Container class */ public static final function createSocketContainer ($socketResource, ShareableInfo $infoInstance = NULL, array $packageData = array()) { // Get a new instance - $containerInstance = new SocketContainer(); + $socketInstance = new SocketContainer(); // Remove unneeded entries unset($packageData[NetworkPackage::PACKAGE_DATA_CONTENT]); @@ -83,24 +90,24 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-CONTAINER: Setting listenerInstance=' . $listenerInstance->__toString() . ' ...'); // Set it here for later usage - $containerInstance->setListenerInstance($listenerInstance); + $socketInstance->setListenerInstance($listenerInstance); } elseif ($helperInstance instanceof ConnectionHelper) { // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-CONTAINER: Setting helperInstance=' . $helperInstance->__toString() . ' ...'); // Set it here for later usage - $containerInstance->setHelperInstance($helperInstance); + $socketInstance->setHelperInstance($helperInstance); } } // END - if // Set the resource ... - $containerInstance->setSocketResource($socketResource); + $socketInstance->setSocketResource($socketResource); // ... and package data - $containerInstance->setPackageData($packageData); + $socketInstance->setPackageData($packageData); // Return the prepared instance - return $containerInstance; + return $socketInstance; } /** @@ -233,6 +240,73 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getProtocolName()) . '-SOCKET: ' . $visitorInstance->__toString() . ' has visited ' . $this->__toString() . ' - EXIT!'); } + /** + * Some "getter" for peer address/port from stored socket + * + * @param $peerAddress Peer address being determined + * @param $peerPort Peer port being determined + * @return $result Result from asking for peer address + * @throws InvalidSocketException If socket is invalid + */ + public function getSocketPeerName (&$peerAddress, &$peerPort) { + // Should be valid socket + if (!$this->isValidSocket()) { + // Throw exception + throw new InvalidSocketException(array($this, $this->getSocketResource()), self::EXCEPTION_INVALID_SOCKET); + } // END - if + + // Init result + $result = FALSE; + + /* + * Which type of socket is set? Please note that UDP sockets doesn't + * support socket_getpeername(): + * http://de2.php.net/manual/en/function.socket-getpeername.php#35656 + */ + if ($this->getSocketType() == 'udp') { + // UDP is WIP: + $this->partialStub('UDP sockets are unfinished.'); + } else { + // Use socket_getpeername() + $result = socket_getpeername($this->getSocketResource(), $peerAddress, $peerPort); + } + + // Return result + return $result; + } + + /** + * Some "getter" for socket type of stored socket + * + * @return $socketType Socket type + * @throws InvalidSocketException If socket is invalid + */ + public function getSocketType () { + // Should be valid socket + if (!$this->isValidSocket()) { + // Throw exception + throw new InvalidSocketException(array($this, $this->getSocketResource()), self::EXCEPTION_INVALID_SOCKET); + } // END - if + + // Get socket protocol first + $socketProtocol = $this->getSocketProtocol(); + + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-CONTAINER: socketProtocol[%s]=%s', gettype($socketProtocol), $socketProtocol)); + + // Get socket option + $socketType = socket_get_option($this->getSocketResource(), getprotobyname($socketProtocol), SO_TYPE); + + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-CONTAINER: socketType[%s]=%s', gettype($socketType), $socketType)); + + // Is it valid? + if ($socketType === FALSE) { + // Something went wrong + $this->handleSocketError(__METHOD__, __LINE__, array('null', '0')); + } // END - if + } + /** * Validates stored stocket * @@ -260,7 +334,7 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable // Should be valid socket if (!$this->isValidSocket()) { // Throw exception - throw new InvalidSocketException(array($this, $this->getSocketResource()), BaseListener::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException(array($this, $this->getSocketResource()), self::EXCEPTION_INVALID_SOCKET); } // END - if // Get it from stored socket resource @@ -273,6 +347,29 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable return $errorCode; } + /** + * Getter for last socket error message + * + * @return $lastSocketErrorMessage Last socket error message + * @throws InvalidSocketException If socket is invalid + */ + public function getLastSocketErrorMessage () { + // Should be valid socket + if (!$this->isValidSocket()) { + // Throw exception + throw new InvalidSocketException(array($this, $this->getSocketResource()), self::EXCEPTION_INVALID_SOCKET); + } // END - if + + // Get it from stored socket resource + $socketResource = $this->getSocketResource(); + + // Get error code and message + $message = socket_strerror(socket_last_error($socketResource)); + + // Return it + return $message; + } + /** * Tries to bind the socket. * @@ -285,7 +382,7 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable // Should be valid socket if (!$this->isValidSocket()) { // Throw exception - throw new InvalidSocketException(array($this, $this->getSocketResource()), BaseListener::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException(array($this, $this->getSocketResource()), self::EXCEPTION_INVALID_SOCKET); } // END - if // Try to bind it to @@ -299,13 +396,13 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable * Tries to listen on the socket * * @return $result Result from listening on socket - * @throws InvalidSocketException If socket is valid + * @throws InvalidSocketException If stored socket is invalid */ public function listenOnSocket () { // Should be valid socket if (!$this->isValidSocket()) { // Throw exception - throw new InvalidSocketException(array($this, $this->getSocketResource()), BaseListener::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException(array($this, $this->getSocketResource()), self::EXCEPTION_INVALID_SOCKET); } // END - if // Try to listen on socket @@ -319,13 +416,13 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable * Tries to set non-blocking I/O on stored socket * * @return $result Result of changing to non-blocking I/O - * @throws InvalidSocketException If socket is valid + * @throws InvalidSocketException If stored socket is invalid */ public function enableSocketNonBlocking () { // Should be valid socket if (!$this->isValidSocket()) { // Throw exception - throw new InvalidSocketException(array($this, $this->getSocketResource()), BaseListener::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException(array($this, $this->getSocketResource()), self::EXCEPTION_INVALID_SOCKET); } // END - if // Try to set non-blocking I/O @@ -356,7 +453,7 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable // This method handles only socket resources if (!$this->isValidSocket()) { // No resource, abort here - throw new InvalidSocketException(array($this, $this->getSocketResource()), BaseListener::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException(array($this, $this->getSocketResource()), self::EXCEPTION_INVALID_SOCKET); } // END - if // Check socket array, 1st element is mostly IP address (or file name), 2nd is port number @@ -377,10 +474,299 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable $handlerName = $this->getSocketErrorHandlerFromCode($errorCode); // Call-back the error handler method - call_user_func_array(array($this, $handlerName), array($this, $socketData)); + call_user_func_array(array($this, $handlerName), array($socketData)); // Finally clear the error because it has been handled $this->clearLastSocketError(); } + /** + * Translates socket error codes into our own internal names which can be + * used for call-backs. + * + * @param $errorCode The error code from socket_last_error() to be translated + * @return $errorName The translated name (all lower-case, with underlines) + */ + public function translateSocketErrorCodeToName ($errorCode) { + // Nothing bad happened by default + $errorName = StorableSocket::SOCKET_CONNECTED; + + // Is the code a number, then we have to change it + switch ($errorCode) { + case 0: // Silently ignored, the socket is connected + break; + + case 11: // "Resource temporary unavailable" + $errorName = StorableSocket::SOCKET_ERROR_RESOURCE_UNAVAILABLE; + break; + + case 13: // "Permission denied" + $errorName = StorableSocket::SOCKET_ERROR_PERMISSION_DENIED; + break; + + case 32: // "Broken pipe" + $errorName = StorableSocket::SOCKET_ERROR_BROKEN_PIPE; + break; + + case 95: // "Operation not supported" + $errorName = StorableSocket::SOCKET_ERROR_OPERATION_NOT_SUPPORTED; + break; + + case 104: // "Connection reset by peer" + $errorName = StorableSocket::SOCKET_ERROR_CONNECTION_RESET_BY_PEER; + break; + + case 107: // "Transport end-point not connected" + case 134: // On some (?) systems for 'transport end-point not connected' + // @TODO On some systems it is 134, on some 107? + $errorName = StorableSocket::SOCKET_ERROR_TRANSPORT_ENDPOINT; + break; + + case 110: // "Connection timed out" + $errorName = StorableSocket::SOCKET_ERROR_CONNECTION_TIMED_OUT; + break; + + case 111: // "Connection refused" + $errorName = StorableSocket::SOCKET_ERROR_CONNECTION_REFUSED; + break; + + case 113: // "No route to host" + $errorName = StorableSocket::SOCKET_ERROR_NO_ROUTE_TO_HOST; + break; + + case 114: // "Operation already in progress" + $errorName = StorableSocket::SOCKET_ERROR_OPERATION_ALREADY_PROGRESS; + break; + + case 115: // "Operation now in progress" + $errorName = StorableSocket::SOCKET_ERROR_OPERATION_IN_PROGRESS; + break; + + default: // Everything else <> 0 + // Unhandled error code detected, so first debug it because we may want to handle it like the others + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-CONTAINER: Unsupported errorCode=%d,message=%s', $errorCode, socket_strerror($errorCode))); + + // Change it only in this class + $errorName = StorableSocket::SOCKET_ERROR_UNKNOWN; + break; + } // END - switch + + // Return translated name + return $errorName; + } + + /** + * Constructs a callable method name from given socket error code. If the + * method is not found, a generic one is used. + * + * @param $errorCode Error code from socket_last_error() + * @return $handlerName Call-back method name for the error handler + * @throws UnsupportedSocketErrorHandlerException If the error handler is not implemented + */ + protected function getSocketErrorHandlerFromCode ($errorCode) { + // Create a name from translated error code + $handlerName = 'handleSocketError' . self::convertToClassName($this->translateSocketErrorCodeToName($errorCode)); + + // Is the call-back method there? + if (!method_exists($this, $handlerName)) { + // Please implement this + throw new UnsupportedSocketErrorHandlerException(array($this, $handlerName, $errorCode), BaseConnectionHelper::EXCEPTION_UNSUPPORTED_ERROR_HANDLER); + } // END - if + + // Return it + return $handlerName; + } + + /************************************************************************ + * Socket error handler call-back methods * + ************************************************************************/ + + /** + * Handles socket error 'permission denied', but does not clear it for + * later debugging purposes. + * + * @param $socketData A valid socket data array (0 = IP/file name, 1 = port) + * @return void + * @throws SocketBindingException The socket could not be bind to + */ + protected function handleSocketErrorPermissionDenied (array $socketData) { + // Get socket error code for verification + $socketError = $this->getLastSocketError(); + + // Get error message + $errorMessage = $this->getLastSocketErrorMessage(); + + // Shutdown this socket + $this->shutdownSocket(); + + // Throw it again + throw new SocketBindingException(array($this, $socketData, $this->getSocketResource(), $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET); + } + + /** + * Handles socket error 'connection timed out', but does not clear it for + * later debugging purposes. + * + * @param $unlData A valid UNL data array + * @return void + * @throws SocketConnectionException The connection attempts fails with a time-out + */ + protected function handleSocketErrorConnectionTimedOut (array $unlData) { + // Get socket error code for verification + $socketError = $this->getLastSocketError(); + + // Get error message + $errorMessage = $this->getLastSocketErrorMessage(); + + // Shutdown this socket + $this->shutdownSocket(); + + // Throw it again + throw new SocketConnectionException(array($this, $this->getSocketResource(), $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET); + } + + /** + * Handles socket error 'resource temporary unavailable', but does not + * clear it for later debugging purposes. + * + * @param $unlData A valid UNL data array + * @return void + * @throws SocketConnectionException The connection attempts fails with a time-out + */ + protected function handleSocketErrorResourceUnavailable (array $unlData) { + // Get socket error code for verification + $socketError = $this->getLastSocketError(); + + // Get error message + $errorMessage = $this->getLastSocketErrorMessage(); + + // Shutdown this socket + $this->shutdownSocket(); + + // Throw it again + throw new SocketConnectionException(array($this, $this->getSocketResource(), $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET); + } + + /** + * Handles socket error 'connection refused', but does not clear it for + * later debugging purposes. + * + * @param $unlData A valid UNL data array + * @return void + * @throws SocketConnectionException The connection attempts fails with a time-out + */ + protected function handleSocketErrorConnectionRefused (array $unlData) { + // Get socket error code for verification + $socketError = $this->getLastSocketError(); + + // Get error message + $errorMessage = $this->getLastSocketErrorMessage(); + + // Shutdown this socket + $this->shutdownSocket(); + + // Throw it again + throw new SocketConnectionException(array($this, $this->getSocketResource(), $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET); + } + + /** + * Handles socket error 'no route to host', but does not clear it for later + * debugging purposes. + * + * @param $unlData A valid UNL data array + * @return void + * @throws SocketConnectionException The connection attempts fails with a time-out + */ + protected function handleSocketErrorNoRouteToHost (array $unlData) { + // Get socket error code for verification + $socketError = $this->getLastSocketError(); + + // Get error message + $errorMessage = $this->getLastSocketErrorMessage(); + + // Shutdown this socket + $this->shutdownSocket(); + + // Throw it again + throw new SocketConnectionException(array($this, $this->getSocketResource(), $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET); + } + + /** + * Handles socket error 'operation already in progress' which happens in + * method connectToPeerByUnlData() on timed out connection + * attempts. + * + * @param $unlData A valid UNL data array + * @return void + * @throws SocketConnectionException The connection attempts fails with a time-out + */ + protected function handleSocketErrorOperationAlreadyProgress (array $unlData) { + // Get socket error code for verification + $socketError = $this->getLastSocketError(); + + // Get error message + $errorMessage = $this->getLastSocketErrorMessage(); + + // Half-shutdown this socket (see there for difference to shutdownSocket()) + $this->halfShutdownSocket(); + + // Throw it again + throw new SocketConnectionException(array($this, $this->getSocketResource(), $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET); + } + + /** + * Handles socket error 'connection reset by peer', but does not clear it for + * later debugging purposes. + * + * @param $unlData A valid UNL data array + * @return void + * @throws SocketConnectionException The connection attempts fails with a time-out + */ + protected function handleSocketErrorConnectionResetByPeer (array $unlData) { + // Get socket error code for verification + $socketError = $this->getLastSocketError(); + + // Get error message + $errorMessage = $this->getLastSocketErrorMessage(); + + // Shutdown this socket + $this->shutdownSocket(); + + // Throw it again + throw new SocketConnectionException(array($this, $this->getSocketResource(), $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET); + } + + /** + * Handles socket error 'operation not supported', but does not clear it for + * later debugging purposes. + * + * @param $unlData A valid UNL data array + * @return void + * @throws SocketOperationException The connection attempts fails with a time-out + */ + protected function handleSocketErrorOperationNotSupported (array $unlData) { + // Get socket error code for verification + $socketError = $this->getLastSocketError(); + + // Get error message + $errorMessage = $this->getLastSocketErrorMessage(); + + // Shutdown this socket + $this->shutdownSocket(); + + // Throw it again + throw new SocketOperationException(array($this, $this->getSocketResource(), $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET); + } + + /** + * Handles socket "error" 'operation now in progress' which can be safely + * passed on with non-blocking connections. + * + * @param $unlData A valid UNL data array + * @return void + */ + protected function handleSocketErrorOperationInProgress (array $unlData) { + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: Operation is now in progress, this is usual for non-blocking connections and is no bug.'); + } + } diff --git a/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php b/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php index 43be54620..594416bc4 100644 --- a/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php +++ b/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php @@ -240,7 +240,7 @@ class PeerStateLookupDatabaseWrapper extends BaseDatabaseWrapper implements Look $lastError = socket_last_error($socketResource); // Doesn't work! - throw new InvalidSocketException(array($this, $socketResource, $lastError, socket_strerror($lastError)), SocketHandler::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException(array($this, $socketResource, $lastError, socket_strerror($lastError)), self::EXCEPTION_INVALID_SOCKET); } // END - if // Debug message diff --git a/application/hub/classes/factories/socket/class_SocketFactory.php b/application/hub/classes/factories/socket/class_SocketFactory.php index e20805b6a..d62d7a55a 100644 --- a/application/hub/classes/factories/socket/class_SocketFactory.php +++ b/application/hub/classes/factories/socket/class_SocketFactory.php @@ -138,7 +138,7 @@ class SocketFactory extends ObjectFactory { // Is the socket resource valid? if (!$socketInstance->isValidSocket()) { // Something bad happened - throw new InvalidSocketException(array($listenerInstance, $socketInstance->getSocketResource()), BaseListener::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException(array($listenerInstance, $socketInstance->getSocketResource()), self::EXCEPTION_INVALID_SOCKET); } // END - if // Get socket error code for verification diff --git a/application/hub/classes/handler/raw_data/network/class_BaseRawDataHandler.php b/application/hub/classes/handler/raw_data/network/class_BaseRawDataHandler.php index 2cdccd865..2544d5289 100644 --- a/application/hub/classes/handler/raw_data/network/class_BaseRawDataHandler.php +++ b/application/hub/classes/handler/raw_data/network/class_BaseRawDataHandler.php @@ -32,24 +32,6 @@ use CoreFramework\Handler\BaseHandler; * along with this program. If not, see . */ class BaseRawDataHandler extends BaseHandler { - // Error codes: - // - Socket raw data stream errors - const SOCKET_ERROR_UNKNOWN = 'unknown_error'; // Unknown error (should not happen) - const SOCKET_ERROR_TRANSPORT_ENDPOINT = 'transport_endpoint'; // Transport endpoint has closed - const SOCKET_ERROR_INVALID_BASE64_MODULO = 'base64_modulo'; // Length is not modulo 4 - const SOCKET_ERROR_INVALID_BASE64_MESSAGE = 'base64_message'; // Raw data is not Base64-encoded - const SOCKET_ERROR_UNHANDLED = 'unhandled_package'; // Unhandled raw data (not bad) - const SOCKET_ERROR_CONNECTION_REFUSED = 'connection_refused'; // The name says it: connection refused - const SOCKET_ERROR_CONNECTION_TIMED_OUT = 'connection_timed_out'; // The name says it: connection attempt has timed-out - const SOCKET_ERROR_OPERATION_IN_PROGRESS = 'operation_in_progress'; // 'Operation now in progress' - const SOCKET_ERROR_OPERATION_ALREADY_PROGRESS = 'operation_already_progress'; // 'Operation already in progress' - const SOCKET_ERROR_RESOURCE_UNAVAILABLE = 'resource_unavailable'; // 'Resource temporary unavailable' - const SOCKET_ERROR_NO_ROUTE_TO_HOST = 'no_route_to_host'; // The name says it: no route to host - const SOCKET_ERROR_CONNECTION_RESET_BY_PEER = 'connection_reset_by_peer'; // Connection reset by peer - const SOCKET_ERROR_BROKEN_PIPE = 'broken_pipe'; // Broken pipe - const SOCKET_ERROR_PERMISSION_DENIED = 'permission_denied'; // Permission denied - const SOCKET_CONNECTED = 'connected'; // Nothing errorous happens, socket is connected - // - Package errors const PACKAGE_ERROR_INVALID_DATA = 'invalid_data'; // Invalid data in package found const PACKAGE_ERROR_INCOMPLETE_DATA = 'incomplete_data'; // Incomplete data sent (e.g. field is missing) diff --git a/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php b/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php index a12c47cfa..b9799d4dc 100644 --- a/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php +++ b/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php @@ -202,161 +202,4 @@ class BaseIpV4ConnectionHelper extends BaseConnectionHelper { $this->initSocketInstance(); } - // ************************************************************************ - // Socket error handler call-back methods - // ************************************************************************ - - /** - * Handles socket error 'connection timed out', but does not clear it for - * later debugging purposes. - * - * @param $socketInstance An instance of a StorableSocket class - * @param $unlData A valid UNL data array - * @return void - * @throws SocketConnectionException The connection attempts fails with a time-out - */ - protected function socketErrorConnectionTimedOutHandler (StorableSocket $socketInstance, array $unlData) { - // Get socket error code for verification - $socketError = $socketInstance->getLastSocketError(); - - // Get error message - $errorMessage = $socketInstance->getLastSocketErrorMessage(); - - // Shutdown this socket - $socketInstance->shutdownSocket(); - - // Throw it again - throw new SocketConnectionException(array($this, $socketInstance, $socketError, $errorMessage), SocketHandler::EXCEPTION_INVALID_SOCKET); - } - - /** - * Handles socket error 'resource temporary unavailable', but does not - * clear it for later debugging purposes. - * - * @param $socketInstance An instance of a StorableSocket class - * @param $unlData A valid UNL data array - * @return void - * @throws SocketConnectionException The connection attempts fails with a time-out - */ - protected function socketErrorResourceUnavailableHandler (StorableSocket $socketInstance, array $unlData) { - // Get socket error code for verification - $socketError = $socketInstance->getLastSocketError(); - - // Get error message - $errorMessage = $socketInstance->getLastSocketErrorMessage(); - - // Shutdown this socket - $socketInstance->shutdownSocket(); - - // Throw it again - throw new SocketConnectionException(array($this, $socketInstance, $socketError, $errorMessage), SocketHandler::EXCEPTION_INVALID_SOCKET); - } - - /** - * Handles socket error 'connection refused', but does not clear it for - * later debugging purposes. - * - * @param $socketInstance An instance of a StorableSocket class - * @param $unlData A valid UNL data array - * @return void - * @throws SocketConnectionException The connection attempts fails with a time-out - */ - protected function socketErrorConnectionRefusedHandler (StorableSocket $socketInstance, array $unlData) { - // Get socket error code for verification - $socketError = $socketInstance->getLastSocketError(); - - // Get error message - $errorMessage = $socketInstance->getLastSocketErrorMessage(); - - // Shutdown this socket - $socketInstance->shutdownSocket(); - - // Throw it again - throw new SocketConnectionException(array($this, $socketInstance, $socketError, $errorMessage), SocketHandler::EXCEPTION_INVALID_SOCKET); - } - - /** - * Handles socket error 'no route to host', but does not clear it for later - * debugging purposes. - * - * @param $socketInstance An instance of a StorableSocket class - * @param $unlData A valid UNL data array - * @return void - * @throws SocketConnectionException The connection attempts fails with a time-out - */ - protected function socketErrorNoRouteToHostHandler (StorableSocket $socketInstance, array $unlData) { - // Get socket error code for verification - $socketError = $socketInstance->getLastSocketError(); - - // Get error message - $errorMessage = $socketInstance->getLastSocketErrorMessage(); - - // Shutdown this socket - $socketInstance->shutdownSocket(); - - // Throw it again - throw new SocketConnectionException(array($this, $socketInstance, $socketError, $errorMessage), SocketHandler::EXCEPTION_INVALID_SOCKET); - } - - /** - * Handles socket error 'operation already in progress' which happens in - * method connectToPeerByUnlData() on timed out connection - * attempts. - * - * @param $socketInstance An instance of a StorableSocket class - * @param $unlData A valid UNL data array - * @return void - * @throws SocketConnectionException The connection attempts fails with a time-out - */ - protected function socketErrorOperationAlreadyProgressHandler (StorableSocket $socketInstance, array $unlData) { - // Get socket error code for verification - $socketError = $socketInstance->getLastSocketError(); - - // Get error message - $errorMessage = $socketInstance->getLastSocketErrorMessage(); - - // Half-shutdown this socket (see there for difference to shutdownSocket()) - $socketInstance->halfShutdownSocket(); - - // Throw it again - throw new SocketConnectionException(array($this, $socketInstance, $socketError, $errorMessage), SocketHandler::EXCEPTION_INVALID_SOCKET); - } - - /** - * Handles socket error 'connection reset by peer', but does not clear it for - * later debugging purposes. - * - * @param $socketInstance An instance of a StorableSocket class - * @param $unlData A valid UNL data array - * @return void - * @throws SocketConnectionException The connection attempts fails with a time-out - */ - protected function socketErrorConnectionResetByPeerHandler (StorableSocket $socketInstance, array $unlData) { - // Get socket error code for verification - $socketError = $socketInstance->getLastSocketError(); - - // Get error message - $errorMessage = $socketInstance->getLastSocketErrorMessage(); - - // Shutdown this socket - $socketInstance->shutdownSocket(); - - // Throw it again - throw new SocketConnectionException(array($this, $socketInstance, $socketError, $errorMessage), SocketHandler::EXCEPTION_INVALID_SOCKET); - } - - /** - * Handles socket "error" 'operation now in progress' which can be safely - * passed on with non-blocking connections. - * - * @param $socketInstance An instance of a StorableSocket class - * @param $unlData A valid UNL data array - * @return void - */ - protected function socketErrorOperationInProgressHandler (StorableSocket $socketInstance, array $unlData) { - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: Operation is now in progress, this is usual for non-blocking connections and is no bug.'); - } } - -// [EOF] -?> diff --git a/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php b/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php index 6859446f1..b6c086708 100644 --- a/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php +++ b/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php @@ -71,7 +71,7 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection * possible here because that method would throw an * InvalidSocketException back. */ - throw new SocketCreationException(array($helperInstance, gettype($socketResource)), SocketHandler::EXCEPTION_SOCKET_CREATION_FAILED); + throw new SocketCreationException(array($helperInstance, gettype($socketResource)), StorableSocket::EXCEPTION_SOCKET_CREATION_FAILED); } // END - if // Get socket error code for verification @@ -83,7 +83,7 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection $helperInstance->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0')); // Then throw again - throw new SocketCreationException(array($helperInstance, gettype($socketResource), $socketError, socket_strerror($socketError)), SocketHandler::EXCEPTION_SOCKET_CREATION_FAILED); + throw new SocketCreationException(array($helperInstance, gettype($socketResource), $socketError, socket_strerror($socketError)), StorableSocket::EXCEPTION_SOCKET_CREATION_FAILED); } // END - if // Debug message @@ -173,7 +173,7 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection // Could not shutdown socket, this is fine if e.g. the other side is not connected, so analyse it if (socket_last_error($this->getSocketResource()) != 107) { // Something bad happened while we shutdown a socket - throw new SocketShutdownException($this, SocketHandler::EXCEPTION_INVALID_SOCKET); + throw new SocketShutdownException($this, self::EXCEPTION_INVALID_SOCKET); } // END - if } // END - if diff --git a/application/hub/classes/listener/class_BaseListener.php b/application/hub/classes/listener/class_BaseListener.php index b1039c8d4..625f0806e 100644 --- a/application/hub/classes/listener/class_BaseListener.php +++ b/application/hub/classes/listener/class_BaseListener.php @@ -6,7 +6,6 @@ namespace Hub\Listener; use Hub\Container\Socket\StorableSocket; use Hub\Factory\Information\Connection\ConnectionInfoFactory; use Hub\Generic\BaseHubSystem; -use Hub\Handler\RawData\BaseRawDataHandler; use Hub\Helper\Connection\BaseConnectionHelper; use Hub\Network\Package\NetworkPackage; use Hub\Pool\Peer\PoolablePeer; @@ -282,99 +281,6 @@ class BaseListener extends BaseHubSystem implements Visitable { throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); } - /** - * Constructs a callable method name from given socket error code. If the - * method is not found, a generic one is used. - * - * @param $errorCode Error code from socket_last_error() - * @return $handlerName Call-back method name for the error handler - * @throws UnsupportedSocketErrorHandlerException If the error handler is not implemented - */ - protected function getSocketErrorHandlerFromCode ($errorCode) { - // Create a name from translated error code - $handlerName = 'socketError' . self::convertToClassName($this->translateSocketErrorCodeToName($errorCode)) . 'Handler'; - - // Is the call-back method there? - if (!method_exists($this, $handlerName)) { - // Please implement this - throw new UnsupportedSocketErrorHandlerException(array($this, $handlerName, $errorCode), BaseConnectionHelper::EXCEPTION_UNSUPPORTED_ERROR_HANDLER); - } // END - if - - // Return it - return $handlerName; - } - - /** - * Translates socket error codes into our own internal names which can be - * used for call-backs. - * - * @param $errorCode The error code from socket_last_error() to be translated - * @return $errorName The translated name (all lower-case, with underlines) - */ - public function translateSocketErrorCodeToName ($errorCode) { - // Nothing bad happened by default - $errorName = BaseRawDataHandler::SOCKET_CONNECTED; - - // Is the code a number, then we have to change it - switch ($errorCode) { - case 0: // Silently ignored, the socket is connected - break; - - case 11: // "Resource temporary unavailable" - $errorName = BaseRawDataHandler::SOCKET_ERROR_RESOURCE_UNAVAILABLE; - break; - - case 13: // "Permission denied" - $errorName = BaseRawDataHandler::SOCKET_ERROR_PERMISSION_DENIED; - break; - - case 32: // "Broken pipe" - $errorName = BaseRawDataHandler::SOCKET_ERROR_BROKEN_PIPE; - break; - - case 104: // "Connection reset by peer" - $errorName = BaseRawDataHandler::SOCKET_ERROR_CONNECTION_RESET_BY_PEER; - break; - - case 107: // "Transport end-point not connected" - case 134: // On some (?) systems for 'transport end-point not connected' - // @TODO On some systems it is 134, on some 107? - $errorName = BaseRawDataHandler::SOCKET_ERROR_TRANSPORT_ENDPOINT; - break; - - case 110: // "Connection timed out" - $errorName = BaseRawDataHandler::SOCKET_ERROR_CONNECTION_TIMED_OUT; - break; - - case 111: // "Connection refused" - $errorName = BaseRawDataHandler::SOCKET_ERROR_CONNECTION_REFUSED; - break; - - case 113: // "No route to host" - $errorName = BaseRawDataHandler::SOCKET_ERROR_NO_ROUTE_TO_HOST; - break; - - case 114: // "Operation already in progress" - $errorName = BaseRawDataHandler::SOCKET_ERROR_OPERATION_ALREADY_PROGRESS; - break; - - case 115: // "Operation now in progress" - $errorName = BaseRawDataHandler::SOCKET_ERROR_OPERATION_IN_PROGRESS; - break; - - default: // Everything else <> 0 - // Unhandled error code detected, so first debug it because we may want to handle it like the others - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-HUB UNKNOWN ERROR CODE = ' . $errorCode . ', MESSAGE = ' . socket_strerror($errorCode)); - - // Change it only in this class - $errorName = BaseRawDataHandler::SOCKET_ERROR_UNKNOWN; - break; - } - - // Return translated name - return $errorName; - } - /** * "Listens" for incoming network packages * @@ -515,31 +421,4 @@ class BaseListener extends BaseHubSystem implements Visitable { $this->getIteratorInstance()->next(); } - // ************************************************************************ - // Socket error handler call-back methods - // ************************************************************************ - - /** - * Handles socket error 'permission denied', but does not clear it for - * later debugging purposes. - * - * @param $socketInstance An instance of a StorableSocket class - * @param $socketData A valid socket data array (0 = IP/file name, 1 = port) - * @return void - * @throws SocketBindingException The socket could not be bind to - */ - protected function socketErrorPermissionDeniedHandler (StorableSocket $socketInstance, array $socketData) { - // Get socket error code for verification - $socketError = $socketInstance->getLastSocketError(); - - // Get error message - $errorMessage = $socketInstance->getLastSocketErrorMessage(); - - // Shutdown this socket - $socketInstance->shutdownSocket(); - - // Throw it again - throw new SocketBindingException(array($this, $socketData, $socketInstance, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET); - } - } diff --git a/application/hub/classes/listener/tcp/class_TcpListener.php b/application/hub/classes/listener/tcp/class_TcpListener.php index 1a387388c..83b003087 100644 --- a/application/hub/classes/listener/tcp/class_TcpListener.php +++ b/application/hub/classes/listener/tcp/class_TcpListener.php @@ -74,7 +74,7 @@ class TcpListener extends BaseListener implements Listenable { // Is the socket resource valid? if (!is_resource($mainSocket)) { // Something bad happened - throw new InvalidSocketException(array($this, $mainSocket), SocketHandler::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException(array($this, $mainSocket), self::EXCEPTION_INVALID_SOCKET); } // END - if // Get socket error code for verification @@ -86,7 +86,7 @@ class TcpListener extends BaseListener implements Listenable { $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0')); /* // Then throw again - throw new InvalidSocketException(array($this, $mainSocket, $socketError, socket_strerror($socketError)), SocketHandler::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException(array($this, $mainSocket, $socketError, socket_strerror($socketError)), self::EXCEPTION_INVALID_SOCKET); */ } // END - if @@ -105,7 +105,7 @@ class TcpListener extends BaseListener implements Listenable { $this->shutdownSocket($mainSocket); // And throw again - throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), SocketHandler::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET); */ } // END - if @@ -129,7 +129,7 @@ class TcpListener extends BaseListener implements Listenable { $this->shutdownSocket($mainSocket); // And throw again - throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), SocketHandler::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET); */ } // END - if @@ -149,7 +149,7 @@ class TcpListener extends BaseListener implements Listenable { $this->shutdownSocket($mainSocket); // And throw again - throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), SocketHandler::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET); */ } // END - if @@ -169,7 +169,7 @@ class TcpListener extends BaseListener implements Listenable { $this->shutdownSocket($mainSocket); // And throw again - throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), SocketHandler::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET); */ } // END - if diff --git a/application/hub/classes/listener/udp/class_UdpListener.php b/application/hub/classes/listener/udp/class_UdpListener.php index c06401292..697f0c82d 100644 --- a/application/hub/classes/listener/udp/class_UdpListener.php +++ b/application/hub/classes/listener/udp/class_UdpListener.php @@ -72,7 +72,7 @@ class UdpListener extends BaseListener implements Listenable { // Is the socket a valid resource or do we have any error? if (!is_resource($mainSocket)) { // Then throw an InvalidSocketException - throw new InvalidSocketException(array($this, $mainSocket), SocketHandler::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException(array($this, $mainSocket), self::EXCEPTION_INVALID_SOCKET); } // END - if /* diff --git a/application/hub/classes/package/assembler/class_PackageAssembler.php b/application/hub/classes/package/assembler/class_PackageAssembler.php index 4c50578a9..3ba238194 100644 --- a/application/hub/classes/package/assembler/class_PackageAssembler.php +++ b/application/hub/classes/package/assembler/class_PackageAssembler.php @@ -3,6 +3,7 @@ namespace Hub\Network\Package\Assembler; // Import hub-specific stuff +use Hub\Container\Socket\StorableSocket; use Hub\Generic\BaseHubSystem; use Hub\Handler\RawData\BaseRawDataHandler; use Hub\Network\Package\NetworkPackage; @@ -267,7 +268,7 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable, // Init fake array $packageContent = array( BaseRawDataHandler::PACKAGE_RAW_DATA => $this->getInputStreamInstance()->streamData($this->pendingData), - BaseRawDataHandler::PACKAGE_ERROR_CODE => BaseRawDataHandler::SOCKET_ERROR_UNHANDLED + BaseRawDataHandler::PACKAGE_ERROR_CODE => StorableSocket::SOCKET_ERROR_UNHANDLED ); /* diff --git a/application/hub/classes/package/class_NetworkPackage.php b/application/hub/classes/package/class_NetworkPackage.php index aa01e2139..1e35c4ada 100644 --- a/application/hub/classes/package/class_NetworkPackage.php +++ b/application/hub/classes/package/class_NetworkPackage.php @@ -1128,8 +1128,8 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R * Also make sure the error code is SOCKET_ERROR_UNHANDLED because we * only want to handle unhandled packages here. */ - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: errorCode=' . $decodedData[BaseRawDataHandler::PACKAGE_ERROR_CODE] . '(' . BaseRawDataHandler::SOCKET_ERROR_UNHANDLED . ')'); - assert($decodedData[BaseRawDataHandler::PACKAGE_ERROR_CODE] == BaseRawDataHandler::SOCKET_ERROR_UNHANDLED); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: errorCode=' . $decodedData[BaseRawDataHandler::PACKAGE_ERROR_CODE] . '(' . StorableSocket::SOCKET_ERROR_UNHANDLED . ')'); + assert($decodedData[BaseRawDataHandler::PACKAGE_ERROR_CODE] == StorableSocket::SOCKET_ERROR_UNHANDLED); // Remove the last chunk SEPARATOR (because there is no need for it) if (substr($decodedData[BaseRawDataHandler::PACKAGE_RAW_DATA], -1, 1) == PackageFragmenter::CHUNK_SEPARATOR) { diff --git a/application/hub/classes/pools/peer/class_DefaultPeerPool.php b/application/hub/classes/pools/peer/class_DefaultPeerPool.php index a6bb89d23..d8272757f 100644 --- a/application/hub/classes/pools/peer/class_DefaultPeerPool.php +++ b/application/hub/classes/pools/peer/class_DefaultPeerPool.php @@ -77,7 +77,7 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { // Is it a valid resource? if (!$socketInstance->isValidSocket()) { // Throw an exception - throw new InvalidSocketException(array($this, $socketInstance->getSocketResource()), SocketHandler::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException(array($this, $socketInstance->getSocketResource()), self::EXCEPTION_INVALID_SOCKET); } // END - if // Get error code diff --git a/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php b/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php index 811224abd..315afcd97 100644 --- a/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php +++ b/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php @@ -84,7 +84,7 @@ class PeerStateResolver extends BaseStateResolver implements StateResolver { // Still no socket resource? if (!$socketInstance->isValidSocket())) { // Then abort here with an exception (may happen after socket_shutdown()) - throw new InvalidSocketException(array($helperInstance, $socketInstance->getSocketResource()), SocketHandler::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException(array($helperInstance, $socketInstance->getSocketResource()), self::EXCEPTION_INVALID_SOCKET); } // END - if } // END - if diff --git a/application/hub/classes/states/peer/class_ b/application/hub/classes/states/peer/class_ index 1dbda959e..e9e2ac927 100644 --- a/application/hub/classes/states/peer/class_ +++ b/application/hub/classes/states/peer/class_ @@ -3,7 +3,7 @@ namespace Hub\State\Peer; // Import application-specific stuff -use Hub\Handler\RawData\BaseRawDataHandler; +use Hub\Container\Socket\StorableSocket; // Import framework stuff use CoreFramework\State\Stateable; @@ -41,7 +41,7 @@ class ???PeerState extends BasePeerState implements PeerStateable { parent::__construct(__CLASS__); // Set state name - $this->setStateName(BaseRawDataHandler::SOCKET_ERROR_!!!); + $this->setStateName(StorableSocket::SOCKET_ERROR_!!!); } /** diff --git a/application/hub/classes/states/peer/connected/class_ConnectedPeerState.php b/application/hub/classes/states/peer/connected/class_ConnectedPeerState.php index 1fc6279f5..9b71fe1e3 100644 --- a/application/hub/classes/states/peer/connected/class_ConnectedPeerState.php +++ b/application/hub/classes/states/peer/connected/class_ConnectedPeerState.php @@ -2,6 +2,9 @@ // Own namespace namespace Hub\State\Peer; +// Import application-specific stuff +use Hub\Container\Socket\StorableSocket; + // Import framework stuff use CoreFramework\State\Stateable; @@ -38,7 +41,7 @@ class ConnectedPeerState extends BasePeerState implements PeerStateable { parent::__construct(__CLASS__); // Set state name - $this->setStateName(BaseRawDataHandler::SOCKET_CONNECTED); + $this->setStateName(StorableSocket::SOCKET_CONNECTED); } /** diff --git a/application/hub/classes/states/peer/errors/class_ConnectionRefusedPeerState.php b/application/hub/classes/states/peer/errors/class_ConnectionRefusedPeerState.php index 7f24ae558..f12869ae6 100644 --- a/application/hub/classes/states/peer/errors/class_ConnectionRefusedPeerState.php +++ b/application/hub/classes/states/peer/errors/class_ConnectionRefusedPeerState.php @@ -3,7 +3,7 @@ namespace Hub\State\Peer; // Import application-specific stuff -use Hub\Handler\RawData\BaseRawDataHandler; +use Hub\Container\Socket\StorableSocket; // Import framework stuff use CoreFramework\State\Stateable; @@ -41,7 +41,7 @@ class ConnectionRefusedPeerState extends BasePeerState implements PeerStateable parent::__construct(__CLASS__); // Set state name - $this->setStateName(BaseRawDataHandler::SOCKET_ERROR_CONNECTION_REFUSED); + $this->setStateName(StorableSocket::SOCKET_ERROR_CONNECTION_REFUSED); } /** diff --git a/application/hub/classes/states/peer/errors/class_ConnectionTimedOutPeerState.php b/application/hub/classes/states/peer/errors/class_ConnectionTimedOutPeerState.php index c295b2b33..7313e9390 100644 --- a/application/hub/classes/states/peer/errors/class_ConnectionTimedOutPeerState.php +++ b/application/hub/classes/states/peer/errors/class_ConnectionTimedOutPeerState.php @@ -3,7 +3,7 @@ namespace Hub\State\Peer; // Import application-specific stuff -use Hub\Handler\RawData\BaseRawDataHandler; +use Hub\Container\Socket\StorableSocket; // Import framework stuff use CoreFramework\State\Stateable; @@ -41,7 +41,7 @@ class ConnectionTimedOutPeerState extends BasePeerState implements PeerStateable parent::__construct(__CLASS__); // Set state name - $this->setStateName(BaseRawDataHandler::SOCKET_ERROR_CONNECTION_TIMED_OUT); + $this->setStateName(StorableSocket::SOCKET_ERROR_CONNECTION_TIMED_OUT); } /** diff --git a/application/hub/classes/states/peer/errors/class_NoRouteToHostPeerState.php b/application/hub/classes/states/peer/errors/class_NoRouteToHostPeerState.php index 29785850f..edaa29f83 100644 --- a/application/hub/classes/states/peer/errors/class_NoRouteToHostPeerState.php +++ b/application/hub/classes/states/peer/errors/class_NoRouteToHostPeerState.php @@ -3,7 +3,7 @@ namespace Hub\State\Peer; // Import application-specific stuff -use Hub\Handler\RawData\BaseRawDataHandler; +use Hub\Container\Socket\StorableSocket; // Import framework stuff use CoreFramework\State\Stateable; @@ -41,7 +41,7 @@ class NoRouteToHostPeerState extends BasePeerState implements PeerStateable { parent::__construct(__CLASS__); // Set state name - $this->setStateName(BaseRawDataHandler::SOCKET_ERROR_NO_ROUTE_TO_HOST); + $this->setStateName(StorableSocket::SOCKET_ERROR_NO_ROUTE_TO_HOST); } /** diff --git a/application/hub/classes/states/peer/errors/class_OperationAlreadyProgressPeerState.php b/application/hub/classes/states/peer/errors/class_OperationAlreadyProgressPeerState.php index c7d7df8b8..99d39acc9 100644 --- a/application/hub/classes/states/peer/errors/class_OperationAlreadyProgressPeerState.php +++ b/application/hub/classes/states/peer/errors/class_OperationAlreadyProgressPeerState.php @@ -3,7 +3,7 @@ namespace Hub\State\Peer; // Import application-specific stuff -use Hub\Handler\RawData\BaseRawDataHandler; +use Hub\Container\Socket\StorableSocket; // Import framework stuff use CoreFramework\State\Stateable; @@ -41,7 +41,7 @@ class OperationAlreadyProgressPeerState extends BasePeerState implements PeerSta parent::__construct(__CLASS__); // Set state name - $this->setStateName(BaseRawDataHandler::SOCKET_ERROR_OPERATION_ALREADY_PROGRESS); + $this->setStateName(StorableSocket::SOCKET_ERROR_OPERATION_ALREADY_PROGRESS); } /** diff --git a/application/hub/classes/states/peer/errors/class_ProblemPeerState.php b/application/hub/classes/states/peer/errors/class_ProblemPeerState.php index 256cd86e2..ca6d00f2d 100644 --- a/application/hub/classes/states/peer/errors/class_ProblemPeerState.php +++ b/application/hub/classes/states/peer/errors/class_ProblemPeerState.php @@ -3,7 +3,7 @@ namespace Hub\State\Peer; // Import application-specific stuff -use Hub\Handler\RawData\BaseRawDataHandler; +use Hub\Container\Socket\StorableSocket; // Import framework stuff use CoreFramework\State\Stateable; @@ -41,7 +41,7 @@ class ProblemPeerState extends BasePeerState implements PeerStateable { parent::__construct(__CLASS__); // Set state name - $this->setStateName(BaseRawDataHandler::SOCKET_ERROR_UNKNOWN); + $this->setStateName(StorableSocket::SOCKET_ERROR_UNKNOWN); } /** diff --git a/application/hub/classes/states/peer/errors/class_TransportEndpointGonePeerState.php b/application/hub/classes/states/peer/errors/class_TransportEndpointGonePeerState.php index 7308b68ba..30aa74d6c 100644 --- a/application/hub/classes/states/peer/errors/class_TransportEndpointGonePeerState.php +++ b/application/hub/classes/states/peer/errors/class_TransportEndpointGonePeerState.php @@ -3,7 +3,7 @@ namespace Hub\State\Peer; // Import application-specific stuff -use Hub\Handler\RawData\BaseRawDataHandler; +use Hub\Container\Socket\StorableSocket; // Import framework stuff use CoreFramework\State\Stateable; @@ -41,7 +41,7 @@ class TransportEndpointGonePeerState extends BasePeerState implements PeerStatea parent::__construct(__CLASS__); // Set state name - $this->setStateName(BaseRawDataHandler::SOCKET_ERROR_TRANSPORT_ENDPOINT); + $this->setStateName(StorableSocket::SOCKET_ERROR_TRANSPORT_ENDPOINT); } /** diff --git a/application/hub/interfaces/container/socket/class_StorableSocket.php b/application/hub/interfaces/container/socket/class_StorableSocket.php index 3afb47d13..06c072cd6 100644 --- a/application/hub/interfaces/container/socket/class_StorableSocket.php +++ b/application/hub/interfaces/container/socket/class_StorableSocket.php @@ -28,6 +28,66 @@ use CoreFramework\Generic\FrameworkInterface; * along with this program. If not, see . */ interface StorableSocket extends FrameworkInterface { + // Error codes: + // - Socket raw data stream errors + const SOCKET_ERROR_UNKNOWN = 'unknown_error'; // Unknown error (should not happen) + const SOCKET_ERROR_TRANSPORT_ENDPOINT = 'transport_endpoint'; // Transport endpoint has closed + const SOCKET_ERROR_INVALID_BASE64_MODULO = 'base64_modulo'; // Length is not modulo 4 + const SOCKET_ERROR_INVALID_BASE64_MESSAGE = 'base64_message'; // Raw data is not Base64-encoded + const SOCKET_ERROR_UNHANDLED = 'unhandled_package'; // Unhandled raw data (not bad) + const SOCKET_ERROR_CONNECTION_REFUSED = 'connection_refused'; // The name says it: connection refused + const SOCKET_ERROR_CONNECTION_TIMED_OUT = 'connection_timed_out'; // The name says it: connection attempt has timed-out + const SOCKET_ERROR_OPERATION_IN_PROGRESS = 'operation_in_progress'; // 'Operation now in progress' + const SOCKET_ERROR_OPERATION_ALREADY_PROGRESS = 'operation_already_progress'; // 'Operation already in progress' + const SOCKET_ERROR_RESOURCE_UNAVAILABLE = 'resource_unavailable'; // 'Resource temporary unavailable' + const SOCKET_ERROR_NO_ROUTE_TO_HOST = 'no_route_to_host'; // The name says it: no route to host + const SOCKET_ERROR_CONNECTION_RESET_BY_PEER = 'connection_reset_by_peer'; // Connection reset by peer + const SOCKET_ERROR_BROKEN_PIPE = 'broken_pipe'; // Broken pipe + const SOCKET_ERROR_PERMISSION_DENIED = 'permission_denied'; // Permission denied + const SOCKET_ERROR_OPERATION_NOT_SUPPORTED = 'operation_not_supported'; // 'Operation not supported' + const SOCKET_CONNECTED = 'connected'; // Nothing errorous happens, socket is connected + + /** + * Tries to bind the socket. + * + * @param $bindAddress Where to bind the socket to (e.g. Uni* socket file) + * @return $result Result from binding socket + * @throws InvalidSocketException If stored socket is invalid + */ + function bindSocketTo ($bindAddress); + + /** + * Tries to listen on the socket + * + * @return $result Result from listening on socket + * @throws InvalidSocketException If stored socket is invalid + */ + function listenOnSocket (); + + /** + * Tries to set non-blocking I/O on stored socket + * + * @return $result Result of changing to non-blocking I/O + * @throws InvalidSocketException If stored socket is invalid + */ + function enableSocketNonBlocking (); + + /** + * Some "getter" for peer address/port from stored socket + * + * @param $peerAddress Peer address being determined + * @param $peerPort Peer port being determined + * @return $result Result from asking for peer address + * @throws InvalidSocketException If stored socket is invalid + */ + function getSocketPeerName (&$peerAddress, &$peerPort); + + /** + * Some "getter" for socket type of stored socket + * + * @return $socketType Socket type + */ + function getSocketType (); /** * Checks whether the given Universal Node Locator matches with the one from package data @@ -79,34 +139,17 @@ interface StorableSocket extends FrameworkInterface { * Getter for last socket error * * @return $lastSocketError Last socket error - * @throws InvalidSocketException If socket is valid + * @throws InvalidSocketException If stored socket is invalid */ function getLastSocketError (); /** - * Tries to bind the socket. - * - * @param $bindAddress Where to bind the socket to (e.g. Uni* socket file) - * @return $result Result from binding socket - * @throws InvalidSocketException If socket is valid - */ - function bindSocketTo ($bindAddress); - - /** - * Tries to listen on the socket - * - * @return $result Result from listening on socket - * @throws InvalidSocketException If socket is valid - */ - function listenOnSocket (); - - /** - * Tries to set non-blocking I/O on stored socket + * Getter for last socket error message * - * @return $result Result of changing to non-blocking I/O - * @throws InvalidSocketException If socket is valid + * @return $lastSocketErrorMessage Last socket error message + * @throws InvalidSocketException If stored socket is invalid */ - function enableSocketNonBlocking (); + function getLastSocketErrorMessage (); /** * Handles socket error for given socket resource and peer data. This method @@ -124,4 +167,13 @@ interface StorableSocket extends FrameworkInterface { */ function handleSocketError ($method, $line, array $socketData); + /** + * Translates socket error codes into our own internal names which can be + * used for call-backs. + * + * @param $errorCode The error code from socket_last_error() to be translated + * @return $errorName The translated name (all lower-case, with underlines) + */ + function translateSocketErrorCodeToName ($errorCode); + } diff --git a/core b/core index 8d7422139..675124434 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit 8d7422139a55477388fcd6a30a1ce41d83780e5e +Subproject commit 6751244349667c1165f05a319f3d66d3f6781b40 -- 2.39.5