]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/helper/connection/class_BaseConnectionHelper.php
'hub' project continued:
[hub.git] / application / hub / main / helper / connection / class_BaseConnectionHelper.php
index 685d079e31ff7fd774788dcbbe9de3a006cc66dd..c854646427cb9d6068499d698051c3f94142f610 100644 (file)
@@ -91,6 +91,12 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
 
                // Register this connection helper
                Registry::getRegistry()->addInstance('connection', $this);
+
+               // Get the fragmenter instance
+               $fragmenterInstance = FragmenterFactory::createFragmenterInstance('package');
+
+               // Set it here
+               $this->setFragmenterInstance($fragmenterInstance);
        }
 
        /**
@@ -295,11 +301,8 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
         * @return      $chunkData              Raw data chunk
         */
        private function getRawDataFromPackageArray (array $packageData) {
-               // Get the fragmenter instance
-               $fragmenterInstance = FragmenterFactory::createFragmenterInstance('package');
-
                // Implode the package data array and fragement the resulting string, returns the final hash
-               $finalHash = $fragmenterInstance->fragmentPackageArray($packageData, $this);
+               $finalHash = $this->getFragmenterInstance()->fragmentPackageArray($packageData, $this);
                if ($finalHash !== true) {
                        $this->currentFinalHash = $finalHash;
                } // END - if
@@ -308,7 +311,7 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: currentFinalHash=' . $this->currentFinalHash);
 
                // Get the next raw data chunk from the fragmenter
-               $rawDataChunk = $fragmenterInstance->getNextRawDataChunk($this->currentFinalHash);
+               $rawDataChunk = $this->getFragmenterInstance()->getNextRawDataChunk($this->currentFinalHash);
 
                // Get chunk hashes and chunk data
                $chunkHashes = array_keys($rawDataChunk);
@@ -316,14 +319,17 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
 
                // Is the required data there?
                //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: chunkHashes[]=' . count($chunkHashes) . ',chunkData[]=' . count($chunkData));
+               //* NOISY-DEBUG: */ $this->debugOutput('chunkData='.print_r($chunkData,true));
                if ((isset($chunkHashes[0])) && (isset($chunkData[0]))) {
                        // Remember this chunk as queued
                        $this->queuedChunks[$chunkHashes[0]] = $chunkData[0];
 
                        // Return the raw data
+                       //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Returning ' . strlen($chunkData[0]) . ' bytes from ' . __METHOD__ . ' ...');
                        return $chunkData[0];
                } else {
                        // Return zero string
+                       //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Returning zero bytes from ' . __METHOD__ . '!');
                        return '';
                }
        }
@@ -359,12 +365,13 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                $totalSentBytes = 0;
 
                // Fill sending buffer with data
-               while ((strlen($rawData) < $bufferSize) && (strlen($dataStream) > 0)) {
+               while (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
+               //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: rawData[' . strlen($rawData) . ']=' . $rawData);
 
                // Nothing to sent is bad news, so assert on it
                assert(strlen($rawData) > 0);
@@ -385,7 +392,13 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                while ($sentBytes !== false) {
                        // And deliver it
                        //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Sending out ' . strlen($encodedData) . ' bytes,bufferSize=' . $bufferSize . ',diff=' . $this->diff);
-                       $sentBytes = @socket_write($socketResource, $encodedData, ($bufferSize - $this->diff));
+                       if ($this->diff >= 0) {
+                               // Send all out (encodedData is smaller than or equal buffer size)
+                               $sentBytes = @socket_write($socketResource, $encodedData, ($bufferSize - $this->diff));
+                       } else {
+                               // Send buffer size out
+                               $sentBytes = @socket_write($socketResource, $encodedData, $bufferSize);
+                       }
 
                        // If there was an error, we don't continue here
                        if ($sentBytes === false) {
@@ -396,7 +409,7 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                                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: */ $this->debugOutput('CONNECTION: All sent! (' . __LINE__ . ')');
+                               //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: All sent! (LINE=' . __LINE__ . ')');
                                break;
                        }
 
@@ -416,13 +429,13 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                        // Can we abort?
                        if (strlen($encodedData) <= 0) {
                                // Abort here, all sent!
-                               //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: All sent! (' . __LINE__ . ')');
+                               //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: All sent! (LINE=' . __LINE__ . ')');
                                break;
                        } // END - if
                } // END - while
 
                // Return sent bytes
-               //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: totalSentBytes=' . $totalSentBytes);
+               //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: totalSentBytes=' . $totalSentBytes . ',diff=' . $this->diff);
                return $totalSentBytes;
        }
 
@@ -432,7 +445,7 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
         * @return      void
         */
        protected final function markConnectionShuttedDown () {
-               /* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: ' . $this->__toString() . ' has been marked as shutted down');
+               //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: ' . $this->__toString() . ' has been marked as shutted down');
                $this->shuttedDown = true;
 
                // And remove the (now invalid) socket
@@ -445,7 +458,7 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
         * @return      $shuttedDown    Whether this connection is shutted down
         */
        public final function isShuttedDown () {
-               /* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: ' . $this->__toString() . ',shuttedDown=' . intval($this->shuttedDown));
+               //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: ' . $this->__toString() . ',shuttedDown=' . intval($this->shuttedDown));
                return $this->shuttedDown;
        }