]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/handler/network/tcp/class_TcpRawDataHandler.php
Added line numbers to debug lines as this will become the 'norm'
[hub.git] / application / hub / main / handler / network / tcp / class_TcpRawDataHandler.php
index e37e6bc84e1a4663a4ace01bae2a2d3e26391f06..fc9c94f9181a3e1e74da1ce0b8eef3c849612844 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 - 2011 Hub Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -52,12 +52,12 @@ class TcpRawDataHandler extends BaseRawDataHandler implements Networkable {
         * Processes raw data from given resource. This is mostly useful for TCP
         * package handling and is implemented in the TcpListener class
         *
-        * @param       $resource       A valid resource identifier
+        * @param       $resource       A valid socket resource array
         * @return      void
         */
-       public function processRawDataFromResource ($resource) {
+       public function processRawDataFromResource (array $socketArray) {
                // Check the resource
-               if (!is_resource($resource)) {
+               if ((!isset($socketArray[BasePool::SOCKET_ARRAY_RESOURCE])) || (!is_resource($socketArray[BasePool::SOCKET_ARRAY_RESOURCE]))) {
                        // Throw an exception
                        throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
                } // END - if
@@ -65,40 +65,49 @@ class TcpRawDataHandler extends BaseRawDataHandler implements Networkable {
                // Reset error code to unhandled
                $this->setErrorCode(self::SOCKET_ERROR_UNHANDLED);
 
-               // Init variables
-               $decodedData = false;
-
                // Debug message
-               //* NOISY-DEBUG: */ $this->debugOutput('HANDLER: Handling TCP package from peer ' . $resource);
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('TCP-HANDLER[' . __LINE__ . ']: Handling TCP package from resource=' . $socketArray[BasePool::SOCKET_ARRAY_RESOURCE] . ',type=' . $socketArray[BasePool::SOCKET_ARRAY_CONN_TYPE] . ',last error=' . socket_strerror(socket_last_error($socketArray[BasePool::SOCKET_ARRAY_RESOURCE])));
 
                /*
                 * Read the raw data from socket. If you change PHP_BINARY_READ to
-                * PHP_NORMAL_READ, this line will endless block. We only have
-                * simultanous threads and no real threads.
+                * PHP_NORMAL_READ, this line will endless block. This script does only
+                * provide simultanous threads, not real.
                 */
-               $rawData = socket_read($resource, $this->getConfigInstance()->getConfigEntry('tcp_buffer_length'), PHP_BINARY_READ);
+               $rawData = socket_read($socketArray[BasePool::SOCKET_ARRAY_RESOURCE], $this->getConfigInstance()->getConfigEntry('tcp_buffer_length'), PHP_BINARY_READ);
 
                // Debug output of read data length
-               //* NOISY-DEBUG: */ $this->debugOutput('TCP-HANDLER: rawData[]=' . strlen($rawData));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('TCP-HANDLER[' . __LINE__ . ']: rawData[' . gettype($rawData) . ']=' . strlen($rawData) . ',MD5=' . md5($rawData) . ',resource=' . $socketArray[BasePool::SOCKET_ARRAY_RESOURCE] . ',error=' . socket_strerror(socket_last_error($socketArray[BasePool::SOCKET_ARRAY_RESOURCE])));
+               //* NOISY-DEBUG: */ if ($rawData !== false) self::createDebugInstance(__CLASS__)->debugOutput('TCP-HANDLER[' . __LINE__ . ']: rawData=' . $rawData);
 
                // Is it valid?
-               if (($rawData === false) || (socket_last_error($resource) > 0)) {
+               if (socket_last_error($socketArray[BasePool::SOCKET_ARRAY_RESOURCE]) == 11) {
+                       // Debug message
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('TCP-HANDLER[' . __LINE__ . ']: Ignoring error 11 (Resource temporary unavailable) from socket resource=' . $socketArray[BasePool::SOCKET_ARRAY_RESOURCE]);
+
+                       /*
+                        * Error code 11 (Resource temporary unavailable) can be safely
+                        * ignored on non-blocking sockets. The socket is currently not
+                        * sending any data.
+                        */
+                        socket_clear_error($socketArray[BasePool::SOCKET_ARRAY_RESOURCE]);
+
+                        // Skip any further processing
+                        return;
+               } elseif (($rawData === false) || (socket_last_error($socketArray[BasePool::SOCKET_ARRAY_RESOURCE]) > 0)) {
                        // Network error or connection lost
-                       $this->setErrorCode(socket_last_error($resource));
+                       $this->setErrorCode(socket_last_error($socketArray[BasePool::SOCKET_ARRAY_RESOURCE]));
                } elseif (empty($rawData)) {
-                       // The peer did send nothing to us
-                       $this->setErrorCode(self::SOCKET_ERROR_EMPTY_DATA);
+                       // The peer did send nothing to us which is now being ignored
+                       return;
                } else {
                        /*
-                        * Low-level checks of the raw data went all fine, we can now
-                        * decode the raw data. This may still fail because of invalid
-                        * encoded data.
+                        * All is fine at this point. So it is okay to add the raw data to
+                        * the stacker. Here it doesn't matter if the raw data is a
+                        * well-formed BASE64-encoded message with start and markers. This
+                        * will be checked later on.
                         */
-                       $decodedData = $this->getInputStreamInstance()->streamData($rawData);
+                       $this->addRawDataToStacker($rawData);
                }
-
-               // Add the (maybe above decoded) data to the stacker
-               $this->addDecodedDataToStacker($decodedData);
        }
 }