SocketInputStream.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: SocketInputStream.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     final class SocketInputStream extends InputStream
00017     {
00027         const READ_ATTEMPTS = 15; // should be enough for everyone (C)
00028         
00029         private $socket = null;
00030         protected $eof  = false;
00031         
00032         public function __construct(Socket $socket)
00033         {
00034             $this->socket = $socket;
00035         }
00036         
00037         public function isEof()
00038         {
00039             return $this->eof;
00040         }
00041         
00042         public function read($length)
00043         {
00044             if ($length == 0 || $this->eof)
00045                 return null;
00046             
00047             try {
00048                 $result = $this->socket->read($length);
00049                 
00050                 if ($result === null)
00051                     $this->eof = true;
00052                 
00053                 $i = 0;
00054                 
00055                 while (
00056                     !$this->eof
00057                     && strlen($result) < $length
00058                     && ($i < self::READ_ATTEMPTS)
00059                 ) {
00060                     // 0.1s sleep insurance if something wrong with socket
00061                     usleep(100000);
00062                     
00063                     $remainingLength = $length - strlen($result);
00064                     
00065                     // NOTE: ignoring timeouts here
00066                     $nextPart = $this->socket->read($remainingLength);
00067                     
00068                     if ($nextPart !== null)
00069                         $result .= $nextPart;
00070                     else
00071                         $this->eof = true;
00072                     
00073                     ++$i;
00074                 }
00075             } catch (NetworkException $e) {
00076                 throw new IOException($e->getMessage());
00077             }
00078             
00079             if (!$this->eof && strlen($result) < $length)
00080                 throw new IOException(
00081                     'connection is too slow or length is too large?'
00082                 );
00083             
00084             return $result;
00085         }
00086     }
00087 ?>

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