StreamConnection.php 7.3 KB

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