3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
20 * @package thrift.transport
23 namespace Thrift\Transport
;
25 use Thrift\Factory\TStringFuncFactory
;
28 * Framed transport. Writes and reads data in chunks that are stamped with
31 * @package thrift.transport
33 class TFramedTransport
extends TTransport
36 * Underlying transport object.
43 * Buffer for read data.
50 * Buffer for queued output data
57 * Whether to frame reads
64 * Whether to frame writes
73 * @param TTransport $transport Underlying transport
75 public function __construct($transport = null, $read = true, $write = true)
77 $this->transport_
= $transport;
79 $this->write_
= $write;
82 public function isOpen()
84 return $this->transport_
->isOpen();
87 public function open()
89 $this->transport_
->open();
92 public function close()
94 $this->transport_
->close();
98 * Reads from the buffer. When more data is required reads another entire
99 * chunk and serves future reads out of that.
101 * @param int $len How much data
103 public function read($len)
106 return $this->transport_
->read($len);
109 if (TStringFuncFactory
::create()->strlen($this->rBuf_
) === 0) {
113 // Just return full buff
114 if ($len >= TStringFuncFactory
::create()->strlen($this->rBuf_
)) {
121 // Return TStringFuncFactory::create()->substr
122 $out = TStringFuncFactory
::create()->substr($this->rBuf_
, 0, $len);
123 $this->rBuf_
= TStringFuncFactory
::create()->substr($this->rBuf_
, $len);
129 * Put previously read data back into the buffer
131 * @param string $data data to return
133 public function putBack($data)
135 if (TStringFuncFactory
::create()->strlen($this->rBuf_
) === 0) {
136 $this->rBuf_
= $data;
138 $this->rBuf_
= ($data . $this->rBuf_
);
143 * Reads a chunk of data into the internal read buffer.
145 private function readFrame()
147 $buf = $this->transport_
->readAll(4);
148 $val = unpack('N', $buf);
151 $this->rBuf_
= $this->transport_
->readAll($sz);
155 * Writes some data to the pending output buffer.
157 * @param string $buf The data
158 * @param int $len Limit of bytes to write
160 public function write($buf, $len = null)
162 if (!$this->write_
) {
163 return $this->transport_
->write($buf, $len);
166 if ($len !== null && $len < TStringFuncFactory
::create()->strlen($buf)) {
167 $buf = TStringFuncFactory
::create()->substr($buf, 0, $len);
169 $this->wBuf_
.= $buf;
173 * Writes the output buffer to the stream in the format of a 4-byte length
174 * followed by the actual data.
176 public function flush()
178 if (!$this->write_ || TStringFuncFactory
::create()->strlen($this->wBuf_
) == 0) {
179 return $this->transport_
->flush();
182 $out = pack('N', TStringFuncFactory
::create()->strlen($this->wBuf_
));
183 $out .= $this->wBuf_
;
185 // Note that we clear the internal wBuf_ prior to the underlying write
186 // to ensure we're in a sane state (i.e. internal buffer cleaned)
187 // if the underlying write throws up an exception
189 $this->transport_
->write($out);
190 $this->transport_
->flush();