3 * A ??? connection helper class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub 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 BaseIpV4ConnectionHelper extends BaseConnectionHelper {
29 private $connectionPort = 0;
32 * Protected constructor
34 * @param $className Name of implementing class
37 protected function __construct ($className) {
38 // Call parent constructor
39 parent::__construct($className);
43 * Getter for port number to satify HandleableProtocol
45 * @return $connectionPort The port number
47 public final function getConnectionPort () {
48 return $this->connectionPort;
52 * Setter for port number to satify HandleableProtocol
54 * @param $connectionPort The port number
57 protected final function setConnectionPort ($connectionPort) {
58 $this->connectionPort = $connectionPort;
62 * Initializes the current connection
65 * @throws SocketOptionException If setting any socket option fails
67 protected function initConnection () {
68 // Get socket resource
69 $socketResource = $this->getSocketResource();
71 // Set the option to reuse the port
72 if (!socket_set_option($socketResource, SOL_SOCKET, SO_REUSEADDR, 1)) {
73 // Handle this socket error with a faked recipientData array
74 $this->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0'));
77 // @TODO Move this to the socket error handler
78 throw new SocketOptionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
82 * Set socket to non-blocking mode before trying to establish a link to
83 * it. This is now the default behaviour for all connection helpers who
84 * call initConnection(); .
86 if (!socket_set_nonblock($socketResource)) {
87 // Handle this socket error with a faked recipientData array
88 $helperInstance->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0'));
91 throw new SocketOptionException(array($helperInstance, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
94 // Last step: mark connection as initialized
95 $this->setIsInitialized(TRUE);
99 * Attempts to connect to a peer by given IP number and port from a valid
100 * unlData array with currently configured timeout.
102 * @param $unlData Valid UNL data array
103 * @return $isConnected Whether the connection went fine
104 * @see Please see http://de.php.net/manual/en/function.socket-connect.php#84465 for original code
105 * @todo Rewrite the while() loop to a iterator to not let the software stay very long here
107 protected function connectToPeerByUnlData (array $unlData) {
108 // Only call this if the connection is initialized by initConnection()
109 assert($this->isInitialized());
111 // Is unlData complete?
112 assert(isset($unlData[UniversalNodeLocator::UNL_PART_PROTOCOL]));
113 assert(isset($unlData[UniversalNodeLocator::UNL_PART_ADDRESS]));
114 assert(isset($unlData[UniversalNodeLocator::UNL_PART_PORT]));
116 // "Cache" socket resource and timeout config
117 $socketResource = $this->getSocketResource();
118 $timeout = $this->getConfigInstance()->getConfigEntry('socket_timeout_seconds');
121 self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Trying to connect to ' . $unlData[UniversalNodeLocator::UNL_PART_ADDRESS] . ':' . $unlData[UniversalNodeLocator::UNL_PART_PORT] . ' with socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' ...');
124 $hasTimedOut = FALSE;
127 // Try to connect until it is connected
128 while ($isConnected = !@socket_connect($socketResource, $unlData[UniversalNodeLocator::UNL_PART_ADDRESS], $unlData[UniversalNodeLocator::UNL_PART_PORT])) {
129 // Get last socket error
130 $socketError = socket_last_error($socketResource);
132 // Log error code and status
133 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: socketError=' . $socketError . ',isConnected=' . intval($isConnected));
135 // Skip any errors which may happen on non-blocking connections
136 if (($socketError == SOCKET_EINPROGRESS) || ($socketError == SOCKET_EALREADY)) {
137 // Now, is that attempt within parameters?
138 if ((time() - $time) >= $timeout) {
140 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: timeout=' . $timeout .' reached, connection attempt failed.');
142 // Didn't work within timeout
143 $isConnected = FALSE;
148 // Sleep about one second
150 } elseif ($socketError != 0) {
152 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: socketError=' . $socketError . ' detected.');
154 // Stop on everything else pronto
155 $isConnected = FALSE;
161 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: socketError=' . $socketError . ',isConnected=' . intval($isConnected) . ',hasTimedOut=' . intval($hasTimedOut) . ' after while() loop.');
164 * All IPv4-based connections are non-blocking used by this program or
165 * else the PHP process will "hang" until a peer connects which is not
166 * what is wanted here. This means, that all connections will end with
167 * isConnected=FALSE here.
169 if (($hasTimedOut === FALSE) && ($socketError == SOCKET_EINPROGRESS)) {
171 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Clearing "operation in progress" as this is for 99.999% chance a non-blocking I/O operation.');
173 // A "connection in progress" has not timed out. All fine.
177 socket_clear_error($socketResource);
180 // Is the peer connected?
181 if ($isConnected === TRUE) {
182 // Connection is fully established here, so change the state.
183 PeerStateFactory::createPeerStateInstanceByName('connected', $this);
186 * There was a problem connecting to the peer (this state is a meta
187 * state until the error handler has found the real cause).
189 PeerStateFactory::createPeerStateInstanceByName('problem', $this);
197 * Marks this connection as shutted down
201 protected final function markConnectionShuttedDown () {
202 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: ' . $this->__toString() . ' has been marked as shutted down');
203 $this->shuttedDown = TRUE;
205 // And remove the (now invalid) socket
206 $this->setSocketResource(FALSE);
209 // ************************************************************************
210 // Socket error handler call-back methods
211 // ************************************************************************
214 * Handles socket error 'connection timed out', but does not clear it for
215 * later debugging purposes.
217 * @param $socketResource A valid socket resource
218 * @param $unlData A valid UNL data array
220 * @throws SocketConnectionException The connection attempts fails with a time-out
222 protected function socketErrorConnectionTimedOutHandler ($socketResource, array $unlData) {
223 // Get socket error code for verification
224 $socketError = socket_last_error($socketResource);
227 $errorMessage = socket_strerror($socketError);
229 // Shutdown this socket
230 $this->shutdownSocket($socketResource);
233 throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
237 * Handles socket error 'resource temporary unavailable', but does not
238 * clear it for later debugging purposes.
240 * @param $socketResource A valid socket resource
241 * @param $unlData A valid UNL data array
243 * @throws SocketConnectionException The connection attempts fails with a time-out
245 protected function socketErrorResourceUnavailableHandler ($socketResource, array $unlData) {
246 // Get socket error code for verification
247 $socketError = socket_last_error($socketResource);
250 $errorMessage = socket_strerror($socketError);
252 // Shutdown this socket
253 $this->shutdownSocket($socketResource);
256 throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
260 * Handles socket error 'connection refused', but does not clear it for
261 * later debugging purposes.
263 * @param $socketResource A valid socket resource
264 * @param $unlData A valid UNL data array
266 * @throws SocketConnectionException The connection attempts fails with a time-out
268 protected function socketErrorConnectionRefusedHandler ($socketResource, array $unlData) {
269 // Get socket error code for verification
270 $socketError = socket_last_error($socketResource);
273 $errorMessage = socket_strerror($socketError);
275 // Shutdown this socket
276 $this->shutdownSocket($socketResource);
279 throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
283 * Handles socket error 'no route to host', but does not clear it for later
284 * debugging purposes.
286 * @param $socketResource A valid socket resource
287 * @param $unlData A valid UNL data array
289 * @throws SocketConnectionException The connection attempts fails with a time-out
291 protected function socketErrorNoRouteToHostHandler ($socketResource, array $unlData) {
292 // Get socket error code for verification
293 $socketError = socket_last_error($socketResource);
296 $errorMessage = socket_strerror($socketError);
298 // Shutdown this socket
299 $this->shutdownSocket($socketResource);
302 throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
306 * Handles socket error 'operation already in progress' which happens in
307 * method connectToPeerByUnlData() on timed out connection
310 * @param $socketResource A valid socket resource
311 * @param $unlData A valid UNL data array
313 * @throws SocketConnectionException The connection attempts fails with a time-out
315 protected function socketErrorOperationAlreadyProgressHandler ($socketResource, array $unlData) {
316 // Get socket error code for verification
317 $socketError = socket_last_error($socketResource);
320 $errorMessage = socket_strerror($socketError);
322 // Half-shutdown this socket (see there for difference to shutdownSocket())
323 $this->halfShutdownSocket($socketResource);
326 throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
330 * Handles socket error 'connection reset by peer', but does not clear it for
331 * later debugging purposes.
333 * @param $socketResource A valid socket resource
334 * @param $unlData A valid UNL data array
336 * @throws SocketConnectionException The connection attempts fails with a time-out
338 protected function socketErrorConnectionResetByPeerHandler ($socketResource, array $unlData) {
339 // Get socket error code for verification
340 $socketError = socket_last_error($socketResource);
343 $errorMessage = socket_strerror($socketError);
345 // Shutdown this socket
346 $this->shutdownSocket($socketResource);
349 throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
353 * Handles socket "error" 'operation now in progress' which can be safely
354 * passed on with non-blocking connections.
356 * @param $socketResource A valid socket resource
357 * @param $unlData A valid UNL data array
360 protected function socketErrorOperationInProgressHandler ($socketResource, array $unlData) {
361 self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Operation is now in progress, this is usual for non-blocking connections and is no bug.');