StreamConnection.php 6.7 KB

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