| 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 |   /** | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 113 |    * Sets the send timeout. | 
 | 114 |    * | 
| David Reiss | 5ab303c | 2009-03-26 04:27:47 +0000 | [diff] [blame] | 115 |    * @param int $timeout  Timeout in milliseconds. | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 116 |    */ | 
 | 117 |   public function setSendTimeout($timeout) { | 
 | 118 |     $this->sendTimeout_ = $timeout; | 
 | 119 |   } | 
 | 120 |  | 
 | 121 |   /** | 
 | 122 |    * Sets the receive timeout. | 
 | 123 |    * | 
| David Reiss | 5ab303c | 2009-03-26 04:27:47 +0000 | [diff] [blame] | 124 |    * @param int $timeout  Timeout in milliseconds. | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 125 |    */ | 
 | 126 |   public function setRecvTimeout($timeout) { | 
 | 127 |     $this->recvTimeout_ = $timeout; | 
 | 128 |   } | 
 | 129 |  | 
 | 130 |   /** | 
 | 131 |    * Sets debugging output on or off | 
 | 132 |    * | 
 | 133 |    * @param bool $debug | 
 | 134 |    */ | 
 | 135 |   public function setDebug($debug) { | 
 | 136 |     $this->debug_ = $debug; | 
 | 137 |   } | 
 | 138 |  | 
 | 139 |   /** | 
| Mark Slee | 0cdc6c8 | 2007-11-13 10:19:08 +0000 | [diff] [blame] | 140 |    * Get the host that this socket is connected to | 
 | 141 |    * | 
 | 142 |    * @return string host | 
 | 143 |    */ | 
 | 144 |   public function getHost() { | 
 | 145 |     return $this->host_; | 
 | 146 |   } | 
 | 147 |  | 
 | 148 |   /** | 
 | 149 |    * Get the remote port that this socket is connected to | 
 | 150 |    * | 
 | 151 |    * @return int port | 
 | 152 |    */ | 
 | 153 |   public function getPort() { | 
 | 154 |     return $this->port_; | 
 | 155 |   } | 
 | 156 |  | 
 | 157 |   /** | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 158 |    * Tests whether this is open | 
 | 159 |    * | 
 | 160 |    * @return bool true if the socket is open | 
 | 161 |    */ | 
 | 162 |   public function isOpen() { | 
 | 163 |     return is_resource($this->handle_); | 
 | 164 |   } | 
 | 165 |  | 
 | 166 |   /** | 
 | 167 |    * Connects the socket. | 
 | 168 |    */ | 
 | 169 |   public function open() { | 
| Mark Slee | 0cdc6c8 | 2007-11-13 10:19:08 +0000 | [diff] [blame] | 170 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 171 |     if ($this->persist_) { | 
| Mark Slee | d7cc1c4 | 2006-10-04 16:49:07 +0000 | [diff] [blame] | 172 |       $this->handle_ = @pfsockopen($this->host_, | 
 | 173 |                                    $this->port_, | 
 | 174 |                                    $errno, | 
 | 175 |                                    $errstr, | 
 | 176 |                                    $this->sendTimeout_/1000.0); | 
 | 177 |     } else { | 
 | 178 |       $this->handle_ = @fsockopen($this->host_, | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 179 |                                   $this->port_, | 
 | 180 |                                   $errno, | 
 | 181 |                                   $errstr, | 
 | 182 |                                   $this->sendTimeout_/1000.0); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 183 |     } | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 184 |  | 
 | 185 |     // Connect failed? | 
 | 186 |     if ($this->handle_ === FALSE) { | 
| Martin Kraemer | 0b64e77 | 2007-02-07 22:39:58 +0000 | [diff] [blame] | 187 |       $error = 'TSocket: Could not connect to '.$this->host_.':'.$this->port_.' ('.$errstr.' ['.$errno.'])'; | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 188 |       if ($this->debug_) { | 
| Mark Slee | e7714a6 | 2007-01-11 01:26:00 +0000 | [diff] [blame] | 189 |         call_user_func($this->debugHandler_, $error); | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 190 |       } | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 191 |       throw new TException($error); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 192 |     } | 
| Mark Slee | 0cdc6c8 | 2007-11-13 10:19:08 +0000 | [diff] [blame] | 193 |  | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 194 |     stream_set_timeout($this->handle_, 0, $this->sendTimeout_*1000); | 
 | 195 |     $this->sendTimeoutSet_ = TRUE; | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 196 |   } | 
 | 197 |  | 
 | 198 |   /** | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 199 |    * Closes the socket. | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 200 |    */ | 
 | 201 |   public function close() { | 
 | 202 |     if (!$this->persist_) { | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 203 |       @fclose($this->handle_); | 
 | 204 |       $this->handle_ = null; | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 205 |     } | 
 | 206 |   } | 
| Mark Slee | 0cdc6c8 | 2007-11-13 10:19:08 +0000 | [diff] [blame] | 207 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 208 |   /** | 
 | 209 |    * Uses stream get contents to do the reading | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 210 |    * | 
 | 211 |    * @param int $len How many bytes | 
 | 212 |    * @return string Binary data | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 213 |    */ | 
 | 214 |   public function readAll($len) { | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 215 |     if ($this->sendTimeoutSet_) { | 
 | 216 |       stream_set_timeout($this->handle_, 0, $this->recvTimeout_*1000); | 
 | 217 |       $this->sendTimeoutSet_ = FALSE; | 
 | 218 |     } | 
| Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 219 |     // This call does not obey stream_set_timeout values! | 
 | 220 |     // $buf = @stream_get_contents($this->handle_, $len); | 
 | 221 |  | 
 | 222 |     $pre = null; | 
| Mark Slee | 29f5f67 | 2006-09-28 03:19:03 +0000 | [diff] [blame] | 223 |     while (TRUE) { | 
| Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 224 |       $buf = @fread($this->handle_, $len); | 
| Mark Slee | e598d07 | 2006-11-21 02:01:22 +0000 | [diff] [blame] | 225 |       if ($buf === FALSE || $buf === '') { | 
| Martin Kraemer | 0b64e77 | 2007-02-07 22:39:58 +0000 | [diff] [blame] | 226 |         $md = stream_get_meta_data($this->handle_); | 
 | 227 |         if ($md['timed_out']) { | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 228 |           throw new TException('TSocket: timed out reading '.$len.' bytes from '. | 
 | 229 |                                $this->host_.':'.$this->port_); | 
| Martin Kraemer | 0b64e77 | 2007-02-07 22:39:58 +0000 | [diff] [blame] | 230 |         } else { | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 231 |           throw new TException('TSocket: Could not read '.$len.' bytes from '. | 
 | 232 |                                $this->host_.':'.$this->port_); | 
| Martin Kraemer | 0b64e77 | 2007-02-07 22:39:58 +0000 | [diff] [blame] | 233 |         } | 
| Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 234 |       } else if (($sz = strlen($buf)) < $len) { | 
 | 235 |         $md = stream_get_meta_data($this->handle_); | 
 | 236 |         if ($md['timed_out']) { | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 237 |           throw new TException('TSocket: timed out reading '.$len.' bytes from '. | 
 | 238 |                                $this->host_.':'.$this->port_); | 
| Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 239 |         } else { | 
 | 240 |           $pre .= $buf; | 
 | 241 |           $len -= $sz; | 
 | 242 |         } | 
 | 243 |       } else { | 
 | 244 |         return $pre.$buf; | 
 | 245 |       } | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 246 |     } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 247 |   } | 
 | 248 |  | 
 | 249 |   /** | 
 | 250 |    * Read from the socket | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 251 |    * | 
 | 252 |    * @param int $len How many bytes | 
 | 253 |    * @return string Binary data | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 254 |    */ | 
 | 255 |   public function read($len) { | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 256 |     if ($this->sendTimeoutSet_) { | 
 | 257 |       stream_set_timeout($this->handle_, 0, $this->recvTimeout_*1000); | 
 | 258 |       $this->sendTimeoutSet_ = FALSE; | 
 | 259 |     } | 
| Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 260 |     $data = @fread($this->handle_, $len); | 
| Mark Slee | e598d07 | 2006-11-21 02:01:22 +0000 | [diff] [blame] | 261 |     if ($data === FALSE || $data === '') { | 
| Martin Kraemer | 0b64e77 | 2007-02-07 22:39:58 +0000 | [diff] [blame] | 262 |       $md = stream_get_meta_data($this->handle_); | 
 | 263 |       if ($md['timed_out']) { | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 264 |         throw new TException('TSocket: timed out reading '.$len.' bytes from '. | 
 | 265 |                              $this->host_.':'.$this->port_); | 
| Martin Kraemer | 0b64e77 | 2007-02-07 22:39:58 +0000 | [diff] [blame] | 266 |       } else { | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 267 |         throw new TException('TSocket: Could not read '.$len.' bytes from '. | 
 | 268 |                              $this->host_.':'.$this->port_); | 
| Martin Kraemer | 0b64e77 | 2007-02-07 22:39:58 +0000 | [diff] [blame] | 269 |       } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 270 |     } | 
 | 271 |     return $data; | 
 | 272 |   } | 
 | 273 |  | 
 | 274 |   /** | 
 | 275 |    * Write to the socket. | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 276 |    * | 
 | 277 |    * @param string $buf The data to write | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 278 |    */ | 
 | 279 |   public function write($buf) { | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 280 |     if (!$this->sendTimeoutSet_) { | 
 | 281 |       stream_set_timeout($this->handle_, 0, $this->sendTimeout_*1000); | 
 | 282 |       $this->sendTimeoutSet_ = TRUE; | 
 | 283 |     } | 
| Mark Slee | d395d57 | 2007-02-27 01:16:55 +0000 | [diff] [blame] | 284 |     while (strlen($buf) > 0) { | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 285 |       $got = @fwrite($this->handle_, $buf); | 
 | 286 |       if ($got === 0 || $got === FALSE) { | 
| Martin Kraemer | 0b64e77 | 2007-02-07 22:39:58 +0000 | [diff] [blame] | 287 |         $md = stream_get_meta_data($this->handle_); | 
 | 288 |         if ($md['timed_out']) { | 
| David Reiss | 1931b12 | 2008-07-17 19:36:34 +0000 | [diff] [blame] | 289 |           throw new TException('TSocket: timed out writing '.strlen($buf).' bytes from '. | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 290 |                                $this->host_.':'.$this->port_); | 
| Martin Kraemer | 0b64e77 | 2007-02-07 22:39:58 +0000 | [diff] [blame] | 291 |         } else { | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 292 |             throw new TException('TSocket: Could not write '.strlen($buf).' bytes '. | 
 | 293 |                                  $this->host_.':'.$this->port_); | 
| Martin Kraemer | 0b64e77 | 2007-02-07 22:39:58 +0000 | [diff] [blame] | 294 |         } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 295 |       } | 
 | 296 |       $buf = substr($buf, $got); | 
 | 297 |     } | 
 | 298 |   } | 
 | 299 |  | 
 | 300 |   /** | 
 | 301 |    * Flush output to the socket. | 
 | 302 |    */ | 
 | 303 |   public function flush() { | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 304 |     $ret = fflush($this->handle_); | 
 | 305 |     if ($ret === FALSE) { | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 306 |       throw new TException('TSocket: Could not flush: '. | 
 | 307 |                            $this->host_.':'.$this->port_); | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 308 |     } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 309 |   } | 
 | 310 | } | 
 | 311 |  | 
 | 312 | ?> |