TSocket.php 8.6 KB

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