Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00015 final class SocketOutputStream extends OutputStream
00016 {
00026 const WRITE_ATTEMPTS = 15;
00027
00028 private $socket = null;
00029
00030 public function __construct(Socket $socket)
00031 {
00032 $this->socket = $socket;
00033 }
00034
00038 public function write($buffer)
00039 {
00040 if ($buffer === null)
00041 return $this;
00042
00043 $totalBytes = strlen($buffer);
00044
00045 try {
00046 $writtenBytes = $this->socket->write($buffer);
00047
00048 if ($writtenBytes === false)
00049 throw new IOTimedOutException(
00050 'writing to socket timed out'
00051 );
00052
00053 $i = 0;
00054
00055 while (
00056 $writtenBytes < $totalBytes
00057 && ($i < self::WRITE_ATTEMPTS)
00058 ) {
00059
00060 usleep(100000);
00061
00062 $remainingBuffer = substr($buffer, $writtenBytes);
00063
00064
00065 $writtenBytes += $this->socket->write($remainingBuffer);
00066
00067 ++$i;
00068 }
00069 } catch (NetworkException $e) {
00070 throw new IOException($e->getMessage());
00071 }
00072
00073 if ($writtenBytes < $totalBytes)
00074 throw new IOException(
00075 'connection is too slow or buffer is too large?'
00076 );
00077
00078 return $this;
00079 }
00080 }
00081 ?>