StreamConnection.php 6.3 KB

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