]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/helper/connection/class_BaseConnectionHelper.php
Added a new task for listener pools and network package readers (for abstract Network...
[hub.git] / application / hub / main / helper / connection / class_BaseConnectionHelper.php
index 4771e4d669b0531472f802200c76efaa38760488..c88e89e53d49d298ac1acf3e3a053c424034cece 100644 (file)
@@ -43,9 +43,9 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
        private $sentData = 0;
 
        /**
-        * Offset
+        * Difference
         */
-       private $offset = 0;
+       private $diff = 0;
 
        /**
         * Connect retries for this connection
@@ -167,7 +167,13 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                }
 
                // Implode the package data array and fragement the resulting string, returns the final hash
-               $this->currentFinalHash = $fragmenterInstance->fragmentPackageArray($packageData, $this);
+               $finalHash = $fragmenterInstance->fragmentPackageArray($packageData, $this);
+               if ($finalHash !== true) {
+                       $this->currentFinalHash = $finalHash;
+               } // END - if
+
+               // Debug message
+               //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: currentFinalHash=' . $this->currentFinalHash);
 
                // Get the next raw data chunk from the fragmenter
                $rawDataChunk = $fragmenterInstance->getNextRawDataChunk($this->currentFinalHash);
@@ -176,11 +182,18 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                $chunkHashes = array_keys($rawDataChunk);
                $chunkData   = array_values($rawDataChunk);
 
-               // Remember this chunk as queued
-               $this->queuedChunks[$chunkHashes[0]] = $chunkData[0];
+               // Is the required data there?
+               //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: chunkHashes[]=' . count($chunkHashes) . ',chunkData[]=' . count($chunkData));
+               if ((isset($chunkHashes[0])) && (isset($chunkData[0]))) {
+                       // Remember this chunk as queued
+                       $this->queuedChunks[$chunkHashes[0]] = $chunkData[0];
 
-               // Return the raw data
-               return $chunkData[0];
+                       // Return the raw data
+                       return $chunkData[0];
+               } else {
+                       // Return zero string
+                       return '';
+               }
        }
 
        /**
@@ -197,43 +210,88 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
        /**
         * Sends raw package data to the recipient
         *
-        * @param       $packageData    Raw package data
-        * @return      $sentBytes              Actual sent bytes to the peer
+        * @param       $packageData            Raw package data
+        * @return      $totalSentBytes         Total sent bytes to the peer
         * @throws      InvalidSocketException  If we got a problem with this socket
         */
        public function sendRawPackageData (array $packageData) {
-               // Convert the package data array to a raw data stream
-               $rawData = $this->getRawDataFromPackageArray($packageData);
+               // Cache buffer length
+               $bufferSize = $this->getConfigInstance()->getConfigEntry($this->getProtocol() . '_buffer_length');
+
+               // Init variables
+               $rawData = '';
+               $dataStream = ' ';
+               $totalSentBytes = 0;
+
+               // Fill sending buffer with data
+               while ((strlen($rawData) < $bufferSize) && (strlen($dataStream) > 0)) {
+                       // Convert the package data array to a raw data stream
+                       $dataStream = $this->getRawDataFromPackageArray($packageData);
+                       //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Adding ' . strlen($dataStream) . ' bytes to the sending buffer ...');
+                       $rawData .= $dataStream;
+               } // END - while
+
+               // Nothing to sent is bad news!
+               assert(strlen($rawData) > 0);
+
+               // Calculate difference
+               $this->diff = $bufferSize - strlen($rawData);
 
                // Get socket resource
                $socketResource = $this->getSocketResource();
 
-               // And deliver it
-               $sentBytes = @socket_write($socketResource, $rawData, $this->getConfigInstance()->getConfigEntry($this->getProtocol() . '_buffer_length') - $this->offset);
+               // Init sent bytes
+               $sentBytes = 0;
 
-               // If there was an error, we don't continue here
-               if ($sentBytes === false) {
-                       // Get socket error code for verification
-                       $socketError = socket_last_error($socketResource);
+               // Deliver all data
+               while ($sentBytes !== false) {
+                       // And deliver it
+                       //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Sending out ' . strlen($rawData) . ' bytes,bufferSize=' . $bufferSize . ',diff=' . $this->diff);
+                       $sentBytes = @socket_write($socketResource, $rawData, ($bufferSize - $this->diff));
 
-                       // Get error message
-                       $errorMessage = socket_strerror($socketError);
+                       // If there was an error, we don't continue here
+                       if ($sentBytes === false) {
+                               // Get socket error code for verification
+                               $socketError = socket_last_error($socketResource);
 
-                       // Shutdown this socket
-                       $this->shutdownSocket($socketResource);
+                               // Get error message
+                               $errorMessage = socket_strerror($socketError);
 
-                       // And throw it
-                       throw new InvalidSocketException(array($this, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
-               } elseif ($sentBytes == 0) {
-                       // Nothing sent is bad news
-                       die(__METHOD__.': Unhandled 0 sent bytes! rawData[]=' . strlen($rawData));
-               }
+                               // Shutdown this socket
+                               $this->shutdownSocket($socketResource);
+
+                               // And throw it
+                               throw new InvalidSocketException(array($this, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       } elseif (($sentBytes == 0) && (strlen($rawData) > 0)) {
+                               // Nothing sent means we are done
+                               //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: All sent! (' . __LINE__ . ')');
+                               break;
+                       }
+
+                       // The difference between sent bytes and length of raw data should not be below zero
+                       assert((strlen($rawData) - $sentBytes) >= 0);
+
+                       // Add total sent bytes
+                       $totalSentBytes += $sentBytes;
+
+                       // Cut out the last unsent bytes
+                       //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Sent out ' . $sentBytes . ' of ' . strlen($rawData) . ' bytes ...');
+                       $rawData = substr($rawData, $sentBytes);
+
+                       // Calculate difference again
+                       $this->diff = $bufferSize - strlen($rawData);
 
-               // The difference between sent bytes and length of raw data should not be below zero
-               assert((strlen($rawData) - $sentBytes) >= 0);
+                       // Can we abort?
+                       if (strlen($rawData) <= 0) {
+                               // Abort here, all sent!
+                               //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: All sent! (' . __LINE__ . ')');
+                               break;
+                       } // END - if
+               } // END - while
 
                // Return sent bytes
-               return $sentBytes;
+               //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: totalSentBytes=' . $totalSentBytes);
+               return $totalSentBytes;
        }
 
        /**