]> git.mxchange.org Git - hub.git/commitdiff
Non-blocking enforced and skipped handling packages from main server socket. This...
authorRoland Häder <roland@mxchange.org>
Tue, 22 Dec 2009 15:12:57 +0000 (15:12 +0000)
committerRoland Häder <roland@mxchange.org>
Tue, 22 Dec 2009 15:12:57 +0000 (15:12 +0000)
application/hub/main/listener/tcp/class_TcpListener.php
application/hub/main/pools/client/class_DefaultClientPool.php

index 9773fc8710644e7947803d88203afcf598a4dd5a..4881e4f2f3d2443420f52ce09a93210ffc151329 100644 (file)
@@ -92,9 +92,26 @@ 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
+
                // "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 +127,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);
@@ -157,7 +175,6 @@ class TcpListener extends BaseListener implements Listenable {
         * "Listens" for incoming network packages
         *
         * @return      void
-        * @todo        0% done
         */
        public function doListen () {
                // Get all readers
@@ -185,8 +202,20 @@ class TcpListener extends BaseListener implements Listenable {
                        // Then accept it
                        $newSocket = socket_accept($this->getSocketResource());
 
-                       // Debug message
-                       $this->debugOutput('LISTENER: Adding new client: ' . $newSocket);
+                       // We want non-blocking here, too
+                       if (!socket_set_nonblock($newSocket)) {
+                               // Get socket error code for verification
+                               $socketError = socket_last_error($newSocket);
+
+                               // Get error message
+                               $errorMessage = socket_strerror($socketError);
+
+                               // Shutdown this socket
+                               $this->shutdownSocket($newSocket);
+
+                               // And throw again
+                               throw new InvalidSocketException(array($this, gettype($newSocket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       } // END - if
 
                        // Add it to the clients
                        $this->getPoolInstance()->addClient($newSocket);
@@ -201,8 +230,11 @@ class TcpListener extends BaseListener implements Listenable {
                // Get the current value
                $current = $this->getIteratorInstance()->current();
 
-               // Handle it here
-               $this->getPackageInstance()->processResourcePackage($current);
+               // Handle it here, if not main socket
+               if ($current !== $this->getSocketResource()) {
+                       // ... or else it will raise warnings like 'Transport endpoint is not connected'
+                       $this->getPackageInstance()->processResourcePackage($current);
+               } // END - if
 
                // Advance to next entry. This should be the last line
                $this->getIteratorInstance()->next();
index 08fb52154de8ea46ecb36211cd1a06826636ea8c..dd4c6c2d1a1c3cb477fa2b34ac5dbe30abab690b 100644 (file)
@@ -89,6 +89,9 @@ class DefaultClientPool extends BasePool implements PoolableClient {
                // Validate the socket
                $this->validateSocket($socketResource);
 
+               // Output error message
+               $this->debugOutput('POOL: Adding client ' . $socketResource);
+
                // Add it finally to the pool
                $this->addPoolEntry($socketResource);
        }