*/
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);
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!');
}
/**
/* 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
*/
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);
$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())));
}
/**
$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);
}
/**
$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);
}
/**
$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);
}
/**
$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);
}
/**
$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);
}
/**
$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);
}
/**
$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);
}
/**
$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);
}
/**
/* 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()) {
// 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__);
}
// 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;
}
$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);
* @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;
}
*/
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,
);
// ... 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;
}
*/
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!');
}
/**
*/
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);
$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;
}
*/
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!');
}
/**
*/
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();
$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
// 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,
$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!');
}
}
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 <webmaster@ship-simu.org>
* @version 0.0.0
* 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
* 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);
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;
}
* @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
// 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')) {
}
// 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()));
// 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');
-Subproject commit 6187a6c39a906ce7ae3ae89cdbc4f6a59a69535d
+Subproject commit 69c5071a6d75b58df3c1f4c6057a09a3ac491dda