]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/helper/connection/class_BaseConnectionHelper.php
The final hash must be used as an array key for serial numbers, else some packages...
[hub.git] / application / hub / main / helper / connection / class_BaseConnectionHelper.php
index 450f3f3af01dfaee0d46e6aa32a3070688fa210d..5391983364c8047f12f80e58a9dc6fd27f2eaa33 100644 (file)
@@ -328,18 +328,18 @@ class BaseConnectionHelper extends BaseHubSystemHelper implements Registerable,
         * @return      $chunkData              Raw data chunk
         */
        private function getRawDataFromPackageArray (array $packageData) {
-               // Implode the package data array and fragement the resulting string, returns the final hash
-               $finalHash = $this->getFragmenterInstance()->fragmentPackageArray($packageData, $this);
-               if ($finalHash !== TRUE) {
-                       $this->currentFinalHash = $finalHash;
-               } // END - if
-
                // Debug message
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: currentFinalHash=' . $this->currentFinalHash);
 
+               // Make sure the final hash is set
+               assert((is_string($this->currentFinalHash)) && (!empty($this->currentFinalHash)));
+
                // Get the next raw data chunk from the fragmenter
                $rawDataChunk = $this->getFragmenterInstance()->getNextRawDataChunk($this->currentFinalHash);
 
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: rawDataChunk=' . print_r($rawDataChunk, TRUE));
+
                // Get chunk hashes and chunk data
                $chunkHashes = array_keys($rawDataChunk);
                $chunkData   = array_values($rawDataChunk);
@@ -383,8 +383,18 @@ class BaseConnectionHelper extends BaseHubSystemHelper implements Registerable,
                // The helper's state must be 'connected'
                $this->getStateInstance()->validatePeerStateConnected();
 
+               // Implode the package data array and fragement the resulting string, returns the final hash
+               $finalHash = $this->getFragmenterInstance()->fragmentPackageArray($packageData, $this);
+               if ($finalHash !== TRUE) {
+                       // Debug message
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Setting finalHash=' . $finalHash . ',currentFinalHash[' . gettype($this->currentFinalHash) . ']=' . $this->currentFinalHash);
+
+                       // Set final hash
+                       $this->currentFinalHash = $finalHash;
+               } // END - if
+
                // Reset serial number
-               $this->getFragmenterInstance()->resetSerialNumber();
+               $this->getFragmenterInstance()->resetSerialNumber($this->currentFinalHash);
 
                // Cache buffer length
                $bufferSize = $this->getConfigInstance()->getConfigEntry($this->getProtocol() . '_buffer_length');
@@ -401,7 +411,7 @@ class BaseConnectionHelper extends BaseHubSystemHelper implements Registerable,
 
                        // Convert the package data array to a raw data stream
                        $dataStream = $this->getRawDataFromPackageArray($packageData);
-                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Adding ' . strlen($dataStream) . ' bytes to the sending buffer ...');
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Adding ' . strlen($dataStream) . ' bytes to the sending buffer ...');
                        $rawData .= $dataStream;
                } // END - while
 
@@ -412,7 +422,7 @@ class BaseConnectionHelper extends BaseHubSystemHelper implements Registerable,
                $encodedData = $this->getOutputStreamInstance()->streamData($rawData);
 
                // Debug message
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: rawData()=' . strlen($rawData) . ',encodedData()=' . strlen($encodedData));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: rawData()=' . strlen($rawData) . ',encodedData()=' . strlen($encodedData));
 
                // Calculate difference
                $this->diff = $bufferSize - strlen($encodedData);
@@ -426,15 +436,15 @@ class BaseConnectionHelper extends BaseHubSystemHelper implements Registerable,
                // Deliver all data
                while ($sentBytes !== FALSE) {
                        // And deliver it
-                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Sending out ' . strlen($encodedData) . ' bytes,bufferSize=' . $bufferSize . ',diff=' . $this->diff);
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Sending out ' . strlen($encodedData) . ' bytes,bufferSize=' . $bufferSize . ',diff=' . $this->diff);
 
                        if ($this->diff >= 0) {
                                // Send all out (encodedData is smaller than or equal buffer size)
-                               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: MD5=' . md5(substr($encodedData, 0, ($bufferSize - $this->diff))));
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: MD5=' . md5(substr($encodedData, 0, ($bufferSize - $this->diff))));
                                $sentBytes = socket_write($socketResource, $encodedData, ($bufferSize - $this->diff));
                        } else {
                                // Send buffer size out
-                               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: MD5=' . md5(substr($encodedData, 0, $bufferSize)));
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: MD5=' . md5(substr($encodedData, 0, $bufferSize)));
                                $sentBytes = socket_write($socketResource, $encodedData, $bufferSize);
                        }
 
@@ -447,7 +457,7 @@ class BaseConnectionHelper extends BaseHubSystemHelper implements Registerable,
                                throw new InvalidSocketException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
                        } elseif (($sentBytes == 0) && (strlen($encodedData) > 0)) {
                                // Nothing sent means we are done
-                               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: All sent! (LINE=' . __LINE__ . ')');
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: All sent! (LINE=' . __LINE__ . ')');
                                break;
                        }
 
@@ -458,7 +468,7 @@ class BaseConnectionHelper extends BaseHubSystemHelper implements Registerable,
                        $totalSentBytes += $sentBytes;
 
                        // Cut out the last unsent bytes
-                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Sent out ' . $sentBytes . ' of ' . strlen($encodedData) . ' bytes ...');
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Sent out ' . $sentBytes . ' of ' . strlen($encodedData) . ' bytes ...');
                        $encodedData = substr($encodedData, $sentBytes);
 
                        // Calculate difference again
@@ -467,13 +477,13 @@ class BaseConnectionHelper extends BaseHubSystemHelper implements Registerable,
                        // Can we abort?
                        if (strlen($encodedData) <= 0) {
                                // Abort here, all sent!
-                               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: All sent! (LINE=' . __LINE__ . ')');
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: All sent! (LINE=' . __LINE__ . ')');
                                break;
                        } // END - if
                } // END - while
 
                // Return sent bytes
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: totalSentBytes=' . $totalSentBytes . ',diff=' . $this->diff);
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: totalSentBytes=' . $totalSentBytes . ',diff=' . $this->diff);
                return $totalSentBytes;
        }