]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/listener/tcp/class_TcpListener.php
Use array_push() instead of [] as array_push() checks if parameter 1 is really an...
[hub.git] / application / hub / main / listener / tcp / class_TcpListener.php
index ce0b8632ea9ba8bb8a822c3a8edd1b2937afd318..680ab86b5e45b492f15e3bcebb5fa74124255f7b 100644 (file)
@@ -74,7 +74,7 @@ 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($mainSocket, array('0.0.0.0', '0'));
+                       $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
                        /*
                        // Then throw again
                        throw new InvalidSocketException(array($this, $mainSocket, $socketError, socket_strerror($socketError)), BaseListener::EXCEPTION_INVALID_SOCKET);
@@ -84,7 +84,7 @@ class TcpListener extends BaseListener implements Listenable {
                // 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($mainSocket, array('0.0.0.0', '0'));
+                       $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
                        /*
                        // Get socket error code for verification
                        $socketError = socket_last_error($mainSocket);
@@ -105,10 +105,10 @@ class TcpListener extends BaseListener implements Listenable {
                 * 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());
+               self::createDebugInstance(__CLASS__)->debugOutput('TCP-LISTENER[' . __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($mainSocket, array('0.0.0.0', '0'));
+                       $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
                        /*
                        // Get socket error code for verification
                        $socketError = socket_last_error($mainSocket);
@@ -125,10 +125,10 @@ class TcpListener extends BaseListener implements Listenable {
                } // END - if
 
                // Start listen for connections
-               $this->debugOutput('TCP-LISTENER: Listening for connections.');
+               self::createDebugInstance(__CLASS__)->debugOutput('TCP-LISTENER[' . __LINE__ . ']: Listening for connections.');
                if (!socket_listen($mainSocket)) {
                        // Handle this socket error with a faked recipientData array
-                       $this->handleSocketError($mainSocket, array('0.0.0.0', '0'));
+                       $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
                        /*
                        // Get socket error code for verification
                        $socketError = socket_last_error($mainSocket);
@@ -145,10 +145,10 @@ class TcpListener extends BaseListener implements Listenable {
                } // END - if
 
                // Now, we want non-blocking mode
-               $this->debugOutput('TCP-LISTENER: Setting non-blocking mode.');
+               self::createDebugInstance(__CLASS__)->debugOutput('TCP-LISTENER[' . __LINE__ . ']: Setting non-blocking mode.');
                if (!socket_set_nonblock($mainSocket)) {
                        // Handle this socket error with a faked recipientData array
-                       $this->handleSocketError($mainSocket, array('0.0.0.0', '0'));
+                       $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
                        /*
                        // Get socket error code for verification
                        $socketError = socket_last_error($mainSocket);
@@ -171,7 +171,7 @@ class TcpListener extends BaseListener implements Listenable {
                $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);
@@ -190,7 +190,7 @@ class TcpListener extends BaseListener implements Listenable {
                $this->setHandlerInstance($handlerInstance);
 
                // Output message
-               $this->debugOutput('TCP-LISTENER: TCP listener now ready on IP ' . $this->getListenAddress() . ', port ' . $this->getListenPort() . ' for service.');
+               self::createDebugInstance(__CLASS__)->debugOutput('TCP-LISTENER[' . __LINE__ . ']: TCP listener now ready on IP ' . $this->getListenAddress() . ', port ' . $this->getListenPort() . ' for service.');
        }
 
        /**
@@ -201,7 +201,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();
 
@@ -216,10 +216,16 @@ class TcpListener extends BaseListener implements Listenable {
 
                // Some new peers found?
                if ($left < 1) {
+                       // Debug message
+                       //* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('TCP-LISTENER[' . __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[' . __LINE__ . ']: serverSocket=' . $this->getSocketResource() . ',readers=' . print_r($readers, true));
+
                // Do we have changed peers?
                if (in_array($this->getSocketResource(), $readers)) {
                        /*
@@ -230,7 +236,9 @@ class TcpListener extends BaseListener implements Listenable {
                         * and wait ...
                         */
                        $newSocket = socket_accept($this->getSocketResource());
-                       /* NOISY-DEBUG: */ $this->debugOutput('TCP-LISTENER: newSocket=' . $newSocket);
+
+                       // Debug message
+                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: newSocket=' . $newSocket . ',serverSocket=' .$this->getSocketResource());
 
                        // Array for timeout settings
                        $options  = array(
@@ -244,21 +252,49 @@ class TcpListener extends BaseListener implements Listenable {
                        // @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($newSocket, array('0.0.0.0', '0'));
+                               $this->handleSocketError(__METHOD__, __LINE__, $newSocket, array('0.0.0.0', '0'));
                        } // END - if
 
-                       // Output result (only debugging!)
+                       // Output result (only for debugging!)
+                       /*
                        $option = socket_get_option($newSocket, SOL_SOCKET, SO_RCVTIMEO);
-                       $this->debugOutput('SO_RCVTIMEO[' . gettype($option) . ']=' . print_r($option, true));
+                       self::createDebugInstance(__CLASS__)->debugOutput('SO_RCVTIMEO[' . gettype($option) . ']=' . print_r($option, true));
+                       */
 
                        // Enable SO_OOBINLINE
                        if (!socket_set_option($newSocket, SOL_SOCKET, SO_OOBINLINE ,1)) {
                                // Handle this socket error with a faked recipientData array
-                               $this->handleSocketError($newSocket, array('0.0.0.0', '0'));
+                               $this->handleSocketError(__METHOD__, __LINE__, $newSocket, array('0.0.0.0', '0'));
+                       } // END - if
+
+                       // 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
+
+                       // Create a faked package data array
+                       $packageData = array(
+                               NetworkPackage::PACKAGE_DATA_SENDER    => $peerName . ':0',
+                               NetworkPackage::PACKAGE_DATA_RECIPIENT => $this->getSessionId(),
+                               NetworkPackage::PACKAGE_DATA_PROTOCOL  => $this->getProtocol(),
+                               NetworkPackage::PACKAGE_DATA_STATUS    => NetworkPackage::PACKAGE_STATUS_FAKED
+                       );
+
+                       // Get a socket registry
+                       $registryInstance = SocketRegistryFactory::createSocketRegistryInstance();
+
+                       // Register the socket with the registry and with the faked array
+                       $registryInstance->registerSocket($this, $newSocket, $packageData);
                } // END - if
 
                // Do we have to rewind?
@@ -270,14 +306,14 @@ 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[' . __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'
-                       /* NOISY-DEBUG: */ $this->debugOutput('TCP-LISTENER: currentSocket=' . $currentSocket . ',server=' . $this->getSocketResource());
                        $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();
        }
 
@@ -286,9 +322,11 @@ class TcpListener extends BaseListener implements Listenable {
         *
         * @param       $packageData    Raw package data
         * @return      $accepts                Whether this listener does accept
+        * @throws      UnsupportedOperationException   If this method is called
         */
        public function ifListenerAcceptsPackageData (array $packageData) {
-               $this->debugBackTrace('This call should not happen. Please report it.');
+               // Please don't call this
+               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
        }
 }