From: Roland Häder Date: Thu, 3 Dec 2020 06:59:26 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=d68b6c277d335179701640a5ccde3d5b5c7118f0;p=hub.git Continued: - renamed StorableSocket->getSocketRecipient() to getSocketRecipientUnl() - commented-in noisy debug lines as a strange "Resource temporary unavailable" happens when SYN cookies are disabled in kernel - added type-hint for primitive variables - not really noisy debug lines but later no more needed Signed-off-by: Roland Häder --- diff --git a/application/hub/classes/container/socket/class_SocketContainer.php b/application/hub/classes/container/socket/class_SocketContainer.php index 166b1b7e0..b9601bd6f 100644 --- a/application/hub/classes/container/socket/class_SocketContainer.php +++ b/application/hub/classes/container/socket/class_SocketContainer.php @@ -200,7 +200,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $matches = (($packageInstance->getRecipientUnl() !== '') && ($packageInstance->getRecipientUnl() === $unl)); // Return result - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: matches=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($matches))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: matches=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($matches))); return $matches; } @@ -212,11 +212,11 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function ifSocketResourceMatches ($socketResource) { // So, does both match? - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: socketResource[' . gettype($socketResource) . ']=' .$socketResource . ',storedResource[' . gettype($this->getSocketResource()) . ']=' . $this->getSocketResource()); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: socketResource[' . gettype($socketResource) . ']=' .$socketResource . ',storedResource[' . gettype($this->getSocketResource()) . ']=' . $this->getSocketResource()); $matches = ((is_resource($socketResource)) && ($socketResource === $this->getSocketResource())); // Return result - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: matches=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($matches))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: matches=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($matches))); return $matches; } @@ -240,7 +240,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $this->accept($visitorInstance); // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: EXIT!', strtoupper($this->getSocketProtocol()))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: EXIT!', strtoupper($this->getSocketProtocol()))); } /** @@ -263,7 +263,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $this->accept($visitorInstance); // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: EXIT!', strtoupper($this->getSocketProtocol()))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: EXIT!', strtoupper($this->getSocketProtocol()))); } /** @@ -329,15 +329,15 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita } /** - * Getter for socket recipient array element + * Getter for socket recipient UNL * - * @return $recipient Recipient array element + * @return $recipient Recipient UNL * @throws LogicException If 'recipient' array element is not found * @throws InvalidSocketException If socket is invalid */ - public function getSocketRecipient () { + public function getSocketRecipientUnl () { // Should be valid socket - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); @@ -347,7 +347,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $packageInstance = $this->getPackageDataInstance(); // Return it - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: packageInstance.recipient=%s - EXIT!', strtoupper($this->getSocketProtocol()), $packageInstance->getRecipientUnl())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: packageInstance.recipient=%s - EXIT!', strtoupper($this->getSocketProtocol()), $packageInstance->getRecipientUnl())); return $packageInstance->getRecipientUnl(); } @@ -359,14 +359,14 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function getSocketRecipientAddress () { // Should be valid socket - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); } // Get recipient - $recipient = $this->getSocketRecipient(); + $recipient = $this->getSocketRecipientUnl(); // Generate UNL instance for it $locatorInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($recipient); @@ -375,7 +375,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $recipientAddress = $locatorInstance->getUnlAddress(); // Return it - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: recipientAddress=%s - EXIT!', strtoupper($this->getSocketProtocol()), $recipientAddress)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: recipientAddress=%s - EXIT!', strtoupper($this->getSocketProtocol()), $recipientAddress)); return $recipientAddress; } @@ -387,14 +387,14 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function getSocketRecipientPort () { // Should be valid socket - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); } // Get recipient - $recipient = $this->getSocketRecipient(); + $recipient = $this->getSocketRecipientUnl(); // Generate UNL instance for it $locatorInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($recipient); @@ -403,7 +403,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $recipientPort = $locatorInstance->getUnlPort(); // Return it - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: recipientPort=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($recipientPort))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: recipientPort=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($recipientPort))); return $recipientPort; } @@ -413,16 +413,13 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @return $isValidSocket Whether the stored socket is valid */ public function isValidSocket () { - // Get socket resource - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); - $socketResource = $this->getSocketResource(); - // Is it valid? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); // @TODO maybe add more checks? is_resource() is still to less - $isValidSocket = (is_resource($socketResource)); + $isValidSocket = (is_resource($this->getSocketResource())); // Return status - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: isValidSocket=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($isValidSocket))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: isValidSocket=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($isValidSocket))); return $isValidSocket; } @@ -434,20 +431,17 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function getLastSocketErrorCode () { // Should be valid socket - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); } - // Get it from stored socket resource - $socketResource = $this->getSocketResource(); - - // Get error code - $errorCode = socket_last_error($socketResource); + // Get error code from socket + $errorCode = socket_last_error($this->getSocketResource()); // Return it - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorCode=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($errorCode))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorCode=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($errorCode))); return $errorCode; } @@ -459,20 +453,17 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function getLastSocketErrorMessage () { // Should be valid socket - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); } - // Get it from stored socket resource - $socketResource = $this->getSocketResource(); - // Get error code and message $errorMessage = socket_strerror($this->getLastSocketErrorCode()); // Return it - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorMessage=%s - EXIT!', strtoupper($this->getSocketProtocol()), $errorMessage)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorMessage=%s - EXIT!', strtoupper($this->getSocketProtocol()), $errorMessage)); return $errorMessage; } @@ -483,22 +474,21 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function translateLastSocketErrorCodeToName () { // Get last error code - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); $errorCode = $this->getLastSocketErrorCode(); // Call "translate" method + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorCode=%d', strtoupper($this->getSocketProtocol()), $errorCode)); $codeName = $this->translateSocketErrorCodeToName($errorCode); // Return it - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: codeName=%s - EXIT!', strtoupper($this->getSocketProtocol()), $codeName)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: codeName=%s - EXIT!', strtoupper($this->getSocketProtocol()), $codeName)); return $codeName; } /** * Tries to bind to socket file * - * @param $bindAddress Where to bind the socket to (e.g. Uni* socket file) - * @param $bindPort Optional port to bind to * @return $result Result from binding socket * @throws InvalidSocketException If socket is invalid * @throws NullPointerException If listener instance is not given @@ -519,7 +509,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $result = socket_bind($this->getSocketResource(), $this->getSocketFile()); // Return result - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d,errorCode=%s - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result), $this->getLastSocketErrorCode())); return $result; } @@ -552,7 +542,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $result = socket_bind($this->getSocketResource(), $address, $port); // Return result - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d,errorCode=%s - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result), $this->getLastSocketErrorCode())); return $result; } @@ -564,7 +554,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function listenToSocket () { // Should be valid socket - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); @@ -574,7 +564,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $result = socket_listen($this->getSocketResource()); // Return result - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d,errorCode=%s - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result), $this->getLastSocketErrorCode())); return $result; } @@ -586,7 +576,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function enableSocketNonBlocking () { // Should be valid socket - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); @@ -596,7 +586,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $result = socket_set_nonblock($this->getSocketResource()); // Return result - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d,errorCode=%s - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result), $this->getLastSocketErrorCode())); return $result; } @@ -608,7 +598,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function enableSocketReuseAddress () { // Should be valid socket - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); @@ -618,7 +608,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $result = $this->setSocketOption(SOL_SOCKET, SO_REUSEADDR, 1); // Return result - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d,errorCode=%s - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result), $this->getLastSocketErrorCode())); return $result; } @@ -637,7 +627,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita } // Get recipient UNL - $unlRecipient = $this->getSocketRecipient(); + $unlRecipient = $this->getSocketRecipientUnl(); // Create UNL instance from it. This will validate the connection /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: unlRecipient=%s', strtoupper($this->getSocketProtocol()), $unlRecipient)); @@ -648,7 +638,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $result = socket_connect($this->getSocketResource(), $locatorInstance->getUnlAddress(), $locatorInstance->getUnlPort()); // Return result - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d,errorCode=%s - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result), $this->getLastSocketErrorCode())); return $result; } @@ -835,7 +825,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita } // Return result - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d,errorCode=%s - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result), $this->getLastSocketErrorCode())); return $result; } @@ -846,7 +836,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function setSocketTimeoutOptions () { // Call setter method with configured values - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); $result = $this->setSocketOption(SOL_SOCKET, SO_RCVTIMEO, [ // Seconds 'sec' => FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($this->getSocketProtocol() . '_socket_accept_wait_sec'), @@ -855,7 +845,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita ]); // Return result - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d,errorCode=%s - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result), $this->getLastSocketErrorCode())); return $result; } @@ -866,11 +856,11 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function enableSocketOutOfBandData () { // Call inner method - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); $result = $this->setSocketOption(SOL_SOCKET, SO_OOBINLINE, 1); // Return result - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d,errorCode=%s - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result), $this->getLastSocketErrorCode())); return $result; } @@ -913,19 +903,19 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $helperInstance = $infoInstance->getHelperInstance(); // Is there a listener instance set? - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: listenerInstance[]=' . gettype($listenerInstance) . ',helperInstance[]=' . gettype($helperInstance)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: listenerInstance[]=' . gettype($listenerInstance) . ',helperInstance[]=' . gettype($helperInstance)); if ($listenerInstance instanceof Listenable) { // Set it here for later usage - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Setting listenerInstance=' . $listenerInstance->__toString() . ' ...'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Setting listenerInstance=' . $listenerInstance->__toString() . ' ...'); $this->setListenerInstance($listenerInstance); } elseif ($helperInstance instanceof ConnectionHelper) { // Set it here for later usage - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Setting helperInstance=' . $helperInstance->__toString() . ' ...'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Setting helperInstance=' . $helperInstance->__toString() . ' ...'); $this->setHelperInstance($helperInstance); } // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: EXIT!', strtoupper($this->getSocketProtocol()))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: EXIT!', strtoupper($this->getSocketProtocol()))); } /** @@ -1005,7 +995,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita } // Return result - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d,errorCode=%s - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result), $this->getLastSocketErrorCode())); return $result; } @@ -1062,7 +1052,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ protected function getSocketErrorHandlerFromCode (int $errorCode) { // Create a name from translated error code - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorCode=%d - CALLED!', strtoupper($this->getSocketProtocol()), $errorCode)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorCode=%d - CALLED!', strtoupper($this->getSocketProtocol()), $errorCode)); $handlerName = 'handleSocketError' . StringUtils::convertToClassName($this->translateSocketErrorCodeToName($errorCode)); // Is the call-back method there? @@ -1072,7 +1062,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita } // Return it - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: handlerName=%d - EXIT!', strtoupper($this->getSocketProtocol()), $handlerName)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: handlerName=%d - EXIT!', strtoupper($this->getSocketProtocol()), $handlerName)); return $handlerName; } @@ -1089,7 +1079,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ protected function handleSocketErrorPermissionDenied () { // Get socket error code for verification - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); $socketError = $this->getLastSocketErrorCode(); // Get error message @@ -1111,7 +1101,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ protected function handleSocketErrorConnectionTimedOut () { // Get socket error code for verification - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); $socketError = $this->getLastSocketErrorCode(); // Get error message @@ -1133,7 +1123,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ protected function handleSocketErrorResourceUnavailable () { // Get socket error code for verification - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); $socketError = $this->getLastSocketErrorCode(); // Get error message @@ -1155,7 +1145,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ protected function handleSocketErrorConnectionRefused () { // Get socket error code for verification - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); $socketError = $this->getLastSocketErrorCode(); // Get error message @@ -1177,7 +1167,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ protected function handleSocketErrorNoRouteToHost () { // Get socket error code for verification - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); $socketError = $this->getLastSocketErrorCode(); // Get error message @@ -1199,7 +1189,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ protected function handleSocketErrorOperationAlreadyProgress () { // Get socket error code for verification - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); $socketError = $this->getLastSocketErrorCode(); // Get error message @@ -1221,7 +1211,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ protected function handleSocketErrorConnectionResetByPeer () { // Get socket error code for verification - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); $socketError = $this->getLastSocketErrorCode(); // Get error message @@ -1243,7 +1233,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ protected function handleSocketErrorOperationNotSupported () { // Get socket error code for verification - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); $socketError = $this->getLastSocketErrorCode(); // Get error message @@ -1264,7 +1254,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ protected function handleSocketErrorOperationInProgress () { // Very common with non-blocking I/O - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-HELPER: Operation is now in progress, this is usual for non-blocking connections and is no bug.'); } @@ -1281,7 +1271,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ private function translateSocketErrorCodeToName (int $errorCode) { // Unknown error code by default - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorCode=%d - CALLED!', strtoupper($this->getSocketProtocol()), $errorCode)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorCode=%d - CALLED!', strtoupper($this->getSocketProtocol()), $errorCode)); $errorName = StorableSocket::SOCKET_ERROR_UNKNOWN; // Is the code a number, then we have to change it @@ -1343,7 +1333,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita } // Return translated name - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorName=%d - EXIT!', strtoupper($this->getSocketProtocol()), $errorName)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorName=%s - EXIT!', strtoupper($this->getSocketProtocol()), $errorName)); return $errorName; } @@ -1356,9 +1346,9 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @return $result Whether calling socket_set_option() was successfull * @throws InvalidSocketException If stored socket is invalid */ - private function setSocketOption ($level, $optionName, $optionValue) { + private function setSocketOption (int $level, int $optionName, $optionValue) { // Should be valid socket - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: level=%s,optionName=%s,optionValue[]=%s - CALLED!', strtoupper($this->getSocketProtocol()), $level, $optionName, gettype($optionValue))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: level=%d,optionName=%d,optionValue[]=%s - CALLED!', strtoupper($this->getSocketProtocol()), $level, $optionName, gettype($optionValue))); if (!$this->isValidSocket()) { // Throw exception throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); @@ -1368,7 +1358,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $result = socket_set_option($this->getSocketResource(), $level, $optionName, $optionValue); // Return result - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d,errorCode=%s - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result), $this->getLastSocketErrorCode())); return $result; } diff --git a/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseFrontend.php b/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseFrontend.php index 4ecb4ea16..34a499fb1 100644 --- a/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseFrontend.php +++ b/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseFrontend.php @@ -120,7 +120,7 @@ class PeerStateLookupDatabaseFrontend extends BaseHubDatabaseFrontend implements if ($dataSetInstance instanceof StoreableCriteria) { // Then remember the search instance in it $dataSetInstance->setSearchInstance($searchInstance); - } // END - if + } // Count the query $entries = $this->doSelectCountByCriteria($searchInstance, array(self::DB_COLUMN_PEER_SESSION_ID => TRUE)); @@ -147,7 +147,7 @@ class PeerStateLookupDatabaseFrontend extends BaseHubDatabaseFrontend implements if (!$this->isSenderNewPeer($packageInstance)) { // Throw an exception because this should normally not happen throw new PeerAlreadyRegisteredException(array($this, $packageInstance), self::EXCEPTION_PEER_ALREADY_REGISTERED); - } // END - if + } // Generate a dataset instance $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_PEER_LOOKUP)); @@ -167,7 +167,7 @@ class PeerStateLookupDatabaseFrontend extends BaseHubDatabaseFrontend implements // ... and cleartext message from it and put both into criteria $dataSetInstance->addCriteria(self::DB_COLUMN_SOCKET_ERROR_CODE, $socketInstance->getLastSocketErrorCode()); $dataSetInstance->addCriteria(self::DB_COLUMN_SOCKET_ERROR_MSG , $socketInstance->getLastSocketErrorMessage()); - } // END - if + } // Add ip address and port $dataSetInstance->addCriteria(self::DB_COLUMN_PEER_IP , $peerAddress); diff --git a/application/hub/classes/factories/info/class_ConnectionInfoFactory.php b/application/hub/classes/factories/info/class_ConnectionInfoFactory.php index 0c6d01001..2c67f426f 100644 --- a/application/hub/classes/factories/info/class_ConnectionInfoFactory.php +++ b/application/hub/classes/factories/info/class_ConnectionInfoFactory.php @@ -50,11 +50,11 @@ class ConnectionInfoFactory extends ObjectFactory { parent::__construct(__CLASS__); // Init array - self::$validTypes = array( + self::$validTypes = [ 'helper', // Fake type 'helper' StorableSocket::CONNECTION_TYPE_SERVER, // Server ("listener") type StorableSocket::CONNECTION_TYPE_INCOMING, // Incoming connection type - ); + ]; } /** @@ -66,13 +66,19 @@ class ConnectionInfoFactory extends ObjectFactory { * @throws InvalidArgumentException If one of the arguments are not valid * @todo Also validate protocol to be sure if there is really a protocol handler for it available */ - public static final function createConnectionInfoInstance ($protocolName, $type) { + public static final function createConnectionInfoInstance (string $protocolName, string $type) { // Init factory instance /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO-FACTORY: protocolName=%s,type=%s - CALLED!', $protocolName, $type)); $factoryInstance = new ConnectionInfoFactory(); // Validate parameter ... - if (!in_array($type, self::$validTypes, TRUE)) { + if (empty($protocolName)) { + // Empty parameter + throw new InvalidArgumentException('Parameter "protocolName" cannot be empty.'); + } elseif (empty($type)) { + // Empty parameter + throw new InvalidArgumentException('Parameter "type" cannot be empty.'); + } elseif (!in_array($type, self::$validTypes, TRUE)) { // Not valid type throw new InvalidArgumentException(sprintf('type=%s is not valid.', $type)); } diff --git a/application/hub/classes/factories/socket/class_SocketFactory.php b/application/hub/classes/factories/socket/class_SocketFactory.php index 64c650439..91f83e676 100644 --- a/application/hub/classes/factories/socket/class_SocketFactory.php +++ b/application/hub/classes/factories/socket/class_SocketFactory.php @@ -162,24 +162,22 @@ class SocketFactory extends ObjectFactory { exit; } - // Debug message - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to ' . $socketInstance->getSocketFile() . ' ...'); - // Try to bind to it + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to ' . $socketInstance->getSocketFile() . ' ...'); if (!$socketInstance->bindSocketToFile()) { // Handle error here $socketInstance->handleSocketError(__METHOD__, __LINE__); } // Start listen for connections - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Listening for connections.'); + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Listening for connections.'); if (!$socketInstance->listenToSocket()) { // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); } // Allow non-blocking I/O - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Setting non-blocking mode.'); + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Setting non-blocking mode.'); if (!$socketInstance->enableSocketNonBlocking()) { // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); diff --git a/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php b/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php index bf689498b..e26b4eb87 100644 --- a/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php +++ b/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php @@ -102,10 +102,11 @@ abstract class BaseIpV4ConnectionHelper extends BaseConnectionHelper { $hasTimedOut = FALSE; $time = time(); - // Try to connect until it is connected - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-IPV4-CONNECTION-HELPER: Trying to connect to %s with socketResource[%s]=%s ...', $this->getSocketInstance()->getSocketRecipient(), gettype($this->getSocketInstance()->getSocketResource()), $this->getSocketInstance()->getSocketResource())); - while ($isConnected = !$this->getSocketInstance()->connectToSocketRecipient()) { - // Get last socket error + // This won't loop forever ... + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-IPV4-CONNECTION-HELPER: Trying to connect to %s with socketResource[%s]=%s ...', $this->getSocketInstance()->getSocketRecipientUnl(), gettype($this->getSocketInstance()->getSocketResource()), $this->getSocketInstance()->getSocketResource())); + while (true) { + // Try to connect until it is connected + $isConnected = $this->getSocketInstance()->connectToSocketRecipient(); $socketError = $this->getSocketInstance()->getLastSocketErrorCode(); // Skip any errors which may happen on non-blocking connections diff --git a/application/hub/classes/pools/class_BasePool.php b/application/hub/classes/pools/class_BasePool.php index c9767346d..6bb95eadf 100644 --- a/application/hub/classes/pools/class_BasePool.php +++ b/application/hub/classes/pools/class_BasePool.php @@ -105,7 +105,7 @@ abstract class BasePool extends BaseHubSystem implements Poolable, Visitable { * @param $list The list identifier we should return * @return $array The requested array */ - public final function getArrayFromList ($list) { + public final function getArrayFromList (string $list) { // Get the array $array = $this->getPoolEntriesInstance()->getArrayFromList($list); diff --git a/application/hub/classes/pools/peer/class_DefaultPeerPool.php b/application/hub/classes/pools/peer/class_DefaultPeerPool.php index 66fa3c699..20cabc4bb 100644 --- a/application/hub/classes/pools/peer/class_DefaultPeerPool.php +++ b/application/hub/classes/pools/peer/class_DefaultPeerPool.php @@ -78,10 +78,8 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { * @throws InvalidSocketException If the given socket has an error */ private function validateSocket (StorableSocket $socketInstance) { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-SYSTEM: socketInstance->socketResource=%s - CALLED!', $socketInstance->getSocketResource())); - // Is it a valid resource? + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-SYSTEM: socketInstance->socketResource=%s - CALLED!', $socketInstance->getSocketResource())); if (!$socketInstance->isValidSocket()) { // Throw an exception throw new InvalidSocketException(array($this, $socketInstance->getSocketResource()), self::EXCEPTION_INVALID_SOCKET); @@ -111,7 +109,7 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: socketResource[%s]=%s,connectionType=%s - CALLED!', gettype($socketInstance->getSocketResource()), $socketInstance->getSocketResource(), $connectionType)); if (empty($connectionType)) { // Throw exception - throw new InvalidArgumentException('connectionType is empty'); + throw new InvalidArgumentException('Parameter "connectionType" is empty'); } // Validate the socket @@ -139,17 +137,18 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DEFAULT-PEER-POOL: Socket resource is server socket (' . $socketInstance->getSocketResource() . '). This is not a bug.'); } - // Debug message - /* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DEFAULT-PEER-POOL: Adding peer ' . $peerAddress . ':' . $peerPort . ',socketResource=' . $socketInstance->getSocketResource() . ',connectionType=' . $connectionType); - // Construct the array - $socketArray = array( + /* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DEFAULT-PEER-POOL: Adding peer ' . $peerAddress . ':' . $peerPort . ',socketResource=' . $socketInstance->getSocketResource() . ',connectionType=' . $connectionType); + $socketArray = [ Poolable::SOCKET_ARRAY_INSTANCE => $socketInstance, Poolable::SOCKET_ARRAY_CONN_TYPE => $connectionType - ); + ]; // Add it finally to the pool $this->addPoolEntry($socketArray); + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DEFAULT-PEER-POOL: EXIT!'); } /** @@ -162,6 +161,7 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { $sockets = $this->getArrayFromList('pool'); // Return it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: sockets()=%d - EXIT!', count($sockets))); return $sockets; } @@ -200,7 +200,7 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: connectionType=%s - CALLED!', $connectionType)); if (empty($connectionType)) { // Throw exception - throw new InvalidArgumentException('connectionType is empty'); + throw new InvalidArgumentException('Parameter "connectionType" is empty'); } // Get the array list @@ -234,26 +234,30 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { * @param $packageInstance An instance of a DeliverablePackage class * @param $connectionType Type of connection, can be 'incoming', 'outgoing', 'server' or default * @return $socketInstance An instance of a StorableSocket class + * @throws InvalidArgumentException If a parameter is not valid * @throws InvalidConnectionTypeException If the provided connection type is not valid * @throws LogicException If an expected array element is missing */ public function getSocketFromPackageInstance (DeliverablePackage $packageInstance, string $connectionType = NULL) { - // Default is no socket + // Validate parameter //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: packageInstance=%s,connectionType[%s]=%s - CALLED!', $packageInstance->__toString(), gettype($connectionType), $connectionType)); + if (empty($connectionType)) { + // Throw exception + throw new InvalidArgumentException('Parameter "connectionType" is empty'); + } + + // Default is no socket $socketInstance = NULL; // Resolve recipient (UNL) into a handler instance $handlerInstance = ProtocolHandlerFactory::createProtocolHandlerFromPackageInstance($packageInstance); - // Get UNL data + // Get UNL instance $locatorInstance = $handlerInstance->getUniversalNodeLocatorInstance(); - // Default is all sockets - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DEFAULT-PEER-POOL: Checking ' . count($this->getAllSockets()) . ' socket(s),locatorInstance->unl=' . $locatorInstance->generateUnl() . ' ...'); - $sockets = $this->getAllSockets(); - // Get all sockets and check them, skip the server socket - foreach ($sockets as $socketArray) { + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DEFAULT-PEER-POOL: Checking ' . count($this->getAllSockets()) . ' socket(s),locatorInstance->unl=' . $locatorInstance->generateUnl() . ' ...'); + foreach ($this->getAllSockets() as $socketArray) { // Is connection type set? /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: socketArray(%d)=%s', count($socketArray), implode(',', $socketArray))); //* DEBUG-DIE: */ die(__METHOD__.':connectionType='.$connectionType.',socketArray='.print_r($socketArray, TRUE)); diff --git a/application/hub/interfaces/container/socket/class_StorableSocket.php b/application/hub/interfaces/container/socket/class_StorableSocket.php index f87dd245a..3fdd62af8 100644 --- a/application/hub/interfaces/container/socket/class_StorableSocket.php +++ b/application/hub/interfaces/container/socket/class_StorableSocket.php @@ -176,7 +176,7 @@ interface StorableSocket extends FrameworkInterface { * @return $result Result of calling socket_write() * @throws InvalidSocketException If stored socket is invalid */ - function writeBufferToSocketByArray (array &$socketBuffer, &$sentBytes); + function writeBufferToSocketByArray (array &$socketBuffer, int &$sentBytes); /** * Getter for socket procotol field @@ -186,13 +186,13 @@ interface StorableSocket extends FrameworkInterface { function getSocketProtocol (); /** - * Getter for socket recipient array element + * Getter for socket recipient UNL * - * @return $recipient Recipient array element + * @return $recipient Recipient UNL * @throws LogicException If 'recipient' array element is not found * @throws InvalidSocketException If stored socket is invalid */ - function getSocketRecipient (); + function getSocketRecipientUnl (); /** * Getter for socket recipient's address part diff --git a/application/hub/interfaces/pool/class_Poolable.php b/application/hub/interfaces/pool/class_Poolable.php index 32738cc31..1775d03de 100644 --- a/application/hub/interfaces/pool/class_Poolable.php +++ b/application/hub/interfaces/pool/class_Poolable.php @@ -38,7 +38,7 @@ interface Poolable extends HubInterface { * @param $list The list identifier we should return * @return $array The requested array */ - function getArrayFromList ($list); + function getArrayFromList (string $list); /** * Getter for pool entries instance