]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/listener/tcp/class_TcpListener.php
Updated 'core'.
[hub.git] / application / hub / main / listener / tcp / class_TcpListener.php
index b2325c4182091ad68b29def5438bc1e9584a295a..d3fbcf35c6dfb11a391de971372d47c781842f1c 100644 (file)
@@ -2,11 +2,11 @@
 /**
  * A TCP connection listener
  *
- * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
  * @license            GNU GPL 3.0 or any newer version
- * @link               http://www.ship-simu.org
+ * @link               http://www.shipsimu.org
  *
  * 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
@@ -30,6 +30,9 @@ class TcpListener extends BaseListener implements Listenable {
        protected function __construct () {
                // Call parent constructor
                parent::__construct(__CLASS__);
+
+               // Set the protocol to TCP
+               $this->setProtocolName('tcp');
        }
 
        /**
@@ -42,12 +45,6 @@ class TcpListener extends BaseListener implements Listenable {
                // Get new instance
                $listenerInstance = new TcpListener();
 
-               // Set the application instance
-               $listenerInstance->setNodeInstance($nodeInstance);
-
-               // Set the protocol to TCP
-               $listenerInstance->setProtocol('tcp');
-
                // Return the prepared instance
                return $listenerInstance;
        }
@@ -65,7 +62,7 @@ class TcpListener extends BaseListener implements Listenable {
                // Is the socket resource valid?
                if (!is_resource($mainSocket)) {
                        // Something bad happened
-                       throw new InvalidSocketException(array($this, gettype($mainSocket), 0, 'invalid'), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       throw new InvalidSocketException(array($this, $mainSocket), BaseListener::EXCEPTION_INVALID_SOCKET);
                } // END - if
 
                // Get socket error code for verification
@@ -73,12 +70,19 @@ class TcpListener extends BaseListener implements Listenable {
 
                // Check if there was an error else
                if ($socketError > 0) {
+                       // Handle this socket error with a faked recipientData array
+                       $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
+                       /*
                        // Then throw again
-                       throw new InvalidSocketException(array($this, gettype($mainSocket), $socketError, socket_strerror($socketError)), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       throw new InvalidSocketException(array($this, $mainSocket, $socketError, socket_strerror($socketError)), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       */
                } // END - if
 
                // Set the option to reuse the port
                if (!socket_set_option($mainSocket, SOL_SOCKET, SO_REUSEADDR, 1)) {
+                       // Handle this socket error with a faked recipientData array
+                       $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
+                       /*
                        // Get socket error code for verification
                        $socketError = socket_last_error($mainSocket);
 
@@ -89,12 +93,20 @@ class TcpListener extends BaseListener implements Listenable {
                        $this->shutdownSocket($mainSocket);
 
                        // And throw again
-                       throw new InvalidSocketException(array($this, gettype($mainSocket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       */
                } // END - if
 
-               // Now, we want non-blocking mode
-               $this->debugOutput('LISTENER: Setting non-blocking mode.');
-               if (!socket_set_nonblock($mainSocket)) {
+               /*
+                * "Bind" the socket to the given address, on given port so this means
+                * that all connections on this port are now our resposibility to
+                * send/recv data, disconnect, etc..
+                */
+               self::createDebugInstance(__CLASS__)->debugOutput('TCP-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Binding to address ' . $this->getListenAddress() . ':' . $this->getListenPort());
+               if (!socket_bind($mainSocket, $this->getListenAddress(), $this->getListenPort())) {
+                       // Handle this socket error with a faked recipientData array
+                       $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
+                       /*
                        // Get socket error code for verification
                        $socketError = socket_last_error($mainSocket);
 
@@ -105,14 +117,16 @@ class TcpListener extends BaseListener implements Listenable {
                        $this->shutdownSocket($mainSocket);
 
                        // And throw again
-                       throw new InvalidSocketException(array($this, gettype($mainSocket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       */
                } // END - if
 
-               // "Bind" the socket to the given address, on given port so this means
-               // that all connections on this port are now our resposibility to
-               // send/recv data, disconnect, etc..
-               $this->debugOutput('LISTENER: Binding to address ' . $this->getListenAddress() . ':' . $this->getListenPort());
-               if (!socket_bind($mainSocket, $this->getListenAddress(), $this->getListenPort())) {
+               // Start listen for connections
+               self::createDebugInstance(__CLASS__)->debugOutput('TCP-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Listening for connections.');
+               if (!socket_listen($mainSocket)) {
+                       // Handle this socket error with a faked recipientData array
+                       $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
+                       /*
                        // Get socket error code for verification
                        $socketError = socket_last_error($mainSocket);
 
@@ -123,12 +137,16 @@ class TcpListener extends BaseListener implements Listenable {
                        $this->shutdownSocket($mainSocket);
 
                        // And throw again
-                       throw new InvalidSocketException(array($this, gettype($mainSocket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       */
                } // END - if
 
-               // Start listen for connections
-               $this->debugOutput('LISTENER: Listening for connections.');
-               if (!socket_listen($mainSocket)) {
+               // Now, we want non-blocking mode
+               self::createDebugInstance(__CLASS__)->debugOutput('TCP-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Setting non-blocking mode.');
+               if (!socket_set_nonblock($mainSocket)) {
+                       // Handle this socket error with a faked recipientData array
+                       $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
+                       /*
                        // Get socket error code for verification
                        $socketError = socket_last_error($mainSocket);
 
@@ -139,17 +157,18 @@ class TcpListener extends BaseListener implements Listenable {
                        $this->shutdownSocket($mainSocket);
 
                        // And throw again
-                       throw new InvalidSocketException(array($this, gettype($mainSocket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       */
                } // END - if
 
                // Set the main socket
                $this->registerServerSocketResource($mainSocket);
 
                // Initialize the peer pool instance
-               $poolInstance = ObjectFactory::createObjectByConfiguredName('peer_pool_class', array($this));
+               $poolInstance = ObjectFactory::createObjectByConfiguredName('node_pool_class', array($this));
 
                // Add main socket
-               $poolInstance->addPeer($mainSocket);
+               $poolInstance->addPeer($mainSocket, BaseConnectionHelper::CONNECTION_TYPE_SERVER);
 
                // And add it to this listener
                $this->setPoolInstance($poolInstance);
@@ -162,13 +181,13 @@ class TcpListener extends BaseListener implements Listenable {
                $this->setIteratorInstance($iteratorInstance);
 
                // Initialize the network package handler
-               $packageInstance = ObjectFactory::createObjectByConfiguredName('tcp_network_package_handler_class');
+               $handlerInstance = ObjectFactory::createObjectByConfiguredName('tcp_raw_data_handler_class');
 
                // Set it in this class
-               $this->setPackageInstance($packageInstance);
+               $this->setHandlerInstance($handlerInstance);
 
                // Output message
-               $this->debugOutput('LISTENER: TCP listener now ready on IP ' . $this->getListenAddress() . ', port ' . $this->getListenPort() . ' for service.');
+               self::createDebugInstance(__CLASS__)->debugOutput('TCP-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: TCP listener now ready on IP ' . $this->getListenAddress() . ', port ' . $this->getListenPort() . ' for service.');
        }
 
        /**
@@ -179,7 +198,7 @@ class TcpListener extends BaseListener implements Listenable {
         */
        public function doListen () {
                // Get all readers
-               $readers = $this->getPoolInstance()->getAllSockets();
+               $readers = $this->getPoolInstance()->getAllSingleSockets();
                $writers = array();
                $excepts = array();
 
@@ -194,32 +213,93 @@ class TcpListener extends BaseListener implements Listenable {
 
                // Some new peers found?
                if ($left < 1) {
+                       // Debug message
+                       //* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('TCP-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: left=' . $left . ',serverSocket=' . $this->getSocketResource() . ',readers=' . print_r($readers, TRUE));
+
                        // Nothing new found
                        return;
                } // END - if
 
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('TCP-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: serverSocket=' . $this->getSocketResource() . ',readers=' . print_r($readers, TRUE));
+
                // Do we have changed peers?
                if (in_array($this->getSocketResource(), $readers)) {
-                       // Then accept it
+                       /*
+                        * Then accept it, if this socket is set to non-blocking IO and the
+                        * connection is NOT sending any data, socket_read() may throw
+                        * error 11 (Resource temporary unavailable). This really nasty
+                        * because if you have blocking IO socket_read() will wait and wait
+                        * and wait ...
+                        */
                        $newSocket = socket_accept($this->getSocketResource());
 
-                       // We want non-blocking here, too
-                       if (!socket_set_nonblock($newSocket)) {
-                               // Get socket error code for verification
-                               $socketError = socket_last_error($newSocket);
+                       // Debug message
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('TCP-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: newSocket=' . $newSocket . ',serverSocket=' .$this->getSocketResource());
+
+                       // Array for timeout settings
+                       $options  = array(
+                               // Seconds
+                               'sec'  => $this->getConfigInstance()->getConfigEntry('tcp_socket_accept_wait_sec'),
+                               // Milliseconds
+                               'usec' => $this->getConfigInstance()->getConfigEntry('tcp_socket_accept_wait_usec')
+                       );
+
+                       // Set timeout to configured seconds
+                       // @TODO Does this work on Windozer boxes???
+                       if (!socket_set_option($newSocket, SOL_SOCKET, SO_RCVTIMEO, $options)) {
+                               // Handle this socket error with a faked recipientData array
+                               $this->handleSocketError(__METHOD__, __LINE__, $newSocket, array('0.0.0.0', '0'));
+                       } // END - if
 
-                               // Get error message
-                               $errorMessage = socket_strerror($socketError);
+                       // Output result (only for debugging!)
+                       /*
+                       $option = socket_get_option($newSocket, SOL_SOCKET, SO_RCVTIMEO);
+                       self::createDebugInstance(__CLASS__)->debugOutput('SO_RCVTIMEO[' . gettype($option) . ']=' . print_r($option, TRUE));
+                       */
 
-                               // Shutdown this socket
-                               $this->shutdownSocket($newSocket);
+                       // Enable SO_OOBINLINE
+                       if (!socket_set_option($newSocket, SOL_SOCKET, SO_OOBINLINE ,1)) {
+                               // Handle this socket error with a faked recipientData array
+                               $this->handleSocketError(__METHOD__, __LINE__, $newSocket, array('0.0.0.0', '0'));
+                       } // END - if
 
-                               // And throw the exception
-                               throw new InvalidSocketException(array($this, gettype($newSocket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       // Set non-blocking
+                       if (!socket_set_nonblock($newSocket)) {
+                               // Handle this socket error with a faked recipientData array
+                               $this->handleSocketError(__METHOD__, __LINE__, $newSocket, array('0.0.0.0', '0'));
                        } // END - if
 
                        // Add it to the peers
-                       $this->getPoolInstance()->addPeer($newSocket);
+                       $this->getPoolInstance()->addPeer($newSocket, BaseConnectionHelper::CONNECTION_TYPE_INCOMING);
+
+                       // Get peer name
+                       if (!socket_getpeername($newSocket, $peerName)) {
+                               // Handle this socket error with a faked recipientData array
+                               $this->handleSocketError(__METHOD__, __LINE__, $newSocket, array('0.0.0.0', '0'));
+                       } // END - if
+
+                       // Get node instance
+                       $nodeInstance = Registry::getRegistry()->getInstance('node');
+
+                       // Create a faked package data array
+                       $packageData = array(
+                               NetworkPackage::PACKAGE_DATA_SENDER    => $peerName . ':0',
+                               NetworkPackage::PACKAGE_DATA_RECIPIENT => $nodeInstance->getSessionId(),
+                               NetworkPackage::PACKAGE_DATA_STATUS    => NetworkPackage::PACKAGE_STATUS_FAKED
+                       );
+
+                       // Get a connection info instance
+                       $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($this->getProtocolName(), 'listener');
+
+                       // Will the info instance with listener data
+                       $infoInstance->fillWithListenerInformation($this);
+
+                       // Get a socket registry
+                       $registryInstance = SocketRegistryFactory::createSocketRegistryInstance();
+
+                       // Register the socket with the registry and with the faked array
+                       $registryInstance->registerSocket($infoInstance, $newSocket, $packageData);
                } // END - if
 
                // Do we have to rewind?
@@ -231,24 +311,27 @@ class TcpListener extends BaseListener implements Listenable {
                // Get the current value
                $currentSocket = $this->getIteratorInstance()->current();
 
-               // Handle it here, if not main socket
-               if ($currentSocket != $this->getSocketResource()) {
+               // Handle it here, if not main server socket
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('TCP-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: currentSocket=' . $currentSocket[BasePool::SOCKET_ARRAY_RESOURCE] . ',type=' . $currentSocket[BasePool::SOCKET_ARRAY_CONN_TYPE] . ',serverSocket=' . $this->getSocketResource());
+               if (($currentSocket[BasePool::SOCKET_ARRAY_CONN_TYPE] != BaseConnectionHelper::CONNECTION_TYPE_SERVER) && ($currentSocket[BasePool::SOCKET_ARRAY_RESOURCE] != $this->getSocketResource())) {
                        // ... or else it will raise warnings like 'Transport endpoint is not connected'
-                       $this->getPackageInstance()->processResourcePackage($currentSocket);
+                       $this->getHandlerInstance()->processRawDataFromResource($currentSocket);
                } // END - if
 
-               // Advance to next entry. This should be the last line
+               // Advance to next entry. This should be the last line.
                $this->getIteratorInstance()->next();
        }
 
        /**
-        * Checks wether the listener would accept the given package data array
+        * Checks whether the listener would accept the given package data array
         *
         * @param       $packageData    Raw package data
-        * @return      $accepts                Wether this listener does accept
+        * @return      $accepts                Whether this listener does accept
+        * @throws      UnsupportedOperationException   If this method is called
         */
-       function ifListenerAcceptsPackageData (array $packageData) {
-               $this->partialStub('This call should not happen. Please report.');
+       public function ifListenerAcceptsPackageData (array $packageData) {
+               // Please don't call this
+               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
        }
 }