StreamConnection.php 6.9 KB

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