diff options
author | Tom N Harris <tnharris@whoopdedo.org> | 2012-06-20 00:52:10 -0400 |
---|---|---|
committer | Tom N Harris <tnharris@whoopdedo.org> | 2012-06-20 00:52:10 -0400 |
commit | 24f112d0c2604958ac73923f758d9d37f229ff11 (patch) | |
tree | bd7f35060708def926e19b1b8af028ce33a24eaa /inc | |
parent | 288188afa98cd0ff65b2c2bf529fba0bff15f634 (diff) | |
download | rpg-24f112d0c2604958ac73923f758d9d37f229ff11.tar.gz rpg-24f112d0c2604958ac73923f758d9d37f229ff11.tar.bz2 |
Efficiently wait on sockets
Diffstat (limited to 'inc')
-rw-r--r-- | inc/HTTPClient.php | 45 |
1 files changed, 37 insertions, 8 deletions
diff --git a/inc/HTTPClient.php b/inc/HTTPClient.php index 534ba220c..f6ab91f4f 100644 --- a/inc/HTTPClient.php +++ b/inc/HTTPClient.php @@ -507,7 +507,8 @@ class HTTPClient { throw new HTTPClientException("Socket disconnected while writing $message"); // wait for stream ready or timeout - if(@stream_select($sel_r, $sel_w, $sel_e, $this->timeout - $time_used) !== false){ + self::selecttimeout($this->timeout - $time_used, $sec, $usec); + if(@stream_select($sel_r, $sel_w, $sel_e, $sec, $usec) !== false){ // write to stream $nbytes = fwrite($socket, substr($data,$written,4096)); if($nbytes === false) @@ -533,6 +534,11 @@ class HTTPClient { * @author Tom N Harris <tnharris@whoopdedo.org> */ function _readData($socket, $nbytes, $message, $ignore_eof = false) { + // select parameters + $sel_r = array($socket); + $sel_w = null; + $sel_e = null; + $r_data = ''; $to_read = $nbytes ? $nbytes : 4096; if ($to_read < 0) $to_read = -$to_read; @@ -544,10 +550,16 @@ class HTTPClient { -100); if(!$ignore_eof && feof($socket)) throw new HTTPClientException("Premature End of File (socket) while reading $message"); - //usleep(1000); - $bytes = fread($socket, $to_read); - $r_data .= $bytes; - $to_read -= strlen($bytes); + + // wait for stream ready or timeout + self::selecttimeout($this->timeout - $time_used, $sec, $usec); + if(@stream_select($sel_r, $sel_w, $sel_e, $sec, $usec) !== false){ + $bytes = fread($socket, $to_read); + if($bytes === false) + throw new HTTPClientException("Failed reading from socket while reading $message", -100); + $r_data .= $bytes; + $to_read -= strlen($bytes); + } } while (strlen($r_data) < $nbytes); return $r_data; } @@ -562,6 +574,11 @@ class HTTPClient { * @author Tom N Harris <tnharris@whoopdedo.org> */ function _readLine($socket, $message) { + // select parameters + $sel_r = array($socket); + $sel_w = null; + $sel_e = null; + $r_data = ''; do { $time_used = $this->_time() - $this->start; @@ -571,8 +588,12 @@ class HTTPClient { -100); if(feof($socket)) throw new HTTPClientException("Premature End of File (socket) while reading $message"); - usleep(1000); - $r_data .= fgets($socket, 1024); + + // wait for stream ready or timeout + self::selecttimeout($this->timeout - $time_used, $sec, $usec); + if(@stream_select($sel_r, $sel_w, $sel_e, $sec, $usec) !== false){ + $r_data = fgets($socket, 1024); + } } while (!preg_match('/\n$/',$r_data)); return $r_data; } @@ -597,12 +618,20 @@ class HTTPClient { /** * Return current timestamp in microsecond resolution */ - function _time(){ + static function _time(){ list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } /** + * Calculate seconds and microseconds + */ + static function selecttimeout($time, &$sec, &$usec){ + $sec = floor($time); + $usec = (int)(($time - $sec) * 1000000); + } + + /** * convert given header string to Header array * * All Keys are lowercased. |