1 | <?php |
||
15 | class Socket extends BaseConnection implements ConnectionInterface |
||
16 | { |
||
17 | /** |
||
18 | * Socket handle |
||
19 | * |
||
20 | * @var resource |
||
21 | */ |
||
22 | protected $socket; |
||
23 | |||
24 | /** |
||
25 | * Response handlers |
||
26 | * |
||
27 | * The characters used as keys are part of the Redis/Disque protocol. |
||
28 | * Disque uses the same response protocol as Redis, therefore |
||
29 | * @see http://redis.io/topics/protocol |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | private $responseHandlers = [ |
||
34 | '+' => Response\StringResponse::class, |
||
35 | '-' => Response\ErrorResponse::class, |
||
36 | ':' => Response\IntResponse::class, |
||
37 | '$' => Response\TextResponse::class, |
||
38 | '*' => Response\ArrayResponse::class |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * @inheritdoc |
||
43 | */ |
||
44 | public function connect($connectionTimeout = 0, $responseTimeout = null) |
||
62 | |||
63 | /** |
||
64 | * @inheritdoc |
||
65 | */ |
||
66 | public function disconnect() |
||
74 | |||
75 | /** |
||
76 | * @inheritdoc |
||
77 | */ |
||
78 | public function isConnected() |
||
82 | |||
83 | /** |
||
84 | * @inheritdoc |
||
85 | */ |
||
86 | public function execute(CommandInterface $command) |
||
107 | |||
108 | /** |
||
109 | * Execute a command on the connection |
||
110 | * |
||
111 | * @param string $data Data to send |
||
112 | * @throws ConnectionException |
||
113 | */ |
||
114 | public function send($data) |
||
130 | |||
131 | /** |
||
132 | * Read data from connection |
||
133 | * |
||
134 | * @param bool $keepWaiting If `true`, timeouts on stream read will be ignored |
||
135 | * @return mixed Data received |
||
136 | * |
||
137 | * @throws ConnectionException |
||
138 | * @throws ResponseException |
||
139 | */ |
||
140 | public function receive($keepWaiting = false) |
||
169 | |||
170 | /** |
||
171 | * Build actual socket |
||
172 | * |
||
173 | * @param string $host Host |
||
174 | * @param int $port Port |
||
175 | * @param float $timeout Timeout |
||
176 | * @return resource Socket |
||
177 | */ |
||
178 | protected function getSocket($host, $port, $timeout) |
||
182 | |||
183 | /** |
||
184 | * Get the first byte from Disque, which contains the data type |
||
185 | * |
||
186 | * @param bool $keepWaiting If `true`, timeouts on stream read will be ignored |
||
187 | * @return string A single char |
||
188 | * @throws ConnectionException |
||
189 | */ |
||
190 | private function getType($keepWaiting = false) |
||
211 | |||
212 | /** |
||
213 | * Get a line of data |
||
214 | * |
||
215 | * @return string Line of data |
||
216 | * @throws ConnectionException |
||
217 | */ |
||
218 | private function getData() |
||
226 | |||
227 | /** |
||
228 | * We should be connected |
||
229 | * |
||
230 | * @return void |
||
231 | * @throws ConnectionException |
||
232 | */ |
||
233 | private function shouldBeConnected() |
||
239 | } |