StreamConnection.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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\Network;
  11. use Predis\ResponseError;
  12. use Predis\ResponseQueued;
  13. use Predis\ServerException;
  14. use Predis\IConnectionParameters;
  15. use Predis\Commands\ICommand;
  16. use Predis\Iterators\MultiBulkResponseSimple;
  17. class StreamConnection extends ConnectionBase
  18. {
  19. private $_mbiterable;
  20. private $_throwErrors;
  21. public function __destruct()
  22. {
  23. if (!$this->_params->connection_persistent) {
  24. $this->disconnect();
  25. }
  26. }
  27. protected function initializeProtocol(IConnectionParameters $parameters)
  28. {
  29. $this->_throwErrors = $parameters->throw_errors;
  30. $this->_mbiterable = $parameters->iterable_multibulk;
  31. }
  32. protected function createResource()
  33. {
  34. $parameters = $this->_params;
  35. $initializer = "{$parameters->scheme}StreamInitializer";
  36. return $this->$initializer($parameters);
  37. }
  38. private function tcpStreamInitializer(IConnectionParameters $parameters)
  39. {
  40. $uri = "tcp://{$parameters->host}:{$parameters->port}/";
  41. $flags = STREAM_CLIENT_CONNECT;
  42. if ($parameters->connection_async) {
  43. $flags |= STREAM_CLIENT_ASYNC_CONNECT;
  44. }
  45. if ($parameters->connection_persistent) {
  46. $flags |= STREAM_CLIENT_PERSISTENT;
  47. }
  48. $resource = @stream_socket_client(
  49. $uri, $errno, $errstr, $parameters->connection_timeout, $flags
  50. );
  51. if (!$resource) {
  52. $this->onConnectionError(trim($errstr), $errno);
  53. }
  54. if (isset($parameters->read_write_timeout)) {
  55. $rwtimeout = $parameters->read_write_timeout;
  56. $rwtimeout = $rwtimeout > 0 ? $rwtimeout : -1;
  57. $timeoutSeconds = floor($rwtimeout);
  58. $timeoutUSeconds = ($rwtimeout - $timeoutSeconds) * 1000000;
  59. stream_set_timeout($resource, $timeoutSeconds, $timeoutUSeconds);
  60. }
  61. return $resource;
  62. }
  63. private function unixStreamInitializer(IConnectionParameters $parameters)
  64. {
  65. $uri = "unix://{$parameters->path}";
  66. $flags = STREAM_CLIENT_CONNECT;
  67. if ($parameters->connection_persistent) {
  68. $flags |= STREAM_CLIENT_PERSISTENT;
  69. }
  70. $resource = @stream_socket_client(
  71. $uri, $errno, $errstr, $parameters->connection_timeout, $flags
  72. );
  73. if (!$resource) {
  74. $this->onConnectionError(trim($errstr), $errno);
  75. }
  76. return $resource;
  77. }
  78. public function connect()
  79. {
  80. parent::connect();
  81. if (count($this->_initCmds) > 0){
  82. $this->sendInitializationCommands();
  83. }
  84. }
  85. public function disconnect()
  86. {
  87. if ($this->isConnected()) {
  88. fclose($this->getResource());
  89. parent::disconnect();
  90. }
  91. }
  92. private function sendInitializationCommands()
  93. {
  94. foreach ($this->_initCmds as $command) {
  95. $this->writeCommand($command);
  96. }
  97. foreach ($this->_initCmds as $command) {
  98. $this->readResponse($command);
  99. }
  100. }
  101. protected function writeBytes($buffer)
  102. {
  103. $socket = $this->getResource();
  104. while (($length = strlen($buffer)) > 0) {
  105. $written = fwrite($socket, $buffer);
  106. if ($length === $written) {
  107. return;
  108. }
  109. if ($written === false || $written === 0) {
  110. $this->onConnectionError('Error while writing bytes to the server');
  111. }
  112. $buffer = substr($buffer, $written);
  113. }
  114. }
  115. public function read() {
  116. $socket = $this->getResource();
  117. $chunk = fgets($socket);
  118. if ($chunk === false || $chunk === '') {
  119. $this->onConnectionError('Error while reading line from the server');
  120. }
  121. $prefix = $chunk[0];
  122. $payload = substr($chunk, 1, -2);
  123. switch ($prefix) {
  124. case '+': // inline
  125. switch ($payload) {
  126. case 'OK':
  127. return true;
  128. case 'QUEUED':
  129. return new ResponseQueued();
  130. default:
  131. return $payload;
  132. }
  133. case '$': // bulk
  134. $size = (int) $payload;
  135. if ($size === -1) {
  136. return null;
  137. }
  138. $bulkData = '';
  139. $bytesLeft = ($size += 2);
  140. do {
  141. $chunk = fread($socket, min($bytesLeft, 4096));
  142. if ($chunk === false || $chunk === '') {
  143. $this->onConnectionError(
  144. 'Error while reading bytes from the server'
  145. );
  146. }
  147. $bulkData .= $chunk;
  148. $bytesLeft = $size - strlen($bulkData);
  149. } while ($bytesLeft > 0);
  150. return substr($bulkData, 0, -2);
  151. case '*': // multi bulk
  152. $count = (int) $payload;
  153. if ($count === -1) {
  154. return null;
  155. }
  156. if ($this->_mbiterable === true) {
  157. return new MultiBulkResponseSimple($this, $count);
  158. }
  159. $multibulk = array();
  160. for ($i = 0; $i < $count; $i++) {
  161. $multibulk[$i] = $this->read();
  162. }
  163. return $multibulk;
  164. case ':': // integer
  165. return (int) $payload;
  166. case '-': // error
  167. if ($this->_throwErrors) {
  168. throw new ServerException($payload);
  169. }
  170. return new ResponseError($payload);
  171. default:
  172. $this->onProtocolError("Unknown prefix: '$prefix'");
  173. }
  174. }
  175. public function writeCommand(ICommand $command)
  176. {
  177. $commandId = $command->getId();
  178. $arguments = $command->getArguments();
  179. $cmdlen = strlen($commandId);
  180. $reqlen = count($arguments) + 1;
  181. $buffer = "*{$reqlen}\r\n\${$cmdlen}\r\n{$commandId}\r\n";
  182. for ($i = 0; $i < $reqlen - 1; $i++) {
  183. $argument = $arguments[$i];
  184. $arglen = strlen($argument);
  185. $buffer .= "\${$arglen}\r\n{$argument}\r\n";
  186. }
  187. $this->writeBytes($buffer);
  188. }
  189. }