/thrift/transport/TSocket.php

https://github.com/GunioRobot/phpcassa · PHP · 320 lines · 128 code · 32 blank · 160 comment · 22 complexity · 4ee33f2052f5e2bb380393e6e7f34b4d MD5 · raw file

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