X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=application%2Fhub%2Fmain%2Fhelper%2Fconnection%2Fclass_BaseConnectionHelper.php;h=c854646427cb9d6068499d698051c3f94142f610;hb=6124e1c12e45fd628f93e3bf27ed481952494146;hp=27ced2e4224bea69087ff3a90d53af7b89159c1f;hpb=f884d1738b81b893f44001100f51661f307ad9a3;p=hub.git diff --git a/application/hub/main/helper/connection/class_BaseConnectionHelper.php b/application/hub/main/helper/connection/class_BaseConnectionHelper.php index 27ced2e42..c85464642 100644 --- a/application/hub/main/helper/connection/class_BaseConnectionHelper.php +++ b/application/hub/main/helper/connection/class_BaseConnectionHelper.php @@ -56,7 +56,7 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc private $isInitialized = false; /** - * Wether this connection is shutted down + * Whether this connection is shutted down */ private $shuttedDown = false; @@ -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); } /** @@ -205,7 +211,7 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc * recipientData array with currently configured timeout. * * @param $recipientData A valid recipient data array, 0=IP; 1=PORT - * @return $isConnected Wether the connection went fine + * @return $isConnected Whether the connection went fine * @see Please see http://de.php.net/manual/en/function.socket-connect.php#84465 for original code * @todo Rewrite the while() loop to a iterator to not let the software stay very long here */ @@ -220,6 +226,9 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc $socketResource = $this->getSocketResource(); $timeout = $this->getConfigInstance()->getConfigEntry('socket_timeout_seconds'); + // Debug output + $this->debugOutput('CONNECTION-HELPER: Trying to connect to ' . $recipientData[0] . ':' . $recipientData[1] . ' with socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' ...'); + // Try to connect until it is connected while ($isConnected = !@socket_connect($socketResource, $recipientData[0], $recipientData[1])) { // Get last socket error @@ -292,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 @@ -305,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); @@ -313,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 ''; } } @@ -356,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); @@ -382,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) { @@ -393,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; } @@ -413,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; } @@ -429,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 @@ -439,10 +455,10 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc /** * Getter for shuttedDown * - * @return $shuttedDown Wether this connection is shutted down + * @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; } @@ -569,7 +585,7 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc * @return void */ protected function socketErrorOperationInProgressHandler ($socketResource) { - $this->debugOutput('CONNECTION: Operation is now in progress, this is usual for non-blocking connections and no bug.'); + $this->debugOutput('CONNECTION: Operation is now in progress, this is usual for non-blocking connections and is no bug.'); } }