StreamConnection.php 6.3 KB

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