*
* @author Roland Haeder <webmaster@ship-simu.org>
* @version 0.0.0
- * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team
+ * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
* @license GNU GPL 3.0 or any newer version
* @link http://www.ship-simu.org
* @todo Find an interface for hub helper
}
/**
- * Creates a socket resource ("connection") for given recipient in package data
+ * Creates a half-connected socket resource ("connection") for given
+ * recipient in package data. After you called this method you still need to
+ * connect to the other node.
*
* @param $packageData Raw package data
* @return $socketResource Socket resource
// Is the socket resource valid?
if (!is_resource($socketResource)) {
- // Something bad happened
+ /*
+ * Something bad happened, calling handleSocketError() is not
+ * possible here because that method would throw an
+ * InvalidSocketException back.
+ */
throw new SocketCreationException(array($helperInstance, gettype($socketResource)), BaseListener::EXCEPTION_SOCKET_CREATION_FAILED);
} // END - if
// Check if there was an error else
if ($socketError > 0) {
// Handle this socket error with a faked recipientData array
- $helperInstance->handleSocketError($socketResource, array('0.0.0.0', '0'));
+ $helperInstance->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0'));
// Then throw again
throw new SocketCreationException(array($helperInstance, gettype($socketResource), $socketError, socket_strerror($socketError)), BaseListener::EXCEPTION_SOCKET_CREATION_FAILED);
} // END - if
- // 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
- $helperInstance->handleSocketError($socketResource, array('0.0.0.0', '0'));
-
- // And throw again
- throw new SocketOptionException(array($helperInstance, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
- } // END - if
-
// Set the resource
$helperInstance->setSocketResource($socketResource);
+ // Init connection
+ $helperInstance->initConnection();
+
// @TODO The whole resolving part should be moved out and made more configurable
// Init recipient data
$recipientData = NULL;
$recipientData = explode(':', HubTools::resolveSessionId($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]));
} catch (NoValidHostnameException $e) {
// Debug message
- $helperInstance->debugOutput('CONNECTION: Failed to resolve ' . $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ':' . $e->getMessage());
+ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __LINE__ . ']: Failed to resolve ' . $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ':' . $e->getMessage());
// Is the recipient equal as configured IP
if (substr($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT], 0, strlen($helperInstance->getConfigInstance()->getConfigEntry('external_ip'))) == $helperInstance->getConfigInstance()->getConfigEntry('external_ip')) {
$helperInstance->setAddress($recipientData[0]);
$helperInstance->setPort($recipientData[1]);
- // Debug message
- $helperInstance->debugOutput('CONNECTION: Connecting to ' . $recipientData[0] . ':' . $recipientData[1]);
-
// Now connect to it
- if (!@socket_connect($socketResource, $recipientData[0], $recipientData[1])) {
+ if (!$helperInstance->connectToPeerByRecipientData($recipientData)) {
// Handle socket error
- // @TODO Handle 115 error ("operation in progress")
- $helperInstance->handleSocketError($socketResource, $recipientData);
- /*
- // Get socket error code for verification
- $socketError = socket_last_error($socketResource);
-
- // And throw again, but not for 'Operation now in progress'; the shutdownSocket() method should wait a little
- if ($socketError != 115) {
- // Get error message
- $errorMessage = socket_strerror($socketError);
-
- // Shutdown this socket
- $helperInstance->shutdownSocket($socketResource);
-
- // Throw it again
- throw new SocketConnectionException(array($helperInstance, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
- } else {
- // Debug output
- $helperInstance->debugOutput('CONNECTION: Operation is in progress, this usual for non-blocking connections.');
- }
- */
+ $helperInstance->handleSocketError(__METHOD__, __LINE__, $socketResource, $recipientData);
} // END - if
- // Set socket in non-blocking mode
- if (!socket_set_nonblock($socketResource)) {
- // Handle this socket error with a faked recipientData array
- $helperInstance->handleSocketError($socketResource, array('0.0.0.0', '0'));
-
- // And throw again
- throw new SocketOptionException(array($helperInstance, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
- } // END - if
-
- // Connection is fully established here, so change the state
- PeerStateFactory::createPeerStateInstanceByName('connected', $helperInstance);
-
// Okay, that should be it. Return it...
return $socketResource;
}
*/
public function doShutdown () {
// Debug message
- $this->debugOutput('HELPER: Shutting down socket resource ' . $this->getSocketResource());
+ self::createDebugInstance(__CLASS__)->debugOutput('HELPER[' . __LINE__ . ']: Shutting down socket resource ' . $this->getSocketResource());
// Clear any previous errors
socket_clear_error($this->getSocketResource());
// Call the shutdown function on the currently set socket
- if (!@socket_shutdown($this->getSocketResource())) {
+ if (!socket_shutdown($this->getSocketResource())) {
// Could not shutdown socket, this is fine if e.g. the other side is not connected, so analyse it
if (socket_last_error($this->getSocketResource()) != 107) {
// Something bad happened while we shutdown a socket
} // END - if
} // END - if
+ // Try to make blocking IO for socket_close()
+ socket_set_block($this->getSocketResource());
+
+ // Drop all data (don't sent any on socket closure)
+ socket_set_option($this->getSocketResource(), SOL_SOCKET, SO_LINGER, array('l_onoff' => 1, 'l_linger' => 0));
+
+ // Finally close socket to free some resources
+ socket_close($this->getSocketResource());
+
// Mark this connection as shutted down
$this->markConnectionShuttedDown();
}