X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=application%2Fhub%2Fmain%2Flistener%2Ftcp%2Fclass_TcpListener.php;h=bdc20b462f98411783f950199ebb81097119a66e;hb=9aa0ae335d8821392ae8a97f9a0c05638a131e66;hp=6cbe71b3b6e3f63cc54926ff93caeba08d7c4d16;hpb=3cdf3e24c3e85f8f98c5e0522d65d9a29ace1e0e;p=hub.git diff --git a/application/hub/main/listener/tcp/class_TcpListener.php b/application/hub/main/listener/tcp/class_TcpListener.php index 6cbe71b3b..bdc20b462 100644 --- a/application/hub/main/listener/tcp/class_TcpListener.php +++ b/application/hub/main/listener/tcp/class_TcpListener.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 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 * @@ -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->setProtocol('tcp'); } /** @@ -38,16 +41,13 @@ 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(); // Set the application instance $listenerInstance->setNodeInstance($nodeInstance); - // Set the protocol to TCP - $listenerInstance->setProtocol('tcp'); - // Return the prepared instance return $listenerInstance; } @@ -92,9 +92,11 @@ 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)) { + // "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('TCP-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); @@ -108,11 +110,9 @@ class TcpListener extends BaseListener implements Listenable { 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())) { + // Start listen for connections + $this->debugOutput('TCP-LISTENER: Listening for connections.'); + if (!socket_listen($mainSocket)) { // Get socket error code for verification $socketError = socket_last_error($mainSocket); @@ -126,9 +126,9 @@ class TcpListener extends BaseListener implements Listenable { throw new InvalidSocketException(array($this, gettype($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 + $this->debugOutput('TCP-LISTENER: Setting non-blocking mode.'); + if (!socket_set_nonblock($mainSocket)) { // Get socket error code for verification $socketError = socket_last_error($mainSocket); @@ -162,19 +162,20 @@ 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.'); + $this->debugOutput('TCP-LISTENER: TCP listener now ready on IP ' . $this->getListenAddress() . ', port ' . $this->getListenPort() . ' for service.'); } /** * "Listens" for incoming network packages * * @return void + * @throws InvalidSocketException If an invalid socket resource has been found */ public function doListen () { // Get all readers @@ -201,6 +202,7 @@ class TcpListener extends BaseListener implements Listenable { if (in_array($this->getSocketResource(), $readers)) { // Then accept it $newSocket = socket_accept($this->getSocketResource()); + //* NOISY-DEBUG: */ $this->debugOutput('TCP-LISTENER: newSocket=' . $newSocket); // We want non-blocking here, too if (!socket_set_nonblock($newSocket)) { @@ -213,7 +215,7 @@ class TcpListener extends BaseListener implements Listenable { // Shutdown this socket $this->shutdownSocket($newSocket); - // And throw again + // And throw the exception throw new InvalidSocketException(array($this, gettype($newSocket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET); } // END - if @@ -228,12 +230,13 @@ class TcpListener extends BaseListener implements Listenable { } // END - if // Get the current value - $current = $this->getIteratorInstance()->current(); + $currentSocket = $this->getIteratorInstance()->current(); // Handle it here, if not main socket - if ($current != $this->getSocketResource()) { + if ($currentSocket != $this->getSocketResource()) { // ... or else it will raise warnings like 'Transport endpoint is not connected' - $this->getPackageInstance()->processResourcePackage($current); + //* NOISY-DEBUG: */ $this->debugOutput('TCP-LISTENER: currentSocket=' . $currentSocket); + $this->getHandlerInstance()->processRawDataFromResource($currentSocket); } // END - if // Advance to next entry. This should be the last line @@ -246,8 +249,8 @@ class TcpListener extends BaseListener implements Listenable { * @param $packageData Raw package data * @return $accepts Wether this listener does accept */ - function ifListenerAcceptsPackageData (array $packageData) { - $this->partialStub('This call should not happen. Please report.'); + public function ifListenerAcceptsPackageData (array $packageData) { + $this->partialStub('This call should not happen. Please report it.'); } }