1 | <?php |
||
24 | class SocketRelay implements RelayInterface |
||
25 | { |
||
26 | /** Supported socket types. */ |
||
27 | const SOCK_TCP = 0; |
||
28 | const SOCK_UNIX = 1; |
||
29 | |||
30 | // @deprecated |
||
31 | const SOCK_TPC = self::SOCK_TCP; |
||
32 | |||
33 | /** @var string */ |
||
34 | private $address; |
||
35 | |||
36 | /** @var int|null */ |
||
37 | private $port; |
||
38 | |||
39 | /** @var int */ |
||
40 | private $type; |
||
41 | |||
42 | /** @var resource|null */ |
||
43 | private $socket; |
||
44 | |||
45 | /** |
||
46 | * Example: |
||
47 | * $relay = new SocketRelay("localhost", 7000); |
||
48 | * $relay = new SocketRelay("/tmp/rpc.sock", null, Socket::UNIX_SOCKET); |
||
49 | * |
||
50 | * @param string $address Localhost, ip address or hostname. |
||
51 | * @param int|null $port Ignored for UNIX sockets. |
||
52 | * @param int $type Default: TCP_SOCKET |
||
53 | * |
||
54 | * @throws Exceptions\InvalidArgumentException |
||
55 | */ |
||
56 | public function __construct(string $address, int $port = null, int $type = self::SOCK_TCP) |
||
86 | |||
87 | /** |
||
88 | * {@inheritdoc} |
||
89 | */ |
||
90 | public function sendPackage( |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | public function send($payload, int $flags = null) |
||
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | public function receiveSync(int &$flags = null) |
||
162 | |||
163 | /** |
||
164 | * @return string |
||
165 | */ |
||
166 | public function getAddress(): string |
||
170 | |||
171 | /** |
||
172 | * @return int|null |
||
173 | */ |
||
174 | public function getPort() |
||
178 | |||
179 | /** |
||
180 | * @return int |
||
181 | */ |
||
182 | public function getType(): int |
||
186 | |||
187 | /** |
||
188 | * @return bool |
||
189 | */ |
||
190 | public function isConnected(): bool |
||
194 | |||
195 | /** |
||
196 | * Ensure socket connection. Returns true if socket successfully connected |
||
197 | * or have already been connected. |
||
198 | * |
||
199 | * @return bool |
||
200 | * |
||
201 | * @throws RelayException |
||
202 | * @throws \Error When sockets are used in unsupported environment. |
||
203 | */ |
||
204 | public function connect(): bool |
||
221 | |||
222 | /** |
||
223 | * Close connection. |
||
224 | * |
||
225 | * @throws RelayException |
||
226 | */ |
||
227 | public function close() |
||
236 | |||
237 | /** |
||
238 | * Destruct connection and disconnect. |
||
239 | */ |
||
240 | public function __destruct() |
||
246 | |||
247 | /** |
||
248 | * @return string |
||
249 | */ |
||
250 | public function __toString(): string |
||
258 | |||
259 | /** |
||
260 | * @return array Prefix [flag, length] |
||
261 | * |
||
262 | * @throws PrefixException |
||
263 | */ |
||
264 | private function fetchPrefix(): array |
||
285 | |||
286 | /** |
||
287 | * @return resource |
||
288 | * @throws GoridgeException |
||
289 | */ |
||
290 | private function createSocket() |
||
298 | } |
||
299 |