@@ -41,15 +41,15 @@ discard block |
||
41 | 41 | 0x0f => 'RESERVED CONTROL 0x0f', |
42 | 42 | ]; |
43 | 43 | |
44 | - const CLOSE_NORMAL = 1000; // mutual closure |
|
45 | - const CLOSE_INTERRUPT = 1001; // abrupt closure due to hangups, reboots, "going away" |
|
46 | - const CLOSE_PROTOCOL_ERROR = 1002; // invalid behavior / framing |
|
47 | - const CLOSE_UNHANDLED_DATA = 1003; // message handler doesn't want the payload |
|
48 | - const CLOSE_BAD_DATA = 1007; // message handler can't understand the payload |
|
49 | - const CLOSE_POLICY_VIOLATION = 1008; // generic "access denied" |
|
50 | - const CLOSE_TOO_LARGE = 1009; // unacceptable payload size |
|
51 | - const CLOSE_EXPECTATION = 1010; // peer closed because it wants extensions (listed in the reason) |
|
52 | - const CLOSE_INTERNAL_ERROR = 1011; // like http 500 |
|
44 | + const CLOSE_NORMAL = 1000; // mutual closure |
|
45 | + const CLOSE_INTERRUPT = 1001; // abrupt closure due to hangups, reboots, "going away" |
|
46 | + const CLOSE_PROTOCOL_ERROR = 1002; // invalid behavior / framing |
|
47 | + const CLOSE_UNHANDLED_DATA = 1003; // message handler doesn't want the payload |
|
48 | + const CLOSE_BAD_DATA = 1007; // message handler can't understand the payload |
|
49 | + const CLOSE_POLICY_VIOLATION = 1008; // generic "access denied" |
|
50 | + const CLOSE_TOO_LARGE = 1009; // unacceptable payload size |
|
51 | + const CLOSE_EXPECTATION = 1010; // peer closed because it wants extensions (listed in the reason) |
|
52 | + const CLOSE_INTERNAL_ERROR = 1011; // like http 500 |
|
53 | 53 | |
54 | 54 | /** |
55 | 55 | * @var bool |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | * @param int $opCode |
80 | 80 | * @param string $payload |
81 | 81 | */ |
82 | - public function __construct (bool $final, int $rsv, int $opCode, string $payload) { |
|
82 | + public function __construct(bool $final, int $rsv, int $opCode, string $payload) { |
|
83 | 83 | $this->final = $final; |
84 | 84 | $this->rsv = $rsv; |
85 | 85 | $this->opCode = $opCode; |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | * |
92 | 92 | * @return string |
93 | 93 | */ |
94 | - public function __toString () { |
|
94 | + public function __toString() { |
|
95 | 95 | if ($this->isClose()) { |
96 | 96 | return $this->getCloseReason(); |
97 | 97 | } |
@@ -105,7 +105,7 @@ discard block |
||
105 | 105 | * |
106 | 106 | * @return int |
107 | 107 | */ |
108 | - final public function getCloseCode (): int { |
|
108 | + final public function getCloseCode(): int { |
|
109 | 109 | $code = substr($this->payload, 0, 2); |
110 | 110 | return isset($code[1]) ? unpack('n', $code)[1] : self::CLOSE_NORMAL; |
111 | 111 | } |
@@ -117,126 +117,126 @@ discard block |
||
117 | 117 | * |
118 | 118 | * @return string |
119 | 119 | */ |
120 | - final public function getCloseReason (): string { |
|
120 | + final public function getCloseReason(): string { |
|
121 | 121 | return substr($this->payload, 2); |
122 | 122 | } |
123 | 123 | |
124 | 124 | /** |
125 | 125 | * @return int |
126 | 126 | */ |
127 | - final public function getLength (): int { |
|
127 | + final public function getLength(): int { |
|
128 | 128 | return strlen($this->payload); |
129 | 129 | } |
130 | 130 | |
131 | 131 | /** |
132 | 132 | * @return string |
133 | 133 | */ |
134 | - public function getName (): string { |
|
134 | + public function getName(): string { |
|
135 | 135 | return self::NAMES[$this->opCode]; |
136 | 136 | } |
137 | 137 | |
138 | 138 | /** |
139 | 139 | * @return int |
140 | 140 | */ |
141 | - final public function getOpCode (): int { |
|
141 | + final public function getOpCode(): int { |
|
142 | 142 | return $this->opCode; |
143 | 143 | } |
144 | 144 | |
145 | 145 | /** |
146 | 146 | * @return string |
147 | 147 | */ |
148 | - final public function getPayload (): string { |
|
148 | + final public function getPayload(): string { |
|
149 | 149 | return $this->payload; |
150 | 150 | } |
151 | 151 | |
152 | 152 | /** |
153 | 153 | * @return int |
154 | 154 | */ |
155 | - final public function getRsv (): int { |
|
155 | + final public function getRsv(): int { |
|
156 | 156 | return $this->rsv; |
157 | 157 | } |
158 | 158 | |
159 | 159 | /** |
160 | 160 | * @return bool |
161 | 161 | */ |
162 | - final public function hasRsv1 (): bool { |
|
163 | - return (bool)($this->rsv & self::RSV1); |
|
162 | + final public function hasRsv1(): bool { |
|
163 | + return (bool) ($this->rsv & self::RSV1); |
|
164 | 164 | } |
165 | 165 | |
166 | 166 | /** |
167 | 167 | * @return bool |
168 | 168 | */ |
169 | - final public function hasRsv2 (): bool { |
|
170 | - return (bool)($this->rsv & self::RSV2); |
|
169 | + final public function hasRsv2(): bool { |
|
170 | + return (bool) ($this->rsv & self::RSV2); |
|
171 | 171 | } |
172 | 172 | |
173 | 173 | /** |
174 | 174 | * @return bool |
175 | 175 | */ |
176 | - final public function hasRsv3 (): bool { |
|
177 | - return (bool)($this->rsv & self::RSV3); |
|
176 | + final public function hasRsv3(): bool { |
|
177 | + return (bool) ($this->rsv & self::RSV3); |
|
178 | 178 | } |
179 | 179 | |
180 | 180 | /** |
181 | 181 | * @return bool |
182 | 182 | */ |
183 | - final public function isBinary (): bool { |
|
183 | + final public function isBinary(): bool { |
|
184 | 184 | return $this->opCode === self::OP_BINARY; |
185 | 185 | } |
186 | 186 | |
187 | 187 | /** |
188 | 188 | * @return bool |
189 | 189 | */ |
190 | - final public function isClose (): bool { |
|
190 | + final public function isClose(): bool { |
|
191 | 191 | return $this->opCode === self::OP_CLOSE; |
192 | 192 | } |
193 | 193 | |
194 | 194 | /** |
195 | 195 | * @return bool |
196 | 196 | */ |
197 | - final public function isContinue (): bool { |
|
197 | + final public function isContinue(): bool { |
|
198 | 198 | return $this->opCode === self::OP_CONTINUE; |
199 | 199 | } |
200 | 200 | |
201 | 201 | /** |
202 | 202 | * @return bool |
203 | 203 | */ |
204 | - final public function isControl (): bool { |
|
204 | + final public function isControl(): bool { |
|
205 | 205 | return $this->opCode >= self::OP_CLOSE; |
206 | 206 | } |
207 | 207 | |
208 | 208 | /** |
209 | 209 | * @return bool |
210 | 210 | */ |
211 | - final public function isData (): bool { |
|
211 | + final public function isData(): bool { |
|
212 | 212 | return $this->opCode < self::OP_CLOSE; |
213 | 213 | } |
214 | 214 | |
215 | 215 | /** |
216 | 216 | * @return bool |
217 | 217 | */ |
218 | - final public function isFinal (): bool { |
|
218 | + final public function isFinal(): bool { |
|
219 | 219 | return $this->final; |
220 | 220 | } |
221 | 221 | |
222 | 222 | /** |
223 | 223 | * @return bool |
224 | 224 | */ |
225 | - final public function isPing (): bool { |
|
225 | + final public function isPing(): bool { |
|
226 | 226 | return $this->opCode === self::OP_PING; |
227 | 227 | } |
228 | 228 | |
229 | 229 | /** |
230 | 230 | * @return bool |
231 | 231 | */ |
232 | - final public function isPong (): bool { |
|
232 | + final public function isPong(): bool { |
|
233 | 233 | return $this->opCode === self::OP_PONG; |
234 | 234 | } |
235 | 235 | |
236 | 236 | /** |
237 | 237 | * @return bool |
238 | 238 | */ |
239 | - final public function isText (): bool { |
|
239 | + final public function isText(): bool { |
|
240 | 240 | return $this->opCode === self::OP_TEXT; |
241 | 241 | } |
242 | 242 |
@@ -22,7 +22,7 @@ |
||
22 | 22 | * @see https://php.net/socket_create_pair |
23 | 23 | * |
24 | 24 | * @param array $extra Variadic constructor arguments. |
25 | - * @return static[] Two instances at indices `0` and `1`. |
|
25 | + * @return StreamClient[] Two instances at indices `0` and `1`. |
|
26 | 26 | * @throws SocketError |
27 | 27 | */ |
28 | 28 | public static function newUnixPair (...$extra) { |
@@ -12,7 +12,7 @@ discard block |
||
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_STREAM; |
17 | 17 | } |
18 | 18 | |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | * @return static[] Two instances at indices `0` and `1`. |
26 | 26 | * @throws SocketError |
27 | 27 | */ |
28 | - public static function newUnixPair (...$extra) { |
|
28 | + public static function newUnixPair(...$extra) { |
|
29 | 29 | if (!@socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $fd)) { |
30 | 30 | throw new SocketError; // reliable errno |
31 | 31 | } |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | * @return string |
45 | 45 | * @throws SocketError The partially read data is attached. |
46 | 46 | */ |
47 | - public function read (int $length): string { |
|
47 | + public function read(int $length): string { |
|
48 | 48 | try { |
49 | 49 | $data = ''; |
50 | 50 | do { |
@@ -71,11 +71,11 @@ discard block |
||
71 | 71 | * @return string |
72 | 72 | * @throws SocketError |
73 | 73 | */ |
74 | - public function recv (int $maxLength, int $flags = 0): string { |
|
74 | + public function recv(int $maxLength, int $flags = 0): string { |
|
75 | 75 | if (false === @socket_recv($this->resource, $data, $maxLength, $flags)) { |
76 | 76 | throw new SocketError($this->resource, SOCKET_EINVAL); |
77 | 77 | } |
78 | - return (string)$data; // cast needed, will be null if 0 bytes are read. |
|
78 | + return (string) $data; // cast needed, will be null if 0 bytes are read. |
|
79 | 79 | } |
80 | 80 | |
81 | 81 | /** |
@@ -85,7 +85,7 @@ discard block |
||
85 | 85 | * @return string |
86 | 86 | * @throws SocketError |
87 | 87 | */ |
88 | - public function recvAll (int $flags = 0): string { |
|
88 | + public function recvAll(int $flags = 0): string { |
|
89 | 89 | $flags = ($flags & ~MSG_WAITALL) | MSG_DONTWAIT; |
90 | 90 | $length = $this->getOption(SO_RCVBUF); |
91 | 91 | try { |
@@ -52,8 +52,7 @@ discard block |
||
52 | 52 | $length -= $chunkSize = strlen($chunk); |
53 | 53 | } while ($chunkSize and $length); |
54 | 54 | return $data; |
55 | - } |
|
56 | - catch (SocketError $e) { |
|
55 | + } catch (SocketError $e) { |
|
57 | 56 | $e->setExtra($data); |
58 | 57 | throw $e; |
59 | 58 | } |
@@ -90,8 +89,7 @@ discard block |
||
90 | 89 | $length = $this->getOption(SO_RCVBUF); |
91 | 90 | try { |
92 | 91 | return $this->recv($length, $flags); |
93 | - } |
|
94 | - catch (SocketError $e) { |
|
92 | + } catch (SocketError $e) { |
|
95 | 93 | if ($e->getCode() === SOCKET_EAGAIN) { // would block |
96 | 94 | return ''; |
97 | 95 | } |
@@ -12,7 +12,7 @@ discard block |
||
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 | |
@@ -28,12 +28,12 @@ discard block |
||
28 | 28 | * @return string |
29 | 29 | * @throws SocketError |
30 | 30 | */ |
31 | - public function recv (int $length, int $flags = 0, string &$name = '', int &$port = 0): string { |
|
31 | + public function recv(int $length, int $flags = 0, string &$name = '', int &$port = 0): string { |
|
32 | 32 | $count = @socket_recvfrom($this->resource, $data, $length, $flags, $name, $port); |
33 | 33 | if ($count === false) { |
34 | 34 | throw new SocketError($this->resource, SOCKET_EOPNOTSUPP); |
35 | 35 | } |
36 | - return (string)$data; // cast needed, will be null if 0 bytes are read |
|
36 | + return (string) $data; // cast needed, will be null if 0 bytes are read |
|
37 | 37 | } |
38 | 38 | |
39 | 39 | } |
40 | 40 | \ No newline at end of file |
@@ -12,7 +12,7 @@ discard block |
||
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_STREAM; |
17 | 17 | } |
18 | 18 | |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | * @return StreamClient |
26 | 26 | * @throws SocketError |
27 | 27 | */ |
28 | - public function accept (): StreamClient { |
|
28 | + public function accept(): StreamClient { |
|
29 | 29 | if (!$resource = @socket_accept($this->resource)) { |
30 | 30 | throw new SocketError($this->resource); // reliable errno |
31 | 31 | } |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | * @return $this |
46 | 46 | * @throws SocketError |
47 | 47 | */ |
48 | - public function listen (int $backlog = 0) { |
|
48 | + public function listen(int $backlog = 0) { |
|
49 | 49 | if (!@socket_listen($this->resource, $backlog)) { |
50 | 50 | throw new SocketError($this->resource); // reliable errno |
51 | 51 | } |
@@ -58,7 +58,7 @@ discard block |
||
58 | 58 | * @param resource $resource The accepted connection. |
59 | 59 | * @return StreamClient |
60 | 60 | */ |
61 | - protected function newClient ($resource): StreamClient { |
|
61 | + protected function newClient($resource): StreamClient { |
|
62 | 62 | return new StreamClient($resource); |
63 | 63 | } |
64 | 64 |
@@ -84,8 +84,7 @@ discard block |
||
84 | 84 | unset($channel); |
85 | 85 | if ($socket instanceof WebSocketClient and $error instanceof WebSocketError) { |
86 | 86 | $socket->close($error->getCode(), $error->getMessage()); |
87 | - } |
|
88 | - else { |
|
87 | + } else { |
|
89 | 88 | $socket->close(); |
90 | 89 | } |
91 | 90 | echo $error; |
@@ -109,11 +108,9 @@ discard block |
||
109 | 108 | foreach ($rwe[$channel] as $id => $socket) { |
110 | 109 | try { |
111 | 110 | $socket->{$method}(); |
112 | - } |
|
113 | - catch (Throwable $error) { |
|
111 | + } catch (Throwable $error) { |
|
114 | 112 | $this->onError($channel, $socket, $error); |
115 | - } |
|
116 | - finally { |
|
113 | + } finally { |
|
117 | 114 | if (!$socket->isOpen()) { |
118 | 115 | $this->remove($socket); |
119 | 116 | } |
@@ -31,12 +31,12 @@ discard block |
||
31 | 31 | * @return int |
32 | 32 | * @throws SocketError |
33 | 33 | */ |
34 | - public static function select (array &$read, array &$write, array &$except, ?float $timeout = null): int { |
|
34 | + public static function select(array &$read, array &$write, array &$except, ?float $timeout = null): int { |
|
35 | 35 | $rwe = [$read, $write, $except]; |
36 | 36 | array_walk_recursive($rwe, function(SocketInterface &$each) { |
37 | 37 | $each = $each->getResource(); |
38 | 38 | }); |
39 | - $uSec = (int)(fmod($timeout, 1) * 1000000); // ignored if timeout is null |
|
39 | + $uSec = (int) (fmod($timeout, 1) * 1000000); // ignored if timeout is null |
|
40 | 40 | $count = @socket_select($rwe[0], $rwe[1], $rwe[2], $timeout, $uSec); // keys are preserved |
41 | 41 | if ($count === false) { |
42 | 42 | $read = $write = $except = []; |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | * @param ReactiveInterface $socket |
55 | 55 | * @return $this |
56 | 56 | */ |
57 | - public function add (ReactiveInterface $socket) { |
|
57 | + public function add(ReactiveInterface $socket) { |
|
58 | 58 | $this->sockets[$socket->getId()] = $socket; |
59 | 59 | return $this; |
60 | 60 | } |
@@ -64,14 +64,14 @@ discard block |
||
64 | 64 | * |
65 | 65 | * @return int |
66 | 66 | */ |
67 | - public function count (): int { |
|
67 | + public function count(): int { |
|
68 | 68 | return count($this->sockets); |
69 | 69 | } |
70 | 70 | |
71 | 71 | /** |
72 | 72 | * @return ReactiveInterface[] |
73 | 73 | */ |
74 | - public function getSockets () { |
|
74 | + public function getSockets() { |
|
75 | 75 | return $this->sockets; |
76 | 76 | } |
77 | 77 | |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | * @param ReactiveInterface $socket |
81 | 81 | * @param Throwable $error |
82 | 82 | */ |
83 | - public function onError (int $channel, $socket, Throwable $error): void { |
|
83 | + public function onError(int $channel, $socket, Throwable $error): void { |
|
84 | 84 | unset($channel); |
85 | 85 | if ($socket instanceof WebSocketClient and $error instanceof WebSocketError) { |
86 | 86 | $socket->close($error->getCode(), $error->getMessage()); |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | * @param float|null $timeout Maximum seconds to block. `NULL` blocks forever. |
102 | 102 | * @return int Number of sockets selected. |
103 | 103 | */ |
104 | - public function react (?float $timeout = null): int { |
|
104 | + public function react(?float $timeout = null): int { |
|
105 | 105 | /** @var ReactiveInterface[][] $rwe */ |
106 | 106 | $rwe = [$this->sockets, [], $this->sockets]; |
107 | 107 | $count = static::select($rwe[0], $rwe[1], $rwe[2], $timeout); |
@@ -129,7 +129,7 @@ discard block |
||
129 | 129 | * @param ReactiveInterface $socket |
130 | 130 | * @return $this |
131 | 131 | */ |
132 | - public function remove (ReactiveInterface $socket) { |
|
132 | + public function remove(ReactiveInterface $socket) { |
|
133 | 133 | unset($this->sockets[$socket->getId()]); |
134 | 134 | return $this; |
135 | 135 | } |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | * @param resource $resource |
58 | 58 | * @param WebSocketServer $server |
59 | 59 | */ |
60 | - public function __construct ($resource, WebSocketServer $server) { |
|
60 | + public function __construct($resource, WebSocketServer $server) { |
|
61 | 61 | parent::__construct($resource); |
62 | 62 | $this->server = $server; |
63 | 63 | $this->handshake = new HandShake($this); |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | * @param string $reason Sent to the peer, if code is >= 1000 |
84 | 84 | * @return $this |
85 | 85 | */ |
86 | - public function close (int $code = null, string $reason = '') { |
|
86 | + public function close(int $code = null, string $reason = '') { |
|
87 | 87 | try { |
88 | 88 | if ($code >= 1000 and $this->isOk()) { |
89 | 89 | $this->frameHandler->writeClose($code, $reason); |
@@ -100,35 +100,35 @@ discard block |
||
100 | 100 | /** |
101 | 101 | * @return FrameHandler |
102 | 102 | */ |
103 | - public function getFrameHandler (): FrameHandler { |
|
103 | + public function getFrameHandler(): FrameHandler { |
|
104 | 104 | return $this->frameHandler; |
105 | 105 | } |
106 | 106 | |
107 | 107 | /** |
108 | 108 | * @return HandShake |
109 | 109 | */ |
110 | - public function getHandshake (): HandShake { |
|
110 | + public function getHandshake(): HandShake { |
|
111 | 111 | return $this->handshake; |
112 | 112 | } |
113 | 113 | |
114 | 114 | /** |
115 | 115 | * @return WebSocketServer |
116 | 116 | */ |
117 | - public function getServer (): WebSocketServer { |
|
117 | + public function getServer(): WebSocketServer { |
|
118 | 118 | return $this->server; |
119 | 119 | } |
120 | 120 | |
121 | 121 | /** |
122 | 122 | * @return int |
123 | 123 | */ |
124 | - public function getState (): int { |
|
124 | + public function getState(): int { |
|
125 | 125 | return $this->state; |
126 | 126 | } |
127 | 127 | |
128 | 128 | /** |
129 | 129 | * @return bool |
130 | 130 | */ |
131 | - final public function isOk (): bool { |
|
131 | + final public function isOk(): bool { |
|
132 | 132 | return $this->state === self::STATE_OK; |
133 | 133 | } |
134 | 134 | |
@@ -140,7 +140,7 @@ discard block |
||
140 | 140 | * @param string $binary |
141 | 141 | * @throws WebSocketError |
142 | 142 | */ |
143 | - public function onBinary (string $binary): void { |
|
143 | + public function onBinary(string $binary): void { |
|
144 | 144 | unset($binary); |
145 | 145 | throw new WebSocketError(Frame::CLOSE_UNHANDLED_DATA, "I don't handle binary data."); |
146 | 146 | } |
@@ -151,7 +151,7 @@ discard block |
||
151 | 151 | * @param int $code |
152 | 152 | * @param string $reason |
153 | 153 | */ |
154 | - public function onClose (int $code, string $reason): void { |
|
154 | + public function onClose(int $code, string $reason): void { |
|
155 | 155 | unset($code, $reason); |
156 | 156 | $this->close(); |
157 | 157 | } |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | * |
164 | 164 | * Closes the connection with a protocol-error frame. |
165 | 165 | */ |
166 | - final public function onOutOfBand (): void { |
|
166 | + final public function onOutOfBand(): void { |
|
167 | 167 | $this->close(Frame::CLOSE_PROTOCOL_ERROR, "Received out-of-band data."); |
168 | 168 | } |
169 | 169 | |
@@ -174,7 +174,7 @@ discard block |
||
174 | 174 | * |
175 | 175 | * @param string $message |
176 | 176 | */ |
177 | - public function onPing (string $message): void { |
|
177 | + public function onPing(string $message): void { |
|
178 | 178 | $this->frameHandler->writePong($message); |
179 | 179 | } |
180 | 180 | |
@@ -185,7 +185,7 @@ discard block |
||
185 | 185 | * |
186 | 186 | * @param string $message |
187 | 187 | */ |
188 | - public function onPong (string $message): void { |
|
188 | + public function onPong(string $message): void { |
|
189 | 189 | // stub |
190 | 190 | } |
191 | 191 | |
@@ -195,7 +195,7 @@ discard block |
||
195 | 195 | * @throws WebSocketError |
196 | 196 | * @throws Throwable |
197 | 197 | */ |
198 | - public function onReadable (): void { |
|
198 | + public function onReadable(): void { |
|
199 | 199 | if (!strlen($this->recv(1, MSG_PEEK))) { // peer has shut down writing, or closed. |
200 | 200 | $this->close(); |
201 | 201 | return; |
@@ -232,7 +232,7 @@ discard block |
||
232 | 232 | * |
233 | 233 | * Does nothing by default. |
234 | 234 | */ |
235 | - protected function onStateOk (): void { |
|
235 | + protected function onStateOk(): void { |
|
236 | 236 | // stub |
237 | 237 | } |
238 | 238 | |
@@ -244,7 +244,7 @@ discard block |
||
244 | 244 | * @param string $text |
245 | 245 | * @throws WebSocketError |
246 | 246 | */ |
247 | - public function onText (string $text): void { |
|
247 | + public function onText(string $text): void { |
|
248 | 248 | unset($text); |
249 | 249 | throw new WebSocketError(Frame::CLOSE_UNHANDLED_DATA, "I don't handle text."); |
250 | 250 | } |
@@ -254,7 +254,7 @@ discard block |
||
254 | 254 | * |
255 | 255 | * @param string $binary |
256 | 256 | */ |
257 | - public function writeBinary (string $binary): void { |
|
257 | + public function writeBinary(string $binary): void { |
|
258 | 258 | $this->frameHandler->writeBinary($binary); |
259 | 259 | } |
260 | 260 | |
@@ -263,7 +263,7 @@ discard block |
||
263 | 263 | * |
264 | 264 | * @param string $text |
265 | 265 | */ |
266 | - public function writeText (string $text): void { |
|
266 | + public function writeText(string $text): void { |
|
267 | 267 | $this->frameHandler->writeText($text); |
268 | 268 | } |
269 | 269 |
@@ -88,8 +88,7 @@ discard block |
||
88 | 88 | if ($code >= 1000 and $this->isOk()) { |
89 | 89 | $this->frameHandler->writeClose($code, $reason); |
90 | 90 | } |
91 | - } |
|
92 | - finally { |
|
91 | + } finally { |
|
93 | 92 | $this->server->remove($this); |
94 | 93 | parent::close(); |
95 | 94 | $this->state = self::STATE_CLOSED; |
@@ -216,12 +215,10 @@ discard block |
||
216 | 215 | case self::STATE_CLOSED: |
217 | 216 | return; |
218 | 217 | } |
219 | - } |
|
220 | - catch (WebSocketError $e) { |
|
218 | + } catch (WebSocketError $e) { |
|
221 | 219 | $this->close($e->getCode(), $e->getMessage()); |
222 | 220 | throw $e; |
223 | - } |
|
224 | - catch (Throwable $e) { |
|
221 | + } catch (Throwable $e) { |
|
225 | 222 | $this->close(Frame::CLOSE_INTERNAL_ERROR); |
226 | 223 | throw $e; |
227 | 224 | } |
@@ -51,21 +51,21 @@ discard block |
||
51 | 51 | /** |
52 | 52 | * @param WebSocketClient $client |
53 | 53 | */ |
54 | - public function __construct (WebSocketClient $client) { |
|
54 | + public function __construct(WebSocketClient $client) { |
|
55 | 55 | $this->client = $client; |
56 | 56 | } |
57 | 57 | |
58 | 58 | /** |
59 | 59 | * @return int |
60 | 60 | */ |
61 | - public function getFragmentSize (): int { |
|
61 | + public function getFragmentSize(): int { |
|
62 | 62 | return $this->fragmentSize; |
63 | 63 | } |
64 | 64 | |
65 | 65 | /** |
66 | 66 | * @return int |
67 | 67 | */ |
68 | - public function getMaxLength (): int { |
|
68 | + public function getMaxLength(): int { |
|
69 | 69 | return $this->maxLength; |
70 | 70 | } |
71 | 71 | |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | * @param Frame $frame |
77 | 77 | * @throws WebSocketError |
78 | 78 | */ |
79 | - protected function onBinary (Frame $frame): void { |
|
79 | + protected function onBinary(Frame $frame): void { |
|
80 | 80 | $this->buffer .= $frame->getPayload(); |
81 | 81 | if ($frame->isFinal()) { |
82 | 82 | $binary = $this->buffer; |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | * |
97 | 97 | * @param Frame $frame |
98 | 98 | */ |
99 | - protected function onClose (Frame $frame): void { |
|
99 | + protected function onClose(Frame $frame): void { |
|
100 | 100 | $this->client->onClose($frame->getCloseCode(), $frame->getCloseReason()); |
101 | 101 | } |
102 | 102 | |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | * @param Frame $frame |
107 | 107 | * @throws WebSocketError |
108 | 108 | */ |
109 | - protected function onContinue (Frame $frame): void { |
|
109 | + protected function onContinue(Frame $frame): void { |
|
110 | 110 | if (!$this->continue) { |
111 | 111 | throw new WebSocketError( |
112 | 112 | Frame::CLOSE_PROTOCOL_ERROR, |
@@ -138,7 +138,7 @@ discard block |
||
138 | 138 | * |
139 | 139 | * @param Frame $frame |
140 | 140 | */ |
141 | - protected function onControl (Frame $frame): void { |
|
141 | + protected function onControl(Frame $frame): void { |
|
142 | 142 | if ($frame->isClose()) { |
143 | 143 | $this->onClose($frame); |
144 | 144 | } |
@@ -158,7 +158,7 @@ discard block |
||
158 | 158 | * |
159 | 159 | * @param Frame $frame |
160 | 160 | */ |
161 | - protected function onData (Frame $frame): void { |
|
161 | + protected function onData(Frame $frame): void { |
|
162 | 162 | $this->onData_SetContinue($frame); |
163 | 163 | if ($frame->isText()) { |
164 | 164 | $this->onText($frame); |
@@ -174,11 +174,11 @@ discard block |
||
174 | 174 | * @param Frame $frame |
175 | 175 | * @throws WebSocketError |
176 | 176 | */ |
177 | - protected function onData_SetContinue (Frame $frame): void { |
|
177 | + protected function onData_SetContinue(Frame $frame): void { |
|
178 | 178 | if ($this->continue) { |
179 | 179 | throw new WebSocketError( |
180 | 180 | Frame::CLOSE_PROTOCOL_ERROR, |
181 | - "Received interleaved {$frame->getName()} against existing " . Frame::NAMES[$this->continue], |
|
181 | + "Received interleaved {$frame->getName()} against existing ".Frame::NAMES[$this->continue], |
|
182 | 182 | $frame |
183 | 183 | ); |
184 | 184 | } |
@@ -196,7 +196,7 @@ discard block |
||
196 | 196 | * |
197 | 197 | * @param Frame $frame |
198 | 198 | */ |
199 | - public function onFrame (Frame $frame): void { |
|
199 | + public function onFrame(Frame $frame): void { |
|
200 | 200 | $this->onFrame_CheckRsv($frame); |
201 | 201 | $this->onFrame_CheckLength($frame); |
202 | 202 | if ($frame->isControl()) { |
@@ -216,7 +216,7 @@ discard block |
||
216 | 216 | * @param Frame $frame |
217 | 217 | * @throws WebSocketError |
218 | 218 | */ |
219 | - protected function onFrame_CheckLength (Frame $frame): void { |
|
219 | + protected function onFrame_CheckLength(Frame $frame): void { |
|
220 | 220 | if ($frame->isData()) { |
221 | 221 | $length = strlen($this->buffer); |
222 | 222 | if ($length + $frame->getLength() > $this->maxLength) { |
@@ -235,7 +235,7 @@ discard block |
||
235 | 235 | * @param Frame $frame |
236 | 236 | * @throws WebSocketError |
237 | 237 | */ |
238 | - protected function onFrame_CheckRsv (Frame $frame): void { |
|
238 | + protected function onFrame_CheckRsv(Frame $frame): void { |
|
239 | 239 | if ($badRsv = $frame->getRsv() & ~$this->client->getHandshake()->getRsv()) { |
240 | 240 | $badRsv = str_pad(base_convert($badRsv >> 4, 10, 2), 3, '0', STR_PAD_LEFT); |
241 | 241 | throw new WebSocketError( |
@@ -251,7 +251,7 @@ discard block |
||
251 | 251 | * |
252 | 252 | * @param Frame $frame |
253 | 253 | */ |
254 | - protected function onPing (Frame $frame): void { |
|
254 | + protected function onPing(Frame $frame): void { |
|
255 | 255 | $this->client->onPing($frame->getPayload()); |
256 | 256 | } |
257 | 257 | |
@@ -260,7 +260,7 @@ discard block |
||
260 | 260 | * |
261 | 261 | * @param Frame $frame |
262 | 262 | */ |
263 | - protected function onPong (Frame $frame): void { |
|
263 | + protected function onPong(Frame $frame): void { |
|
264 | 264 | $this->client->onPong($frame->getPayload()); |
265 | 265 | } |
266 | 266 | |
@@ -271,7 +271,7 @@ discard block |
||
271 | 271 | * @param Frame $frame |
272 | 272 | * @throws WebSocketError |
273 | 273 | */ |
274 | - protected function onText (Frame $frame): void { |
|
274 | + protected function onText(Frame $frame): void { |
|
275 | 275 | $this->buffer .= $frame->getPayload(); |
276 | 276 | if ($frame->isFinal()) { |
277 | 277 | if (!mb_detect_encoding($this->buffer, 'UTF-8', true)) { |
@@ -287,7 +287,7 @@ discard block |
||
287 | 287 | * @param int $bytes |
288 | 288 | * @return $this |
289 | 289 | */ |
290 | - public function setFragmentSize (int $bytes) { |
|
290 | + public function setFragmentSize(int $bytes) { |
|
291 | 291 | $this->fragmentSize = $bytes; |
292 | 292 | return $this; |
293 | 293 | } |
@@ -296,7 +296,7 @@ discard block |
||
296 | 296 | * @param int $bytes |
297 | 297 | * @return $this |
298 | 298 | */ |
299 | - public function setMaxLength (int $bytes) { |
|
299 | + public function setMaxLength(int $bytes) { |
|
300 | 300 | $this->maxLength = $bytes; |
301 | 301 | return $this; |
302 | 302 | } |
@@ -307,7 +307,7 @@ discard block |
||
307 | 307 | * @param int $opCode |
308 | 308 | * @param string $payload |
309 | 309 | */ |
310 | - public function write (int $opCode, string $payload): void { |
|
310 | + public function write(int $opCode, string $payload): void { |
|
311 | 311 | $offset = 0; |
312 | 312 | $total = strlen($payload); |
313 | 313 | do { |
@@ -323,7 +323,7 @@ discard block |
||
323 | 323 | /** |
324 | 324 | * @param string $payload |
325 | 325 | */ |
326 | - public function writeBinary (string $payload): void { |
|
326 | + public function writeBinary(string $payload): void { |
|
327 | 327 | $this->write(Frame::OP_BINARY, $payload); |
328 | 328 | } |
329 | 329 | |
@@ -331,8 +331,8 @@ discard block |
||
331 | 331 | * @param int $code |
332 | 332 | * @param string $reason |
333 | 333 | */ |
334 | - public function writeClose (int $code = Frame::CLOSE_NORMAL, string $reason = ''): void { |
|
335 | - $this->writeFrame(true, Frame::OP_CLOSE, pack('n', $code) . $reason); |
|
334 | + public function writeClose(int $code = Frame::CLOSE_NORMAL, string $reason = ''): void { |
|
335 | + $this->writeFrame(true, Frame::OP_CLOSE, pack('n', $code).$reason); |
|
336 | 336 | } |
337 | 337 | |
338 | 338 | /** |
@@ -342,7 +342,7 @@ discard block |
||
342 | 342 | * @param int $opCode |
343 | 343 | * @param string $payload |
344 | 344 | */ |
345 | - protected function writeFrame (bool $final, int $opCode, string $payload): void { |
|
345 | + protected function writeFrame(bool $final, int $opCode, string $payload): void { |
|
346 | 346 | if ($opCode & 0x08 and !$final) { |
347 | 347 | throw new LogicException("Would have sent a fragmented control frame ({$opCode}) {$payload}"); |
348 | 348 | } |
@@ -359,27 +359,27 @@ discard block |
||
359 | 359 | else { |
360 | 360 | $head .= chr($length); |
361 | 361 | } |
362 | - $this->client->write($head . $payload); |
|
362 | + $this->client->write($head.$payload); |
|
363 | 363 | } |
364 | 364 | |
365 | 365 | /** |
366 | 366 | * @param string $payload |
367 | 367 | */ |
368 | - public function writePing (string $payload = ''): void { |
|
368 | + public function writePing(string $payload = ''): void { |
|
369 | 369 | $this->writeFrame(true, Frame::OP_PING, $payload); |
370 | 370 | } |
371 | 371 | |
372 | 372 | /** |
373 | 373 | * @param string $payload |
374 | 374 | */ |
375 | - public function writePong (string $payload = ''): void { |
|
375 | + public function writePong(string $payload = ''): void { |
|
376 | 376 | $this->writeFrame(true, Frame::OP_PONG, $payload); |
377 | 377 | } |
378 | 378 | |
379 | 379 | /** |
380 | 380 | * @param string $payload |
381 | 381 | */ |
382 | - public function writeText (string $payload): void { |
|
382 | + public function writeText(string $payload): void { |
|
383 | 383 | $this->write(Frame::OP_TEXT, $payload); |
384 | 384 | } |
385 | 385 | } |
386 | 386 | \ No newline at end of file |
@@ -117,12 +117,10 @@ discard block |
||
117 | 117 | try { |
118 | 118 | if ($this->continue === Frame::OP_TEXT) { |
119 | 119 | $this->onText($frame); |
120 | - } |
|
121 | - else { |
|
120 | + } else { |
|
122 | 121 | $this->onBinary($frame); |
123 | 122 | } |
124 | - } |
|
125 | - finally { |
|
123 | + } finally { |
|
126 | 124 | if ($frame->isFinal()) { |
127 | 125 | $this->continue = null; |
128 | 126 | } |
@@ -141,14 +139,11 @@ discard block |
||
141 | 139 | protected function onControl (Frame $frame): void { |
142 | 140 | if ($frame->isClose()) { |
143 | 141 | $this->onClose($frame); |
144 | - } |
|
145 | - elseif ($frame->isPing()) { |
|
142 | + } elseif ($frame->isPing()) { |
|
146 | 143 | $this->onPing($frame); |
147 | - } |
|
148 | - elseif ($frame->isPong()) { |
|
144 | + } elseif ($frame->isPong()) { |
|
149 | 145 | $this->onPong($frame); |
150 | - } |
|
151 | - else { |
|
146 | + } else { |
|
152 | 147 | throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, "Unsupported control frame.", $frame); |
153 | 148 | } |
154 | 149 | } |
@@ -162,8 +157,7 @@ discard block |
||
162 | 157 | $this->onData_SetContinue($frame); |
163 | 158 | if ($frame->isText()) { |
164 | 159 | $this->onText($frame); |
165 | - } |
|
166 | - elseif ($frame->isBinary()) { |
|
160 | + } elseif ($frame->isBinary()) { |
|
167 | 161 | $this->onBinary($frame); |
168 | 162 | } |
169 | 163 | } |
@@ -201,11 +195,9 @@ discard block |
||
201 | 195 | $this->onFrame_CheckLength($frame); |
202 | 196 | if ($frame->isControl()) { |
203 | 197 | $this->onControl($frame); |
204 | - } |
|
205 | - elseif ($frame->isContinue()) { |
|
198 | + } elseif ($frame->isContinue()) { |
|
206 | 199 | $this->onContinue($frame); |
207 | - } |
|
208 | - else { |
|
200 | + } else { |
|
209 | 201 | $this->onData($frame); |
210 | 202 | } |
211 | 203 | } |
@@ -351,12 +343,10 @@ discard block |
||
351 | 343 | if ($length > 65535) { |
352 | 344 | $head .= chr(127); |
353 | 345 | $head .= pack('J', $length); |
354 | - } |
|
355 | - elseif ($length >= 126) { |
|
346 | + } elseif ($length >= 126) { |
|
356 | 347 | $head .= chr(126); |
357 | 348 | $head .= pack('n', $length); |
358 | - } |
|
359 | - else { |
|
349 | + } else { |
|
360 | 350 | $head .= chr($length); |
361 | 351 | } |
362 | 352 | $this->client->write($head . $payload); |
@@ -31,7 +31,7 @@ discard block |
||
31 | 31 | * @param $resource |
32 | 32 | * @param Reactor $reactor |
33 | 33 | */ |
34 | - public function __construct ($resource, Reactor $reactor) { |
|
34 | + public function __construct($resource, Reactor $reactor) { |
|
35 | 35 | parent::__construct($resource); |
36 | 36 | $reactor->add($this); |
37 | 37 | $this->reactor = $reactor; |
@@ -40,7 +40,7 @@ discard block |
||
40 | 40 | /** |
41 | 41 | * @return WebSocketClient |
42 | 42 | */ |
43 | - public function accept (): WebSocketClient { |
|
43 | + public function accept(): WebSocketClient { |
|
44 | 44 | /** |
45 | 45 | * @see newClient() |
46 | 46 | * @var WebSocketClient $client |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | * @param int $opCode |
58 | 58 | * @param string $payload |
59 | 59 | */ |
60 | - public function broadcast (int $opCode, string $payload) { |
|
60 | + public function broadcast(int $opCode, string $payload) { |
|
61 | 61 | foreach ($this->clients as $client) { |
62 | 62 | if ($client->isOk()) { |
63 | 63 | $client->getFrameHandler()->write($opCode, $payload); |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | /** |
69 | 69 | * @param string $payload |
70 | 70 | */ |
71 | - public function broadcastBinary (string $payload) { |
|
71 | + public function broadcastBinary(string $payload) { |
|
72 | 72 | $this->broadcast(Frame::OP_BINARY, $payload); |
73 | 73 | } |
74 | 74 | |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | * |
78 | 78 | * @param string $payload |
79 | 79 | */ |
80 | - public function broadcastPing (string $payload = '') { |
|
80 | + public function broadcastPing(string $payload = '') { |
|
81 | 81 | $this->broadcast(Frame::OP_PING, $payload); |
82 | 82 | } |
83 | 83 | |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | * |
87 | 87 | * @param string $text |
88 | 88 | */ |
89 | - public function broadcastText (string $text) { |
|
89 | + public function broadcastText(string $text) { |
|
90 | 90 | $this->broadcast(Frame::OP_TEXT, $text); |
91 | 91 | } |
92 | 92 | |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | * @param string $reason |
98 | 98 | * @return $this |
99 | 99 | */ |
100 | - public function close (int $code = Frame::CLOSE_INTERRUPT, $reason = '') { |
|
100 | + public function close(int $code = Frame::CLOSE_INTERRUPT, $reason = '') { |
|
101 | 101 | foreach ($this->clients as $client) { |
102 | 102 | try { |
103 | 103 | $client->close($code, $reason); // clients remove themselves |
@@ -115,14 +115,14 @@ discard block |
||
115 | 115 | * |
116 | 116 | * @return int |
117 | 117 | */ |
118 | - public function count (): int { |
|
118 | + public function count(): int { |
|
119 | 119 | return count($this->clients); |
120 | 120 | } |
121 | 121 | |
122 | 122 | /** |
123 | 123 | * @return WebSocketClient[] |
124 | 124 | */ |
125 | - public function getClients () { |
|
125 | + public function getClients() { |
|
126 | 126 | return $this->clients; |
127 | 127 | } |
128 | 128 | |
@@ -130,21 +130,21 @@ discard block |
||
130 | 130 | * @param resource $resource |
131 | 131 | * @return WebSocketClient |
132 | 132 | */ |
133 | - protected function newClient ($resource): WebSocketClient { |
|
133 | + protected function newClient($resource): WebSocketClient { |
|
134 | 134 | return new WebSocketClient($resource, $this); |
135 | 135 | } |
136 | 136 | |
137 | 137 | /** |
138 | 138 | * WebSocket servers never get OOB data. |
139 | 139 | */ |
140 | - final public function onOutOfBand (): void { |
|
140 | + final public function onOutOfBand(): void { |
|
141 | 141 | // do nothing |
142 | 142 | } |
143 | 143 | |
144 | 144 | /** |
145 | 145 | * Auto-accept. |
146 | 146 | */ |
147 | - public function onReadable (): void { |
|
147 | + public function onReadable(): void { |
|
148 | 148 | $this->accept(); |
149 | 149 | } |
150 | 150 | |
@@ -153,7 +153,7 @@ discard block |
||
153 | 153 | * |
154 | 154 | * @param WebSocketClient $client |
155 | 155 | */ |
156 | - public function remove ($client): void { |
|
156 | + public function remove($client): void { |
|
157 | 157 | unset($this->clients[$client->getId()]); |
158 | 158 | $this->reactor->remove($client); |
159 | 159 | } |
@@ -101,8 +101,7 @@ |
||
101 | 101 | foreach ($this->clients as $client) { |
102 | 102 | try { |
103 | 103 | $client->close($code, $reason); // clients remove themselves |
104 | - } |
|
105 | - catch (Exception $e) { |
|
104 | + } catch (Exception $e) { |
|
106 | 105 | continue; |
107 | 106 | } |
108 | 107 | } |