]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/listener/tcp/class_TcpListener.php
Introduced getListIterator() to all lists implementing Listable
[hub.git] / application / hub / main / listener / tcp / class_TcpListener.php
index 9773fc8710644e7947803d88203afcf598a4dd5a..005831ffc2f9c5e4676e3805fa910d765aba8cf6 100644 (file)
@@ -4,7 +4,7 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 Hub Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -38,7 +38,7 @@ class TcpListener extends BaseListener implements Listenable {
         * @param       $nodeInstance           A NodeHelper instance
         * @return      $listenerInstance       An instance a prepared listener class
         */
-       public final static function createTcpListener (NodeHelper $nodeInstance) {
+       public static final function createTcpListener (NodeHelper $nodeInstance) {
                // Get new instance
                $listenerInstance = new TcpListener();
 
@@ -58,7 +58,7 @@ class TcpListener extends BaseListener implements Listenable {
         * @return      void
         * @throws      InvalidSocketException  Thrown if the socket could not be initialized
         */
-       public function initListener() {
+       public function initListener () {
                // Create a streaming socket, of type TCP/IP
                $mainSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
 
@@ -95,6 +95,7 @@ class TcpListener extends BaseListener implements Listenable {
                // "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())) {
                        // Get socket error code for verification
                        $socketError = socket_last_error($mainSocket);
@@ -110,6 +111,7 @@ class TcpListener extends BaseListener implements Listenable {
                } // END - if
 
                // Start listen for connections
+               $this->debugOutput('LISTENER: Listening for connections.');
                if (!socket_listen($mainSocket)) {
                        // Get socket error code for verification
                        $socketError = socket_last_error($mainSocket);
@@ -124,14 +126,30 @@ class TcpListener extends BaseListener implements Listenable {
                        throw new InvalidSocketException(array($this, gettype($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)) {
+                       // Get socket error code for verification
+                       $socketError = socket_last_error($mainSocket);
+
+                       // Get error message
+                       $errorMessage = socket_strerror($socketError);
+
+                       // Shutdown this socket
+                       $this->shutdownSocket($mainSocket);
+
+                       // And throw again
+                       throw new InvalidSocketException(array($this, gettype($mainSocket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+               } // END - if
+
                // Set the main socket
-               $this->setSocketResource($mainSocket);
+               $this->registerServerSocketResource($mainSocket);
 
-               // Initialize the client pool instance
-               $poolInstance = ObjectFactory::createObjectByConfiguredName('client_pool_class', array($this));
+               // Initialize the peer pool instance
+               $poolInstance = ObjectFactory::createObjectByConfiguredName('peer_pool_class', array($this));
 
                // Add main socket
-               $poolInstance->addClient($mainSocket);
+               $poolInstance->addPeer($mainSocket);
 
                // And add it to this listener
                $this->setPoolInstance($poolInstance);
@@ -157,7 +175,7 @@ class TcpListener extends BaseListener implements Listenable {
         * "Listens" for incoming network packages
         *
         * @return      void
-        * @todo        0% done
+        * @throws      InvalidSocketException  If an invalid socket resource has been found
         */
        public function doListen () {
                // Get all readers
@@ -165,7 +183,7 @@ class TcpListener extends BaseListener implements Listenable {
                $writers = array();
                $excepts = array();
 
-               // Check if we have some clients left
+               // Check if we have some peers left
                $left = socket_select(
                        $readers,
                        $writers,
@@ -174,22 +192,35 @@ class TcpListener extends BaseListener implements Listenable {
                        150
                );
 
-               // Some new clients found?
+               // Some new peers found?
                if ($left < 1) {
                        // Nothing new found
                        return;
                } // END - if
 
-               // Do we have changed clients?
+               // Do we have changed peers?
                if (in_array($this->getSocketResource(), $readers)) {
                        // Then accept it
                        $newSocket = socket_accept($this->getSocketResource());
+                       //* NOISY-DEBUG: */ $this->debugOutput('LISTENER: newSocket=' . $newSocket);
+
+                       // We want non-blocking here, too
+                       if (!socket_set_nonblock($newSocket)) {
+                               // Get socket error code for verification
+                               $socketError = socket_last_error($newSocket);
 
-                       // Debug message
-                       $this->debugOutput('LISTENER: Adding new client: ' . $newSocket);
+                               // Get error message
+                               $errorMessage = socket_strerror($socketError);
 
-                       // Add it to the clients
-                       $this->getPoolInstance()->addClient($newSocket);
+                               // Shutdown this socket
+                               $this->shutdownSocket($newSocket);
+
+                               // And throw the exception
+                               throw new InvalidSocketException(array($this, gettype($newSocket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       } // END - if
+
+                       // Add it to the peers
+                       $this->getPoolInstance()->addPeer($newSocket);
                } // END - if
 
                // Do we have to rewind?
@@ -199,14 +230,28 @@ class TcpListener extends BaseListener implements Listenable {
                } // END - if
 
                // Get the current value
-               $current = $this->getIteratorInstance()->current();
+               $currentSocket = $this->getIteratorInstance()->current();
 
-               // Handle it here
-               $this->getPackageInstance()->processResourcePackage($current);
+               // Handle it here, if not main socket
+               if ($currentSocket != $this->getSocketResource()) {
+                       // ... or else it will raise warnings like 'Transport endpoint is not connected'
+                       //* NOISY-DEBUG: */ $this->debugOutput('LISTENER: currentSocket=' . $currentSocket);
+                       $this->getPackageInstance()->processResourceRawData($currentSocket);
+               } // END - if
 
                // Advance to next entry. This should be the last line
                $this->getIteratorInstance()->next();
        }
+
+       /**
+        * Checks wether the listener would accept the given package data array
+        *
+        * @param       $packageData    Raw package data
+        * @return      $accepts                Wether this listener does accept
+        */
+       public function ifListenerAcceptsPackageData (array $packageData) {
+               $this->partialStub('This call should not happen. Please report it.');
+       }
 }
 
 // [EOF]