Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 1 | <?php |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 |
Mark Slee | 4902c05 | 2007-03-01 00:31:30 +0000 | [diff] [blame] | 10 | * |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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. |
Mark Slee | 4902c05 | 2007-03-01 00:31:30 +0000 | [diff] [blame] | 19 | * |
| 20 | * @package thrift.transport |
Mark Slee | 4902c05 | 2007-03-01 00:31:30 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 23 | |
Mark Slee | 4902c05 | 2007-03-01 00:31:30 +0000 | [diff] [blame] | 24 | /** |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 25 | * Sockets implementation of the TTransport interface. |
| 26 | * |
| 27 | * @package thrift.transport |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 28 | */ |
| 29 | class TSocket extends TTransport { |
| 30 | |
| 31 | /** |
| 32 | * Handle to PHP socket |
| 33 | * |
| 34 | * @var resource |
| 35 | */ |
| 36 | private $handle_ = null; |
| 37 | |
| 38 | /** |
| 39 | * Remote hostname |
Mark Slee | 0cdc6c8 | 2007-11-13 10:19:08 +0000 | [diff] [blame] | 40 | * |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 41 | * @var string |
| 42 | */ |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 43 | protected $host_ = 'localhost'; |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Remote port |
| 47 | * |
| 48 | * @var int |
| 49 | */ |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 50 | protected $port_ = '9090'; |
| 51 | |
| 52 | /** |
| 53 | * Send timeout in milliseconds |
| 54 | * |
| 55 | * @var int |
| 56 | */ |
| 57 | private $sendTimeout_ = 100; |
| 58 | |
| 59 | /** |
| 60 | * Recv timeout in milliseconds |
| 61 | * |
| 62 | * @var int |
| 63 | */ |
| 64 | private $recvTimeout_ = 750; |
| 65 | |
| 66 | /** |
| 67 | * Is send timeout set? |
| 68 | * |
| 69 | * @var bool |
| 70 | */ |
| 71 | private $sendTimeoutSet_ = FALSE; |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Persistent socket or plain? |
| 75 | * |
| 76 | * @var bool |
| 77 | */ |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 78 | private $persist_ = FALSE; |
| 79 | |
| 80 | /** |
| 81 | * Debugging on? |
| 82 | * |
| 83 | * @var bool |
| 84 | */ |
robert | b0fac3e | 2007-01-15 23:53:25 +0000 | [diff] [blame] | 85 | protected $debug_ = FALSE; |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 86 | |
| 87 | /** |
Mark Slee | ad58f95 | 2007-01-03 19:23:50 +0000 | [diff] [blame] | 88 | * Debug handler |
| 89 | * |
| 90 | * @var mixed |
| 91 | */ |
robert | b0fac3e | 2007-01-15 23:53:25 +0000 | [diff] [blame] | 92 | protected $debugHandler_ = null; |
Mark Slee | ad58f95 | 2007-01-03 19:23:50 +0000 | [diff] [blame] | 93 | |
| 94 | /** |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 95 | * Socket constructor |
| 96 | * |
Mark Slee | ad58f95 | 2007-01-03 19:23:50 +0000 | [diff] [blame] | 97 | * @param string $host Remote hostname |
| 98 | * @param int $port Remote port |
| 99 | * @param bool $persist Whether to use a persistent socket |
| 100 | * @param string $debugHandler Function to call for error logging |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 101 | */ |
Mark Slee | ad58f95 | 2007-01-03 19:23:50 +0000 | [diff] [blame] | 102 | public function __construct($host='localhost', |
| 103 | $port=9090, |
| 104 | $persist=FALSE, |
| 105 | $debugHandler=null) { |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 106 | $this->host_ = $host; |
| 107 | $this->port_ = $port; |
| 108 | $this->persist_ = $persist; |
Mark Slee | ad58f95 | 2007-01-03 19:23:50 +0000 | [diff] [blame] | 109 | $this->debugHandler_ = $debugHandler ? $debugHandler : 'error_log'; |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
Bryan Duxbury | 17115d7 | 2010-08-12 16:59:19 +0000 | [diff] [blame^] | 113 | * @param resource $handle |
| 114 | * @return void |
| 115 | */ |
| 116 | public function setHandle($handle) { |
| 117 | $this->handle_ = $handle; |
| 118 | } |
| 119 | |
| 120 | /** |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 121 | * Sets the send timeout. |
| 122 | * |
David Reiss | 5ab303c | 2009-03-26 04:27:47 +0000 | [diff] [blame] | 123 | * @param int $timeout Timeout in milliseconds. |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 124 | */ |
| 125 | public function setSendTimeout($timeout) { |
| 126 | $this->sendTimeout_ = $timeout; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Sets the receive timeout. |
| 131 | * |
David Reiss | 5ab303c | 2009-03-26 04:27:47 +0000 | [diff] [blame] | 132 | * @param int $timeout Timeout in milliseconds. |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 133 | */ |
| 134 | public function setRecvTimeout($timeout) { |
| 135 | $this->recvTimeout_ = $timeout; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Sets debugging output on or off |
| 140 | * |
| 141 | * @param bool $debug |
| 142 | */ |
| 143 | public function setDebug($debug) { |
| 144 | $this->debug_ = $debug; |
| 145 | } |
| 146 | |
| 147 | /** |
Mark Slee | 0cdc6c8 | 2007-11-13 10:19:08 +0000 | [diff] [blame] | 148 | * Get the host that this socket is connected to |
| 149 | * |
| 150 | * @return string host |
| 151 | */ |
| 152 | public function getHost() { |
| 153 | return $this->host_; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Get the remote port that this socket is connected to |
| 158 | * |
| 159 | * @return int port |
| 160 | */ |
| 161 | public function getPort() { |
| 162 | return $this->port_; |
| 163 | } |
| 164 | |
| 165 | /** |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 166 | * Tests whether this is open |
| 167 | * |
| 168 | * @return bool true if the socket is open |
| 169 | */ |
| 170 | public function isOpen() { |
| 171 | return is_resource($this->handle_); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Connects the socket. |
| 176 | */ |
| 177 | public function open() { |
Bryan Duxbury | 17115d7 | 2010-08-12 16:59:19 +0000 | [diff] [blame^] | 178 | if ($this->isOpen()) { |
| 179 | throw new TTransportException('Socket already connected', TTransportException::ALREADY_OPEN); |
| 180 | } |
| 181 | |
| 182 | if (empty($this->host_)) { |
| 183 | throw new TTransportException('Cannot open null host', TTransportException::NOT_OPEN); |
| 184 | } |
| 185 | |
| 186 | if ($this->port_ <= 0) { |
| 187 | throw new TTransportException('Cannot open without port', TTransportException::NOT_OPEN); |
| 188 | } |
Mark Slee | 0cdc6c8 | 2007-11-13 10:19:08 +0000 | [diff] [blame] | 189 | |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 190 | if ($this->persist_) { |
Mark Slee | d7cc1c4 | 2006-10-04 16:49:07 +0000 | [diff] [blame] | 191 | $this->handle_ = @pfsockopen($this->host_, |
| 192 | $this->port_, |
| 193 | $errno, |
| 194 | $errstr, |
| 195 | $this->sendTimeout_/1000.0); |
| 196 | } else { |
| 197 | $this->handle_ = @fsockopen($this->host_, |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 198 | $this->port_, |
| 199 | $errno, |
| 200 | $errstr, |
| 201 | $this->sendTimeout_/1000.0); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 202 | } |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 203 | |
| 204 | // Connect failed? |
| 205 | if ($this->handle_ === FALSE) { |
Martin Kraemer | 0b64e77 | 2007-02-07 22:39:58 +0000 | [diff] [blame] | 206 | $error = 'TSocket: Could not connect to '.$this->host_.':'.$this->port_.' ('.$errstr.' ['.$errno.'])'; |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 207 | if ($this->debug_) { |
Mark Slee | e7714a6 | 2007-01-11 01:26:00 +0000 | [diff] [blame] | 208 | call_user_func($this->debugHandler_, $error); |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 209 | } |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 210 | throw new TException($error); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 211 | } |
Mark Slee | 0cdc6c8 | 2007-11-13 10:19:08 +0000 | [diff] [blame] | 212 | |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 213 | stream_set_timeout($this->handle_, 0, $this->sendTimeout_*1000); |
| 214 | $this->sendTimeoutSet_ = TRUE; |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /** |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 218 | * Closes the socket. |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 219 | */ |
| 220 | public function close() { |
| 221 | if (!$this->persist_) { |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 222 | @fclose($this->handle_); |
| 223 | $this->handle_ = null; |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
Mark Slee | 0cdc6c8 | 2007-11-13 10:19:08 +0000 | [diff] [blame] | 226 | |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 227 | /** |
| 228 | * Uses stream get contents to do the reading |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 229 | * |
| 230 | * @param int $len How many bytes |
| 231 | * @return string Binary data |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 232 | */ |
| 233 | public function readAll($len) { |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 234 | if ($this->sendTimeoutSet_) { |
| 235 | stream_set_timeout($this->handle_, 0, $this->recvTimeout_*1000); |
| 236 | $this->sendTimeoutSet_ = FALSE; |
| 237 | } |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 238 | // This call does not obey stream_set_timeout values! |
| 239 | // $buf = @stream_get_contents($this->handle_, $len); |
| 240 | |
| 241 | $pre = null; |
Mark Slee | 29f5f67 | 2006-09-28 03:19:03 +0000 | [diff] [blame] | 242 | while (TRUE) { |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 243 | $buf = @fread($this->handle_, $len); |
Mark Slee | e598d07 | 2006-11-21 02:01:22 +0000 | [diff] [blame] | 244 | if ($buf === FALSE || $buf === '') { |
Martin Kraemer | 0b64e77 | 2007-02-07 22:39:58 +0000 | [diff] [blame] | 245 | $md = stream_get_meta_data($this->handle_); |
| 246 | if ($md['timed_out']) { |
Bryan Duxbury | 17115d7 | 2010-08-12 16:59:19 +0000 | [diff] [blame^] | 247 | throw new TTransportException('TSocket: timed out reading '.$len.' bytes from '. |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 248 | $this->host_.':'.$this->port_); |
Martin Kraemer | 0b64e77 | 2007-02-07 22:39:58 +0000 | [diff] [blame] | 249 | } else { |
Bryan Duxbury | 17115d7 | 2010-08-12 16:59:19 +0000 | [diff] [blame^] | 250 | throw new TTransportException('TSocket: Could not read '.$len.' bytes from '. |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 251 | $this->host_.':'.$this->port_); |
Martin Kraemer | 0b64e77 | 2007-02-07 22:39:58 +0000 | [diff] [blame] | 252 | } |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 253 | } else if (($sz = strlen($buf)) < $len) { |
| 254 | $md = stream_get_meta_data($this->handle_); |
| 255 | if ($md['timed_out']) { |
Bryan Duxbury | 17115d7 | 2010-08-12 16:59:19 +0000 | [diff] [blame^] | 256 | throw new TTransportException('TSocket: timed out reading '.$len.' bytes from '. |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 257 | $this->host_.':'.$this->port_); |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 258 | } else { |
| 259 | $pre .= $buf; |
| 260 | $len -= $sz; |
| 261 | } |
| 262 | } else { |
| 263 | return $pre.$buf; |
| 264 | } |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 265 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Read from the socket |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 270 | * |
| 271 | * @param int $len How many bytes |
| 272 | * @return string Binary data |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 273 | */ |
| 274 | public function read($len) { |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 275 | if ($this->sendTimeoutSet_) { |
| 276 | stream_set_timeout($this->handle_, 0, $this->recvTimeout_*1000); |
| 277 | $this->sendTimeoutSet_ = FALSE; |
| 278 | } |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 279 | $data = @fread($this->handle_, $len); |
Mark Slee | e598d07 | 2006-11-21 02:01:22 +0000 | [diff] [blame] | 280 | if ($data === FALSE || $data === '') { |
Martin Kraemer | 0b64e77 | 2007-02-07 22:39:58 +0000 | [diff] [blame] | 281 | $md = stream_get_meta_data($this->handle_); |
| 282 | if ($md['timed_out']) { |
Bryan Duxbury | 17115d7 | 2010-08-12 16:59:19 +0000 | [diff] [blame^] | 283 | throw new TTransportException('TSocket: timed out reading '.$len.' bytes from '. |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 284 | $this->host_.':'.$this->port_); |
Martin Kraemer | 0b64e77 | 2007-02-07 22:39:58 +0000 | [diff] [blame] | 285 | } else { |
Bryan Duxbury | 17115d7 | 2010-08-12 16:59:19 +0000 | [diff] [blame^] | 286 | throw new TTransportException('TSocket: Could not read '.$len.' bytes from '. |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 287 | $this->host_.':'.$this->port_); |
Martin Kraemer | 0b64e77 | 2007-02-07 22:39:58 +0000 | [diff] [blame] | 288 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 289 | } |
| 290 | return $data; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Write to the socket. |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 295 | * |
| 296 | * @param string $buf The data to write |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 297 | */ |
| 298 | public function write($buf) { |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 299 | if (!$this->sendTimeoutSet_) { |
| 300 | stream_set_timeout($this->handle_, 0, $this->sendTimeout_*1000); |
| 301 | $this->sendTimeoutSet_ = TRUE; |
| 302 | } |
Mark Slee | d395d57 | 2007-02-27 01:16:55 +0000 | [diff] [blame] | 303 | while (strlen($buf) > 0) { |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 304 | $got = @fwrite($this->handle_, $buf); |
| 305 | if ($got === 0 || $got === FALSE) { |
Martin Kraemer | 0b64e77 | 2007-02-07 22:39:58 +0000 | [diff] [blame] | 306 | $md = stream_get_meta_data($this->handle_); |
| 307 | if ($md['timed_out']) { |
Bryan Duxbury | 17115d7 | 2010-08-12 16:59:19 +0000 | [diff] [blame^] | 308 | throw new TTransportException('TSocket: timed out writing '.strlen($buf).' bytes from '. |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 309 | $this->host_.':'.$this->port_); |
Martin Kraemer | 0b64e77 | 2007-02-07 22:39:58 +0000 | [diff] [blame] | 310 | } else { |
Bryan Duxbury | 17115d7 | 2010-08-12 16:59:19 +0000 | [diff] [blame^] | 311 | throw new TTransportException('TSocket: Could not write '.strlen($buf).' bytes '. |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 312 | $this->host_.':'.$this->port_); |
Martin Kraemer | 0b64e77 | 2007-02-07 22:39:58 +0000 | [diff] [blame] | 313 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 314 | } |
| 315 | $buf = substr($buf, $got); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Flush output to the socket. |
| 321 | */ |
| 322 | public function flush() { |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 323 | $ret = fflush($this->handle_); |
| 324 | if ($ret === FALSE) { |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 325 | throw new TException('TSocket: Could not flush: '. |
| 326 | $this->host_.':'.$this->port_); |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 327 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 328 | } |
| 329 | } |