| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 1 | <?php | 
 | 2 |  | 
 | 3 | /** | 
 | 4 |  * Sockets implementation of the TTransport interface. | 
 | 5 |  * | 
 | 6 |  * @package thrift.transport | 
 | 7 |  * @author Mark Slee <mcslee@facebook.com> | 
 | 8 |  */ | 
 | 9 | class TSocket extends TTransport { | 
 | 10 |  | 
 | 11 |   /** | 
 | 12 |    * Handle to PHP socket | 
 | 13 |    * | 
 | 14 |    * @var resource | 
 | 15 |    */ | 
 | 16 |   private $handle_ = null; | 
 | 17 |  | 
 | 18 |   /** | 
 | 19 |    * Remote hostname | 
 | 20 |    *  | 
 | 21 |    * @var string | 
 | 22 |    */ | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 23 |   protected $host_ = 'localhost'; | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 24 |  | 
 | 25 |   /** | 
 | 26 |    * Remote port | 
 | 27 |    * | 
 | 28 |    * @var int | 
 | 29 |    */ | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 30 |   protected $port_ = '9090'; | 
 | 31 |  | 
 | 32 |   /** | 
 | 33 |    * Send timeout in milliseconds | 
 | 34 |    * | 
 | 35 |    * @var int | 
 | 36 |    */ | 
 | 37 |   private $sendTimeout_ = 100; | 
 | 38 |  | 
 | 39 |   /** | 
 | 40 |    * Recv timeout in milliseconds | 
 | 41 |    * | 
 | 42 |    * @var int | 
 | 43 |    */ | 
 | 44 |   private $recvTimeout_ = 750; | 
 | 45 |  | 
 | 46 |   /** | 
 | 47 |    * Is send timeout set? | 
 | 48 |    * | 
 | 49 |    * @var bool | 
 | 50 |    */ | 
 | 51 |   private $sendTimeoutSet_ = FALSE; | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 52 |  | 
 | 53 |   /** | 
 | 54 |    * Persistent socket or plain? | 
 | 55 |    * | 
 | 56 |    * @var bool | 
 | 57 |    */ | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 58 |   private $persist_ = FALSE; | 
 | 59 |  | 
 | 60 |   /** | 
 | 61 |    * Debugging on? | 
 | 62 |    * | 
 | 63 |    * @var bool | 
 | 64 |    */ | 
 | 65 |   private $debug_ = FALSE; | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 66 |  | 
 | 67 |   /** | 
 | 68 |    * Socket constructor | 
 | 69 |    * | 
 | 70 |    * @param string $host    Remote hostname | 
 | 71 |    * @param int    $port    Remote port | 
 | 72 |    * @param bool   $persist Whether to use a persistent socket | 
 | 73 |    */ | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 74 |   public function __construct($host='localhost', $port=9090, $persist=FALSE) { | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 75 |     $this->host_ = $host; | 
 | 76 |     $this->port_ = $port; | 
 | 77 |     $this->persist_ = $persist; | 
 | 78 |   } | 
 | 79 |  | 
 | 80 |   /** | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 81 |    * Sets the send timeout. | 
 | 82 |    * | 
 | 83 |    * @param int $timeout | 
 | 84 |    */ | 
 | 85 |   public function setSendTimeout($timeout) { | 
 | 86 |     $this->sendTimeout_ = $timeout; | 
 | 87 |   } | 
 | 88 |  | 
 | 89 |   /** | 
 | 90 |    * Sets the receive timeout. | 
 | 91 |    * | 
 | 92 |    * @param int $timeout | 
 | 93 |    */ | 
 | 94 |   public function setRecvTimeout($timeout) { | 
 | 95 |     $this->recvTimeout_ = $timeout; | 
 | 96 |   } | 
 | 97 |  | 
 | 98 |   /** | 
 | 99 |    * Sets debugging output on or off | 
 | 100 |    * | 
 | 101 |    * @param bool $debug | 
 | 102 |    */ | 
 | 103 |   public function setDebug($debug) { | 
 | 104 |     $this->debug_ = $debug; | 
 | 105 |   } | 
 | 106 |  | 
 | 107 |   /** | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 108 |    * Tests whether this is open | 
 | 109 |    * | 
 | 110 |    * @return bool true if the socket is open | 
 | 111 |    */ | 
 | 112 |   public function isOpen() { | 
 | 113 |     return is_resource($this->handle_); | 
 | 114 |   } | 
 | 115 |  | 
 | 116 |   /** | 
 | 117 |    * Connects the socket. | 
 | 118 |    */ | 
 | 119 |   public function open() { | 
 | 120 |     if ($this->persist_) { | 
| Mark Slee | d7cc1c4 | 2006-10-04 16:49:07 +0000 | [diff] [blame] | 121 |       $this->handle_ = @pfsockopen($this->host_, | 
 | 122 |                                    $this->port_, | 
 | 123 |                                    $errno, | 
 | 124 |                                    $errstr, | 
 | 125 |                                    $this->sendTimeout_/1000.0); | 
 | 126 |     } else { | 
 | 127 |       $this->handle_ = @fsockopen($this->host_, | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 128 |                                   $this->port_, | 
 | 129 |                                   $errno, | 
 | 130 |                                   $errstr, | 
 | 131 |                                   $this->sendTimeout_/1000.0); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 132 |     } | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 133 |  | 
 | 134 |     // Connect failed? | 
 | 135 |     if ($this->handle_ === FALSE) { | 
 | 136 |       $error = 'TSocket: Could not connect to '.$this->host_.':'.$this->port_; | 
 | 137 |       if ($this->debug_) { | 
 | 138 |         error_log($error); | 
 | 139 |       } | 
 | 140 |       throw new Exception($error); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 141 |     } | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 142 |      | 
 | 143 |     stream_set_timeout($this->handle_, 0, $this->sendTimeout_*1000); | 
 | 144 |     $this->sendTimeoutSet_ = TRUE; | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 145 |   } | 
 | 146 |  | 
 | 147 |   /** | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 148 |    * Closes the socket. | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 149 |    */ | 
 | 150 |   public function close() { | 
 | 151 |     if (!$this->persist_) { | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 152 |       @fclose($this->handle_); | 
 | 153 |       $this->handle_ = null; | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 154 |     } | 
 | 155 |   } | 
 | 156 |    | 
 | 157 |   /** | 
 | 158 |    * Uses stream get contents to do the reading | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 159 |    * | 
 | 160 |    * @param int $len How many bytes | 
 | 161 |    * @return string Binary data | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 162 |    */ | 
 | 163 |   public function readAll($len) { | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 164 |     if ($this->sendTimeoutSet_) { | 
 | 165 |       stream_set_timeout($this->handle_, 0, $this->recvTimeout_*1000); | 
 | 166 |       $this->sendTimeoutSet_ = FALSE; | 
 | 167 |     } | 
| Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 168 |     // This call does not obey stream_set_timeout values! | 
 | 169 |     // $buf = @stream_get_contents($this->handle_, $len); | 
 | 170 |  | 
 | 171 |     $pre = null; | 
| Mark Slee | 29f5f67 | 2006-09-28 03:19:03 +0000 | [diff] [blame] | 172 |     while (TRUE) { | 
| Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 173 |       $buf = @fread($this->handle_, $len); | 
| Mark Slee | 29f5f67 | 2006-09-28 03:19:03 +0000 | [diff] [blame] | 174 |       if (!$buf) { | 
| Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 175 |         throw new Exception('TSocket: Could not read '.$len.' bytes from '. | 
 | 176 |                             $this->host_.':'.$this->port_); | 
 | 177 |       } else if (($sz = strlen($buf)) < $len) { | 
 | 178 |         $md = stream_get_meta_data($this->handle_); | 
 | 179 |         if ($md['timed_out']) { | 
 | 180 |           throw new Exception('TSocket: timed out reading '.$len.' bytes from '. | 
 | 181 |                               $this->host_.':'.$this->port_); | 
 | 182 |         } else { | 
 | 183 |           $pre .= $buf; | 
 | 184 |           $len -= $sz; | 
 | 185 |         } | 
 | 186 |       } else { | 
 | 187 |         return $pre.$buf; | 
 | 188 |       } | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 189 |     } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 190 |   } | 
 | 191 |  | 
 | 192 |   /** | 
 | 193 |    * Read from the socket | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 194 |    * | 
 | 195 |    * @param int $len How many bytes | 
 | 196 |    * @return string Binary data | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 197 |    */ | 
 | 198 |   public function read($len) { | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 199 |     if ($this->sendTimeoutSet_) { | 
 | 200 |       stream_set_timeout($this->handle_, 0, $this->recvTimeout_*1000); | 
 | 201 |       $this->sendTimeoutSet_ = FALSE; | 
 | 202 |     } | 
| Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 203 |     $data = @fread($this->handle_, $len); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 204 |     if ($data === FALSE) { | 
 | 205 |       throw new Exception('TSocket: Could not read '.$len.' bytes from '. | 
 | 206 |                           $this->host_.':'.$this->port_); | 
 | 207 |     } | 
 | 208 |     return $data; | 
 | 209 |   } | 
 | 210 |  | 
 | 211 |   /** | 
 | 212 |    * Write to the socket. | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 213 |    * | 
 | 214 |    * @param string $buf The data to write | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 215 |    */ | 
 | 216 |   public function write($buf) { | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 217 |     if (!$this->sendTimeoutSet_) { | 
 | 218 |       stream_set_timeout($this->handle_, 0, $this->sendTimeout_*1000); | 
 | 219 |       $this->sendTimeoutSet_ = TRUE; | 
 | 220 |     } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 221 |     while (!empty($buf)) { | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 222 |       $got = @fwrite($this->handle_, $buf); | 
 | 223 |       if ($got === 0 || $got === FALSE) { | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 224 |         throw new Exception('TSocket: Could not write '.strlen($buf).' bytes '. | 
 | 225 |                             $this->host_.':'.$this->port_); | 
 | 226 |       } | 
 | 227 |       $buf = substr($buf, $got); | 
 | 228 |     } | 
 | 229 |   } | 
 | 230 |  | 
 | 231 |   /** | 
 | 232 |    * Flush output to the socket. | 
 | 233 |    */ | 
 | 234 |   public function flush() { | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 235 |     $ret = fflush($this->handle_); | 
 | 236 |     if ($ret === FALSE) { | 
 | 237 |       throw new Exception('TSocket: Could not flush: '. | 
 | 238 |                           $this->host_.':'.$this->port_); | 
 | 239 |     } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 240 |   } | 
 | 241 | } | 
 | 242 |  | 
 | 243 | ?> |