]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/helper/connection/class_BaseConnectionHelper.php
Refactured handling of socket errors, old code is commeted out and will be removed...
[hub.git] / application / hub / main / helper / connection / class_BaseConnectionHelper.php
index 5a2d92443ff57a945d136057cc1a8c2b746d3df5..d7ad4ee35274342ec066fbfb72de7175a8d04b5a 100644 (file)
@@ -22,6 +22,9 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 class BaseConnectionHelper extends BaseHubHelper implements Registerable, ProtocolHandler {
+       // Exception codes
+       const EXCEPTION_UNSUPPORTED_ERROR_HANDLER = 0x900;
+
        /**
         * Protocol used
         */
@@ -72,9 +75,6 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                // Call parent constructor
                parent::__construct($className);
 
-               // Register this connection helper
-               Registry::getRegistry()->addInstance('connection', $this);
-
                // Initialize output stream
                $streamInstance = ObjectFactory::createObjectByConfiguredName('node_raw_data_output_stream_class');
 
@@ -83,14 +83,17 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
 
                // Init state which sets the state to 'init'
                $this->initState();
+
+               // Register this connection helper
+               Registry::getRegistry()->addInstance('connection', $this);
        }
 
        /**
-        * Getter for real class name
+        * Getter for real class name, overwrites generic method and is final
         *
         * @return      $class  Name of this class
         */
-       public function __toString () {
+       public final function __toString () {
                // Class name representation
                $class = self::getConnectionClassName($this->getAddress(), $this->getPort(), parent::__toString());
 
@@ -271,7 +274,7 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                        $rawData .= $dataStream;
                } // END - while
 
-               // Nothing to sent is bad news!
+               // Nothing to sent is bad news, so assert on it
                assert(strlen($rawData) > 0);
 
                // Encode the raw data with our output-stream
@@ -294,24 +297,18 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
 
                        // If there was an error, we don't continue here
                        if ($sentBytes === false) {
-                               // Get socket error code for verification
-                               $socketError = socket_last_error($socketResource);
-
-                               // Get error message
-                               $errorMessage = socket_strerror($socketError);
-
-                               // Shutdown this socket
-                               $this->shutdownSocket($socketResource);
+                               // Handle the error with a faked recipientData array
+                               $this->handleSocketError($socketResource, array('0.0.0.0', '0'));
 
                                // And throw it
-                               throw new InvalidSocketException(array($this, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                               throw new InvalidSocketException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
                        } elseif (($sentBytes == 0) && (strlen($encodedData) > 0)) {
                                // Nothing sent means we are done
                                //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: All sent! (' . __LINE__ . ')');
                                break;
                        }
 
-                       // The difference between sent bytes and length of raw data should not be below zero
+                       // The difference between sent bytes and length of raw data should not go below zero
                        assert((strlen($encodedData) - $sentBytes) >= 0);
 
                        // Add total sent bytes
@@ -356,6 +353,31 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                /* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: ' . $this->__toString() . ',shuttedDown=' . intval($this->shuttedDown));
                return $this->shuttedDown;
        }
+
+       // ************************************************************************
+       //                 Socket error handler call-back methods
+       // ************************************************************************
+
+       /**
+        * Handles socket error 'connection timed out', but does not clear it for
+        * later debugging purposes.
+        *
+        * @param       $socketResource         A valid socket resource
+        * @throws      SocketConnectionException       The connection attempts fails with a time-out
+        */
+       private function socketErrorConnectionTimedOutHandler ($socketResource) {
+               // Get socket error code for verification
+               $socketError = socket_last_error($socketResource);
+
+               // Get error message
+               $errorMessage = socket_strerror($socketError);
+
+               // Shutdown this socket
+               $this->shutdownSocket($socketResource);
+
+               // Throw it again
+               throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+       }
 }
 
 // [EOF]