SocketOutputStream.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007 by Ivan Y. Khvostishkov                            *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: SocketOutputStream.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     final class SocketOutputStream extends OutputStream
00017     {
00027         const WRITE_ATTEMPTS = 15; // should be enough for everyone (C)
00028         
00029         private $socket = null;
00030         
00031         public function __construct(Socket $socket)
00032         {
00033             $this->socket = $socket;
00034         }
00035         
00039         public function write($buffer)
00040         {
00041             if ($buffer === null)
00042                 return $this;
00043             
00044             $totalBytes = strlen($buffer);
00045             
00046             try {
00047                 $writtenBytes = $this->socket->write($buffer);
00048                 
00049                 if ($writtenBytes === false)
00050                     throw new IOTimedOutException(
00051                         'writing to socket timed out'
00052                     );
00053                 
00054                 $i = 0;
00055                 
00056                 while (
00057                     $writtenBytes < $totalBytes
00058                     && ($i < self::WRITE_ATTEMPTS)
00059                 ) {
00060                     // 0.1s sleep insurance if something wrong with socket
00061                     usleep(100000);
00062                     
00063                     $remainingBuffer = substr($buffer, $writtenBytes);
00064                     
00065                     // NOTE: ignoring timeouts here
00066                     $writtenBytes += $this->socket->write($remainingBuffer);
00067                     
00068                     ++$i;
00069                 }
00070             } catch (NetworkException $e) {
00071                 throw new IOException($e->getMessage());
00072             }
00073             
00074             if ($writtenBytes < $totalBytes)
00075                 throw new IOException(
00076                     'connection is too slow or buffer is too large?'
00077                 );
00078             
00079             return $this;
00080         }
00081     }
00082 ?>

Generated on Sun Dec 9 21:56:24 2007 for onPHP by  doxygen 1.5.4