From: Mark Slee Date: Wed, 25 Oct 2006 19:54:20 +0000 (+0000) Subject: Give options for one-way or two-way buffering in PHP Thrift transports X-Git-Tag: 0.2.0~1629 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=60275c7a674fed08b0b1de0b463a7d0a579dcd23;p=common%2Fthrift.git Give options for one-way or two-way buffering in PHP Thrift transports Reviewed By: aditya git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664843 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/php/src/transport/TFramedTransport.php b/lib/php/src/transport/TFramedTransport.php index 21846601..31cb3764 100644 --- a/lib/php/src/transport/TFramedTransport.php +++ b/lib/php/src/transport/TFramedTransport.php @@ -30,13 +30,29 @@ class TFramedTransport extends TTransport { */ private $wBuf_; + /** + * Whether to frame reads + * + * @var bool + */ + private $read_; + + /** + * Whether to frame writes + * + * @var bool + */ + private $write_; + /** * Constructor. * * @param TTransport $transport Underlying transport */ - public function __construct($transport=null) { + public function __construct($transport=null, $read=true, $write=true) { $this->transport_ = $transport; + $this->read_ = $read; + $this->write_ = $write; } /** @@ -46,24 +62,24 @@ class TFramedTransport extends TTransport { * @param int $len How much data */ public function read($len) { - $out = ''; - $need = $len; - $have = strlen($this->rBuf_); - if ($need > $have) { - $out = $this->rBuf_; - $need -= $have; - $this->readFrame(); + if (!$this->read_) { + return $this->transport_->read($len); } - $give = $need; - if (strlen($this->rBuf_) < $give) { - $out .= $this->rBuf_; - $this->rBuf_ = ''; - } else { - $out .= substr($this->rBuf_, 0, $give); - $this->rBuf_ = substr($this->rBuf_, $give); + if (empty($this->rBuf_)) { + $this->readFrame(); } + // Just return full buff + if ($len > strlen($this->rBuf_)) { + $out = $this->rBuf_; + $this->rBuf_ = null; + return $out; + } + + // Return substr + $out = substr($this->rBuf_, 0, $len); + $this->rBuf_ = substr($this->rBuf_, $len); return $out; } @@ -85,6 +101,10 @@ class TFramedTransport extends TTransport { * @param int $len Limit of bytes to write */ public function write($buf, $len=null) { + if (!$this->write_) { + return $this->transport_->write($buf, $len); + } + if ($len !== null && $len < strlen($buf)) { $buf = substr($buf, 0, $len); } @@ -96,6 +116,10 @@ class TFramedTransport extends TTransport { * followed by the actual data. */ public function flush() { + if (!$this->write_) { + return $this->transport_->flush(); + } + $out = pack('N', strlen($this->wBuf_)); $out .= $this->wBuf_; $this->transport_->write($out);