]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/helper/connection/class_BaseConnectionHelper.php
The final hash must be used as an array key for serial numbers, else some packages...
[hub.git] / application / hub / main / helper / connection / class_BaseConnectionHelper.php
index 0f2ffcc01fd6f9dd05d03d67a858efeb1f385fbd..5391983364c8047f12f80e58a9dc6fd27f2eaa33 100644 (file)
@@ -2,11 +2,11 @@
 /**
  * A general ConnectionHelper class
  *
- * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
  * @license            GNU GPL 3.0 or any newer version
- * @link               http://www.ship-simu.org
+ * @link               http://www.shipsimu.org
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-class BaseConnectionHelper extends BaseHubHelper implements Registerable, ProtocolHandler {
+class BaseConnectionHelper extends BaseHubSystemHelper implements Registerable, ProtocolHandler {
+       // Exception codes
+       const EXCEPTION_UNSUPPORTED_ERROR_HANDLER = 0x9100;
+
+       /**
+        * Connection type 'incoming'
+        */
+       const CONNECTION_TYPE_INCOMING = 'incoming';
+
+       /**
+        * Connection type 'outgoing'
+        */
+       const CONNECTION_TYPE_OUTGOING = 'outgoing';
+
+       /**
+        * Connection type 'server'
+        */
+       const CONNECTION_TYPE_SERVER   = 'server';
+
        /**
         * Protocol used
         */
@@ -43,19 +61,29 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
        private $sentData = 0;
 
        /**
-        * Offset
+        * Difference
         */
-       private $offset = 0;
+       private $diff = 0;
 
        /**
-        * Connect retries for this connection
+        * Whether this connection is initialized
         */
-       private $retryCount = 0;
+       private $isInitialized = FALSE;
 
        /**
-        * Wether this connection is shutted down
+        * Whether this connection is shutted down
         */
-       private $shuttedDown = false;
+       private $shuttedDown = FALSE;
+
+       /**
+        * Currently queued chunks
+        */
+       private $queuedChunks = array();
+
+       /**
+        * Current final hash
+        */
+       private $currentFinalHash = '';
 
        /**
         * Protected constructor
@@ -67,8 +95,36 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                // Call parent constructor
                parent::__construct($className);
 
+               // Initialize output stream
+               $streamInstance = ObjectFactory::createObjectByConfiguredName('node_raw_data_output_stream_class');
+
+               // And add it to this connection helper
+               $this->setOutputStreamInstance($streamInstance);
+
+               // Init state which sets the state to 'init'
+               $this->initState();
+
                // Register this connection helper
                Registry::getRegistry()->addInstance('connection', $this);
+
+               // Get the fragmenter instance
+               $fragmenterInstance = FragmenterFactory::createFragmenterInstance('package');
+
+               // Set it here
+               $this->setFragmenterInstance($fragmenterInstance);
+       }
+
+       /**
+        * Getter for real class name, overwrites generic method and is final
+        *
+        * @return      $class  Name of this class
+        */
+       public final function __toString () {
+               // Class name representation
+               $class = self::getConnectionClassName($this->getAddress(), $this->getPort(), parent::__toString());
+
+               // Return it
+               return $class;
        }
 
        /**
@@ -129,140 +185,461 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
        }
 
        /**
-        * "Accept" a visitor by simply calling it back
+        * Initializes the current connection
         *
-        * @param       $visitorInstance        A Visitor instance
         * @return      void
+        * @throws      SocketOptionException   If setting any socket option fails
         */
-       protected final function accept (Visitor $visitorInstance) {
-               // Just call the visitor
-               $visitorInstance->visitConnectionHelper($this);
+       protected function initConnection () {
+               // Get socket resource
+               $socketResource = $this->getSocketResource();
+
+               // Set the option to reuse the port
+               if (!socket_set_option($socketResource, SOL_SOCKET, SO_REUSEADDR, 1)) {
+                       // Handle this socket error with a faked recipientData array
+                       $this->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0'));
+
+                       // And throw again
+                       // @TODO Move this to the socket error handler
+                       throw new SocketOptionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+               } // END - if
+
+               /*
+                * Set socket to non-blocking mode before trying to establish a link to
+                * it. This is now the default behaviour for all connection helpers who
+                * call initConnection(); .
+                */
+               if (!socket_set_nonblock($socketResource)) {
+                       // Handle this socket error with a faked recipientData array
+                       $helperInstance->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0'));
+
+                       // And throw again
+                       throw new SocketOptionException(array($helperInstance, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+               } // END - if
+
+               // Last step: mark connection as initialized
+               $this->isInitialized = TRUE;
+       }
+
+       /**
+        * Attempts to connect to a peer by given IP number and port from a valid
+        * recipientData array with currently configured timeout.
+        *
+        * @param       $recipientData  A valid recipient data array, 0=IP; 1=PORT
+        * @return      $isConnected    Whether the connection went fine
+        * @see         Please see http://de.php.net/manual/en/function.socket-connect.php#84465 for original code
+        * @todo        Rewrite the while() loop to a iterator to not let the software stay very long here
+        */
+       protected function connectToPeerByRecipientData (array $recipientData) {
+               // Only call this if the connection is initialized by initConnection()
+               assert($this->isInitialized === TRUE);
+
+               // Get current time
+               $time = time();
+
+               // "Cache" socket resource and timeout config
+               $socketResource = $this->getSocketResource();
+               $timeout = $this->getConfigInstance()->getConfigEntry('socket_timeout_seconds');
+
+               // Debug output
+               self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Trying to connect to ' . $recipientData[0] . ':' . $recipientData[1] . ' with socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' ...');
+
+               // Try to connect until it is connected
+               while ($isConnected = !@socket_connect($socketResource, $recipientData[0], $recipientData[1])) {
+                       // Get last socket error
+                       $socketError = socket_last_error($socketResource);
+
+                       // Skip any errors which may happen on non-blocking connections
+                       if (($socketError == SOCKET_EINPROGRESS) || ($socketError == SOCKET_EALREADY)) {
+                               // Now, is that attempt within parameters?
+                               if ((time() - $time) >= $timeout) {
+                                       // Didn't work within timeout
+                                       $isConnected = FALSE;
+                                       break;
+                               } // END - if
+
+                               // Sleep about one second
+                               $this->idle(1000);
+                       } elseif ($socketError != 0) {
+                               // Stop on everything else pronto
+                               $isConnected = FALSE;
+                               break;
+                       }
+               } // END - while
+
+               // Is the peer connected?
+               if ($isConnected === TRUE) {
+                       // Connection is fully established here, so change the state.
+                       PeerStateFactory::createPeerStateInstanceByName('connected', $this);
+               } else {
+                       /*
+                        * There was a problem connecting to the peer (this state is a meta
+                        * state until the error handler has found the real cause).
+                        */
+                       PeerStateFactory::createPeerStateInstanceByName('problem', $this);
+               }
+
+               // Return status
+               return $isConnected;
+       }
+
+       /**
+        * Static "getter" for this connection class' name
+        *
+        * @param       $address        IP address
+        * @param       $port           Port number
+        * @param       $className      Original class name
+        * @return      $class          Expanded class name
+        */
+       public static function getConnectionClassName ($address, $port, $className) {
+               // Construct it
+               $class = $address . ':' . $port . ':' . $className;
+
+               // ... and return it
+               return $class;
+       }
+
+       /**
+        * Initializes the peer's state which sets it to 'init'
+        *
+        * @return      void
+        */
+       private function initState() {
+               /*
+                * Get the state factory and create the initial state, we don't need
+                * the state instance here
+                */
+               PeerStateFactory::createPeerStateInstanceByName('init', $this);
        }
 
        /**
         * "Getter" for raw data from a package array. A fragmenter is used which
         * will returns us only so many raw data which fits into the back buffer.
         * The rest is being held in a back-buffer and waits there for the next
-        * cycle and while be then sent. This is done by a FIFO.
+        * cycle and while be then sent.
         *
         * This method does 4 simple steps:
         * 1) Aquire fragmenter object instance from the factory
         * 2) Handle over the package data array to the fragmenter
-        * 3) Request a chunk (which "pops" the chunk from the fragmenter's FIFO)
-        * 4) Finally return the chunk to the caller
+        * 3) Request a chunk
+        * 4) Finally return the chunk (array) to the caller
         *
         * @param       $packageData    Raw package data array
-        * @return      $rawData                Raw package data bytes
+        * @return      $chunkData              Raw data chunk
         */
        private function getRawDataFromPackageArray (array $packageData) {
-               // Get the fragmenter instance
-               $fragmenterInstance = ObjectFactory::createObjectByConfiguredName('package_fragmenter_class');
-
-               // Implode the package data array and fragement the resulting string
-               $fragmenterInstance->fragmentPackageArray($packageData, $this);
-
-               // Get the next raw data chunk from the fragmenter's FIFO
-               $rawData = $fragmenterInstance->getNextRawDataChunk($packageData);
-               /* DEBUG: */ $this->debugOutput('rawData['.strlen($rawData).']='.$rawData);
-               /* DEBUG: */ die();
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: currentFinalHash=' . $this->currentFinalHash);
+
+               // Make sure the final hash is set
+               assert((is_string($this->currentFinalHash)) && (!empty($this->currentFinalHash)));
+
+               // Get the next raw data chunk from the fragmenter
+               $rawDataChunk = $this->getFragmenterInstance()->getNextRawDataChunk($this->currentFinalHash);
+
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: rawDataChunk=' . print_r($rawDataChunk, TRUE));
+
+               // Get chunk hashes and chunk data
+               $chunkHashes = array_keys($rawDataChunk);
+               $chunkData   = array_values($rawDataChunk);
+
+               // Is the required data there?
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: chunkHashes[]=' . count($chunkHashes) . ',chunkData[]=' . count($chunkData));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('chunkData='.print_r($chunkData, TRUE));
+               if ((isset($chunkHashes[0])) && (isset($chunkData[0]))) {
+                       // Remember this chunk as queued
+                       $this->queuedChunks[$chunkHashes[0]] = $chunkData[0];
+
+                       // Return the raw data
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Returning ' . strlen($chunkData[0]) . ' bytes from ' . __METHOD__ . ' ...');
+                       return $chunkData[0];
+               } else {
+                       // Return zero string
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Returning zero bytes from ' . __METHOD__ . '!');
+                       return '';
+               }
+       }
 
-               // Return it
-               return $rawData;
+       /**
+        * "Accept" a visitor by simply calling it back
+        *
+        * @param       $visitorInstance        A Visitor instance
+        * @return      void
+        */
+       protected final function accept (Visitor $visitorInstance) {
+               // Just call the visitor
+               $visitorInstance->visitConnectionHelper($this);
        }
 
        /**
         * Sends raw package data to the recipient
         *
-        * @param       $packageData    Raw package data
-        * @return      $sentBytes              Actual sent bytes to the peer
+        * @param       $packageData            Raw package data
+        * @return      $totalSentBytes         Total sent bytes to the peer
         * @throws      InvalidSocketException  If we got a problem with this socket
         */
        public function sendRawPackageData (array $packageData) {
-               // Convert the package data array to a raw data stream
-               $rawData = $this->getRawDataFromPackageArray($packageData);
+               // The helper's state must be 'connected'
+               $this->getStateInstance()->validatePeerStateConnected();
+
+               // Implode the package data array and fragement the resulting string, returns the final hash
+               $finalHash = $this->getFragmenterInstance()->fragmentPackageArray($packageData, $this);
+               if ($finalHash !== TRUE) {
+                       // Debug message
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Setting finalHash=' . $finalHash . ',currentFinalHash[' . gettype($this->currentFinalHash) . ']=' . $this->currentFinalHash);
+
+                       // Set final hash
+                       $this->currentFinalHash = $finalHash;
+               } // END - if
+
+               // Reset serial number
+               $this->getFragmenterInstance()->resetSerialNumber($this->currentFinalHash);
+
+               // Cache buffer length
+               $bufferSize = $this->getConfigInstance()->getConfigEntry($this->getProtocol() . '_buffer_length');
+
+               // Init variables
+               $rawData        = '';
+               $dataStream     = ' ';
+               $totalSentBytes = 0;
+
+               // Fill sending buffer with data
+               while (strlen($dataStream) > 0) {
+                       // Debug message
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: packageData=' . print_r($packageData, TRUE));
+
+                       // Convert the package data array to a raw data stream
+                       $dataStream = $this->getRawDataFromPackageArray($packageData);
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Adding ' . strlen($dataStream) . ' bytes to the sending buffer ...');
+                       $rawData .= $dataStream;
+               } // END - while
+
+               // Nothing to sent is bad news, so assert on it
+               assert(strlen($rawData) > 0);
+
+               // Encode the raw data with our output-stream
+               $encodedData = $this->getOutputStreamInstance()->streamData($rawData);
+
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: rawData()=' . strlen($rawData) . ',encodedData()=' . strlen($encodedData));
+
+               // Calculate difference
+               $this->diff = $bufferSize - strlen($encodedData);
 
                // Get socket resource
                $socketResource = $this->getSocketResource();
 
-               // And deliver it
-               $sentBytes = @socket_write($socketResource, $rawData, $this->getConfigInstance()->getConfigEntry($this->getProtocol() . '_buffer_length') - $this->offset);
+               // Init sent bytes
+               $sentBytes = 0;
+
+               // Deliver all data
+               while ($sentBytes !== FALSE) {
+                       // And deliver it
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Sending out ' . strlen($encodedData) . ' bytes,bufferSize=' . $bufferSize . ',diff=' . $this->diff);
+
+                       if ($this->diff >= 0) {
+                               // Send all out (encodedData is smaller than or equal buffer size)
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: MD5=' . md5(substr($encodedData, 0, ($bufferSize - $this->diff))));
+                               $sentBytes = socket_write($socketResource, $encodedData, ($bufferSize - $this->diff));
+                       } else {
+                               // Send buffer size out
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: MD5=' . md5(substr($encodedData, 0, $bufferSize)));
+                               $sentBytes = socket_write($socketResource, $encodedData, $bufferSize);
+                       }
+
+                       // If there was an error, we don't continue here
+                       if ($sentBytes === FALSE) {
+                               // Handle the error with a faked recipientData array
+                               $this->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0'));
+
+                               // And throw it
+                               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: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: All sent! (LINE=' . __LINE__ . ')');
+                               break;
+                       }
+
+                       // The difference between sent bytes and length of raw data should not go below zero
+                       assert((strlen($encodedData) - $sentBytes) >= 0);
+
+                       // Add total sent bytes
+                       $totalSentBytes += $sentBytes;
+
+                       // Cut out the last unsent bytes
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Sent out ' . $sentBytes . ' of ' . strlen($encodedData) . ' bytes ...');
+                       $encodedData = substr($encodedData, $sentBytes);
+
+                       // Calculate difference again
+                       $this->diff = $bufferSize - strlen($encodedData);
+
+                       // Can we abort?
+                       if (strlen($encodedData) <= 0) {
+                               // Abort here, all sent!
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: All sent! (LINE=' . __LINE__ . ')');
+                               break;
+                       } // END - if
+               } // END - while
 
-               // If there was an error, we don't continue here
-               if ($sentBytes === false) {
-                       // Get socket error code for verification
-                       $socketError = socket_last_error($socketResource);
+               // Return sent bytes
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: totalSentBytes=' . $totalSentBytes . ',diff=' . $this->diff);
+               return $totalSentBytes;
+       }
 
-                       // Get error message
-                       $errorMessage = socket_strerror($socketError);
+       /**
+        * Marks this connection as shutted down
+        *
+        * @return      void
+        */
+       protected final function markConnectionShuttedDown () {
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: ' . $this->__toString() . ' has been marked as shutted down');
+               $this->shuttedDown = TRUE;
 
-                       // Shutdown this socket
-                       $this->shutdownSocket($socketResource);
+               // And remove the (now invalid) socket
+               $this->setSocketResource(FALSE);
+       }
 
-                       // And throw it
-                       throw new InvalidSocketException(array($this, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
-               } // END - if
+       /**
+        * Getter for shuttedDown
+        *
+        * @return      $shuttedDown    Whether this connection is shutted down
+        */
+       public final function isShuttedDown () {
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: ' . $this->__toString() . ',shuttedDown=' . intval($this->shuttedDown));
+               return $this->shuttedDown;
+       }
 
-               // The difference between sent bytes and length of raw data should not be below zero
-               assert((strlen($rawData) - $sentBytes) >= 0);
+       // ************************************************************************
+       //                 Socket error handler call-back methods
+       // ************************************************************************
 
-               // Return sent bytes
-               return $sentBytes;
+       /**
+        * Handles socket error 'connection timed out', but does not clear it for
+        * later debugging purposes.
+        *
+        * @param       $socketResource         A valid socket resource
+        * @param       $recipientData          An array with two elements: 0=IP number, 1=port number
+        * @return      void
+        * @throws      SocketConnectionException       The connection attempts fails with a time-out
+        */
+       protected function socketErrorConnectionTimedOutHandler ($socketResource, array $recipientData) {
+               // 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);
        }
 
        /**
-        * Getter for real class name
+        * Handles socket error 'resource temporary unavailable', but does not
+        * clear it for later debugging purposes.
         *
-        * @return      $class  Name of this class
+        * @param       $socketResource         A valid socket resource
+        * @param       $recipientData          An array with two elements: 0=IP number, 1=port number
+        * @return      void
+        * @throws      SocketConnectionException       The connection attempts fails with a time-out
         */
-       public function __toString () {
-               // Class name representation
-               $class = $this->getAddress() . ':' . $this->getPort() . ':' . parent::__toString();
+       protected function socketErrorResourceUnavailableHandler ($socketResource, array $recipientData) {
+               // Get socket error code for verification
+               $socketError = socket_last_error($socketResource);
 
-               // Return it
-               return $class;
+               // 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);
        }
 
        /**
-        * Checks wether the connect retry is exhausted
+        * Handles socket error 'connection refused', but does not clear it for
+        * later debugging purposes.
         *
-        * @return      $isExhaused             Wether connect retry is exchausted
+        * @param       $socketResource         A valid socket resource
+        * @param       $recipientData          An array with two elements: 0=IP number, 1=port number
+        * @return      void
+        * @throws      SocketConnectionException       The connection attempts fails with a time-out
         */
-       public final function isConnectRetryExhausted () {
-               // Construct config entry
-               $configEntry = $this->getProtocol() . '_connect_retry_max';
+       protected function socketErrorConnectionRefusedHandler ($socketResource, array $recipientData) {
+               // Get socket error code for verification
+               $socketError = socket_last_error($socketResource);
 
-               // Check it out
-               $isExhausted = ($this->retryCount >=  $this->getConfigInstance()->getConfigEntry($configEntry));
+               // Get error message
+               $errorMessage = socket_strerror($socketError);
 
-               // Return it
-               return $isExhausted;
+               // Shutdown this socket
+               $this->shutdownSocket($socketResource);
+
+               // Throw it again
+               throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
        }
 
        /**
-        * Increases the connect retry count
+        * Handles socket error 'no route to host', but does not clear it for later
+        * debugging purposes.
         *
+        * @param       $socketResource         A valid socket resource
+        * @param       $recipientData          An array with two elements: 0=IP number, 1=port number
         * @return      void
+        * @throws      SocketConnectionException       The connection attempts fails with a time-out
         */
-       public final function increaseConnectRetry () {
-               $this->retryCount++;
+       protected function socketErrorNoRouteToHostHandler ($socketResource, array $recipientData) {
+               // 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);
        }
 
        /**
-        * Marks this connection as shutted down
+        * Handles socket error 'operation already in progress' which happens in
+        * method connectToPeerByRecipientData() on timed out connection
+        * attempts.
         *
+        * @param       $socketResource         A valid socket resource
+        * @param       $recipientData          An array with two elements: 0=IP number, 1=port number
         * @return      void
+        * @throws      SocketConnectionException       The connection attempts fails with a time-out
         */
-       protected final function markConnectionShutdown () {
-               $this->shuttedDown = true;
+       protected function socketErrorOperationAlreadyProgressHandler ($socketResource, array $recipientData) {
+               // Get socket error code for verification
+               $socketError = socket_last_error($socketResource);
+
+               // Get error message
+               $errorMessage = socket_strerror($socketError);
+
+               // Half-shutdown this socket (see there for difference to shutdownSocket())
+               $this->halfShutdownSocket($socketResource);
+
+               // Throw it again
+               throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
        }
 
        /**
-        * Getter for shuttedDown
+        * Handles socket "error" 'operation now in progress' which can be safely
+        * passed on with non-blocking connections.
         *
-        * @return      $shuttedDown    Wether this connection is shutted down
+        * @param       $socketResource         A valid socket resource
+        * @param       $recipientData          An array with two elements: 0=IP number, 1=port number
+        * @return      void
         */
-       public final function isShuttedDown () {
-               return $this->shuttedDown;
+       protected function socketErrorOperationInProgressHandler ($socketResource, array $recipientData) {
+               self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Operation is now in progress, this is usual for non-blocking connections and is no bug.');
        }
 }