3 * A general ConnectionHelper class
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class BaseConnectionHelper extends BaseHubSystemHelper implements Registerable, ProtocolHandler {
26 const EXCEPTION_UNSUPPORTED_ERROR_HANDLER = 0x9100;
29 * Connection type 'incoming'
31 const CONNECTION_TYPE_INCOMING = 'incoming';
34 * Connection type 'outgoing'
36 const CONNECTION_TYPE_OUTGOING = 'outgoing';
39 * Connection type 'server'
41 const CONNECTION_TYPE_SERVER = 'server';
46 private $protocol = 'invalid';
61 private $sentData = 0;
69 * Whether this connection is initialized
71 private $isInitialized = FALSE;
74 * Whether this connection is shutted down
76 private $shuttedDown = FALSE;
79 * Currently queued chunks
81 private $queuedChunks = array();
86 private $currentFinalHash = '';
89 * Protected constructor
91 * @param $className Name of the class
94 protected function __construct ($className) {
95 // Call parent constructor
96 parent::__construct($className);
98 // Initialize output stream
99 $streamInstance = ObjectFactory::createObjectByConfiguredName('node_raw_data_output_stream_class');
101 // And add it to this connection helper
102 $this->setOutputStreamInstance($streamInstance);
104 // Init state which sets the state to 'init'
107 // Register this connection helper
108 Registry::getRegistry()->addInstance('connection', $this);
110 // Get the fragmenter instance
111 $fragmenterInstance = FragmenterFactory::createFragmenterInstance('package');
114 $this->setFragmenterInstance($fragmenterInstance);
118 * Getter for real class name, overwrites generic method and is final
120 * @return $class Name of this class
122 public final function __toString () {
123 // Class name representation
124 $class = self::getConnectionClassName($this->getAddress(), $this->getPort(), parent::__toString());
131 * Getter for port number to satify ProtocolHandler
133 * @return $port The port number
135 public final function getPort () {
140 * Setter for port number to satify ProtocolHandler
142 * @param $port The port number
145 protected final function setPort ($port) {
150 * Getter for protocol
152 * @return $protocol Used protocol
154 public final function getProtocol () {
155 return $this->protocol;
159 * Setter for protocol
161 * @param $protocol Used protocol
164 protected final function setProtocol ($protocol) {
165 $this->protocol = $protocol;
169 * Getter for IP address
171 * @return $address The IP address
173 public final function getAddress () {
174 return $this->address;
178 * Setter for IP address
180 * @param $address The IP address
183 protected final function setAddress ($address) {
184 $this->address = $address;
188 * Initializes the current connection
191 * @throws SocketOptionException If setting any socket option fails
193 protected function initConnection () {
194 // Get socket resource
195 $socketResource = $this->getSocketResource();
197 // Set the option to reuse the port
198 if (!socket_set_option($socketResource, SOL_SOCKET, SO_REUSEADDR, 1)) {
199 // Handle this socket error with a faked recipientData array
200 $this->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0'));
203 // @TODO Move this to the socket error handler
204 throw new SocketOptionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
208 * Set socket to non-blocking mode before trying to establish a link to
209 * it. This is now the default behaviour for all connection helpers who
210 * call initConnection(); .
212 if (!socket_set_nonblock($socketResource)) {
213 // Handle this socket error with a faked recipientData array
214 $helperInstance->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0'));
217 throw new SocketOptionException(array($helperInstance, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
220 // Last step: mark connection as initialized
221 $this->isInitialized = TRUE;
225 * Attempts to connect to a peer by given IP number and port from a valid
226 * recipientData array with currently configured timeout.
228 * @param $recipientData A valid recipient data array, 0=IP; 1=PORT
229 * @return $isConnected Whether the connection went fine
230 * @see Please see http://de.php.net/manual/en/function.socket-connect.php#84465 for original code
231 * @todo Rewrite the while() loop to a iterator to not let the software stay very long here
233 protected function connectToPeerByRecipientData (array $recipientData) {
234 // Only call this if the connection is initialized by initConnection()
235 assert($this->isInitialized === TRUE);
240 // "Cache" socket resource and timeout config
241 $socketResource = $this->getSocketResource();
242 $timeout = $this->getConfigInstance()->getConfigEntry('socket_timeout_seconds');
245 self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Trying to connect to ' . $recipientData[0] . ':' . $recipientData[1] . ' with socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' ...');
247 // Try to connect until it is connected
248 while ($isConnected = !@socket_connect($socketResource, $recipientData[0], $recipientData[1])) {
249 // Get last socket error
250 $socketError = socket_last_error($socketResource);
252 // Skip any errors which may happen on non-blocking connections
253 if (($socketError == SOCKET_EINPROGRESS) || ($socketError == SOCKET_EALREADY)) {
254 // Now, is that attempt within parameters?
255 if ((time() - $time) >= $timeout) {
256 // Didn't work within timeout
257 $isConnected = FALSE;
261 // Sleep about one second
263 } elseif ($socketError != 0) {
264 // Stop on everything else pronto
265 $isConnected = FALSE;
270 // Is the peer connected?
271 if ($isConnected === TRUE) {
272 // Connection is fully established here, so change the state.
273 PeerStateFactory::createPeerStateInstanceByName('connected', $this);
276 * There was a problem connecting to the peer (this state is a meta
277 * state until the error handler has found the real cause).
279 PeerStateFactory::createPeerStateInstanceByName('problem', $this);
287 * Static "getter" for this connection class' name
289 * @param $address IP address
290 * @param $port Port number
291 * @param $className Original class name
292 * @return $class Expanded class name
294 public static function getConnectionClassName ($address, $port, $className) {
296 $class = $address . ':' . $port . ':' . $className;
303 * Initializes the peer's state which sets it to 'init'
307 private function initState() {
309 * Get the state factory and create the initial state, we don't need
310 * the state instance here
312 PeerStateFactory::createPeerStateInstanceByName('init', $this);
316 * "Getter" for raw data from a package array. A fragmenter is used which
317 * will returns us only so many raw data which fits into the back buffer.
318 * The rest is being held in a back-buffer and waits there for the next
319 * cycle and while be then sent.
321 * This method does 2 simple steps:
322 * 1) Request a chunk from set fragmenter instance
323 * 2) Finally return the chunk (array) to the caller
325 * @param $packageData Raw package data array
326 * @return $chunkData Raw data chunk
328 private function getRawDataFromPackageArray (array $packageData) {
330 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: currentFinalHash=' . $this->currentFinalHash);
332 // Make sure the final hash is set
333 assert((is_string($this->currentFinalHash)) && (!empty($this->currentFinalHash)));
335 // Get the next raw data chunk from the fragmenter
336 $rawDataChunk = $this->getFragmenterInstance()->getNextRawDataChunk($this->currentFinalHash);
339 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: rawDataChunk=' . print_r($rawDataChunk, TRUE));
341 // Get chunk hashes and chunk data
342 $chunkHashes = array_keys($rawDataChunk);
343 $chunkData = array_values($rawDataChunk);
345 // Is the required data there?
346 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: chunkHashes[]=' . count($chunkHashes) . ',chunkData[]=' . count($chunkData));
347 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('chunkData='.print_r($chunkData, TRUE));
348 if ((isset($chunkHashes[0])) && (isset($chunkData[0]))) {
349 // Remember this chunk as queued
350 $this->queuedChunks[$chunkHashes[0]] = $chunkData[0];
352 // Return the raw data
353 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Returning ' . strlen($chunkData[0]) . ' bytes from ' . __METHOD__ . ' ...');
354 return $chunkData[0];
356 // Return zero string
357 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Returning zero bytes from ' . __METHOD__ . '!');
363 * "Accept" a visitor by simply calling it back
365 * @param $visitorInstance A Visitor instance
368 protected final function accept (Visitor $visitorInstance) {
369 // Just call the visitor
370 $visitorInstance->visitConnectionHelper($this);
374 * Sends raw package data to the recipient
376 * @param $packageData Raw package data
377 * @return $totalSentBytes Total sent bytes to the peer
378 * @throws InvalidSocketException If we got a problem with this socket
380 public function sendRawPackageData (array $packageData) {
381 // The helper's state must be 'connected'
382 $this->getStateInstance()->validatePeerStateConnected();
384 // Implode the package data array and fragement the resulting string, returns the final hash
385 $finalHash = $this->getFragmenterInstance()->fragmentPackageArray($packageData, $this);
386 if ($finalHash !== TRUE) {
388 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Setting finalHash=' . $finalHash . ',currentFinalHash[' . gettype($this->currentFinalHash) . ']=' . $this->currentFinalHash);
391 $this->currentFinalHash = $finalHash;
394 // Reset serial number
395 $this->getFragmenterInstance()->resetSerialNumber($this->currentFinalHash);
397 // Cache buffer length
398 $bufferSize = $this->getConfigInstance()->getConfigEntry($this->getProtocol() . '_buffer_length');
405 // Fill sending buffer with data
406 while (strlen($dataStream) > 0) {
408 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: packageData=' . print_r($packageData, TRUE));
410 // Convert the package data array to a raw data stream
411 $dataStream = $this->getRawDataFromPackageArray($packageData);
412 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Adding ' . strlen($dataStream) . ' bytes to the sending buffer ...');
413 $rawData .= $dataStream;
416 // Nothing to sent is bad news, so assert on it
417 assert(strlen($rawData) > 0);
419 // Encode the raw data with our output-stream
420 $encodedData = $this->getOutputStreamInstance()->streamData($rawData);
423 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: rawData()=' . strlen($rawData) . ',encodedData()=' . strlen($encodedData));
425 // Calculate difference
426 $this->diff = $bufferSize - strlen($encodedData);
428 // Get socket resource
429 $socketResource = $this->getSocketResource();
435 while ($sentBytes !== FALSE) {
437 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Sending out ' . strlen($encodedData) . ' bytes,bufferSize=' . $bufferSize . ',diff=' . $this->diff);
439 if ($this->diff >= 0) {
440 // Send all out (encodedData is smaller than or equal buffer size)
441 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: MD5=' . md5(substr($encodedData, 0, ($bufferSize - $this->diff))));
442 $sentBytes = socket_write($socketResource, $encodedData, ($bufferSize - $this->diff));
444 // Send buffer size out
445 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: MD5=' . md5(substr($encodedData, 0, $bufferSize)));
446 $sentBytes = socket_write($socketResource, $encodedData, $bufferSize);
449 // If there was an error, we don't continue here
450 if ($sentBytes === FALSE) {
451 // Handle the error with a faked recipientData array
452 $this->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0'));
455 throw new InvalidSocketException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
456 } elseif (($sentBytes == 0) && (strlen($encodedData) > 0)) {
457 // Nothing sent means we are done
458 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: All sent! (LINE=' . __LINE__ . ')');
462 // The difference between sent bytes and length of raw data should not go below zero
463 assert((strlen($encodedData) - $sentBytes) >= 0);
465 // Add total sent bytes
466 $totalSentBytes += $sentBytes;
468 // Cut out the last unsent bytes
469 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Sent out ' . $sentBytes . ' of ' . strlen($encodedData) . ' bytes ...');
470 $encodedData = substr($encodedData, $sentBytes);
472 // Calculate difference again
473 $this->diff = $bufferSize - strlen($encodedData);
476 if (strlen($encodedData) <= 0) {
477 // Abort here, all sent!
478 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: All sent! (LINE=' . __LINE__ . ')');
484 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: totalSentBytes=' . $totalSentBytes . ',diff=' . $this->diff);
485 return $totalSentBytes;
489 * Marks this connection as shutted down
493 protected final function markConnectionShuttedDown () {
494 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: ' . $this->__toString() . ' has been marked as shutted down');
495 $this->shuttedDown = TRUE;
497 // And remove the (now invalid) socket
498 $this->setSocketResource(FALSE);
502 * Getter for shuttedDown
504 * @return $shuttedDown Whether this connection is shutted down
506 public final function isShuttedDown () {
507 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: ' . $this->__toString() . ',shuttedDown=' . intval($this->shuttedDown));
508 return $this->shuttedDown;
511 // ************************************************************************
512 // Socket error handler call-back methods
513 // ************************************************************************
516 * Handles socket error 'connection timed out', but does not clear it for
517 * later debugging purposes.
519 * @param $socketResource A valid socket resource
520 * @param $recipientData An array with two elements: 0=IP number, 1=port number
522 * @throws SocketConnectionException The connection attempts fails with a time-out
524 protected function socketErrorConnectionTimedOutHandler ($socketResource, array $recipientData) {
525 // Get socket error code for verification
526 $socketError = socket_last_error($socketResource);
529 $errorMessage = socket_strerror($socketError);
531 // Shutdown this socket
532 $this->shutdownSocket($socketResource);
535 throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
539 * Handles socket error 'resource temporary unavailable', but does not
540 * clear it for later debugging purposes.
542 * @param $socketResource A valid socket resource
543 * @param $recipientData An array with two elements: 0=IP number, 1=port number
545 * @throws SocketConnectionException The connection attempts fails with a time-out
547 protected function socketErrorResourceUnavailableHandler ($socketResource, array $recipientData) {
548 // Get socket error code for verification
549 $socketError = socket_last_error($socketResource);
552 $errorMessage = socket_strerror($socketError);
554 // Shutdown this socket
555 $this->shutdownSocket($socketResource);
558 throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
562 * Handles socket error 'connection refused', but does not clear it for
563 * later debugging purposes.
565 * @param $socketResource A valid socket resource
566 * @param $recipientData An array with two elements: 0=IP number, 1=port number
568 * @throws SocketConnectionException The connection attempts fails with a time-out
570 protected function socketErrorConnectionRefusedHandler ($socketResource, array $recipientData) {
571 // Get socket error code for verification
572 $socketError = socket_last_error($socketResource);
575 $errorMessage = socket_strerror($socketError);
577 // Shutdown this socket
578 $this->shutdownSocket($socketResource);
581 throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
585 * Handles socket error 'no route to host', but does not clear it for later
586 * debugging purposes.
588 * @param $socketResource A valid socket resource
589 * @param $recipientData An array with two elements: 0=IP number, 1=port number
591 * @throws SocketConnectionException The connection attempts fails with a time-out
593 protected function socketErrorNoRouteToHostHandler ($socketResource, array $recipientData) {
594 // Get socket error code for verification
595 $socketError = socket_last_error($socketResource);
598 $errorMessage = socket_strerror($socketError);
600 // Shutdown this socket
601 $this->shutdownSocket($socketResource);
604 throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
608 * Handles socket error 'operation already in progress' which happens in
609 * method connectToPeerByRecipientData() on timed out connection
612 * @param $socketResource A valid socket resource
613 * @param $recipientData An array with two elements: 0=IP number, 1=port number
615 * @throws SocketConnectionException The connection attempts fails with a time-out
617 protected function socketErrorOperationAlreadyProgressHandler ($socketResource, array $recipientData) {
618 // Get socket error code for verification
619 $socketError = socket_last_error($socketResource);
622 $errorMessage = socket_strerror($socketError);
624 // Half-shutdown this socket (see there for difference to shutdownSocket())
625 $this->halfShutdownSocket($socketResource);
628 throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
632 * Handles socket "error" 'operation now in progress' which can be safely
633 * passed on with non-blocking connections.
635 * @param $socketResource A valid socket resource
636 * @param $recipientData An array with two elements: 0=IP number, 1=port number
639 protected function socketErrorOperationInProgressHandler ($socketResource, array $recipientData) {
640 self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Operation is now in progress, this is usual for non-blocking connections and is no bug.');