From: Mark Slee Date: Sat, 17 Nov 2007 00:27:17 +0000 (+0000) Subject: Fix PHP TBufferedTransport readAll() to work with TSocket timeouts X-Git-Tag: 0.2.0~1124 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=f5a6b0c10939a2c0dd33fa88b8ce4e3f44a33186;p=common%2Fthrift.git Fix PHP TBufferedTransport readAll() to work with TSocket timeouts Reviewed By: lucas Test Plan: Invoke Buffered Transport wrapped around socket git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665348 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/php/src/transport/TBufferedTransport.php b/lib/php/src/transport/TBufferedTransport.php index 8f410dfc..eb76167e 100644 --- a/lib/php/src/transport/TBufferedTransport.php +++ b/lib/php/src/transport/TBufferedTransport.php @@ -85,6 +85,33 @@ class TBufferedTransport extends TTransport { } } + /** + * The reason that we customize readAll here is that the majority of PHP + * streams are already internally buffered by PHP. The socket stream, for + * example, buffers internally and blocks if you call read with $len greater + * than the amount of data available, unlike recv() in C. + * + * Therefore, use the readAll method of the wrapped transport inside + * the buffered readAll. + */ + public function readAll($len) { + $have = strlen($this->rBuf_); + if ($have == 0) { + $data = $this->transport_->readAll($len); + } else if ($have < $len) { + $data = $this->rBuf_; + $this->rBuf_ = ''; + $data .= $this->transport_->readAll($len - $have); + } else if ($have == $len) { + $data = $this->rBuf_; + $this->rBuf_ = ''; + } else if ($have > $len) { + $data = substr($this->rBuf_, 0, $len); + $this->rBuf_ = substr($this->rBuf_, $len); + } + return $data; + } + public function read($len) { if (strlen($this->rBuf_) === 0) { $this->rBuf_ = $this->transport_->read($this->rBufSize_);