From 3f723e98ac7aad5ad2a7b1bd28d5dca852707768 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 22 Dec 2009 15:12:57 +0000 Subject: [PATCH] Non-blocking enforced and skipped handling packages from main server socket. This fixes a 'Transport endpoint is not connected' warning --- .../main/listener/tcp/class_TcpListener.php | 42 ++++++++++++++++--- .../pools/client/class_DefaultClientPool.php | 3 ++ 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/application/hub/main/listener/tcp/class_TcpListener.php b/application/hub/main/listener/tcp/class_TcpListener.php index 9773fc871..4881e4f2f 100644 --- a/application/hub/main/listener/tcp/class_TcpListener.php +++ b/application/hub/main/listener/tcp/class_TcpListener.php @@ -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(); diff --git a/application/hub/main/pools/client/class_DefaultClientPool.php b/application/hub/main/pools/client/class_DefaultClientPool.php index 08fb52154..dd4c6c2d1 100644 --- a/application/hub/main/pools/client/class_DefaultClientPool.php +++ b/application/hub/main/pools/client/class_DefaultClientPool.php @@ -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); } -- 2.39.5