]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/php/lib/Transport/TBufferedTransport.php
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / php / lib / Transport / TBufferedTransport.php
1 <?php
2 /*
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
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
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
18 * under the License.
19 *
20 * @package thrift.transport
21 */
22
23 namespace Thrift\Transport;
24
25 use Thrift\Exception\TTransportException;
26 use Thrift\Factory\TStringFuncFactory;
27
28 /**
29 * Buffered transport. Stores data to an internal buffer that it doesn't
30 * actually write out until flush is called. For reading, we do a greedy
31 * read and then serve data out of the internal buffer.
32 *
33 * @package thrift.transport
34 */
35 class TBufferedTransport extends TTransport
36 {
37 /**
38 * The underlying transport
39 *
40 * @var TTransport
41 */
42 protected $transport_;
43
44 /**
45 * The receive buffer size
46 *
47 * @var int
48 */
49 protected $rBufSize_ = 512;
50
51 /**
52 * The write buffer size
53 *
54 * @var int
55 */
56 protected $wBufSize_ = 512;
57
58 /**
59 * The write buffer.
60 *
61 * @var string
62 */
63 protected $wBuf_ = '';
64
65 /**
66 * The read buffer.
67 *
68 * @var string
69 */
70 protected $rBuf_ = '';
71
72 /**
73 * Constructor. Creates a buffered transport around an underlying transport
74 */
75 public function __construct($transport, $rBufSize = 512, $wBufSize = 512)
76 {
77 $this->transport_ = $transport;
78 $this->rBufSize_ = $rBufSize;
79 $this->wBufSize_ = $wBufSize;
80 }
81
82 public function isOpen()
83 {
84 return $this->transport_->isOpen();
85 }
86
87 /**
88 * @inheritdoc
89 *
90 * @throws TTransportException
91 */
92 public function open()
93 {
94 $this->transport_->open();
95 }
96
97 public function close()
98 {
99 $this->transport_->close();
100 }
101
102 public function putBack($data)
103 {
104 if (TStringFuncFactory::create()->strlen($this->rBuf_) === 0) {
105 $this->rBuf_ = $data;
106 } else {
107 $this->rBuf_ = ($data . $this->rBuf_);
108 }
109 }
110
111 /**
112 * The reason that we customize readAll here is that the majority of PHP
113 * streams are already internally buffered by PHP. The socket stream, for
114 * example, buffers internally and blocks if you call read with $len greater
115 * than the amount of data available, unlike recv() in C.
116 *
117 * Therefore, use the readAll method of the wrapped transport inside
118 * the buffered readAll.
119 *
120 * @throws TTransportException
121 */
122 public function readAll($len)
123 {
124 $have = TStringFuncFactory::create()->strlen($this->rBuf_);
125 if ($have == 0) {
126 $data = $this->transport_->readAll($len);
127 } elseif ($have < $len) {
128 $data = $this->rBuf_;
129 $this->rBuf_ = '';
130 $data .= $this->transport_->readAll($len - $have);
131 } elseif ($have == $len) {
132 $data = $this->rBuf_;
133 $this->rBuf_ = '';
134 } elseif ($have > $len) {
135 $data = TStringFuncFactory::create()->substr($this->rBuf_, 0, $len);
136 $this->rBuf_ = TStringFuncFactory::create()->substr($this->rBuf_, $len);
137 }
138
139 return $data;
140 }
141
142 /**
143 * @inheritdoc
144 *
145 * @param int $len
146 * @return string
147 * @throws TTransportException
148 */
149 public function read($len)
150 {
151 if (TStringFuncFactory::create()->strlen($this->rBuf_) === 0) {
152 $this->rBuf_ = $this->transport_->read($this->rBufSize_);
153 }
154
155 if (TStringFuncFactory::create()->strlen($this->rBuf_) <= $len) {
156 $ret = $this->rBuf_;
157 $this->rBuf_ = '';
158
159 return $ret;
160 }
161
162 $ret = TStringFuncFactory::create()->substr($this->rBuf_, 0, $len);
163 $this->rBuf_ = TStringFuncFactory::create()->substr($this->rBuf_, $len);
164
165 return $ret;
166 }
167
168 /**
169 * @inheritdoc
170 *
171 * @param string $buf
172 * @throws TTransportException
173 */
174 public function write($buf)
175 {
176 $this->wBuf_ .= $buf;
177 if (TStringFuncFactory::create()->strlen($this->wBuf_) >= $this->wBufSize_) {
178 $out = $this->wBuf_;
179
180 // Note that we clear the internal wBuf_ prior to the underlying write
181 // to ensure we're in a sane state (i.e. internal buffer cleaned)
182 // if the underlying write throws up an exception
183 $this->wBuf_ = '';
184 $this->transport_->write($out);
185 }
186 }
187
188 /**
189 * @inheritdoc
190 *
191 * @throws TTransportException
192 */
193 public function flush()
194 {
195 if (TStringFuncFactory::create()->strlen($this->wBuf_) > 0) {
196 $out = $this->wBuf_;
197
198 // Note that we clear the internal wBuf_ prior to the underlying write
199 // to ensure we're in a sane state (i.e. internal buffer cleaned)
200 // if the underlying write throws up an exception
201 $this->wBuf_ = '';
202 $this->transport_->write($out);
203 }
204 $this->transport_->flush();
205 }
206 }