@@ -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 | } |
@@ -31,21 +31,21 @@ discard block |
||
31 | 31 | */ |
32 | 32 | protected $sizeLimit = 10 * 1024 * 1024; // 10 MiB |
33 | 33 | |
34 | - public function __construct (WebSocketClient $client) { |
|
34 | + public function __construct(WebSocketClient $client) { |
|
35 | 35 | $this->client = $client; |
36 | 36 | } |
37 | 37 | |
38 | 38 | /** |
39 | 39 | * @return int |
40 | 40 | */ |
41 | - public function getSizeLimit (): int { |
|
41 | + public function getSizeLimit(): int { |
|
42 | 42 | return $this->sizeLimit; |
43 | 43 | } |
44 | 44 | |
45 | 45 | /** |
46 | 46 | * @return Frame|null |
47 | 47 | */ |
48 | - protected function parse () { |
|
48 | + protected function parse() { |
|
49 | 49 | if ( |
50 | 50 | $this->parse_1_opCode() |
51 | 51 | and $this->parse_2_length() |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | /** |
69 | 69 | * @return bool |
70 | 70 | */ |
71 | - protected function parse_1_opCode (): bool { |
|
71 | + protected function parse_1_opCode(): bool { |
|
72 | 72 | if (!isset($this->current['opCode'])) { |
73 | 73 | if (null === $char = $this->shift(1)) { |
74 | 74 | return false; |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | * |
100 | 100 | * @return bool |
101 | 101 | */ |
102 | - protected function parse_2_length (): bool { |
|
102 | + protected function parse_2_length(): bool { |
|
103 | 103 | // first pass |
104 | 104 | if (!isset($this->current['lengthSize'])) { |
105 | 105 | if (null === $byte = $this->shift(1)) { |
@@ -140,7 +140,7 @@ discard block |
||
140 | 140 | * |
141 | 141 | * @return bool |
142 | 142 | */ |
143 | - protected function parse_3_mask (): bool { |
|
143 | + protected function parse_3_mask(): bool { |
|
144 | 144 | if (!isset($this->current['mask'])) { |
145 | 145 | if (null === $mask = $this->shift(4)) { |
146 | 146 | return false; |
@@ -153,7 +153,7 @@ discard block |
||
153 | 153 | /** |
154 | 154 | * @return bool |
155 | 155 | */ |
156 | - protected function parse_4_payload (): bool { |
|
156 | + protected function parse_4_payload(): bool { |
|
157 | 157 | if (!isset($this->current['payload'])) { |
158 | 158 | $length = $this->current['length']; |
159 | 159 | if (null === $payload = $this->shift($length)) { |
@@ -175,7 +175,7 @@ discard block |
||
175 | 175 | * |
176 | 176 | * @return Generator|Frame[] |
177 | 177 | */ |
178 | - public function recvAll () { |
|
178 | + public function recvAll() { |
|
179 | 179 | $this->buffer .= $this->client->recvAll(); |
180 | 180 | while ($frame = $this->parse()) { |
181 | 181 | yield $frame; |
@@ -186,7 +186,7 @@ discard block |
||
186 | 186 | * @param int $bytes |
187 | 187 | * @return $this |
188 | 188 | */ |
189 | - public function setSizeLimit (int $bytes) { |
|
189 | + public function setSizeLimit(int $bytes) { |
|
190 | 190 | $this->sizeLimit = $bytes; |
191 | 191 | return $this; |
192 | 192 | } |
@@ -197,7 +197,7 @@ discard block |
||
197 | 197 | * @param int $bytes |
198 | 198 | * @return null|string |
199 | 199 | */ |
200 | - protected function shift (int $bytes): ?string { |
|
200 | + protected function shift(int $bytes): ?string { |
|
201 | 201 | if (strlen($this->buffer) < $bytes) { |
202 | 202 | return null; |
203 | 203 | } |
@@ -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 | } |
@@ -33,35 +33,35 @@ discard block |
||
33 | 33 | /** |
34 | 34 | * @param WebSocketClient $client |
35 | 35 | */ |
36 | - public function __construct (WebSocketClient $client) { |
|
36 | + public function __construct(WebSocketClient $client) { |
|
37 | 37 | $this->client = $client; |
38 | 38 | } |
39 | 39 | |
40 | 40 | /** |
41 | 41 | * @return int |
42 | 42 | */ |
43 | - final public function getMaxLength (): int { |
|
43 | + final public function getMaxLength(): int { |
|
44 | 44 | return $this->maxLength; |
45 | 45 | } |
46 | 46 | |
47 | 47 | /** |
48 | 48 | * @return bool |
49 | 49 | */ |
50 | - final public function isBinaryStream (): bool { |
|
50 | + final public function isBinaryStream(): bool { |
|
51 | 51 | return $this->binaryStream; |
52 | 52 | } |
53 | 53 | |
54 | 54 | /** |
55 | 55 | * @return bool |
56 | 56 | */ |
57 | - final public function isTextStream (): bool { |
|
57 | + final public function isTextStream(): bool { |
|
58 | 58 | return $this->textStream; |
59 | 59 | } |
60 | 60 | |
61 | 61 | /** |
62 | 62 | * @param string $binary |
63 | 63 | */ |
64 | - public function onBinary (string $binary): void { |
|
64 | + public function onBinary(string $binary): void { |
|
65 | 65 | unset($binary); |
66 | 66 | throw new WebSocketError(Frame::CLOSE_UNHANDLED_DATA, "I don't handle binary data."); |
67 | 67 | } |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | /** |
70 | 70 | * @param string $text |
71 | 71 | */ |
72 | - public function onText (string $text): void { |
|
72 | + public function onText(string $text): void { |
|
73 | 73 | $this->onText_CheckUtf8($text); |
74 | 74 | throw new WebSocketError(Frame::CLOSE_UNHANDLED_DATA, "I don't handle text."); |
75 | 75 | } |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | * |
80 | 80 | * @param string $text |
81 | 81 | */ |
82 | - protected function onText_CheckUtf8 (string $text): void { |
|
82 | + protected function onText_CheckUtf8(string $text): void { |
|
83 | 83 | if (!mb_detect_encoding($text, 'UTF-8', true)) { |
84 | 84 | throw new WebSocketError(Frame::CLOSE_BAD_DATA, "Received TEXT is not UTF-8."); |
85 | 85 | } |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | * @param bool $flag |
90 | 90 | * @return $this |
91 | 91 | */ |
92 | - public function setBinaryStream (bool $flag) { |
|
92 | + public function setBinaryStream(bool $flag) { |
|
93 | 93 | $this->binaryStream = $flag; |
94 | 94 | return $this; |
95 | 95 | } |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | * @param int $maxLength |
99 | 99 | * @return $this |
100 | 100 | */ |
101 | - public function setMaxLength (int $maxLength) { |
|
101 | + public function setMaxLength(int $maxLength) { |
|
102 | 102 | $this->maxLength = $maxLength; |
103 | 103 | return $this; |
104 | 104 | } |
@@ -107,7 +107,7 @@ discard block |
||
107 | 107 | * @param bool $flag |
108 | 108 | * @return $this |
109 | 109 | */ |
110 | - public function setTextStream (bool $flag) { |
|
110 | + public function setTextStream(bool $flag) { |
|
111 | 111 | $this->textStream = $flag; |
112 | 112 | return $this; |
113 | 113 | } |
@@ -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; |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | return $client; |
47 | 47 | } |
48 | 48 | |
49 | - public function broadcastBinary (string $payload) { |
|
49 | + public function broadcastBinary(string $payload) { |
|
50 | 50 | $this->broadcastFrame($payload, Frame::OP_BINARY); |
51 | 51 | } |
52 | 52 | |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | * @param int $opCode |
58 | 58 | * @param bool $final |
59 | 59 | */ |
60 | - public function broadcastFrame (string $payload, int $opCode = Frame::OP_TEXT, bool $final = true) { |
|
60 | + public function broadcastFrame(string $payload, int $opCode = Frame::OP_TEXT, bool $final = true) { |
|
61 | 61 | foreach ($this->clients as $client) { |
62 | 62 | if ($client->isOk()) { |
63 | 63 | $client->getFrameHandler()->write(...func_get_args()); |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | * |
71 | 71 | * @param string $payload |
72 | 72 | */ |
73 | - public function broadcastPing (string $payload = '') { |
|
73 | + public function broadcastPing(string $payload = '') { |
|
74 | 74 | $this->broadcastFrame($payload, Frame::OP_PING); |
75 | 75 | } |
76 | 76 | |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | * |
80 | 80 | * @param string $text |
81 | 81 | */ |
82 | - public function broadcastText (string $text) { |
|
82 | + public function broadcastText(string $text) { |
|
83 | 83 | $this->broadcastFrame($text, Frame::OP_TEXT); |
84 | 84 | } |
85 | 85 | |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | * @param string $reason |
91 | 91 | * @return StreamServer |
92 | 92 | */ |
93 | - public function close (int $code = Frame::CLOSE_INTERRUPT, $reason = '') { |
|
93 | + public function close(int $code = Frame::CLOSE_INTERRUPT, $reason = '') { |
|
94 | 94 | foreach ($this->clients as $client) { |
95 | 95 | try { |
96 | 96 | $client->close($code, $reason); |
@@ -108,14 +108,14 @@ discard block |
||
108 | 108 | * |
109 | 109 | * @return int |
110 | 110 | */ |
111 | - public function count (): int { |
|
111 | + public function count(): int { |
|
112 | 112 | return count($this->clients); |
113 | 113 | } |
114 | 114 | |
115 | 115 | /** |
116 | 116 | * @return WebSocketClient[] |
117 | 117 | */ |
118 | - public function getClients () { |
|
118 | + public function getClients() { |
|
119 | 119 | return $this->clients; |
120 | 120 | } |
121 | 121 | |
@@ -123,7 +123,7 @@ discard block |
||
123 | 123 | * @param resource $resource |
124 | 124 | * @return WebSocketClient |
125 | 125 | */ |
126 | - protected function newClient ($resource) { |
|
126 | + protected function newClient($resource) { |
|
127 | 127 | return new WebSocketClient($resource, $this); |
128 | 128 | } |
129 | 129 | |
@@ -132,13 +132,13 @@ discard block |
||
132 | 132 | * |
133 | 133 | * @inheritDoc |
134 | 134 | */ |
135 | - final public function onOutOfBand (): void { |
|
135 | + final public function onOutOfBand(): void { |
|
136 | 136 | } |
137 | 137 | |
138 | 138 | /** |
139 | 139 | * @inheritDoc |
140 | 140 | */ |
141 | - public function onReadable (): void { |
|
141 | + public function onReadable(): void { |
|
142 | 142 | $this->accept(); |
143 | 143 | } |
144 | 144 | |
@@ -147,7 +147,7 @@ discard block |
||
147 | 147 | * |
148 | 148 | * @param WebSocketClient $client |
149 | 149 | */ |
150 | - public function remove (WebSocketClient $client) { |
|
150 | + public function remove(WebSocketClient $client) { |
|
151 | 151 | unset($this->clients[$client->getId()]); |
152 | 152 | $this->reactor->remove($client); |
153 | 153 | } |
@@ -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 | } |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | * @param $resource |
52 | 52 | * @param WebSocketServer $server |
53 | 53 | */ |
54 | - public function __construct ($resource, WebSocketServer $server) { |
|
54 | + public function __construct($resource, WebSocketServer $server) { |
|
55 | 55 | parent::__construct($resource); |
56 | 56 | $this->server = $server; |
57 | 57 | } |
@@ -74,10 +74,10 @@ discard block |
||
74 | 74 | * @param string $reason |
75 | 75 | * @return StreamClient|void |
76 | 76 | */ |
77 | - public function close (int $code = null, string $reason = '') { |
|
77 | + public function close(int $code = null, string $reason = '') { |
|
78 | 78 | try { |
79 | 79 | if ($code >= 1000 and $this->state === self::STATE_OK) { |
80 | - $payload = pack('n', $code) . $reason; |
|
80 | + $payload = pack('n', $code).$reason; |
|
81 | 81 | $this->getFrameHandler()->write($payload, Frame::OP_CLOSE); |
82 | 82 | $this->shutdown(self::CH_WRITE); |
83 | 83 | } |
@@ -92,49 +92,49 @@ discard block |
||
92 | 92 | /** |
93 | 93 | * @return FrameHandler |
94 | 94 | */ |
95 | - public function getFrameHandler () { |
|
95 | + public function getFrameHandler() { |
|
96 | 96 | return $this->frameHandler ?? $this->frameHandler = new FrameHandler($this); |
97 | 97 | } |
98 | 98 | |
99 | 99 | /** |
100 | 100 | * @return FrameReader |
101 | 101 | */ |
102 | - public function getFrameReader () { |
|
102 | + public function getFrameReader() { |
|
103 | 103 | return $this->frameReader ?? $this->frameReader = new FrameReader($this); |
104 | 104 | } |
105 | 105 | |
106 | 106 | /** |
107 | 107 | * @return HandShake |
108 | 108 | */ |
109 | - public function getHandshake () { |
|
109 | + public function getHandshake() { |
|
110 | 110 | return $this->handshake ?? $this->handshake = new HandShake($this); |
111 | 111 | } |
112 | 112 | |
113 | 113 | /** |
114 | 114 | * @return MessageHandler |
115 | 115 | */ |
116 | - public function getMessageHandler () { |
|
116 | + public function getMessageHandler() { |
|
117 | 117 | return $this->messageHandler ?? $this->messageHandler = new MessageHandler($this); |
118 | 118 | } |
119 | 119 | |
120 | 120 | /** |
121 | 121 | * @return WebSocketServer |
122 | 122 | */ |
123 | - public function getServer () { |
|
123 | + public function getServer() { |
|
124 | 124 | return $this->server; |
125 | 125 | } |
126 | 126 | |
127 | 127 | /** |
128 | 128 | * @return int |
129 | 129 | */ |
130 | - public function getState (): int { |
|
130 | + public function getState(): int { |
|
131 | 131 | return $this->state; |
132 | 132 | } |
133 | 133 | |
134 | 134 | /** |
135 | 135 | * @return bool |
136 | 136 | */ |
137 | - final public function isOk (): bool { |
|
137 | + final public function isOk(): bool { |
|
138 | 138 | return $this->state === self::STATE_OK; |
139 | 139 | } |
140 | 140 | |
@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | * |
144 | 144 | * The RFC says the connection must be dropped if any unsupported activity occurs. |
145 | 145 | */ |
146 | - final public function onOutOfBand (): void { |
|
146 | + final public function onOutOfBand(): void { |
|
147 | 147 | $this->close(Frame::CLOSE_PROTOCOL_ERROR, "Received out-of-band data."); |
148 | 148 | } |
149 | 149 | |
@@ -152,7 +152,7 @@ discard block |
||
152 | 152 | * |
153 | 153 | * @throws Exception |
154 | 154 | */ |
155 | - public function onReadable (): void { |
|
155 | + public function onReadable(): void { |
|
156 | 156 | if (!strlen($this->recv(1, MSG_PEEK))) { // peer has shut down writing, or closed. |
157 | 157 | $this->close(); |
158 | 158 | return; |
@@ -188,7 +188,7 @@ discard block |
||
188 | 188 | /** |
189 | 189 | * Stub. |
190 | 190 | */ |
191 | - protected function onStateOk (): void { |
|
191 | + protected function onStateOk(): void { |
|
192 | 192 | |
193 | 193 | } |
194 | 194 | |
@@ -196,7 +196,7 @@ discard block |
||
196 | 196 | * @param FrameHandler $frameHandler |
197 | 197 | * @return $this |
198 | 198 | */ |
199 | - public function setFrameHandler (FrameHandler $frameHandler) { |
|
199 | + public function setFrameHandler(FrameHandler $frameHandler) { |
|
200 | 200 | $this->frameHandler = $frameHandler; |
201 | 201 | return $this; |
202 | 202 | } |
@@ -205,7 +205,7 @@ discard block |
||
205 | 205 | * @param FrameReader $frameReader |
206 | 206 | * @return $this |
207 | 207 | */ |
208 | - public function setFrameReader (FrameReader $frameReader) { |
|
208 | + public function setFrameReader(FrameReader $frameReader) { |
|
209 | 209 | $this->frameReader = $frameReader; |
210 | 210 | return $this; |
211 | 211 | } |
@@ -214,7 +214,7 @@ discard block |
||
214 | 214 | * @param MessageHandler $messageHandler |
215 | 215 | * @return $this |
216 | 216 | */ |
217 | - public function setMessageHandler (MessageHandler $messageHandler) { |
|
217 | + public function setMessageHandler(MessageHandler $messageHandler) { |
|
218 | 218 | $this->messageHandler = $messageHandler; |
219 | 219 | return $this; |
220 | 220 | } |
@@ -81,8 +81,7 @@ discard block |
||
81 | 81 | $this->getFrameHandler()->write($payload, Frame::OP_CLOSE); |
82 | 82 | $this->shutdown(self::CH_WRITE); |
83 | 83 | } |
84 | - } |
|
85 | - finally { |
|
84 | + } finally { |
|
86 | 85 | $this->state = self::STATE_CLOSE; |
87 | 86 | $this->server->remove($this); |
88 | 87 | parent::close(); |
@@ -174,12 +173,10 @@ discard block |
||
174 | 173 | case self::STATE_CLOSE: |
175 | 174 | return; |
176 | 175 | } |
177 | - } |
|
178 | - catch (WebSocketError $e) { |
|
176 | + } catch (WebSocketError $e) { |
|
179 | 177 | $this->close($e->getCode(), $e->getMessage()); |
180 | 178 | throw $e; |
181 | - } |
|
182 | - catch (Exception $e) { |
|
179 | + } catch (Exception $e) { |
|
183 | 180 | $this->close(Frame::CLOSE_INTERNAL_ERROR); |
184 | 181 | throw $e; |
185 | 182 | } |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | */ |
34 | 34 | protected $text = ''; |
35 | 35 | |
36 | - public function __construct (WebSocketClient $client) { |
|
36 | + public function __construct(WebSocketClient $client) { |
|
37 | 37 | $this->client = $client; |
38 | 38 | } |
39 | 39 | |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | * |
45 | 45 | * @param Frame $binary |
46 | 46 | */ |
47 | - protected function onBinary (Frame $binary): void { |
|
47 | + protected function onBinary(Frame $binary): void { |
|
48 | 48 | $handler = $this->client->getMessageHandler(); |
49 | 49 | if ($handler->isBinaryStream()) { |
50 | 50 | $handler->onBinary($binary->getPayload()); |
@@ -73,7 +73,7 @@ discard block |
||
73 | 73 | * |
74 | 74 | * @param Frame $close |
75 | 75 | */ |
76 | - protected function onClose (Frame $close): void { |
|
76 | + protected function onClose(Frame $close): void { |
|
77 | 77 | $this->client->close($close->getCloseCode()); |
78 | 78 | } |
79 | 79 | |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | * |
83 | 83 | * @param Frame $frame |
84 | 84 | */ |
85 | - protected function onContinue (Frame $frame): void { |
|
85 | + protected function onContinue(Frame $frame): void { |
|
86 | 86 | switch ($this->continue) { |
87 | 87 | case Frame::OP_TEXT: |
88 | 88 | $this->onText($frame); |
@@ -108,7 +108,7 @@ discard block |
||
108 | 108 | * |
109 | 109 | * @param Frame $control |
110 | 110 | */ |
111 | - protected function onControl (Frame $control): void { |
|
111 | + protected function onControl(Frame $control): void { |
|
112 | 112 | if ($control->isClose()) { |
113 | 113 | $this->onClose($control); |
114 | 114 | } |
@@ -125,7 +125,7 @@ discard block |
||
125 | 125 | * |
126 | 126 | * @param Frame $data |
127 | 127 | */ |
128 | - protected function onData (Frame $data): void { |
|
128 | + protected function onData(Frame $data): void { |
|
129 | 129 | if ($data->isText()) { |
130 | 130 | $this->onText($data); |
131 | 131 | } |
@@ -141,7 +141,7 @@ discard block |
||
141 | 141 | * |
142 | 142 | * @param Frame $frame |
143 | 143 | */ |
144 | - public function onFrame (Frame $frame): void { |
|
144 | + public function onFrame(Frame $frame): void { |
|
145 | 145 | $this->onFrame_CheckRsv($frame); |
146 | 146 | $frame->validate(); |
147 | 147 | if ($frame->isControl()) { |
@@ -160,7 +160,7 @@ discard block |
||
160 | 160 | * |
161 | 161 | * @param Frame $frame |
162 | 162 | */ |
163 | - protected function onFrame_CheckRsv (Frame $frame): void { |
|
163 | + protected function onFrame_CheckRsv(Frame $frame): void { |
|
164 | 164 | if ($badRsv = $frame->getRsv() & ~$this->client->getHandshake()->getRsv()) { |
165 | 165 | $badRsv = str_pad(base_convert($badRsv, 10, 2), 3, 0, STR_PAD_LEFT); |
166 | 166 | throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, "Received unknown RSV: 0b{$badRsv}"); |
@@ -174,7 +174,7 @@ discard block |
||
174 | 174 | * |
175 | 175 | * @param Frame $ping |
176 | 176 | */ |
177 | - protected function onPing (Frame $ping): void { |
|
177 | + protected function onPing(Frame $ping): void { |
|
178 | 178 | $this->writePong($ping->getPayload()); |
179 | 179 | } |
180 | 180 | |
@@ -185,7 +185,7 @@ discard block |
||
185 | 185 | * |
186 | 186 | * @param Frame $pong |
187 | 187 | */ |
188 | - protected function onPong (Frame $pong): void { |
|
188 | + protected function onPong(Frame $pong): void { |
|
189 | 189 | // stub |
190 | 190 | } |
191 | 191 | |
@@ -196,7 +196,7 @@ discard block |
||
196 | 196 | * |
197 | 197 | * @param Frame $text |
198 | 198 | */ |
199 | - protected function onText (Frame $text): void { |
|
199 | + protected function onText(Frame $text): void { |
|
200 | 200 | $handler = $this->client->getMessageHandler(); |
201 | 201 | if ($handler->isTextStream()) { |
202 | 202 | $handler->onText($text->getPayload()); |
@@ -221,7 +221,7 @@ discard block |
||
221 | 221 | * @param int $opCode |
222 | 222 | * @param bool $final |
223 | 223 | */ |
224 | - public function write (string $payload, int $opCode = Frame::OP_TEXT, bool $final = true): void { |
|
224 | + public function write(string $payload, int $opCode = Frame::OP_TEXT, bool $final = true): void { |
|
225 | 225 | $head = chr(($final ? 0x80 : 0) | $opCode); |
226 | 226 | $length = strlen($payload); |
227 | 227 | if ($length > 65535) { |
@@ -235,22 +235,22 @@ discard block |
||
235 | 235 | else { |
236 | 236 | $head .= chr($length); |
237 | 237 | } |
238 | - $this->client->write($head . $payload); |
|
238 | + $this->client->write($head.$payload); |
|
239 | 239 | } |
240 | 240 | |
241 | - public function writeBinary (string $payload, bool $final = true): void { |
|
241 | + public function writeBinary(string $payload, bool $final = true): void { |
|
242 | 242 | $this->write($payload, Frame::OP_BINARY, $final); |
243 | 243 | } |
244 | 244 | |
245 | - public function writePing (string $payload = ''): void { |
|
245 | + public function writePing(string $payload = ''): void { |
|
246 | 246 | $this->write($payload, Frame::OP_PING); |
247 | 247 | } |
248 | 248 | |
249 | - public function writePong (string $payload = ''): void { |
|
249 | + public function writePong(string $payload = ''): void { |
|
250 | 250 | $this->write($payload, Frame::OP_PONG); |
251 | 251 | } |
252 | 252 | |
253 | - public function writeText (string $payload, bool $final = true): void { |
|
253 | + public function writeText(string $payload, bool $final = true): void { |
|
254 | 254 | $this->write($payload, Frame::OP_TEXT, $final); |
255 | 255 | } |
256 | 256 | } |
257 | 257 | \ No newline at end of file |
@@ -48,8 +48,7 @@ discard block |
||
48 | 48 | $handler = $this->client->getMessageHandler(); |
49 | 49 | if ($handler->isBinaryStream()) { |
50 | 50 | $handler->onBinary($binary->getPayload()); |
51 | - } |
|
52 | - else { |
|
51 | + } else { |
|
53 | 52 | if (strlen($this->binary) + $binary->getLength() > $handler->getMaxLength()) { |
54 | 53 | throw new WebSocketError(Frame::CLOSE_TOO_LARGE, $handler->getMaxLength(), $binary); |
55 | 54 | } |
@@ -111,11 +110,9 @@ discard block |
||
111 | 110 | protected function onControl (Frame $control): void { |
112 | 111 | if ($control->isClose()) { |
113 | 112 | $this->onClose($control); |
114 | - } |
|
115 | - elseif ($control->isPing()) { |
|
113 | + } elseif ($control->isPing()) { |
|
116 | 114 | $this->onPing($control); |
117 | - } |
|
118 | - elseif ($control->isPong()) { |
|
115 | + } elseif ($control->isPong()) { |
|
119 | 116 | $this->onPong($control); |
120 | 117 | } |
121 | 118 | } |
@@ -128,8 +125,7 @@ discard block |
||
128 | 125 | protected function onData (Frame $data): void { |
129 | 126 | if ($data->isText()) { |
130 | 127 | $this->onText($data); |
131 | - } |
|
132 | - elseif ($data->isBinary()) { |
|
128 | + } elseif ($data->isBinary()) { |
|
133 | 129 | $this->onBinary($data); |
134 | 130 | } |
135 | 131 | } |
@@ -146,11 +142,9 @@ discard block |
||
146 | 142 | $frame->validate(); |
147 | 143 | if ($frame->isControl()) { |
148 | 144 | $this->onControl($frame); |
149 | - } |
|
150 | - elseif ($frame->isContinue()) { |
|
145 | + } elseif ($frame->isContinue()) { |
|
151 | 146 | $this->onContinue($frame); |
152 | - } |
|
153 | - else { |
|
147 | + } else { |
|
154 | 148 | $this->onData($frame); |
155 | 149 | } |
156 | 150 | } |
@@ -200,8 +194,7 @@ discard block |
||
200 | 194 | $handler = $this->client->getMessageHandler(); |
201 | 195 | if ($handler->isTextStream()) { |
202 | 196 | $handler->onText($text->getPayload()); |
203 | - } |
|
204 | - else { |
|
197 | + } else { |
|
205 | 198 | if (strlen($this->text) + $text->getLength() > $handler->getMaxLength()) { |
206 | 199 | throw new WebSocketError(Frame::CLOSE_TOO_LARGE, $handler->getMaxLength(), $text); |
207 | 200 | } |
@@ -227,12 +220,10 @@ discard block |
||
227 | 220 | if ($length > 65535) { |
228 | 221 | $head .= chr(127); |
229 | 222 | $head .= pack('J', $length); |
230 | - } |
|
231 | - elseif ($length >= 126) { |
|
223 | + } elseif ($length >= 126) { |
|
232 | 224 | $head .= chr(126); |
233 | 225 | $head .= pack('n', $length); |
234 | - } |
|
235 | - else { |
|
226 | + } else { |
|
236 | 227 | $head .= chr($length); |
237 | 228 | } |
238 | 229 | $this->client->write($head . $payload); |
@@ -17,15 +17,15 @@ discard block |
||
17 | 17 | const OP_PING = 0x09; |
18 | 18 | const OP_PONG = 0x0a; |
19 | 19 | |
20 | - const CLOSE_NORMAL = 1000; // mutual closure |
|
21 | - const CLOSE_INTERRUPT = 1001; // abrupt closure due to hangups, reboots, "going away" |
|
22 | - const CLOSE_PROTOCOL_ERROR = 1002; // invalid behavior / framing |
|
23 | - const CLOSE_UNHANDLED_DATA = 1003; // message handler doesn't want the payload |
|
24 | - const CLOSE_BAD_DATA = 1007; // message handler can't understand the payload |
|
25 | - const CLOSE_POLICY_VIOLATION = 1008; // generic "access denied" |
|
26 | - const CLOSE_TOO_LARGE = 1009; // unacceptable payload size |
|
27 | - const CLOSE_EXPECTATION = 1010; // peer closed because it wants extensions (listed in the reason) |
|
28 | - const CLOSE_INTERNAL_ERROR = 1011; // like http 500 |
|
20 | + const CLOSE_NORMAL = 1000; // mutual closure |
|
21 | + const CLOSE_INTERRUPT = 1001; // abrupt closure due to hangups, reboots, "going away" |
|
22 | + const CLOSE_PROTOCOL_ERROR = 1002; // invalid behavior / framing |
|
23 | + const CLOSE_UNHANDLED_DATA = 1003; // message handler doesn't want the payload |
|
24 | + const CLOSE_BAD_DATA = 1007; // message handler can't understand the payload |
|
25 | + const CLOSE_POLICY_VIOLATION = 1008; // generic "access denied" |
|
26 | + const CLOSE_TOO_LARGE = 1009; // unacceptable payload size |
|
27 | + const CLOSE_EXPECTATION = 1010; // peer closed because it wants extensions (listed in the reason) |
|
28 | + const CLOSE_INTERNAL_ERROR = 1011; // like http 500 |
|
29 | 29 | |
30 | 30 | /** |
31 | 31 | * @var bool |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | * @param int $opCode |
54 | 54 | * @param string $payload |
55 | 55 | */ |
56 | - public function __construct (bool $final, int $rsv, int $opCode, string $payload) { |
|
56 | + public function __construct(bool $final, int $rsv, int $opCode, string $payload) { |
|
57 | 57 | $this->final = $final; |
58 | 58 | $this->rsv = $rsv; |
59 | 59 | $this->opCode = $opCode; |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | * |
66 | 66 | * @return string |
67 | 67 | */ |
68 | - final public function __toString () { |
|
68 | + final public function __toString() { |
|
69 | 69 | if ($this->isClose()) { |
70 | 70 | return $this->getCloseReason(); |
71 | 71 | } |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | * |
80 | 80 | * @return int |
81 | 81 | */ |
82 | - public function getCloseCode (): int { |
|
82 | + public function getCloseCode(): int { |
|
83 | 83 | if ($this->getLength() >= 2) { |
84 | 84 | return unpack('n', substr($this->payload, 0, 2))[1]; |
85 | 85 | } |
@@ -93,112 +93,112 @@ discard block |
||
93 | 93 | * |
94 | 94 | * @return string |
95 | 95 | */ |
96 | - public function getCloseReason (): string { |
|
96 | + public function getCloseReason(): string { |
|
97 | 97 | return substr($this->payload, 2); |
98 | 98 | } |
99 | 99 | |
100 | 100 | /** |
101 | 101 | * @return int |
102 | 102 | */ |
103 | - final public function getLength (): int { |
|
103 | + final public function getLength(): int { |
|
104 | 104 | return strlen($this->payload); |
105 | 105 | } |
106 | 106 | |
107 | 107 | /** |
108 | 108 | * @return int |
109 | 109 | */ |
110 | - final public function getOpCode (): int { |
|
110 | + final public function getOpCode(): int { |
|
111 | 111 | return $this->opCode; |
112 | 112 | } |
113 | 113 | |
114 | 114 | /** |
115 | 115 | * @return string |
116 | 116 | */ |
117 | - final public function getPayload (): string { |
|
117 | + final public function getPayload(): string { |
|
118 | 118 | return $this->payload; |
119 | 119 | } |
120 | 120 | |
121 | 121 | /** |
122 | 122 | * @return int |
123 | 123 | */ |
124 | - final public function getRsv (): int { |
|
124 | + final public function getRsv(): int { |
|
125 | 125 | return $this->rsv; |
126 | 126 | } |
127 | 127 | |
128 | 128 | /** |
129 | 129 | * @return bool |
130 | 130 | */ |
131 | - final public function hasRsv1 (): bool { |
|
131 | + final public function hasRsv1(): bool { |
|
132 | 132 | return (bool)($this->rsv & 0b100); |
133 | 133 | } |
134 | 134 | |
135 | 135 | /** |
136 | 136 | * @return bool |
137 | 137 | */ |
138 | - final public function hasRsv2 (): bool { |
|
138 | + final public function hasRsv2(): bool { |
|
139 | 139 | return (bool)($this->rsv & 0b010); |
140 | 140 | } |
141 | 141 | |
142 | 142 | /** |
143 | 143 | * @return bool |
144 | 144 | */ |
145 | - final public function hasRsv3 (): bool { |
|
145 | + final public function hasRsv3(): bool { |
|
146 | 146 | return (bool)($this->rsv & 0b001); |
147 | 147 | } |
148 | 148 | |
149 | 149 | /** |
150 | 150 | * @return bool |
151 | 151 | */ |
152 | - final public function isBinary (): bool { |
|
152 | + final public function isBinary(): bool { |
|
153 | 153 | return $this->opCode === self::OP_BINARY; |
154 | 154 | } |
155 | 155 | |
156 | 156 | /** |
157 | 157 | * @return bool |
158 | 158 | */ |
159 | - final public function isClose (): bool { |
|
159 | + final public function isClose(): bool { |
|
160 | 160 | return $this->opCode === self::OP_CLOSE; |
161 | 161 | } |
162 | 162 | |
163 | 163 | /** |
164 | 164 | * @return bool |
165 | 165 | */ |
166 | - final public function isContinue (): bool { |
|
166 | + final public function isContinue(): bool { |
|
167 | 167 | return $this->opCode === self::OP_CONTINUE; |
168 | 168 | } |
169 | 169 | |
170 | 170 | /** |
171 | 171 | * @return bool |
172 | 172 | */ |
173 | - final public function isControl (): bool { |
|
173 | + final public function isControl(): bool { |
|
174 | 174 | return (bool)($this->opCode & 0b1000); |
175 | 175 | } |
176 | 176 | |
177 | 177 | /** |
178 | 178 | * @return bool |
179 | 179 | */ |
180 | - final public function isFinal (): bool { |
|
180 | + final public function isFinal(): bool { |
|
181 | 181 | return $this->final; |
182 | 182 | } |
183 | 183 | |
184 | 184 | /** |
185 | 185 | * @return bool |
186 | 186 | */ |
187 | - final public function isPing (): bool { |
|
187 | + final public function isPing(): bool { |
|
188 | 188 | return $this->opCode === self::OP_PING; |
189 | 189 | } |
190 | 190 | |
191 | 191 | /** |
192 | 192 | * @return bool |
193 | 193 | */ |
194 | - final public function isPong (): bool { |
|
194 | + final public function isPong(): bool { |
|
195 | 195 | return $this->opCode === self::OP_PONG; |
196 | 196 | } |
197 | 197 | |
198 | 198 | /** |
199 | 199 | * @return bool |
200 | 200 | */ |
201 | - final public function isText (): bool { |
|
201 | + final public function isText(): bool { |
|
202 | 202 | return $this->opCode === self::OP_TEXT; |
203 | 203 | } |
204 | 204 | |
@@ -207,7 +207,7 @@ discard block |
||
207 | 207 | * |
208 | 208 | * @throws WebSocketError |
209 | 209 | */ |
210 | - public function validate (): void { |
|
210 | + public function validate(): void { |
|
211 | 211 | if ($this->isControl()) { |
212 | 212 | if ($this->opCode > self::OP_PONG) { |
213 | 213 | throw new WebSocketError(self::CLOSE_PROTOCOL_ERROR, "Reserved control opcode ($this->opCode)."); |
@@ -217,8 +217,7 @@ |
||
217 | 217 | if (!$this->isFinal()) { |
218 | 218 | throw new WebSocketError(self::CLOSE_PROTOCOL_ERROR, "Fragmented control frame."); |
219 | 219 | } |
220 | - } |
|
221 | - elseif ($this->opCode > self::OP_BINARY) { |
|
220 | + } elseif ($this->opCode > self::OP_BINARY) { |
|
222 | 221 | throw new WebSocketError(self::CLOSE_PROTOCOL_ERROR, "Reserved data opcode ($this->opCode)."); |
223 | 222 | } |
224 | 223 | } |