*/
private $connectionType = 'invalid';
- /**
- * Port number used
- */
- private $connectionPort = 0;
-
/**
* (IP) Adress used
*/
$this->connectionType = $connectionType;
}
- /**
- * Getter for port number to satify HandleableProtocol
- *
- * @return $connectionPort The port number
- */
- public final function getConnectionPort () {
- return $this->connectionPort;
- }
-
- /**
- * Setter for port number to satify HandleableProtocol
- *
- * @param $connectionPort The port number
- * @return void
- */
- protected final function setConenctionPort ($connectionPort) {
- $this->connectionPort = $connectionPort;
- }
-
/**
* Getter for IP address
*
}
/**
- * Initializes the current connection
+ * Setter for isInitialized
*
+ * @param $isInitialized Name of used protocol in this connection
* @return void
- * @throws SocketOptionException If setting any socket option fails
*/
- protected function initConnection () {
- // Get socket resource
- $socketResource = $this->getSocketResource();
-
- // Set the option to reuse the port
- if (!socket_set_option($socketResource, SOL_SOCKET, SO_REUSEADDR, 1)) {
- // Handle this socket error with a faked recipientData array
- $this->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0'));
-
- // And throw again
- // @TODO Move this to the socket error handler
- throw new SocketOptionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
- } // END - if
-
- /*
- * Set socket to non-blocking mode before trying to establish a link to
- * it. This is now the default behaviour for all connection helpers who
- * call initConnection(); .
- */
- if (!socket_set_nonblock($socketResource)) {
- // Handle this socket error with a faked recipientData array
- $helperInstance->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0'));
-
- // And throw again
- throw new SocketOptionException(array($helperInstance, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
- } // END - if
-
- // Last step: mark connection as initialized
- $this->isInitialized = TRUE;
+ protected final function setIsInitialized ($isInitialized) {
+ $this->isInitialized = $isInitialized;
}
/**
- * Attempts to connect to a peer by given IP number and port from a valid
- * recipientData array with currently configured timeout.
+ * Getter for isInitialized (NOTE: no 'get' prefix for boolean attributes!)
*
- * @param $recipientData A valid recipient data array, 0=IP; 1=PORT
- * @return $isConnected Whether the connection went fine
- * @see Please see http://de.php.net/manual/en/function.socket-connect.php#84465 for original code
- * @todo Rewrite the while() loop to a iterator to not let the software stay very long here
+ * @return $isInitialized Name of used protocol in this connection
*/
- protected function connectToPeerByRecipientData (array $recipientData) {
- // Only call this if the connection is initialized by initConnection()
- assert($this->isInitialized === TRUE);
-
- // Get current time
- $time = time();
-
- // "Cache" socket resource and timeout config
- $socketResource = $this->getSocketResource();
- $timeout = $this->getConfigInstance()->getConfigEntry('socket_timeout_seconds');
-
- // Debug output
- self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Trying to connect to ' . $recipientData[0] . ':' . $recipientData[1] . ' with socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' ...');
-
- // Try to connect until it is connected
- while ($isConnected = !@socket_connect($socketResource, $recipientData[0], $recipientData[1])) {
- // Get last socket error
- $socketError = socket_last_error($socketResource);
-
- // Skip any errors which may happen on non-blocking connections
- if (($socketError == SOCKET_EINPROGRESS) || ($socketError == SOCKET_EALREADY)) {
- // Now, is that attempt within parameters?
- if ((time() - $time) >= $timeout) {
- // Didn't work within timeout
- $isConnected = FALSE;
- break;
- } // END - if
-
- // Sleep about one second
- $this->idle(1000);
- } elseif ($socketError != 0) {
- // Stop on everything else pronto
- $isConnected = FALSE;
- break;
- }
- } // END - while
-
- // Is the peer connected?
- if ($isConnected === TRUE) {
- // Connection is fully established here, so change the state.
- PeerStateFactory::createPeerStateInstanceByName('connected', $this);
- } else {
- /*
- * There was a problem connecting to the peer (this state is a meta
- * state until the error handler has found the real cause).
- */
- PeerStateFactory::createPeerStateInstanceByName('problem', $this);
- }
-
- // Return status
- return $isConnected;
+ protected final function isInitialized () {
+ return $this->isInitialized;
}
/**
$this->getPackageInstance()->getStackInstance()->pushNamed(NetworkPackage::STACKER_NAME_OUTGOING_STREAM, $encodedDataArray);
}
- /**
- * Marks this connection as shutted down
- *
- * @return void
- */
- protected final function markConnectionShuttedDown () {
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: ' . $this->__toString() . ' has been marked as shutted down');
- $this->shuttedDown = TRUE;
-
- // And remove the (now invalid) socket
- $this->setSocketResource(FALSE);
- }
-
/**
* Getter for shuttedDown
*
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: ' . $this->__toString() . ',shuttedDown=' . intval($this->shuttedDown));
return $this->shuttedDown;
}
-
- // ************************************************************************
- // Socket error handler call-back methods
- // ************************************************************************
-
- /**
- * Handles socket error 'connection timed out', but does not clear it for
- * later debugging purposes.
- *
- * @param $socketResource A valid socket resource
- * @param $recipientData An array with two elements: 0=IP number, 1=port number
- * @return void
- * @throws SocketConnectionException The connection attempts fails with a time-out
- */
- protected function socketErrorConnectionTimedOutHandler ($socketResource, array $recipientData) {
- // Get socket error code for verification
- $socketError = socket_last_error($socketResource);
-
- // Get error message
- $errorMessage = socket_strerror($socketError);
-
- // Shutdown this socket
- $this->shutdownSocket($socketResource);
-
- // Throw it again
- throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
- }
-
- /**
- * Handles socket error 'resource temporary unavailable', but does not
- * clear it for later debugging purposes.
- *
- * @param $socketResource A valid socket resource
- * @param $recipientData An array with two elements: 0=IP number, 1=port number
- * @return void
- * @throws SocketConnectionException The connection attempts fails with a time-out
- */
- protected function socketErrorResourceUnavailableHandler ($socketResource, array $recipientData) {
- // Get socket error code for verification
- $socketError = socket_last_error($socketResource);
-
- // Get error message
- $errorMessage = socket_strerror($socketError);
-
- // Shutdown this socket
- $this->shutdownSocket($socketResource);
-
- // Throw it again
- throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
- }
-
- /**
- * Handles socket error 'connection refused', but does not clear it for
- * later debugging purposes.
- *
- * @param $socketResource A valid socket resource
- * @param $recipientData An array with two elements: 0=IP number, 1=port number
- * @return void
- * @throws SocketConnectionException The connection attempts fails with a time-out
- */
- protected function socketErrorConnectionRefusedHandler ($socketResource, array $recipientData) {
- // Get socket error code for verification
- $socketError = socket_last_error($socketResource);
-
- // Get error message
- $errorMessage = socket_strerror($socketError);
-
- // Shutdown this socket
- $this->shutdownSocket($socketResource);
-
- // Throw it again
- throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
- }
-
- /**
- * Handles socket error 'no route to host', but does not clear it for later
- * debugging purposes.
- *
- * @param $socketResource A valid socket resource
- * @param $recipientData An array with two elements: 0=IP number, 1=port number
- * @return void
- * @throws SocketConnectionException The connection attempts fails with a time-out
- */
- protected function socketErrorNoRouteToHostHandler ($socketResource, array $recipientData) {
- // Get socket error code for verification
- $socketError = socket_last_error($socketResource);
-
- // Get error message
- $errorMessage = socket_strerror($socketError);
-
- // Shutdown this socket
- $this->shutdownSocket($socketResource);
-
- // Throw it again
- throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
- }
-
- /**
- * Handles socket error 'operation already in progress' which happens in
- * method connectToPeerByRecipientData() on timed out connection
- * attempts.
- *
- * @param $socketResource A valid socket resource
- * @param $recipientData An array with two elements: 0=IP number, 1=port number
- * @return void
- * @throws SocketConnectionException The connection attempts fails with a time-out
- */
- protected function socketErrorOperationAlreadyProgressHandler ($socketResource, array $recipientData) {
- // Get socket error code for verification
- $socketError = socket_last_error($socketResource);
-
- // Get error message
- $errorMessage = socket_strerror($socketError);
-
- // Half-shutdown this socket (see there for difference to shutdownSocket())
- $this->halfShutdownSocket($socketResource);
-
- // Throw it again
- throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
- }
-
- /**
- * Handles socket error 'connection reset by peer', but does not clear it for
- * later debugging purposes.
- *
- * @param $socketResource A valid socket resource
- * @param $recipientData An array with two elements: 0=IP number, 1=port number
- * @return void
- * @throws SocketConnectionException The connection attempts fails with a time-out
- */
- protected function socketErrorConnectionResetByPeerHandler ($socketResource, array $recipientData) {
- // Get socket error code for verification
- $socketError = socket_last_error($socketResource);
-
- // Get error message
- $errorMessage = socket_strerror($socketError);
-
- // Shutdown this socket
- $this->shutdownSocket($socketResource);
-
- // Throw it again
- throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
- }
-
- /**
- * Handles socket "error" 'operation now in progress' which can be safely
- * passed on with non-blocking connections.
- *
- * @param $socketResource A valid socket resource
- * @param $recipientData An array with two elements: 0=IP number, 1=port number
- * @return void
- */
- protected function socketErrorOperationInProgressHandler ($socketResource, array $recipientData) {
- self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Operation is now in progress, this is usual for non-blocking connections and is no bug.');
- }
}
// [EOF]