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) |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | public function send($payload, int $flags = null): self |
||
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | public function receiveSync(int &$flags = null) |
||
135 | |||
136 | /** |
||
137 | * @return string |
||
138 | */ |
||
139 | public function getAddress(): string |
||
143 | |||
144 | /** |
||
145 | * @return int|null |
||
146 | */ |
||
147 | public function getPort() |
||
151 | |||
152 | /** |
||
153 | * @return int |
||
154 | */ |
||
155 | public function getType(): int |
||
159 | |||
160 | /** |
||
161 | * @return bool |
||
162 | */ |
||
163 | public function isConnected(): bool |
||
167 | |||
168 | /** |
||
169 | * Ensure socket connection. Returns true if socket successfully connected |
||
170 | * or have already been connected. |
||
171 | * |
||
172 | * @return bool |
||
173 | * |
||
174 | * @throws RelayException |
||
175 | * @throws \Error When sockets are used in unsupported environment. |
||
176 | */ |
||
177 | public function connect(): bool |
||
194 | |||
195 | /** |
||
196 | * Close connection. |
||
197 | * |
||
198 | * @throws RelayException |
||
199 | */ |
||
200 | public function close() |
||
209 | |||
210 | /** |
||
211 | * Destruct connection and disconnect. |
||
212 | */ |
||
213 | public function __destruct() |
||
219 | |||
220 | /** |
||
221 | * @return string |
||
222 | */ |
||
223 | public function __toString(): string |
||
231 | |||
232 | /** |
||
233 | * @return array Prefix [flag, length] |
||
234 | * |
||
235 | * @throws PrefixException |
||
236 | */ |
||
237 | private function fetchPrefix(): array |
||
258 | |||
259 | /** |
||
260 | * @return resource |
||
261 | * @throws GoridgeException |
||
262 | */ |
||
263 | private function createSocket() |
||
275 | } |
||
276 |