Permalink
Browse files

Merged sendStream() into send()

  • Loading branch information...
1 parent f9af7c4 commit c1d9c1517ec5ec94c47d7590547755ae433f4657 @boenrobot boenrobot committed Oct 14, 2011
Showing with 35 additions and 45 deletions.
  1. +30 −40 src/PEAR2/Net/Transmitter/Stream.php
  2. +3 −3 tests/ClientTest.php
  3. +2 −2 tests/ServerTest.php
@@ -181,60 +181,50 @@ public function setChunk($size, $direction = self::DIRECTION_ALL)
}
/**
- * Sends a string over the wrapped stream.
+ * Sends a string or stream over the wrapped stream.
*
- * @param string $string The string to send.
+ * @param string|resource $contents The string or stream to send.
*
* @return int The number of bytes sent.
*/
- public function send($string)
+ public function send($contents)
{
$bytes = 0;
- $bytesToSend = (double) sprintf('%u', strlen($string));
$chunkSize = $this->chunkSize[self::DIRECTION_SEND];
- while ($bytes < $bytesToSend) {
- if ($this->isAcceptingData()) {
- $bytesNow = @fwrite(
- $this->stream, substr($string, $bytes, $chunkSize)
- );
- if (0 != $bytesNow) {
- $bytes += $bytesNow;
- } else {
- throw $this->createException(
- 'Failed while sending string.', 2
+ if (self::isStream($contents)) {
+ while (!feof($contents)) {
+ if ($this->isAcceptingData()) {
+ $bytesNow = @stream_copy_to_stream(
+ $contents, $this->stream, $chunkSize
);
+ if (0 != $bytesNow) {
+ $bytes += $bytesNow;
+ } else {
+ throw $this->createException(
+ 'Failed while sending stream.', 3
+ );
+ }
}
}
- }
- return $bytes;
- }
-
- /**
- * Sends a stream over the wrapped stream.
- *
- * @param resource $stream The stream to send.
- *
- * @return int The number of bytes sent.
- */
- public function sendStream($stream)
- {
- $bytes = 0;
- $chunkSize = $this->chunkSize[self::DIRECTION_SEND];
- while (!feof($stream)) {
- if ($this->isAcceptingData()) {
- $bytesNow = @stream_copy_to_stream(
- $stream, $this->stream, $chunkSize
- );
- if (0 != $bytesNow) {
- $bytes += $bytesNow;
- } else {
- throw $this->createException(
- 'Failed while sending stream.', 3
+ fseek($contents, -$bytes, SEEK_CUR);
+ }else {
+ $contents = (string) $contents;
+ $bytesToSend = (double) sprintf('%u', strlen($contents));
+ while ($bytes < $bytesToSend) {
+ if ($this->isAcceptingData()) {
+ $bytesNow = @fwrite(
+ $this->stream, substr($contents, $bytes, $chunkSize)
);
+ if (0 != $bytesNow) {
+ $bytes += $bytesNow;
+ } else {
+ throw $this->createException(
+ 'Failed while sending string.', 2
+ );
+ }
}
}
}
- fseek($stream, -$bytes, SEEK_CUR);
return $bytes;
}
View
@@ -44,7 +44,7 @@ public function testOneByteEchoStreamSend()
$stream = fopen('php://temp', 'r+b');
fwrite($stream, '3');
rewind($stream);
- $this->client->sendStream($stream);
+ $this->client->send($stream);
$this->assertEquals(
stream_get_contents($stream), $this->client->receive(1),
'Wrong byte echoed.'
@@ -57,7 +57,7 @@ public function test3MegaBytesEchoStreamSend()
$stream = fopen('php://temp', 'r+b');
fwrite($stream, str_repeat('4', $size));
rewind($stream);
- $this->client->sendStream($stream);
+ $this->client->send($stream);
$this->assertEquals(
stream_get_contents($stream), $this->client->receive($size),
'Wrong contents echoed.'
@@ -160,7 +160,7 @@ public function testClientSendingIncompleteDataStream()
fwrite($stream, str_repeat('8', $size));
rewind($stream);
try {
- $this->client->sendStream($stream);
+ $this->client->send($stream);
} catch(SocketException $e) {
$this->assertEquals(3, $e->getCode(), 'Improper exception code.');
}
View
@@ -74,7 +74,7 @@ public function testOneByteEchoStreamSend()
fwrite($stream, $this->conn->receive(1));
rewind($stream);
$this->assertEquals(
- 1, $this->conn->sendStream($stream),
+ 1, $this->conn->send($stream),
'Wrong amount echoed.'
);
}
@@ -86,7 +86,7 @@ public function test3MegaBytesEchoStreamSend()
fwrite($stream, $this->conn->receive($size));
rewind($stream);
$this->assertEquals(
- $size, $this->conn->sendStream($stream),
+ $size, $this->conn->send($stream),
'Wrong amount echoed.'
);
}

0 comments on commit c1d9c15

Please sign in to comment.