]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/resolver/state/peer/class_PeerStateResolver.php
Continued on development of 'hub' project with many refactorings/addings:
[hub.git] / application / hub / main / resolver / state / peer / class_PeerStateResolver.php
index 077a5bcf2de2dd7c016cbc7fb70bc421a5c95982..b51175fd86acb1e900b280a438a4b5fb7096a33e 100644 (file)
@@ -55,6 +55,7 @@ class PeerStateResolver extends BaseStateResolver implements StateResolver {
         * @param       $packageData            Raw package data
         * @param       $socketResource         A valid socket resource
         * @return      $stateInstance          An instance of the resolved state
+        * @throws      InvalidSocketException  If socketResource, even from getSocketResource() is no valid resource
         * @todo        ~30% done
         */
        public static function resolveStateByPackage (ConnectionHelper $helperInstance, array $packageData, $socketResource) {
@@ -68,29 +69,19 @@ class PeerStateResolver extends BaseStateResolver implements StateResolver {
                if (!is_resource($socketResource)) {
                        // No, so get socket resource from helper
                        $socketResource = $helperInstance->getSocketResource();
+
+                       // Still no socket resource?
+                       if (!is_resource($socketResource)) {
+                               // Then abort here with an exception (may happen after socket_shutdown())
+                               throw new InvalidSocketException(array($helperInstance, $socketResource, 'unknown', 'unknown'), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       } // END - if
                } // END - if
 
                // Get error code from it
                $errorCode = socket_last_error($socketResource);
 
-               // Is the code a number, then we have to change it
-               if (($errorCode == 134) || ($errorCode == 107)) {
-                       // Transport endpoint not connected, should be handled else!
-                       // @TODO On some systems it is 134, on some 107?
-                       $errorCode = BaseRawDataHandler::SOCKET_ERROR_TRANSPORT_ENDPOINT;
-               } elseif ($errorCode == 111) {
-                       // Error 'connection refused'
-                       $errorCode = BaseRawDataHandler::SOCKET_ERROR_CONNECTION_REFUSED;
-               } elseif ($errorCode > 0) {
-                       // Unhandled error code detected, so first debug it because we may want to handle it like the others
-                       $resolverInstance->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] UNKNOWN ERROR CODE = ' . $errorCode, ', MESSAGE = ' . socket_strerror($errorCode));
-
-                       // Change it only in this class
-                       $errorCode = BaseRawDataHandler::SOCKET_ERROR_UNKNOWN;
-               } else {
-                       // Nothing bad happend
-                       $errorCode = BaseRawDataHandler::SOCKET_CONNECTED;
-               }
+               // Translate the error code to an own name
+               $errorCode = $helperInstance->translateSocketErrorCodeToName($errorCode);
 
                // Create a state instance based on $errorCode. This factory does the hard work for us
                $stateInstance = PeerStateFactory::createPeerStateInstanceBySocketStatusCode($helperInstance, $packageData, $socketResource, $errorCode);