X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=application%2Fhub%2Fmain%2Fhelper%2Fconnection%2Ftcp%2Fclass_TcpConnectionHelper.php;h=0fbfee3739b5ba4c0b21f6fe29665f083141d14a;hb=9a1243bea20f7602ef7cdfe912a7db5ee958fba8;hp=bf92729dd6eaede720c27528bc5de9b0048e3b59;hpb=39800cef903083fe82bb4e0d599b7c603c7eb44c;p=hub.git diff --git a/application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php b/application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php index bf92729dd..0fbfee373 100644 --- a/application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php +++ b/application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 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 @@ -22,7 +22,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class TcpConnectionHelper extends BaseConnectionHelper { +class TcpConnectionHelper extends BaseConnectionHelper implements ConnectionHelper { /** * Protected constructor * @@ -37,11 +37,16 @@ class TcpConnectionHelper extends BaseConnectionHelper { } /** - * 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 - * @throws InvalidSocketException If the socket is invalid + * @throws SocketCreationException If the socket could not be created + * @throws SocketOptionException If a socket option could not be set + * @throws SocketConnectionException If a connection could not be opened + * @todo $errorCode/-Message are now in handleSocketError()'s call-back methods */ public static function createConnectionFromPackageData (array $packageData) { // Create an instance @@ -52,8 +57,12 @@ class TcpConnectionHelper extends BaseConnectionHelper { // Is the socket resource valid? if (!is_resource($socketResource)) { - // Something bad happened - throw new InvalidSocketException(array($helperInstance, gettype($socketResource), 0, 'invalid'), BaseListener::EXCEPTION_INVALID_SOCKET); + /* + * 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 // Get socket error code for verification @@ -61,91 +70,83 @@ class TcpConnectionHelper extends BaseConnectionHelper { // Check if there was an error else if ($socketError > 0) { - // Then throw again - throw new InvalidSocketException(array($helperInstance, gettype($socketResource), $socketError, socket_strerror($socketError)), BaseListener::EXCEPTION_INVALID_SOCKET); - } // END - if - - // Set the option to reuse the port - if (!socket_set_option($socketResource, SOL_SOCKET, SO_REUSEADDR, 1)) { - // Get socket error code for verification - $socketError = socket_last_error($socketResource); - - // Get error message - $errorMessage = socket_strerror($socketError); - - // Shutdown this socket - $helperInstance->shutdownSocket($socketResource); - - // And throw again - throw new InvalidSocketException(array($helperInstance, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET); - } // END - if + // Handle this socket error with a faked recipientData array + $helperInstance->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0')); - // Now, we want non-blocking mode - if (!socket_set_nonblock($socketResource)) { - // Get socket error code for verification - $socketError = socket_last_error($socketResource); - - // Get error message - $errorMessage = socket_strerror($socketError); - - // Shutdown this socket - $helperInstance->shutdownSocket($socketResource); - - // And throw again - throw new InvalidSocketException(array($helperInstance, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET); + // Then throw again + throw new SocketCreationException(array($helperInstance, gettype($socketResource), $socketError, socket_strerror($socketError)), BaseListener::EXCEPTION_SOCKET_CREATION_FAILED); } // END - if // Set the resource $helperInstance->setSocketResource($socketResource); - // Resolve any session ids; 0 = IP, 1 = Port - $recipientData = explode(':', HubTools::resolveSessionId($packageData['recipient'])); + // Init connection + $helperInstance->initConnection(); + + // @TODO The whole resolving part should be moved out and made more configurable + // Init recipient data + $recipientData = NULL; + + // Try to solve the recipient + try { + // Resolve any session ids; 0 = IP, 1 = Port + $recipientData = explode(':', HubTools::resolveSessionId($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])); + } catch (NoValidHostnameException $e) { + // Debug message + 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')) { + // This connects to ship-simu.org and requests /ip.php which will return our external IP number + $recipientData[0] = ConsoleTools::determineExternalIp(); + + // Do we have hostname:ip match? + if (strpos($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT], ':') === false) { + // No hostname:ip! + $helperInstance->debugInstance($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ' does not contain ":". Please fix this.'); + } // END - if + + // "explode" the hostname:ip, so index 1 will be the port number + $recipientArray = explode(':', $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]); + + // Add the port + $recipientData[1] = $recipientArray[1]; + } else { + // It doesn't match, we need to take care of this later + $helperInstance->debugInstance($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . '!=' . $helperInstance->getConfigInstance()->getConfigEntry('external_ip')); + } + } // Set ip/port $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])) { - // Get socket error code for verification - $socketError = socket_last_error($socketResource); - - // And throw again, but not for 'Operation now in progress', we 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 InvalidSocketException(array($helperInstance, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET); - } else { - // Debug output - $helperInstance->debugOutput('CONNECTION: Operation is in progress, maybe recipient is down?'); - } + if (!$helperInstance->connectToPeerByRecipientData($recipientData)) { + // Handle socket error + $helperInstance->handleSocketError(__METHOD__, __LINE__, $socketResource, $recipientData); } // END - if - // Okay, that should be it. So return it... + // Okay, that should be it. Return it... return $socketResource; } /** - * Do the shutdown sequence for TCP connections + * Do the shutdown sequence for this connection helper * - * @todo We may want to implement a filter for ease notification of other objects like our pool * @return void * @throws SocketShutdownException If the current socket could not be shut down + * @todo We may want to implement a filter for ease notification of other objects like our pool */ public function doShutdown () { + // Debug message + 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 @@ -153,8 +154,17 @@ class TcpConnectionHelper extends BaseConnectionHelper { } // 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->markConnectionShutdown(); + $this->markConnectionShuttedDown(); } }