1 | <?php |
||
13 | class Reactor implements Countable { |
||
14 | |||
15 | /** |
||
16 | * All sockets in the reactor, keyed by ID. |
||
17 | * |
||
18 | * @var ReactiveInterface[] |
||
19 | */ |
||
20 | protected $sockets = []; |
||
21 | |||
22 | /** |
||
23 | * Selects instances. Can be used to select non-reactive sockets. |
||
24 | * |
||
25 | * @see https://php.net/socket_select |
||
26 | * |
||
27 | * @param SocketInterface[] $read |
||
28 | * @param SocketInterface[] $write |
||
29 | * @param SocketInterface[] $except |
||
30 | * @param float|null $timeout Maximum seconds to block. `NULL` blocks forever. |
||
31 | * @return int |
||
32 | * @throws SocketError |
||
33 | */ |
||
34 | public static function select (array &$read, array &$write, array &$except, ?float $timeout = null): int { |
||
50 | |||
51 | /** |
||
52 | * Adds a reactive socket for selection. |
||
53 | * |
||
54 | * @param ReactiveInterface $socket |
||
55 | * @return $this |
||
56 | */ |
||
57 | public function add (ReactiveInterface $socket) { |
||
61 | |||
62 | /** |
||
63 | * The number of reactive sockets in the reactor. |
||
64 | * |
||
65 | * @return int |
||
66 | */ |
||
67 | public function count (): int { |
||
70 | |||
71 | /** |
||
72 | * @return ReactiveInterface[] |
||
73 | */ |
||
74 | public function getSockets () { |
||
77 | |||
78 | /** |
||
79 | * @param int $channel |
||
80 | * @param ReactiveInterface $socket |
||
81 | * @param Throwable $error |
||
82 | */ |
||
83 | public function onError (int $channel, $socket, Throwable $error) { |
||
93 | |||
94 | /** |
||
95 | * Selects the reactor's sockets and calls their reactive methods. |
||
96 | * |
||
97 | * Invoke this in a loop that checks {@link Reactor::count()} a condition. |
||
98 | * |
||
99 | * Closed sockets are automatically removed from the reactor. |
||
100 | * |
||
101 | * @param float|null $timeout Maximum seconds to block. `NULL` blocks forever. |
||
102 | * @return int Number of sockets selected. |
||
103 | */ |
||
104 | public function react (?float $timeout = null): int { |
||
125 | |||
126 | /** |
||
127 | * Removes a socket from the reactor. |
||
128 | * |
||
129 | * @param ReactiveInterface $socket |
||
130 | * @return $this |
||
131 | */ |
||
132 | public function remove (ReactiveInterface $socket) { |
||
136 | } |
||
137 |