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 SocketCreationException If the socket could not be created
45 * @throws SocketOptionException If a socket option could not be set
46 * @throws SocketConnectionException If a connection could not be opened
48 public static function createConnectionFromPackageData (array $packageData) {
50 $helperInstance = new TcpConnectionHelper();
52 // Create a socket instance
53 $socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
55 // Is the socket resource valid?
56 if (!is_resource($socketResource)) {
57 // Something bad happened
58 throw new SocketCreationException(array($helperInstance, gettype($socketResource), 0, 'invalid'), BaseListener::EXCEPTION_SOCKET_CREATION_FAILED);
61 // Get socket error code for verification
62 $socketError = socket_last_error($socketResource);
64 // Check if there was an error else
65 if ($socketError > 0) {
66 // Shutdown this socket
67 $helperInstance->shutdownSocket($socketResource);
70 throw new SocketCreationException(array($helperInstance, gettype($socketResource), $socketError, socket_strerror($socketError)), BaseListener::EXCEPTION_SOCKET_CREATION_FAILED);
73 // Set the option to reuse the port
74 if (!socket_set_option($socketResource, SOL_SOCKET, SO_REUSEADDR, 1)) {
75 // Get socket error code for verification
76 $socketError = socket_last_error($socketResource);
79 $errorMessage = socket_strerror($socketError);
81 // Shutdown this socket
82 $helperInstance->shutdownSocket($socketResource);
85 throw new SocketOptionException(array($helperInstance, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
89 $helperInstance->setSocketResource($socketResource);
91 // Resolve any session ids; 0 = IP, 1 = Port
92 $recipientData = explode(':', HubTools::resolveSessionId($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]));
95 $helperInstance->setAddress($recipientData[0]);
96 $helperInstance->setPort($recipientData[1]);
99 $helperInstance->debugOutput('CONNECTION: Connecting to ' . $recipientData[0] . ':' . $recipientData[1]);
102 if (!@socket_connect($socketResource, $recipientData[0], $recipientData[1])) {
103 // Get socket error code for verification
104 $socketError = socket_last_error($socketResource);
106 // And throw again, but not for 'Operation now in progress', we should wait a little
107 if ($socketError != 115) {
109 $errorMessage = socket_strerror($socketError);
111 // Shutdown this socket
112 $helperInstance->shutdownSocket($socketResource);
115 throw new SocketConnectionException(array($helperInstance, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
118 $helperInstance->debugOutput('CONNECTION: Operation is in progress, this usual for non-blocking connections.');
122 // Now, we want non-blocking mode
123 if (!socket_set_nonblock($socketResource)) {
124 // Get socket error code for verification
125 $socketError = socket_last_error($socketResource);
128 $errorMessage = socket_strerror($socketError);
130 // Shutdown this socket
131 $helperInstance->shutdownSocket($socketResource);
134 throw new SocketOptionException(array($helperInstance, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
137 // We have a connection, so change the state
138 PeerStateFactory::createPeerStateInstanceByName('connected', $this);
140 // Okay, that should be it. So return it...
141 return $socketResource;
145 * Do the shutdown sequence for this connection helper
148 * @throws SocketShutdownException If the current socket could not be shut down
149 * @todo We may want to implement a filter for ease notification of other objects like our pool
151 public function doShutdown () {
153 $this->debugOutput('HELPER: Shutting down socket resource ' . $this->getSocketResource());
155 // Clear any previous errors
156 socket_clear_error($this->getSocketResource());
158 // Call the shutdown function on the currently set socket
159 if (!@socket_shutdown($this->getSocketResource())) {
160 // Could not shutdown socket, this is fine if e.g. the other side is not connected, so analyse it
161 if (socket_last_error($this->getSocketResource()) != 107) {
162 // Something bad happened while we shutdown a socket
163 throw new SocketShutdownException($this, BaseListener::EXCEPTION_INVALID_SOCKET);
167 // Mark this connection as shutted down
168 $this->markConnectionShuttedDown();