From d8c2f36ffd18c913f32004f32332ee494de46760 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Wed, 2 Dec 2020 22:21:52 +0100 Subject: [PATCH] Continued: - fixed E_NOTICE and other stuff - added config entry for disabling (default: enabled) reusing TCP addresses - commented-in noisy debug lines SYNC flooding happens on my system with this program - updated core framework MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../socket/class_SocketContainer.php | 29 ++++---- .../factories/socket/class_SocketFactory.php | 10 ++- .../package/class_NetworkPackageHandler.php | 6 +- .../connection/class_BaseConnectionHelper.php | 48 +++++++------- .../ipv4/class_BaseIpV4ConnectionHelper.php | 66 +++++++++---------- .../ipv4/tcp/class_TcpConnectionHelper.php | 8 ++- application/hub/config.php | 3 + core | 2 +- 8 files changed, 88 insertions(+), 84 deletions(-) diff --git a/application/hub/classes/container/socket/class_SocketContainer.php b/application/hub/classes/container/socket/class_SocketContainer.php index d6eaa232c..166b1b7e0 100644 --- a/application/hub/classes/container/socket/class_SocketContainer.php +++ b/application/hub/classes/container/socket/class_SocketContainer.php @@ -883,7 +883,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function clearLastSocketError () { // Should be valid socket - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Clearing socket error - CALLED!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Clearing socket error, socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); @@ -896,7 +896,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita socket_clear_error($this->getSocketResource()); // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Error cleared - EXIT!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Error cleared - EXIT!'); } /** @@ -983,7 +983,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: sentBytes[%s]=%d,result=%d', strtoupper($this->getSocketProtocol()), gettype($sentBytes), $sentBytes, intval($result))); if ($result === FALSE) { // Failed delivery! - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: socket_write() failed, please evalutate. - EXIT!', strtoupper($this->getSocketProtocol()))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: socket_write() failed, please evaluate. - EXIT!', strtoupper($this->getSocketProtocol()))); return FALSE; } elseif (($sentBytes === 0) && (strlen($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA]) > 0)) { // Nothing sent means all data has been sent @@ -1023,7 +1023,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function handleSocketError (string $method, int $line) { // This method handles only socket resources - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-SYSTEM: Handling socket errorCode=%d - CALLED!', $this->getLastSocketErrorCode())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: method=%s,line=%d,lastSocketErrorCode=%d - CALLED!', strtoupper($this->getSocketProtocol()), $method, $line, $this->getLastSocketErrorCode())); if (!$this->isValidSocket()) { // No resource, abort here throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); @@ -1042,13 +1042,14 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $handlerName = $this->getSocketErrorHandlerFromCode($errorCode); // Call-back the error handler method - call_user_func_array(array($this, $handlerName)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: handlerName=%s', strtoupper($this->getSocketProtocol()), $handlerName)); + call_user_func_array([$this, $handlerName], []); // Finally clear the error because it has been handled $this->clearLastSocketError(); // 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()))); } /** @@ -1098,7 +1099,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $this->shutdownSocket(); // Throw it again - throw new SocketBindingException(array($this, $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET); + throw new SocketBindingException([$this, $socketError, $errorMessage], self::EXCEPTION_INVALID_SOCKET); } /** @@ -1120,7 +1121,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $this->shutdownSocket(); // Throw it again - throw new SocketConnectionException(array($this, $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET); + throw new SocketConnectionException([$this, $socketError, $errorMessage], self::EXCEPTION_INVALID_SOCKET); } /** @@ -1142,7 +1143,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $this->shutdownSocket(); // Throw it again - throw new SocketConnectionException(array($this, $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET); + throw new SocketConnectionException([$this, $socketError, $errorMessage], self::EXCEPTION_INVALID_SOCKET); } /** @@ -1164,7 +1165,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $this->shutdownSocket(); // Throw it again - throw new SocketConnectionException(array($this, $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET); + throw new SocketConnectionException([$this, $socketError, $errorMessage], self::EXCEPTION_INVALID_SOCKET); } /** @@ -1186,7 +1187,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $this->shutdownSocket(); // Throw it again - throw new SocketConnectionException(array($this, $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET); + throw new SocketConnectionException([$this, $socketError, $errorMessage], self::EXCEPTION_INVALID_SOCKET); } /** @@ -1208,7 +1209,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $this->halfShutdownSocket(); // Throw it again - throw new SocketConnectionException(array($this, $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET); + throw new SocketConnectionException([$this, $socketError, $errorMessage], self::EXCEPTION_INVALID_SOCKET); } /** @@ -1230,7 +1231,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $this->shutdownSocket(); // Throw it again - throw new SocketConnectionException(array($this, $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET); + throw new SocketConnectionException([$this, $socketError, $errorMessage], self::EXCEPTION_INVALID_SOCKET); } /** @@ -1252,7 +1253,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $this->shutdownSocket(); // Throw it again - throw new SocketOperationException(array($this, $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET); + throw new SocketOperationException([$this, $socketError, $errorMessage], self::EXCEPTION_INVALID_SOCKET); } /** diff --git a/application/hub/classes/factories/socket/class_SocketFactory.php b/application/hub/classes/factories/socket/class_SocketFactory.php index 8e06d53d6..64c650439 100644 --- a/application/hub/classes/factories/socket/class_SocketFactory.php +++ b/application/hub/classes/factories/socket/class_SocketFactory.php @@ -204,16 +204,14 @@ class SocketFactory extends ObjectFactory { /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: packageInstance=%s - CALLED!', $packageInstance->__toString())); $socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: Created socketResource[%s]=%s ...', gettype($socketResource), $socketResource)); - // Construct container class, this won't be reached if an exception is thrown - $socketInstance = ObjectFactory::createObjectByConfiguredName('socket_container_class', array( + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: Created socketResource[%s]=%s ...', gettype($socketResource), $socketResource)); + $socketInstance = ObjectFactory::createObjectByConfiguredName('socket_container_class', [ $socketResource, StorableSocket::SOCKET_PROTOCOL_TCP, $packageInstance, StorableSocket::CONNECTION_TYPE_OUTGOING, - )); + ]); // Is the socket resource valid? if (!$socketInstance->isValidSocket()) { @@ -233,7 +231,7 @@ class SocketFactory extends ObjectFactory { // Set the option to reuse the port //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Enabling re-use address ...'); - if (!$socketInstance->enableSocketReuseAddress()) { + if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('tcp_socket_enable_reuse_address') && !$socketInstance->enableSocketReuseAddress()) { // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); } diff --git a/application/hub/classes/handler/package/class_NetworkPackageHandler.php b/application/hub/classes/handler/package/class_NetworkPackageHandler.php index 6e4ebeb56..b94154cb1 100644 --- a/application/hub/classes/handler/package/class_NetworkPackageHandler.php +++ b/application/hub/classes/handler/package/class_NetworkPackageHandler.php @@ -1129,11 +1129,11 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei // If there was an error, don't continue here if (($sentBytes === 0) && (strlen($encodedDataArray[self::RAW_INDEX_ENCODED_DATA]) > 0)) { // Nothing sent means all data has been sent - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: All sent! (LINE=' . __LINE__ . ')'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: All sent! (LINE=' . __LINE__ . ')'); return; } elseif (strlen($encodedDataArray[self::RAW_INDEX_ENCODED_DATA]) == 0) { // Abort here, all sent! - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: All sent! (LINE=' . __LINE__ . ')'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: All sent! (LINE=' . __LINE__ . ')'); return; } @@ -1141,7 +1141,7 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei $encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE] = $socketInstance; // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: Pushing raw data back to stacker, as still some data is pending delivery.'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Pushing raw data back to stacker, as still some data is pending delivery.'); // Push array back in stack $this->getStackInstance()->pushNamed(self::STACKER_NAME_OUTGOING_STREAM, $encodedDataArray); diff --git a/application/hub/classes/helper/connection/class_BaseConnectionHelper.php b/application/hub/classes/helper/connection/class_BaseConnectionHelper.php index 22e7edd54..6c2bdc56f 100644 --- a/application/hub/classes/helper/connection/class_BaseConnectionHelper.php +++ b/application/hub/classes/helper/connection/class_BaseConnectionHelper.php @@ -141,7 +141,7 @@ abstract class BaseConnectionHelper extends BaseHubSystemHelper implements Visit * @return $shuttedDown Whether this connection is shutted down */ public final function isShuttedDown () { - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: ' . $this->__toString() . ',shuttedDown=' . intval($this->shuttedDown)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-CONNECTION-HELPER: ' . $this->__toString() . ',shuttedDown=' . intval($this->shuttedDown)); return $this->shuttedDown; } @@ -190,12 +190,12 @@ abstract class BaseConnectionHelper extends BaseHubSystemHelper implements Visit */ private function getConnectionClassNameFromSocket () { // Get recipient address/port - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: CALLED!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-CONNECTION-HELPER: CALLED!'); $recipientAddress = $this->getSocketInstance()->getSocketRecipientAddress(); $recipientPort = $this->getSocketInstance()->getSocketRecipientPort(); // Construct it - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: recipientAddress=%s,recipientPort=%d', $recipientAddress, $recipientPort)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: recipientAddress=%s,recipientPort=%d', $recipientAddress, $recipientPort)); $className = sprintf('%s:%d:%s', $recipientAddress, $recipientPort, @@ -203,7 +203,7 @@ abstract class BaseConnectionHelper extends BaseHubSystemHelper implements Visit ); // ... and return it - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: className=%s - EXIT!', $className)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: className=%s - EXIT!', $className)); return $className; } @@ -214,11 +214,11 @@ abstract class BaseConnectionHelper extends BaseHubSystemHelper implements Visit */ private function initState() { // Get the state factory and create the initial state. - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: CALLED!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-CONNECTION-HELPER: CALLED!'); PeerStateFactory::createPeerStateInstanceByName('init', $this); // Trace message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: EXIT!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-CONNECTION-HELPER: EXIT!'); } /** @@ -236,14 +236,14 @@ abstract class BaseConnectionHelper extends BaseHubSystemHelper implements Visit */ private function getRawDataFromPackageArray (DeliverablePackage $packageInstance) { // Make sure the final hash is set - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: currentFinalHash=' . $this->currentFinalHash); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-CONNECTION-HELPER: currentFinalHash=' . $this->currentFinalHash); assert((is_string($this->currentFinalHash)) && (!empty($this->currentFinalHash))); // Get the next raw data chunk from the fragmenter $rawDataChunk = $this->getFragmenterInstance()->getNextRawDataChunk($this->currentFinalHash); // Get chunk hashes and chunk data - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: rawDataChunk=' . print_r($rawDataChunk, TRUE)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-CONNECTION-HELPER: rawDataChunk=' . print_r($rawDataChunk, TRUE)); $chunkHashes = array_keys($rawDataChunk); $chunkData = array_values($rawDataChunk); @@ -251,19 +251,19 @@ abstract class BaseConnectionHelper extends BaseHubSystemHelper implements Visit $rawData = ''; // Is the required data there? - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: chunkHashes()=' . count($chunkHashes) . ',chunkData()=' . count($chunkData)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-CONNECTION-HELPER: chunkHashes()=' . count($chunkHashes) . ',chunkData()=' . count($chunkData)); //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->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__, __LINE__)->debugOutput('CONNECTION-HELPER: rawData()=' . strlen($chunkData[0]) . ' bytes.'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-CONNECTION-HELPER: rawData()=' . strlen($chunkData[0]) . ' bytes.'); $rawData = $chunkData[0]; } // END - if // Return raw data - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: rawData(%d)=%s - EXIT!', strlen($rawData), $rawData)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: rawData(%d)=%s - EXIT!', strlen($rawData), $rawData)); return $rawData; } @@ -275,13 +275,13 @@ abstract class BaseConnectionHelper extends BaseHubSystemHelper implements Visit */ public function accept (Visitor $visitorInstance) { // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: visitorInstance=%s - CALLED!', $visitorInstance->__toString())); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: visitorInstance=%s - CALLED!', $visitorInstance->__toString())); // Just call the visitor $visitorInstance->visitConnectionHelper($this); // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: EXIT!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-CONNECTION-HELPER: EXIT!'); } /** @@ -292,7 +292,7 @@ abstract class BaseConnectionHelper extends BaseHubSystemHelper implements Visit */ public function sendRawPackageData (DeliverablePackage $packageInstance) { // Trace message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: packageInstance=%s - CALLED!', $packageInstance->__toString())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: packageInstance=%s - CALLED!', $packageInstance->__toString())); // The helper's state must be 'connected' $this->getStateInstance()->validatePeerStateConnected(); @@ -301,10 +301,10 @@ abstract class BaseConnectionHelper extends BaseHubSystemHelper implements Visit $finalHash = $this->getFragmenterInstance()->fragmentPackageArray($packageInstance, $this); // Is the final hash set? - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: finalHash[%s]=%s', gettype($finalHash), $finalHash)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: finalHash[%s]=%s', gettype($finalHash), $finalHash)); if ($finalHash !== TRUE) { // Set final hash - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: Setting finalHash=%s,currentFinalHash[%s]=%s', $finalHash, gettype($this->currentFinalHash), $this->currentFinalHash)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: Setting finalHash=%s,currentFinalHash[%s]=%s', $finalHash, gettype($this->currentFinalHash), $this->currentFinalHash)); $this->currentFinalHash = $finalHash; } // END - if @@ -318,32 +318,32 @@ abstract class BaseConnectionHelper extends BaseHubSystemHelper implements Visit // Fill sending buffer with data while (TRUE) { // Convert the package data array to a raw data stream - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: dataStream()=%d - BEFORE!', strlen($dataStream))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: dataStream()=%d - BEFORE!', strlen($dataStream))); $dataStream = $this->getRawDataFromPackageArray($packageInstance); // Is it empty? - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: dataStream()=%d - AFTER!', strlen($dataStream))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: dataStream()=%d - AFTER!', strlen($dataStream))); if (strlen($dataStream) == 0) { // Abort here - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: dataStream is now empty, exiting loop ...'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-CONNECTION-HELPER: dataStream is now empty, exiting loop ...'); break; } // END - if // Add raw data - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: Adding %d bytes to the sending buffer ...', strlen($dataStream))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: Adding %d bytes to the sending buffer ...', strlen($dataStream))); $rawData .= $dataStream; } // END - while // Calculate buffer size - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: rawData()=%d - after loop ...', strlen($rawData))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: rawData()=%d - after loop ...', strlen($rawData))); $bufferSize = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($this->getProtocolName() . '_buffer_length'); // Encode the raw data with our output-stream - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: bufferSize=%d', $bufferSize)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: bufferSize=%d', $bufferSize)); $encodedData = $this->getOutputStreamInstance()->streamData($rawData); // Init array - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: socketResource[%s]=%s', gettype($this->getSocketInstance()->getSocketResource()), $this->getSocketInstance()->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: socketResource[%s]=%s', gettype($this->getSocketInstance()->getSocketResource()), $this->getSocketInstance()->getSocketResource())); $encodedDataArray = array( NetworkPackageHandler::RAW_INDEX_FINAL_HASH => $this->currentFinalHash, NetworkPackageHandler::RAW_INDEX_ENCODED_DATA => $encodedData, @@ -357,7 +357,7 @@ abstract class BaseConnectionHelper extends BaseHubSystemHelper implements Visit $this->getPackageHandlerInstance()->getStackInstance()->pushNamed(NetworkPackageHandler::STACKER_NAME_OUTGOING_STREAM, $encodedDataArray); // Trace message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: EXIT!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-CONNECTION-HELPER: EXIT!'); } } diff --git a/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php b/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php index c0b72c7e9..bf689498b 100644 --- a/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php +++ b/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php @@ -12,8 +12,11 @@ use Org\Shipsimu\Hub\Traits\Container\Socket\StorableSocketTrait; use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap; use Org\Mxchange\CoreFramework\Traits\Handler\HandleableTrait; +// Import SPL stuff +use \BadMethodCallException; + /** - * A ??? connection helper class + * A IPv4 connection helper class * * @author Roland Haeder * @version 0.0.0 @@ -80,60 +83,60 @@ abstract class BaseIpV4ConnectionHelper extends BaseConnectionHelper { * instance with currently configured timeout. * * @return $isConnected Whether the connection went fine + * @throws BadMethodCallException If this connection helper is not yet initialized * @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 connectToPeerBySocketRecipient () { - //* DEBUG-DIE: */ die(__METHOD__.':socketInstance='.print_r($this->getSocketInstance(), TRUE)); - // Only call this if the connection is fully initialized - assert($this->isInitialized()); + //* DEBUG-DIE: */ die(__METHOD__.':socketInstance='.print_r($this->getSocketInstance(), TRUE)); + if (!$this->isInitialized()) { + // Bad method call + throw new BadMethodCallException(sprintf('BASE-IPV4-CONNECTION-HELPER: protocolName=%s is not yet initialized.', $this->getProtocolName())); + } // "Cache" socket resource and timeout config $timeout = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('socket_timeout_seconds'); - // Debug output - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: Trying to connect to %s with socketResource[%s]=%s ...', $this->getSocketInstance()->getSocketRecipient(), gettype($this->getSocketInstance()->getSocketResource()), $this->getSocketInstance()->getSocketResource())); - // Get current time $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 $socketError = $this->getSocketInstance()->getLastSocketErrorCode(); - // Log error code and status - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: 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 - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: timeout=' . $timeout .' reached, connection attempt failed.'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-IPV4-CONNECTION-HELPER: socketResource=%s,socketError=%d (%s),isConnected=%s', $this->getSocketInstance()->getSocketResource(), $socketError, $this->getSocketInstance()->getLastSocketErrorMessage(), intval($isConnected))); + if ($socketError == SOCKET_EINPROGRESS) { + // "Operation in progress" is common for non-blocking I/O + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-IPV4-CONNECTION-HELPER: socketResource=%s - BREAK!', $this->getSocketInstance()->getSocketResource())); + break; + } elseif ($socketError == SOCKET_EALREADY) { + // Calculate passed time + $reached = (time() - $time); + // Now, is that attempt within parameters? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-IPV4-CONNECTION-HELPER: socketResource=%s,reached=%d,timeout=%d', $this->getSocketInstance()->getSocketResource(), $reached, $timeout)); + if ($reached >= $timeout) { // Didn't work within timeout + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-IPV4-CONNECTION-HELPER: timeout=%d reached, connection attempt failed. - BREAK!', $timeout)); $isConnected = FALSE; $hasTimedOut = TRUE; break; - } // END - if + } // Sleep about one second $this->idle(1000); } elseif ($socketError != 0) { - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: socketError=' . $socketError . ' detected.'); - // Stop on everything else pronto + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-IPV4-CONNECTION-HELPER: socketResource=%s,socketError=%d detected, setting isConnected=FALSE - BREAK!', $this->getSocketInstance()->getSocketResource(), $socketError)); $isConnected = FALSE; break; } - } // END - while - - // Log error code - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: socketError=' . $socketError . ',isConnected=' . intval($isConnected) . ',hasTimedOut=' . intval($hasTimedOut) . ' after while() loop.'); + } /* * All IPv4-based connections are non-blocking used by this program or @@ -141,21 +144,18 @@ abstract class BaseIpV4ConnectionHelper extends BaseConnectionHelper { * what is wanted here. This means, that all connections will end with * isConnected=FALSE here. */ + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-IPV4-CONNECTION-HELPER: socketResource=%s,socketError=%d,isConnected=%d,hasTimedOut=%d after while() loop.', $this->getSocketInstance()->getSocketResource(), $socketError, intval($isConnected), intval($hasTimedOut))); if (($hasTimedOut === FALSE) && ($socketError == SOCKET_EINPROGRESS)) { - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: 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. + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-IPV4-CONNECTION-HELPER: Clearing "operation in progress" as this is for 99.999% chance a non-blocking I/O operation.'); $isConnected = TRUE; // Clear error $this->getSocketInstance()->clearLastSocketError(); - } // END - if - - // Log error code - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: socketError=' . $socketError . ',isConnected=' . intval($isConnected) . ',hasTimedOut=' . intval($hasTimedOut) . ' after while() loop.'); + } // Is the peer connected? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-IPV4-CONNECTION-HELPER: socketResource=%s,socketError=%d,isConnected=%d,hasTimedOut=%d after while() loop.', $this->getSocketInstance()->getSocketResource(), $socketError, intval($isConnected), intval($hasTimedOut))); if ($isConnected === TRUE) { // Connection is fully established here, so change the state. PeerStateFactory::createPeerStateInstanceByName('connected', $this); @@ -167,10 +167,8 @@ abstract class BaseIpV4ConnectionHelper extends BaseConnectionHelper { PeerStateFactory::createPeerStateInstanceByName('problem', $this); } - // Log error code - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: isConnected=' . intval($isConnected) . ' - EXIT!'); - // Return status + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-IPV4-CONNECTION-HELPER: isConnected=%s - EXIT!', intval($isConnected))); return $isConnected; } @@ -180,7 +178,7 @@ abstract class BaseIpV4ConnectionHelper extends BaseConnectionHelper { * @return void */ protected final function markConnectionShuttedDown () { - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: ' . $this->__toString() . ' has been marked as shutted down'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-IPV4-CONNECTION-HELPER: ' . $this->__toString() . ' has been marked as shutted down'); $this->shuttedDown = TRUE; // And remove the (now invalid) socket diff --git a/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php b/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php index 3ae8e5bf7..1609fe028 100644 --- a/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php +++ b/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php @@ -84,9 +84,12 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection // Set handler instance $helperInstance->setHandlerInstance($handlerInstance); + + // Mark helper as initialized + $helperInstance->setIsInitialized(true); } catch (NoValidHostnameException $e) { // Debug message - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: Failed to resolve %s: %s', $packageInstance->getRecipientUnl(), $e->getMessage())); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TCPv4-CONNECTION-HELPER: Failed to resolve %s: %s', $packageInstance->getRecipientUnl(), $e->getMessage())); // Is the recipient equal as configured UNL? if (substr($packageInstance->getRecipientUnl(), 0, strlen(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('external_address'))) == FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('external_address')) { @@ -123,13 +126,14 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection } // Now connect to it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-CONNECTION-HELPER: Invoking helperInstance->connectToPeerBySocketRecipient() ...'); if (!$helperInstance->connectToPeerBySocketRecipient()) { // Debug message self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TCP-CONNECTION-HELPER: helperInstance=%s,locatorInstance->unlData=%s', $helperInstance->__toString(), print_r($locatorInstance->getUnlData(), TRUE))); // Handle socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); - } // END - if + } // Okay, that should be it. Return it... /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TCP-CONNECTION-HELPER: socketInstance=%s - EXIT!', $socketInstance->__toString())); diff --git a/application/hub/config.php b/application/hub/config.php index da82e3287..dde2e1d85 100644 --- a/application/hub/config.php +++ b/application/hub/config.php @@ -787,6 +787,9 @@ $cfg->setConfigEntry('tcp_socket_accept_wait_sec', 3); // CFG: TCP-SOCKET-ACCEPT-WAIT-USEC $cfg->setConfigEntry('tcp_socket_accept_wait_usec', 0); +// CFG: TCP-SOCKET-ENABLE-REUSE-ADDRESS +$cfg->setConfigEntry('tcp_socket_enable_reuse_address', true); + // CFG: NODE-STATE-CHECKED-PACKAGE-CLASS $cfg->setConfigEntry('node_state_checked_package_class', 'NewConnectionNodeState'); diff --git a/core b/core index 6187a6c39..69c5071a6 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit 6187a6c39a906ce7ae3ae89cdbc4f6a59a69535d +Subproject commit 69c5071a6d75b58df3c1f4c6057a09a3ac491dda -- 2.39.5