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