@@ -93,8 +93,7 @@ |
||
| 93 | 93 | foreach ($rwe[0] as $id => $socket) { |
| 94 | 94 | $socket->onReadable(); |
| 95 | 95 | } |
| 96 | - } |
|
| 97 | - finally { |
|
| 96 | + } finally { |
|
| 98 | 97 | array_walk_recursive($rwe, function(ReactiveInterface $each) { |
| 99 | 98 | if (!$each->isOpen()) { |
| 100 | 99 | $this->remove($each); |
@@ -28,12 +28,12 @@ discard block |
||
| 28 | 28 | * @return int |
| 29 | 29 | * @throws SocketError |
| 30 | 30 | */ |
| 31 | - public static function select (array &$read, array &$write, array &$except, ?float $timeout = null): int { |
|
| 31 | + public static function select(array &$read, array &$write, array &$except, ?float $timeout = null): int { |
|
| 32 | 32 | $rwe = [$read, $write, $except]; |
| 33 | 33 | array_walk_recursive($rwe, function(SocketInterface &$each) { |
| 34 | 34 | $each = $each->getResource(); |
| 35 | 35 | }); |
| 36 | - $uSec = (int)(fmod($timeout, 1) * 1000000); // ignored if timeout is null |
|
| 36 | + $uSec = (int) (fmod($timeout, 1) * 1000000); // ignored if timeout is null |
|
| 37 | 37 | $count = @socket_select($rwe[0], $rwe[1], $rwe[2], $timeout, $uSec); // keys are preserved |
| 38 | 38 | if ($count === false) { |
| 39 | 39 | $read = $write = $except = []; |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | * @param ReactiveInterface $socket |
| 52 | 52 | * @return $this |
| 53 | 53 | */ |
| 54 | - public function add (ReactiveInterface $socket) { |
|
| 54 | + public function add(ReactiveInterface $socket) { |
|
| 55 | 55 | $this->sockets[$socket->getId()] = $socket; |
| 56 | 56 | return $this; |
| 57 | 57 | } |
@@ -61,14 +61,14 @@ discard block |
||
| 61 | 61 | * |
| 62 | 62 | * @return int |
| 63 | 63 | */ |
| 64 | - public function count () { |
|
| 64 | + public function count() { |
|
| 65 | 65 | return count($this->sockets); |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | /** |
| 69 | 69 | * @return SocketInterface[] |
| 70 | 70 | */ |
| 71 | - public function getSockets () { |
|
| 71 | + public function getSockets() { |
|
| 72 | 72 | return $this->sockets; |
| 73 | 73 | } |
| 74 | 74 | |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | * @param float|null $timeout Maximum seconds to block. `NULL` blocks forever. |
| 83 | 83 | * @return int Number of sockets selected. |
| 84 | 84 | */ |
| 85 | - public function react (?float $timeout = null): int { |
|
| 85 | + public function react(?float $timeout = null): int { |
|
| 86 | 86 | /** @var ReactiveInterface[][] $rwe */ |
| 87 | 87 | $rwe = [$this->sockets, [], $this->sockets]; |
| 88 | 88 | $count = static::select($rwe[0], $rwe[1], $rwe[2], $timeout); |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | * @param int|ReactiveInterface $id |
| 111 | 111 | * @return $this |
| 112 | 112 | */ |
| 113 | - public function remove ($id) { |
|
| 113 | + public function remove($id) { |
|
| 114 | 114 | unset($this->sockets[$id instanceof ReactiveInterface ? $id->getId() : $id]); |
| 115 | 115 | return $this; |
| 116 | 116 | } |
@@ -12,7 +12,7 @@ |
||
| 12 | 12 | * |
| 13 | 13 | * @return int |
| 14 | 14 | */ |
| 15 | - final public static function getType (): int { |
|
| 15 | + final public static function getType(): int { |
|
| 16 | 16 | return SOCK_DGRAM; |
| 17 | 17 | } |
| 18 | 18 | |
@@ -14,13 +14,13 @@ |
||
| 14 | 14 | * |
| 15 | 15 | * @return void |
| 16 | 16 | */ |
| 17 | - public function onOutOfBand (): void; |
|
| 17 | + public function onOutOfBand(): void; |
|
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | 20 | * Called by the reactor when the socket has readable data. |
| 21 | 21 | * |
| 22 | 22 | * @return void |
| 23 | 23 | */ |
| 24 | - public function onReadable (): void; |
|
| 24 | + public function onReadable(): void; |
|
| 25 | 25 | |
| 26 | 26 | } |
| 27 | 27 | \ No newline at end of file |
@@ -45,32 +45,32 @@ discard block |
||
| 45 | 45 | */ |
| 46 | 46 | protected $sizeLimit = 4096; |
| 47 | 47 | |
| 48 | - public function __construct (WebSocketClient $client) { |
|
| 48 | + public function __construct(WebSocketClient $client) { |
|
| 49 | 49 | $this->client = $client; |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | /** |
| 53 | 53 | * @return string[] |
| 54 | 54 | */ |
| 55 | - public function getHeaders () { |
|
| 55 | + public function getHeaders() { |
|
| 56 | 56 | return $this->headers; |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | /** |
| 60 | 60 | * @return string |
| 61 | 61 | */ |
| 62 | - public function getMethod (): string { |
|
| 62 | + public function getMethod(): string { |
|
| 63 | 63 | return $this->method; |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | /** |
| 67 | 67 | * @return int |
| 68 | 68 | */ |
| 69 | - public function getRsv (): int { |
|
| 69 | + public function getRsv(): int { |
|
| 70 | 70 | return $this->rsv; |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | - public function negotiate (): bool { |
|
| 73 | + public function negotiate(): bool { |
|
| 74 | 74 | $this->buffer .= $this->client->recvAll(); |
| 75 | 75 | try { |
| 76 | 76 | if (strlen($this->buffer) > $this->sizeLimit) { |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | $key = strtolower(trim($key)); |
| 91 | 91 | $value = trim($value); |
| 92 | 92 | if (isset($this->headers[$key])) { |
| 93 | - $this->headers[$key] .= ', ' . $value; |
|
| 93 | + $this->headers[$key] .= ', '.$value; |
|
| 94 | 94 | } |
| 95 | 95 | else { |
| 96 | 96 | $this->headers[$key] = $value; |
@@ -108,19 +108,19 @@ discard block |
||
| 108 | 108 | } |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | - protected function upgrade (): void { |
|
| 111 | + protected function upgrade(): void { |
|
| 112 | 112 | $this->client->write(implode("\r\n", [ |
| 113 | 113 | "HTTP/1.1 101 Switching Protocols", |
| 114 | 114 | "Connection: Upgrade", |
| 115 | 115 | "Upgrade: websocket", |
| 116 | - "Sec-WebSocket-Accept: " . base64_encode(sha1($this->headers['sec-websocket-key'] . self::RFC_GUID, true)), |
|
| 116 | + "Sec-WebSocket-Accept: ".base64_encode(sha1($this->headers['sec-websocket-key'].self::RFC_GUID, true)), |
|
| 117 | 117 | ])); |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | /** |
| 121 | 121 | * Validates the received HTTP handshake headers, or throws. |
| 122 | 122 | */ |
| 123 | - protected function validate (): void { |
|
| 123 | + protected function validate(): void { |
|
| 124 | 124 | if (!( |
| 125 | 125 | $check = 'method = http 1.1' |
| 126 | 126 | and preg_match('/HTTP\/1\.1$/i', $this->method) |
@@ -91,8 +91,7 @@ discard block |
||
| 91 | 91 | $value = trim($value); |
| 92 | 92 | if (isset($this->headers[$key])) { |
| 93 | 93 | $this->headers[$key] .= ', ' . $value; |
| 94 | - } |
|
| 95 | - else { |
|
| 94 | + } else { |
|
| 96 | 95 | $this->headers[$key] = $value; |
| 97 | 96 | } |
| 98 | 97 | } |
@@ -101,8 +100,7 @@ discard block |
||
| 101 | 100 | $this->upgrade(); |
| 102 | 101 | $this->client->write("\r\n\r\n"); |
| 103 | 102 | return true; |
| 104 | - } |
|
| 105 | - catch (WebSocketError $e) { |
|
| 103 | + } catch (WebSocketError $e) { |
|
| 106 | 104 | $this->client->write("HTTP/1.1 {$e->getCode()}\r\n\r\n"); |
| 107 | 105 | throw $e; |
| 108 | 106 | } |
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | */ |
| 26 | 26 | protected $frame; |
| 27 | 27 | |
| 28 | - public function __construct (int $code, string $message = '', Frame $frame = null, Throwable $previous = null) { |
|
| 28 | + public function __construct(int $code, string $message = '', Frame $frame = null, Throwable $previous = null) { |
|
| 29 | 29 | parent::__construct($message, $code, $previous); |
| 30 | 30 | $this->frame = $frame; |
| 31 | 31 | } |
@@ -33,14 +33,14 @@ discard block |
||
| 33 | 33 | /** |
| 34 | 34 | * @return mixed |
| 35 | 35 | */ |
| 36 | - public function getExtra () { |
|
| 36 | + public function getExtra() { |
|
| 37 | 37 | return $this->extra; |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | /** |
| 41 | 41 | * @return Frame|null |
| 42 | 42 | */ |
| 43 | - public function getFrame () { |
|
| 43 | + public function getFrame() { |
|
| 44 | 44 | return $this->frame; |
| 45 | 45 | } |
| 46 | 46 | |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | * @param mixed $extra |
| 49 | 49 | * @return $this |
| 50 | 50 | */ |
| 51 | - public function setExtra ($extra) { |
|
| 51 | + public function setExtra($extra) { |
|
| 52 | 52 | $this->extra = $extra; |
| 53 | 53 | return $this; |
| 54 | 54 | } |
@@ -94,8 +94,7 @@ |
||
| 94 | 94 | foreach ($this->clients as $client) { |
| 95 | 95 | try { |
| 96 | 96 | $client->close($code, $reason); |
| 97 | - } |
|
| 98 | - catch (Exception $e) { |
|
| 97 | + } catch (Exception $e) { |
|
| 99 | 98 | continue; |
| 100 | 99 | } |
| 101 | 100 | } |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | * @param $resource |
| 30 | 30 | * @param Reactor $reactor |
| 31 | 31 | */ |
| 32 | - public function __construct ($resource, Reactor $reactor) { |
|
| 32 | + public function __construct($resource, Reactor $reactor) { |
|
| 33 | 33 | parent::__construct($resource); |
| 34 | 34 | $reactor->add($this); |
| 35 | 35 | $this->reactor = $reactor; |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | /** |
| 39 | 39 | * @return WebSocketClient |
| 40 | 40 | */ |
| 41 | - public function accept () { |
|
| 41 | + public function accept() { |
|
| 42 | 42 | /** @var WebSocketClient $client */ |
| 43 | 43 | $client = parent::accept(); |
| 44 | 44 | $this->clients[$client->getId()] = $client; |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | * @param int $opCode |
| 53 | 53 | * @param string $payload |
| 54 | 54 | */ |
| 55 | - public function broadcast (int $opCode, string $payload) { |
|
| 55 | + public function broadcast(int $opCode, string $payload) { |
|
| 56 | 56 | foreach ($this->clients as $client) { |
| 57 | 57 | if ($client->isOk()) { |
| 58 | 58 | $client->getFrameHandler()->write($opCode, $payload); |
@@ -63,7 +63,7 @@ discard block |
||
| 63 | 63 | /** |
| 64 | 64 | * @param string $payload |
| 65 | 65 | */ |
| 66 | - public function broadcastBinary (string $payload) { |
|
| 66 | + public function broadcastBinary(string $payload) { |
|
| 67 | 67 | $this->broadcast(Frame::OP_BINARY, $payload); |
| 68 | 68 | } |
| 69 | 69 | |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | * |
| 73 | 73 | * @param string $payload |
| 74 | 74 | */ |
| 75 | - public function broadcastPing (string $payload = '') { |
|
| 75 | + public function broadcastPing(string $payload = '') { |
|
| 76 | 76 | $this->broadcast(Frame::OP_PING, $payload); |
| 77 | 77 | } |
| 78 | 78 | |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | * |
| 82 | 82 | * @param string $text |
| 83 | 83 | */ |
| 84 | - public function broadcastText (string $text) { |
|
| 84 | + public function broadcastText(string $text) { |
|
| 85 | 85 | $this->broadcast(Frame::OP_TEXT, $text); |
| 86 | 86 | } |
| 87 | 87 | |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | * @param string $reason |
| 93 | 93 | * @return StreamServer |
| 94 | 94 | */ |
| 95 | - public function close (int $code = Frame::CLOSE_INTERRUPT, $reason = '') { |
|
| 95 | + public function close(int $code = Frame::CLOSE_INTERRUPT, $reason = '') { |
|
| 96 | 96 | foreach ($this->clients as $client) { |
| 97 | 97 | try { |
| 98 | 98 | $client->close($code, $reason); |
@@ -110,14 +110,14 @@ discard block |
||
| 110 | 110 | * |
| 111 | 111 | * @return int |
| 112 | 112 | */ |
| 113 | - public function count (): int { |
|
| 113 | + public function count(): int { |
|
| 114 | 114 | return count($this->clients); |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | /** |
| 118 | 118 | * @return WebSocketClient[] |
| 119 | 119 | */ |
| 120 | - public function getClients () { |
|
| 120 | + public function getClients() { |
|
| 121 | 121 | return $this->clients; |
| 122 | 122 | } |
| 123 | 123 | |
@@ -125,17 +125,17 @@ discard block |
||
| 125 | 125 | * @param resource $resource |
| 126 | 126 | * @return WebSocketClient |
| 127 | 127 | */ |
| 128 | - protected function newClient ($resource) { |
|
| 128 | + protected function newClient($resource) { |
|
| 129 | 129 | return new WebSocketClient($resource, $this); |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | /** |
| 133 | 133 | * Servers never get OOB data. |
| 134 | 134 | */ |
| 135 | - final public function onOutOfBand (): void { |
|
| 135 | + final public function onOutOfBand(): void { |
|
| 136 | 136 | } |
| 137 | 137 | |
| 138 | - public function onReadable (): void { |
|
| 138 | + public function onReadable(): void { |
|
| 139 | 139 | $this->accept(); |
| 140 | 140 | } |
| 141 | 141 | |
@@ -144,7 +144,7 @@ discard block |
||
| 144 | 144 | * |
| 145 | 145 | * @param WebSocketClient $client |
| 146 | 146 | */ |
| 147 | - public function remove (WebSocketClient $client) { |
|
| 147 | + public function remove(WebSocketClient $client) { |
|
| 148 | 148 | unset($this->clients[$client->getId()]); |
| 149 | 149 | $this->reactor->remove($client); |
| 150 | 150 | } |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | * |
| 19 | 19 | * @return string |
| 20 | 20 | */ |
| 21 | - public function __toString () { |
|
| 21 | + public function __toString() { |
|
| 22 | 22 | try { |
| 23 | 23 | return implode(':', $this->getPeerName()); |
| 24 | 24 | } |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | * @return $this |
| 42 | 42 | * @throws SocketError |
| 43 | 43 | */ |
| 44 | - public function connect (string $address, int $port = 0) { |
|
| 44 | + public function connect(string $address, int $port = 0) { |
|
| 45 | 45 | if (!@socket_connect($this->resource, $address, $port)) { |
| 46 | 46 | // ignore expected errors for non-blocking connections |
| 47 | 47 | $errno = SocketError::getLast($this->resource); |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | * @return array `[ 0 => address, 1 => port ]` |
| 62 | 62 | * @throws SocketError |
| 63 | 63 | */ |
| 64 | - public function getPeerName (): array { |
|
| 64 | + public function getPeerName(): array { |
|
| 65 | 65 | if ($this->getDomain() === AF_UNIX) { |
| 66 | 66 | return [$this->getOption(17), 0]; // SO_PEERCRED is not exposed by PHP |
| 67 | 67 | } |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | * @return int Total bytes sent. |
| 82 | 82 | * @throws SocketError |
| 83 | 83 | */ |
| 84 | - public function send (string $data, int $flags = 0): int { |
|
| 84 | + public function send(string $data, int $flags = 0): int { |
|
| 85 | 85 | $count = @socket_send($this->resource, $data, strlen($data), $flags); |
| 86 | 86 | if ($count === false) { |
| 87 | 87 | throw new SocketError($this->resource); // reliable errno |
@@ -96,7 +96,7 @@ discard block |
||
| 96 | 96 | * @return $this |
| 97 | 97 | * @throws SocketError `int` total bytes sent is set as the extra data. |
| 98 | 98 | */ |
| 99 | - public function write (string $data) { |
|
| 99 | + public function write(string $data) { |
|
| 100 | 100 | $length = strlen($data); |
| 101 | 101 | $total = 0; |
| 102 | 102 | while ($total < $length) { |
@@ -21,8 +21,7 @@ discard block |
||
| 21 | 21 | public function __toString () { |
| 22 | 22 | try { |
| 23 | 23 | return implode(':', $this->getPeerName()); |
| 24 | - } |
|
| 25 | - catch (Throwable $e) { |
|
| 24 | + } catch (Throwable $e) { |
|
| 26 | 25 | return "?{$this->resource}"; |
| 27 | 26 | } |
| 28 | 27 | } |
@@ -102,8 +101,7 @@ discard block |
||
| 102 | 101 | while ($total < $length) { |
| 103 | 102 | try { |
| 104 | 103 | $total += $this->awaitWritable()->send(substr($data, $total)); |
| 105 | - } |
|
| 106 | - catch (SocketError $e) { |
|
| 104 | + } catch (SocketError $e) { |
|
| 107 | 105 | $e->setExtra($total); |
| 108 | 106 | throw $e; |
| 109 | 107 | } |
@@ -27,20 +27,20 @@ |
||
| 27 | 27 | * |
| 28 | 28 | * @return int |
| 29 | 29 | */ |
| 30 | - public function getId (): int; |
|
| 30 | + public function getId(): int; |
|
| 31 | 31 | |
| 32 | 32 | /** |
| 33 | 33 | * Returns the underlying socket resource. |
| 34 | 34 | * |
| 35 | 35 | * @return resource |
| 36 | 36 | */ |
| 37 | - public function getResource (); |
|
| 37 | + public function getResource(); |
|
| 38 | 38 | |
| 39 | 39 | /** |
| 40 | 40 | * Whether the underlying resource is usable. |
| 41 | 41 | * |
| 42 | 42 | * @return bool |
| 43 | 43 | */ |
| 44 | - public function isOpen (): bool; |
|
| 44 | + public function isOpen(): bool; |
|
| 45 | 45 | |
| 46 | 46 | } |
| 47 | 47 | \ No newline at end of file |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | * @param resource $resource PHP socket resource, or `null` for the global error. |
| 30 | 30 | * @return int If the resource is closed or not a socket, `SOCKET_EBADF` is returned. |
| 31 | 31 | */ |
| 32 | - public static function getLast ($resource = null) { |
|
| 32 | + public static function getLast($resource = null) { |
|
| 33 | 33 | if (isset($resource)) { |
| 34 | 34 | if (@get_resource_type($resource) !== 'Socket') { |
| 35 | 35 | return SOCKET_EBADF; // Bad file descriptor |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | * @param int $fallback Code to assume if one can't be found via the subject. |
| 63 | 63 | * @param SocketError|null $previous Slippage of a prior error. |
| 64 | 64 | */ |
| 65 | - public function __construct ($subject = null, $fallback = 0, SocketError $previous = null) { |
|
| 65 | + public function __construct($subject = null, $fallback = 0, SocketError $previous = null) { |
|
| 66 | 66 | if ($errno = is_int($subject) ? $subject : static::getLast($subject)) { |
| 67 | 67 | $message = socket_strerror($errno); |
| 68 | 68 | } |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | /** |
| 78 | 78 | * @return mixed |
| 79 | 79 | */ |
| 80 | - public function getExtra () { |
|
| 80 | + public function getExtra() { |
|
| 81 | 81 | return $this->extra; |
| 82 | 82 | } |
| 83 | 83 | |
@@ -85,7 +85,7 @@ discard block |
||
| 85 | 85 | * @param mixed $extra |
| 86 | 86 | * @return $this |
| 87 | 87 | */ |
| 88 | - public function setExtra ($extra) { |
|
| 88 | + public function setExtra($extra) { |
|
| 89 | 89 | $this->extra = $extra; |
| 90 | 90 | return $this; |
| 91 | 91 | } |
@@ -65,8 +65,7 @@ |
||
| 65 | 65 | public function __construct ($subject = null, $fallback = 0, SocketError $previous = null) { |
| 66 | 66 | if ($errno = is_int($subject) ? $subject : static::getLast($subject)) { |
| 67 | 67 | $message = socket_strerror($errno); |
| 68 | - } |
|
| 69 | - else { |
|
| 68 | + } else { |
|
| 70 | 69 | $errno = $fallback; |
| 71 | 70 | $last = error_get_last(); |
| 72 | 71 | $message = "{$last['message']} in {$last['file']}:{$last['line']}"; |