]> git.mxchange.org Git - hub.git/blobdiff - application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php
Yepp, the 2nd check was double + re-aranged the log messages that before
[hub.git] / application / hub / classes / helper / connection / ipv4 / class_BaseIpV4ConnectionHelper.php
index 3db9b16c97afc4ff6d8c9eaba8afd9271dc191f4..895acb9ac9037bf6840ae002a5224f1565d5e203 100644 (file)
@@ -113,9 +113,6 @@ class BaseIpV4ConnectionHelper extends BaseConnectionHelper {
                assert(isset($unlData[UniversalNodeLocator::UNL_PART_ADDRESS]));
                assert(isset($unlData[UniversalNodeLocator::UNL_PART_PORT]));
 
-               // Get current time
-               $time = time();
-
                // "Cache" socket resource and timeout config
                $socketResource = $this->getSocketResource();
                $timeout = $this->getConfigInstance()->getConfigEntry('socket_timeout_seconds');
@@ -123,29 +120,63 @@ class BaseIpV4ConnectionHelper extends BaseConnectionHelper {
                // Debug output
                self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Trying to connect to ' . $unlData[UniversalNodeLocator::UNL_PART_ADDRESS] . ':' . $unlData[UniversalNodeLocator::UNL_PART_PORT] . ' with socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' ...');
 
+               // Get current time
+               $hasTimedOut = FALSE;
+               $time = time();
+
                // Try to connect until it is connected
                while ($isConnected = !@socket_connect($socketResource, $unlData[UniversalNodeLocator::UNL_PART_ADDRESS], $unlData[UniversalNodeLocator::UNL_PART_PORT])) {
                        // Get last socket error
                        $socketError = socket_last_error($socketResource);
 
+                       // Log error code and status
+                       /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: socketError=' . $socketError . ',isConnected=' . intval($isConnected));
+
                        // 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) {
+                                       // Debug message
+                                       /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: timeout=' . $timeout .' reached, connection attempt failed.');
+
                                        // Didn't work within timeout
                                        $isConnected = FALSE;
+                                       $hasTimedOut = TRUE;
                                        break;
                                } // END - if
 
                                // Sleep about one second
                                $this->idle(1000);
                        } elseif ($socketError != 0) {
+                               // Debug message
+                               /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: socketError=' . $socketError . ' detected.');
+
                                // Stop on everything else pronto
                                $isConnected = FALSE;
                                break;
                        }
                } // END - while
 
+               // Log error code
+               /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: socketError=' . $socketError . ',isConnected=' . intval($isConnected) . ',hasTimedOut=' . intval($hasTimedOut) . ' after while() loop.');
+
+               /*
+                * All IPv4-based connections are non-blocking used by this program or
+                * else the PHP process will "hang" until a peer connects which is not
+                * what is wanted here. This means, that all connections will end with
+                * isConnected=FALSE here.
+                */
+               if (($hasTimedOut === FALSE) && ($socketError == SOCKET_EINPROGRESS)) {
+                       // Debug message
+                       /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Clearing "operation in progress" as this is for 99.999% chance a non-blocking I/O operation.');
+
+                       // A "connection in progress" has not timed out. All fine.
+                       $isConnected = TRUE;
+
+                       // Clear error
+                       socket_clear_error($socketResource);
+               } // END - if
+
                // Is the peer connected?
                if ($isConnected === TRUE) {
                        // Connection is fully established here, so change the state.