* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 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 * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ class TcpConnectionHelper extends BaseConnectionHelper implements Helper { /** * Protected constructor * * @return void */ protected function __construct () { // Call parent constructor parent::__construct(__CLASS__); // Set protocol $this->setProtocol('tcp'); } /** * Creates a socket resource ("connection") for given recipient in package data * * @param $packageData Raw package data * @return $socketResource Socket resource * @throws InvalidSocketException If the socket is invalid */ public static function createConnectionFromPackageData (array $packageData) { // Create an instance $helperInstance = new TcpConnectionHelper(); // Create a socket instance $socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); // 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); } // END - if // Get socket error code for verification $socketError = socket_last_error($socketResource); // 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 // 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); } // END - if // Set the resource $helperInstance->setSocketResource($socketResource); // Resolve any session ids; 0 = IP, 1 = Port $recipientData = explode(':', HubTools::resolveSessionId($packageData['recipient'])); // 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, this usual for non-blocking connections.'); } } // END - if // Okay, that should be it. So return it... return $socketResource; } /** * Do the shutdown sequence for TCP connections * * @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 */ public function doShutdown () { // Clear any previous errors socket_clear_error($this->getSocketResource()); // Call the shutdown function on the currently set socket 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 throw new SocketShutdownException($this, BaseListener::EXCEPTION_INVALID_SOCKET); } // END - if } // END - if // Mark this connection as shutted down $this->markConnectionShutdown(); } } // [EOF] ?>