// Register this connection helper
Registry::getRegistry()->addInstance('connection', $this);
+
+ // Get the fragmenter instance
+ $fragmenterInstance = FragmenterFactory::createFragmenterInstance('package');
+
+ // Set it here
+ $this->setFragmenterInstance($fragmenterInstance);
}
/**
* @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
//* 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);
// 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 '';
}
}
$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);
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) {
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;
}
// 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;
}
* @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
* @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;
}