3 * A TCP connection helper class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
10 * @todo Find an interface for hub helper
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 class TcpConnectionHelper extends BaseConnectionHelper implements ConnectionHelper {
27 * Protected constructor
31 protected function __construct () {
32 // Call parent constructor
33 parent::__construct(__CLASS__);
36 $this->setProtocol('tcp');
40 * Creates a socket resource ("connection") for given recipient in package data
42 * @param $packageData Raw package data
43 * @return $socketResource Socket resource
44 * @throws InvalidSocketException If the socket is invalid
46 public static function createConnectionFromPackageData (array $packageData) {
48 $helperInstance = new TcpConnectionHelper();
50 // Create a socket instance
51 $socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
53 // Is the socket resource valid?
54 if (!is_resource($socketResource)) {
55 // Something bad happened
56 throw new InvalidSocketException(array($helperInstance, gettype($socketResource), 0, 'invalid'), BaseListener::EXCEPTION_INVALID_SOCKET);
59 // Get socket error code for verification
60 $socketError = socket_last_error($socketResource);
62 // Check if there was an error else
63 if ($socketError > 0) {
65 throw new InvalidSocketException(array($helperInstance, gettype($socketResource), $socketError, socket_strerror($socketError)), BaseListener::EXCEPTION_INVALID_SOCKET);
68 // Set the option to reuse the port
69 if (!socket_set_option($socketResource, SOL_SOCKET, SO_REUSEADDR, 1)) {
70 // Get socket error code for verification
71 $socketError = socket_last_error($socketResource);
74 $errorMessage = socket_strerror($socketError);
76 // Shutdown this socket
77 $helperInstance->shutdownSocket($socketResource);
80 throw new InvalidSocketException(array($helperInstance, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
84 $helperInstance->setSocketResource($socketResource);
86 // Resolve any session ids; 0 = IP, 1 = Port
87 $recipientData = explode(':', HubTools::resolveSessionId($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]));
90 $helperInstance->setAddress($recipientData[0]);
91 $helperInstance->setPort($recipientData[1]);
94 $helperInstance->debugOutput('CONNECTION: Connecting to ' . $recipientData[0] . ':' . $recipientData[1]);
97 if (!@socket_connect($socketResource, $recipientData[0], $recipientData[1])) {
98 // Get socket error code for verification
99 $socketError = socket_last_error($socketResource);
101 // And throw again, but not for 'Operation now in progress', we should wait a little
102 if ($socketError != 115) {
104 $errorMessage = socket_strerror($socketError);
106 // Shutdown this socket
107 $helperInstance->shutdownSocket($socketResource);
110 throw new InvalidSocketException(array($helperInstance, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
113 $helperInstance->debugOutput('CONNECTION: Operation is in progress, this usual for non-blocking connections.');
117 // Now, we want non-blocking mode
118 if (!socket_set_nonblock($socketResource)) {
119 // Get socket error code for verification
120 $socketError = socket_last_error($socketResource);
123 $errorMessage = socket_strerror($socketError);
125 // Shutdown this socket
126 $helperInstance->shutdownSocket($socketResource);
129 throw new InvalidSocketException(array($helperInstance, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
132 // Okay, that should be it. So return it...
133 return $socketResource;
137 * Do the shutdown sequence for this connection helper
140 * @throws SocketShutdownException If the current socket could not be shut down
141 * @todo We may want to implement a filter for ease notification of other objects like our pool
143 public function doShutdown () {
144 // Clear any previous errors
145 socket_clear_error($this->getSocketResource());
147 // Call the shutdown function on the currently set socket
148 if (!@socket_shutdown($this->getSocketResource())) {
149 // Could not shutdown socket, this is fine if e.g. the other side is not connected, so analyse it
150 if (socket_last_error($this->getSocketResource()) != 107) {
151 // Something bad happened while we shutdown a socket
152 throw new SocketShutdownException($this, BaseListener::EXCEPTION_INVALID_SOCKET);
156 // Mark this connection as shutted down
157 $this->markConnectionShutdown();