StreamConnection.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Connection;
  11. use Predis\Command\CommandInterface;
  12. use Predis\Response;
  13. /**
  14. * Standard connection to Redis servers implemented on top of PHP's streams.
  15. * The connection parameters supported by this class are:
  16. *
  17. * - scheme: it can be either 'tcp' or 'unix'.
  18. * - host: hostname or IP address of the server.
  19. * - port: TCP port of the server.
  20. * - timeout: timeout to perform the connection.
  21. * - read_write_timeout: timeout of read / write operations.
  22. * - async_connect: performs the connection asynchronously.
  23. * - tcp_nodelay: enables or disables Nagle's algorithm for coalescing.
  24. * - persistent: the connection is left intact after a GC collection.
  25. *
  26. * @author Daniele Alessandri <suppakilla@gmail.com>
  27. */
  28. class StreamConnection extends AbstractConnection
  29. {
  30. /**
  31. * Disconnects from the server and destroys the underlying resource when
  32. * PHP's garbage collector kicks in only if the connection has not been
  33. * marked as persistent.
  34. */
  35. public function __destruct()
  36. {
  37. if (isset($this->parameters) && !$this->parameters->persistent) {
  38. $this->disconnect();
  39. }
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. protected function createResource()
  45. {
  46. $parameters = $this->parameters;
  47. $initializer = "{$parameters->scheme}StreamInitializer";
  48. return $this->$initializer($parameters);
  49. }
  50. /**
  51. * Initializes a TCP stream resource.
  52. *
  53. * @param ConnectionParametersInterface $parameters Parameters used to initialize the connection.
  54. * @return resource
  55. */
  56. private function tcpStreamInitializer(ConnectionParametersInterface $parameters)
  57. {
  58. $uri = "tcp://{$parameters->host}:{$parameters->port}/";
  59. $flags = STREAM_CLIENT_CONNECT;
  60. if (isset($parameters->async_connect) && $parameters->async_connect) {
  61. $flags |= STREAM_CLIENT_ASYNC_CONNECT;
  62. }
  63. if (isset($parameters->persistent) && $parameters->persistent) {
  64. $flags |= STREAM_CLIENT_PERSISTENT;
  65. }
  66. $resource = @stream_socket_client($uri, $errno, $errstr, $parameters->timeout, $flags);
  67. if (!$resource) {
  68. $this->onConnectionError(trim($errstr), $errno);
  69. }
  70. if (isset($parameters->read_write_timeout)) {
  71. $rwtimeout = $parameters->read_write_timeout;
  72. $rwtimeout = $rwtimeout > 0 ? $rwtimeout : -1;
  73. $timeoutSeconds = floor($rwtimeout);
  74. $timeoutUSeconds = ($rwtimeout - $timeoutSeconds) * 1000000;
  75. stream_set_timeout($resource, $timeoutSeconds, $timeoutUSeconds);
  76. }
  77. if (isset($parameters->tcp_nodelay) && version_compare(PHP_VERSION, '5.4.0') >= 0) {
  78. $socket = socket_import_stream($resource);
  79. socket_set_option($socket, SOL_TCP, TCP_NODELAY, (int) $parameters->tcp_nodelay);
  80. }
  81. return $resource;
  82. }
  83. /**
  84. * Initializes a UNIX stream resource.
  85. *
  86. * @param ConnectionParametersInterface $parameters Parameters used to initialize the connection.
  87. * @return resource
  88. */
  89. private function unixStreamInitializer(ConnectionParametersInterface $parameters)
  90. {
  91. $uri = "unix://{$parameters->path}";
  92. $flags = STREAM_CLIENT_CONNECT;
  93. if ($parameters->persistent) {
  94. $flags |= STREAM_CLIENT_PERSISTENT;
  95. }
  96. $resource = @stream_socket_client($uri, $errno, $errstr, $parameters->timeout, $flags);
  97. if (!$resource) {
  98. $this->onConnectionError(trim($errstr), $errno);
  99. }
  100. return $resource;
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function connect()
  106. {
  107. parent::connect();
  108. if ($this->initCmds) {
  109. foreach ($this->initCmds as $command) {
  110. $this->executeCommand($command);
  111. }
  112. }
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function disconnect()
  118. {
  119. if ($this->isConnected()) {
  120. fclose($this->getResource());
  121. parent::disconnect();
  122. }
  123. }
  124. /**
  125. * Performs a write operation on the stream of the buffer containing a
  126. * command serialized with the Redis wire protocol.
  127. *
  128. * @param string $buffer Redis wire protocol representation of a command.
  129. */
  130. protected function writeBytes($buffer)
  131. {
  132. $socket = $this->getResource();
  133. while (($length = strlen($buffer)) > 0) {
  134. $written = fwrite($socket, $buffer);
  135. if ($length === $written) {
  136. return;
  137. }
  138. if ($written === false || $written === 0) {
  139. $this->onConnectionError('Error while writing bytes to the server');
  140. }
  141. $buffer = substr($buffer, $written);
  142. }
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function read()
  148. {
  149. $socket = $this->getResource();
  150. $chunk = fgets($socket);
  151. if ($chunk === false || $chunk === '') {
  152. $this->onConnectionError('Error while reading line from the server');
  153. }
  154. $prefix = $chunk[0];
  155. $payload = substr($chunk, 1, -2);
  156. switch ($prefix) {
  157. case '+': // inline
  158. switch ($payload) {
  159. case 'OK':
  160. return true;
  161. case 'QUEUED':
  162. return new Response\StatusQueued();
  163. default:
  164. return $payload;
  165. }
  166. case '$': // bulk
  167. $size = (int) $payload;
  168. if ($size === -1) {
  169. return null;
  170. }
  171. $bulkData = '';
  172. $bytesLeft = ($size += 2);
  173. do {
  174. $chunk = fread($socket, min($bytesLeft, 4096));
  175. if ($chunk === false || $chunk === '') {
  176. $this->onConnectionError('Error while reading bytes from the server');
  177. }
  178. $bulkData .= $chunk;
  179. $bytesLeft = $size - strlen($bulkData);
  180. } while ($bytesLeft > 0);
  181. return substr($bulkData, 0, -2);
  182. case '*': // multi bulk
  183. $count = (int) $payload;
  184. if ($count === -1) {
  185. return null;
  186. }
  187. $multibulk = array();
  188. for ($i = 0; $i < $count; $i++) {
  189. $multibulk[$i] = $this->read();
  190. }
  191. return $multibulk;
  192. case ':': // integer
  193. return (int) $payload;
  194. case '-': // error
  195. return new Response\Error($payload);
  196. default:
  197. $this->onProtocolError("Unknown prefix: '$prefix'");
  198. }
  199. }
  200. /**
  201. * {@inheritdoc}
  202. */
  203. public function writeCommand(CommandInterface $command)
  204. {
  205. $commandId = $command->getId();
  206. $arguments = $command->getArguments();
  207. $cmdlen = strlen($commandId);
  208. $reqlen = count($arguments) + 1;
  209. $buffer = "*{$reqlen}\r\n\${$cmdlen}\r\n{$commandId}\r\n";
  210. for ($i = 0; $i < $reqlen - 1; $i++) {
  211. $argument = $arguments[$i];
  212. $arglen = strlen($argument);
  213. $buffer .= "\${$arglen}\r\n{$argument}\r\n";
  214. }
  215. $this->writeBytes($buffer);
  216. }
  217. }