|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Spiral\Goridge; |
| 6 | + |
| 7 | +use Spiral\Goridge\RPC\Exception\RPCException; |
| 8 | +use function socket_select; |
| 9 | + |
| 10 | +class MultiRelayHelper |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @param array<array-key, RelayInterface> $relays |
| 14 | + * @return array-key[]|false |
| 15 | + * @internal |
| 16 | + * Returns either |
| 17 | + * - an array of array keys, even if only one |
| 18 | + * - or false if none |
| 19 | + */ |
| 20 | + public static function findRelayWithMessage(array $relays, int $timeoutInMicroseconds = 0): array|false |
| 21 | + { |
| 22 | + if (count($relays) === 0) { |
| 23 | + return false; |
| 24 | + } |
| 25 | + |
| 26 | + if ($relays[array_key_first($relays)] instanceof SocketRelay) { |
| 27 | + $sockets = []; |
| 28 | + $socketIdToRelayIndexMap = []; |
| 29 | + foreach ($relays as $relayIndex => $relay) { |
| 30 | + assert($relay instanceof SocketRelay); |
| 31 | + |
| 32 | + // Enforce connection |
| 33 | + if ($relay->socket === null) { |
| 34 | + // Important: Do not force reconnect here as it would otherwise completely ruin further handling |
| 35 | + continue; |
| 36 | + } |
| 37 | + |
| 38 | + $sockets[] = $relay->socket; |
| 39 | + $socketIdToRelayIndexMap[spl_object_id($relay->socket)] = $relayIndex; |
| 40 | + } |
| 41 | + |
| 42 | + if (count($sockets) === 0) { |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + $writes = null; |
| 47 | + $except = null; |
| 48 | + $changes = socket_select($sockets, $writes, $except, 0, $timeoutInMicroseconds); |
| 49 | + |
| 50 | + if ($changes > 0) { |
| 51 | + $indexes = []; |
| 52 | + foreach ($sockets as $socket) { |
| 53 | + $indexes[] = $socketIdToRelayIndexMap[spl_object_id($socket)] ?? throw new RPCException("Invalid socket??"); |
| 54 | + } |
| 55 | + |
| 56 | + return $indexes; |
| 57 | + } else { |
| 58 | + return false; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if ($relays[array_key_first($relays)] instanceof StreamRelay) { |
| 63 | + $streams = []; |
| 64 | + $streamNameToRelayIndexMap = []; |
| 65 | + foreach ($relays as $relayIndex => $relay) { |
| 66 | + assert($relay instanceof StreamRelay); |
| 67 | + |
| 68 | + $streams[] = $relay->in; |
| 69 | + $streamNameToRelayIndexMap[(string)$relay->in] = $relayIndex; |
| 70 | + } |
| 71 | + |
| 72 | + $writes = null; |
| 73 | + $except = null; |
| 74 | + $changes = stream_select($streams, $writes, $except, 0, $timeoutInMicroseconds); |
| 75 | + |
| 76 | + if ($changes > 0) { |
| 77 | + $indexes = []; |
| 78 | + foreach ($streams as $stream) { |
| 79 | + $indexes[] = $streamNameToRelayIndexMap[(string)$stream] ?? throw new RPCException("Invalid stream??"); |
| 80 | + } |
| 81 | + |
| 82 | + return $indexes; |
| 83 | + } else { |
| 84 | + return false; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @param array<array-key, RelayInterface> $relays |
| 93 | + * @return array-key[]|false |
| 94 | + * @internal |
| 95 | + * Returns either |
| 96 | + * - an array of array keys, even if only one |
| 97 | + * - or false if none |
| 98 | + */ |
| 99 | + public static function checkConnected(array $relays): array|false |
| 100 | + { |
| 101 | + if (count($relays) === 0) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + $keysNotConnected = []; |
| 106 | + foreach ($relays as $key => $relay) { |
| 107 | + if ($relay instanceof ConnectedRelayInterface && !$relay->isConnected()) { |
| 108 | + $relay->connect(); |
| 109 | + $keysNotConnected[] = $key; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return $keysNotConnected; |
| 114 | + } |
| 115 | +} |
0 commit comments