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 | /** |
Bryan Duxbury | c46f32c | 2011-03-04 01:25:17 +0000 | [diff] [blame] | 53 | * Send timeout in seconds. |
| 54 | * |
| 55 | * Combined with sendTimeoutUsec this is used for send timeouts. |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 56 | * |
| 57 | * @var int |
| 58 | */ |
Bryan Duxbury | c46f32c | 2011-03-04 01:25:17 +0000 | [diff] [blame] | 59 | private $sendTimeoutSec_ = 0; |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 60 | |
| 61 | /** |
Bryan Duxbury | c46f32c | 2011-03-04 01:25:17 +0000 | [diff] [blame] | 62 | * Send timeout in microseconds. |
| 63 | * |
| 64 | * Combined with sendTimeoutSec this is used for send timeouts. |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 65 | * |
| 66 | * @var int |
| 67 | */ |
Bryan Duxbury | c46f32c | 2011-03-04 01:25:17 +0000 | [diff] [blame] | 68 | private $sendTimeoutUsec_ = 100000; |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 69 | |
| 70 | /** |
Bryan Duxbury | c46f32c | 2011-03-04 01:25:17 +0000 | [diff] [blame] | 71 | * Recv timeout in seconds |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 72 | * |
Bryan Duxbury | c46f32c | 2011-03-04 01:25:17 +0000 | [diff] [blame] | 73 | * Combined with recvTimeoutUsec this is used for recv timeouts. |
| 74 | * |
| 75 | * @var int |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 76 | */ |
Bryan Duxbury | c46f32c | 2011-03-04 01:25:17 +0000 | [diff] [blame] | 77 | private $recvTimeoutSec_ = 0; |
| 78 | |
| 79 | /** |
| 80 | * Recv timeout in microseconds |
| 81 | * |
| 82 | * Combined with recvTimeoutSec this is used for recv timeouts. |
| 83 | * |
| 84 | * @var int |
| 85 | */ |
| 86 | private $recvTimeoutUsec_ = 750000; |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * Persistent socket or plain? |
| 90 | * |
| 91 | * @var bool |
| 92 | */ |
Jake Farrell | 10ed8e8 | 2011-04-13 19:42:35 +0000 | [diff] [blame] | 93 | protected $persist_ = FALSE; |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * Debugging on? |
| 97 | * |
| 98 | * @var bool |
| 99 | */ |
robert | b0fac3e | 2007-01-15 23:53:25 +0000 | [diff] [blame] | 100 | protected $debug_ = FALSE; |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 101 | |
| 102 | /** |
Mark Slee | ad58f95 | 2007-01-03 19:23:50 +0000 | [diff] [blame] | 103 | * Debug handler |
| 104 | * |
| 105 | * @var mixed |
| 106 | */ |
robert | b0fac3e | 2007-01-15 23:53:25 +0000 | [diff] [blame] | 107 | protected $debugHandler_ = null; |
Mark Slee | ad58f95 | 2007-01-03 19:23:50 +0000 | [diff] [blame] | 108 | |
| 109 | /** |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 110 | * Socket constructor |
| 111 | * |
Mark Slee | ad58f95 | 2007-01-03 19:23:50 +0000 | [diff] [blame] | 112 | * @param string $host Remote hostname |
| 113 | * @param int $port Remote port |
| 114 | * @param bool $persist Whether to use a persistent socket |
| 115 | * @param string $debugHandler Function to call for error logging |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 116 | */ |
Mark Slee | ad58f95 | 2007-01-03 19:23:50 +0000 | [diff] [blame] | 117 | public function __construct($host='localhost', |
| 118 | $port=9090, |
| 119 | $persist=FALSE, |
| 120 | $debugHandler=null) { |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 121 | $this->host_ = $host; |
| 122 | $this->port_ = $port; |
| 123 | $this->persist_ = $persist; |
Mark Slee | ad58f95 | 2007-01-03 19:23:50 +0000 | [diff] [blame] | 124 | $this->debugHandler_ = $debugHandler ? $debugHandler : 'error_log'; |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
Bryan Duxbury | 17115d7 | 2010-08-12 16:59:19 +0000 | [diff] [blame] | 128 | * @param resource $handle |
| 129 | * @return void |
| 130 | */ |
| 131 | public function setHandle($handle) { |
| 132 | $this->handle_ = $handle; |
| 133 | } |
| 134 | |
| 135 | /** |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 136 | * Sets the send timeout. |
| 137 | * |
David Reiss | 5ab303c | 2009-03-26 04:27:47 +0000 | [diff] [blame] | 138 | * @param int $timeout Timeout in milliseconds. |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 139 | */ |
| 140 | public function setSendTimeout($timeout) { |
Bryan Duxbury | c46f32c | 2011-03-04 01:25:17 +0000 | [diff] [blame] | 141 | $this->sendTimeoutSec_ = floor($timeout / 1000); |
| 142 | $this->sendTimeoutUsec_ = |
| 143 | ($timeout - ($this->sendTimeoutSec_ * 1000)) * 1000; |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Sets the receive timeout. |
| 148 | * |
David Reiss | 5ab303c | 2009-03-26 04:27:47 +0000 | [diff] [blame] | 149 | * @param int $timeout Timeout in milliseconds. |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 150 | */ |
| 151 | public function setRecvTimeout($timeout) { |
Bryan Duxbury | c46f32c | 2011-03-04 01:25:17 +0000 | [diff] [blame] | 152 | $this->recvTimeoutSec_ = floor($timeout / 1000); |
| 153 | $this->recvTimeoutUsec_ = |
| 154 | ($timeout - ($this->recvTimeoutSec_ * 1000)) * 1000; |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Sets debugging output on or off |
| 159 | * |
| 160 | * @param bool $debug |
| 161 | */ |
| 162 | public function setDebug($debug) { |
| 163 | $this->debug_ = $debug; |
| 164 | } |
| 165 | |
| 166 | /** |
Mark Slee | 0cdc6c8 | 2007-11-13 10:19:08 +0000 | [diff] [blame] | 167 | * Get the host that this socket is connected to |
| 168 | * |
| 169 | * @return string host |
| 170 | */ |
| 171 | public function getHost() { |
| 172 | return $this->host_; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Get the remote port that this socket is connected to |
| 177 | * |
| 178 | * @return int port |
| 179 | */ |
| 180 | public function getPort() { |
| 181 | return $this->port_; |
| 182 | } |
| 183 | |
| 184 | /** |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 185 | * Tests whether this is open |
| 186 | * |
| 187 | * @return bool true if the socket is open |
| 188 | */ |
| 189 | public function isOpen() { |
| 190 | return is_resource($this->handle_); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Connects the socket. |
| 195 | */ |
| 196 | public function open() { |
Bryan Duxbury | 17115d7 | 2010-08-12 16:59:19 +0000 | [diff] [blame] | 197 | if ($this->isOpen()) { |
| 198 | throw new TTransportException('Socket already connected', TTransportException::ALREADY_OPEN); |
| 199 | } |
| 200 | |
| 201 | if (empty($this->host_)) { |
| 202 | throw new TTransportException('Cannot open null host', TTransportException::NOT_OPEN); |
| 203 | } |
| 204 | |
| 205 | if ($this->port_ <= 0) { |
| 206 | throw new TTransportException('Cannot open without port', TTransportException::NOT_OPEN); |
| 207 | } |
Mark Slee | 0cdc6c8 | 2007-11-13 10:19:08 +0000 | [diff] [blame] | 208 | |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 209 | if ($this->persist_) { |
Mark Slee | d7cc1c4 | 2006-10-04 16:49:07 +0000 | [diff] [blame] | 210 | $this->handle_ = @pfsockopen($this->host_, |
| 211 | $this->port_, |
| 212 | $errno, |
| 213 | $errstr, |
Bryan Duxbury | c46f32c | 2011-03-04 01:25:17 +0000 | [diff] [blame] | 214 | $this->sendTimeoutSec_ + ($this->sendTimeoutUsec_ / 1000000)); |
Mark Slee | d7cc1c4 | 2006-10-04 16:49:07 +0000 | [diff] [blame] | 215 | } else { |
| 216 | $this->handle_ = @fsockopen($this->host_, |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 217 | $this->port_, |
| 218 | $errno, |
| 219 | $errstr, |
Bryan Duxbury | c46f32c | 2011-03-04 01:25:17 +0000 | [diff] [blame] | 220 | $this->sendTimeoutSec_ + ($this->sendTimeoutUsec_ / 1000000)); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 221 | } |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 222 | |
| 223 | // Connect failed? |
| 224 | if ($this->handle_ === FALSE) { |
Martin Kraemer | 0b64e77 | 2007-02-07 22:39:58 +0000 | [diff] [blame] | 225 | $error = 'TSocket: Could not connect to '.$this->host_.':'.$this->port_.' ('.$errstr.' ['.$errno.'])'; |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 226 | if ($this->debug_) { |
Mark Slee | e7714a6 | 2007-01-11 01:26:00 +0000 | [diff] [blame] | 227 | call_user_func($this->debugHandler_, $error); |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 228 | } |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 229 | throw new TException($error); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
| 233 | /** |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 234 | * Closes the socket. |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 235 | */ |
| 236 | public function close() { |
| 237 | if (!$this->persist_) { |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 238 | @fclose($this->handle_); |
| 239 | $this->handle_ = null; |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 240 | } |
| 241 | } |
Mark Slee | 0cdc6c8 | 2007-11-13 10:19:08 +0000 | [diff] [blame] | 242 | |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 243 | /** |
Bryan Duxbury | c46f32c | 2011-03-04 01:25:17 +0000 | [diff] [blame] | 244 | * Read from the socket at most $len bytes. |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 245 | * |
Bryan Duxbury | c46f32c | 2011-03-04 01:25:17 +0000 | [diff] [blame] | 246 | * This method will not wait for all the requested data, it will return as |
| 247 | * soon as any data is received. |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 248 | * |
Bryan Duxbury | c46f32c | 2011-03-04 01:25:17 +0000 | [diff] [blame] | 249 | * @param int $len Maximum number of bytes to read. |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 250 | * @return string Binary data |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 251 | */ |
| 252 | public function read($len) { |
Bryan Duxbury | c46f32c | 2011-03-04 01:25:17 +0000 | [diff] [blame] | 253 | $null = null; |
| 254 | $read = array($this->handle_); |
| 255 | $readable = @stream_select($read, $null, $null, $this->recvTimeoutSec_, $this->recvTimeoutUsec_); |
| 256 | |
| 257 | if ($readable > 0) { |
| 258 | $data = @stream_socket_recvfrom($this->handle_, $len); |
| 259 | if ($data === false) { |
| 260 | throw new TTransportException('TSocket: Could not read '.$len.' bytes from '. |
| 261 | $this->host_.':'.$this->port_); |
| 262 | } elseif($data == '' && feof($this->handle_)) { |
| 263 | throw new TTransportException('TSocket read 0 bytes'); |
| 264 | } |
| 265 | |
| 266 | return $data; |
| 267 | } else if ($readable === 0) { |
Bryan Duxbury | 17115d7 | 2010-08-12 16:59:19 +0000 | [diff] [blame] | 268 | throw new TTransportException('TSocket: timed out reading '.$len.' bytes from '. |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 269 | $this->host_.':'.$this->port_); |
Martin Kraemer | 0b64e77 | 2007-02-07 22:39:58 +0000 | [diff] [blame] | 270 | } else { |
Bryan Duxbury | 17115d7 | 2010-08-12 16:59:19 +0000 | [diff] [blame] | 271 | throw new TTransportException('TSocket: Could not read '.$len.' bytes from '. |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 272 | $this->host_.':'.$this->port_); |
Martin Kraemer | 0b64e77 | 2007-02-07 22:39:58 +0000 | [diff] [blame] | 273 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 274 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 275 | |
| 276 | /** |
| 277 | * Write to the socket. |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 278 | * |
| 279 | * @param string $buf The data to write |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 280 | */ |
| 281 | public function write($buf) { |
Bryan Duxbury | c46f32c | 2011-03-04 01:25:17 +0000 | [diff] [blame] | 282 | $null = null; |
| 283 | $write = array($this->handle_); |
| 284 | |
| 285 | // keep writing until all the data has been written |
Jake Farrell | b03a59c | 2011-11-29 16:45:51 +0000 | [diff] [blame] | 286 | while (TStringFuncFactory::create()->strlen($buf) > 0) { |
Bryan Duxbury | c46f32c | 2011-03-04 01:25:17 +0000 | [diff] [blame] | 287 | // wait for stream to become available for writing |
| 288 | $writable = @stream_select($null, $write, $null, $this->sendTimeoutSec_, $this->sendTimeoutUsec_); |
| 289 | if ($writable > 0) { |
| 290 | // write buffer to stream |
| 291 | $written = @stream_socket_sendto($this->handle_, $buf); |
| 292 | if ($written === -1 || $written === false) { |
Jake Farrell | b03a59c | 2011-11-29 16:45:51 +0000 | [diff] [blame] | 293 | throw new TTransportException('TSocket: Could not write '.TStringFuncFactory::create()->strlen($buf).' bytes '. |
Bryan Duxbury | c46f32c | 2011-03-04 01:25:17 +0000 | [diff] [blame] | 294 | $this->host_.':'.$this->port_); |
| 295 | } |
| 296 | // determine how much of the buffer is left to write |
Jake Farrell | b03a59c | 2011-11-29 16:45:51 +0000 | [diff] [blame] | 297 | $buf = TStringFuncFactory::create()->substr($buf, $written); |
Bryan Duxbury | c46f32c | 2011-03-04 01:25:17 +0000 | [diff] [blame] | 298 | } else if ($writable === 0) { |
Jake Farrell | b03a59c | 2011-11-29 16:45:51 +0000 | [diff] [blame] | 299 | throw new TTransportException('TSocket: timed out writing '.TStringFuncFactory::create()->strlen($buf).' bytes from '. |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 300 | $this->host_.':'.$this->port_); |
Martin Kraemer | 0b64e77 | 2007-02-07 22:39:58 +0000 | [diff] [blame] | 301 | } else { |
Jake Farrell | b03a59c | 2011-11-29 16:45:51 +0000 | [diff] [blame] | 302 | throw new TTransportException('TSocket: Could not write '.TStringFuncFactory::create()->strlen($buf).' bytes '. |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 303 | $this->host_.':'.$this->port_); |
Martin Kraemer | 0b64e77 | 2007-02-07 22:39:58 +0000 | [diff] [blame] | 304 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 305 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 306 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 307 | |
| 308 | /** |
| 309 | * Flush output to the socket. |
Bryan Duxbury | c46f32c | 2011-03-04 01:25:17 +0000 | [diff] [blame] | 310 | * |
| 311 | * Since read(), readAll() and write() operate on the sockets directly, |
| 312 | * this is a no-op |
| 313 | * |
| 314 | * If you wish to have flushable buffering behaviour, wrap this TSocket |
| 315 | * in a TBufferedTransport. |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 316 | */ |
| 317 | public function flush() { |
Bryan Duxbury | c46f32c | 2011-03-04 01:25:17 +0000 | [diff] [blame] | 318 | // no-op |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 319 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 320 | } |