@@ -43,21 +43,21 @@ discard block |
||
| 43 | 43 | /** |
| 44 | 44 | * @param WebSocketClient $client |
| 45 | 45 | */ |
| 46 | - public function __construct (WebSocketClient $client) { |
|
| 46 | + public function __construct(WebSocketClient $client) { |
|
| 47 | 47 | $this->client = $client; |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | /** |
| 51 | 51 | * @return string[] |
| 52 | 52 | */ |
| 53 | - public function getHeaders () { |
|
| 53 | + public function getHeaders() { |
|
| 54 | 54 | return $this->headers; |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | /** |
| 58 | 58 | * @return string |
| 59 | 59 | */ |
| 60 | - public function getMethod (): string { |
|
| 60 | + public function getMethod(): string { |
|
| 61 | 61 | return $this->method; |
| 62 | 62 | } |
| 63 | 63 | |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | * @throws WebSocketError |
| 69 | 69 | * @throws Throwable |
| 70 | 70 | */ |
| 71 | - public function onReadable (): bool { |
|
| 71 | + public function onReadable(): bool { |
|
| 72 | 72 | // read into the buffer |
| 73 | 73 | $this->buffer .= $bytes = $this->client->recvAll(); |
| 74 | 74 | |
@@ -100,7 +100,7 @@ discard block |
||
| 100 | 100 | $key = strtolower(trim($key)); |
| 101 | 101 | $value = trim($value); |
| 102 | 102 | if (isset($this->headers[$key])) { |
| 103 | - $this->headers[$key] .= ', ' . $value; |
|
| 103 | + $this->headers[$key] .= ', '.$value; |
|
| 104 | 104 | } |
| 105 | 105 | else { |
| 106 | 106 | $this->headers[$key] = $value; |
@@ -129,8 +129,8 @@ discard block |
||
| 129 | 129 | /** |
| 130 | 130 | * Sends the connection upgrade headers. |
| 131 | 131 | */ |
| 132 | - protected function upgrade (): void { |
|
| 133 | - $key = base64_encode(sha1($this->headers['sec-websocket-key'] . self::RFC_GUID, true)); |
|
| 132 | + protected function upgrade(): void { |
|
| 133 | + $key = base64_encode(sha1($this->headers['sec-websocket-key'].self::RFC_GUID, true)); |
|
| 134 | 134 | $this->client->write(implode("\r\n", [ |
| 135 | 135 | "HTTP/1.1 101 Switching Protocols", |
| 136 | 136 | "Connection: Upgrade", |
@@ -144,7 +144,7 @@ discard block |
||
| 144 | 144 | * |
| 145 | 145 | * @throws WebSocketError |
| 146 | 146 | */ |
| 147 | - protected function validate (): void { |
|
| 147 | + protected function validate(): void { |
|
| 148 | 148 | if (!( |
| 149 | 149 | $check = 'method = http 1.1' |
| 150 | 150 | and preg_match('/HTTP\/1\.1$/i', $this->method) |
@@ -101,19 +101,16 @@ |
||
| 101 | 101 | $value = trim($value); |
| 102 | 102 | if (isset($this->headers[$key])) { |
| 103 | 103 | $this->headers[$key] .= ', ' . $value; |
| 104 | - } |
|
| 105 | - else { |
|
| 104 | + } else { |
|
| 106 | 105 | $this->headers[$key] = $value; |
| 107 | 106 | } |
| 108 | 107 | } |
| 109 | 108 | $this->buffer = ''; // wipe the buffer |
| 110 | 109 | $this->validate(); |
| 111 | - } |
|
| 112 | - catch (WebSocketError $e) { // catch and respond with HTTP error and rethrow |
|
| 110 | + } catch (WebSocketError $e) { // catch and respond with HTTP error and rethrow |
|
| 113 | 111 | $this->client->write("HTTP/1.1 {$e->getCode()} WebSocket Handshake Failure\r\n\r\n"); |
| 114 | 112 | throw $e; |
| 115 | - } |
|
| 116 | - catch (Throwable $e) { // catch everything else and respond with HTTP 500 and rethrow |
|
| 113 | + } catch (Throwable $e) { // catch everything else and respond with HTTP 500 and rethrow |
|
| 117 | 114 | $this->client->write("HTTP/1.1 500 WebSocket Internal Error\r\n\r\n"); |
| 118 | 115 | throw $e; |
| 119 | 116 | } |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | * @param resource $resource |
| 53 | 53 | * @param WebSocketServer $server |
| 54 | 54 | */ |
| 55 | - public function __construct ($resource, WebSocketServer $server) { |
|
| 55 | + public function __construct($resource, WebSocketServer $server) { |
|
| 56 | 56 | parent::__construct($resource); |
| 57 | 57 | $this->server = $server; |
| 58 | 58 | $this->handshake = new Handshake($this); |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | * @param string $reason Sent to the peer, if code is >= 1000 |
| 78 | 78 | * @return $this |
| 79 | 79 | */ |
| 80 | - public function close (int $code = null, string $reason = '') { |
|
| 80 | + public function close(int $code = null, string $reason = '') { |
|
| 81 | 81 | try { |
| 82 | 82 | if ($code >= 1000 and $this->isOk()) { |
| 83 | 83 | $this->frameHandler->writeClose($code, $reason); |
@@ -94,39 +94,39 @@ discard block |
||
| 94 | 94 | /** |
| 95 | 95 | * @return FrameHandler |
| 96 | 96 | */ |
| 97 | - public function getFrameHandler (): FrameHandler { |
|
| 97 | + public function getFrameHandler(): FrameHandler { |
|
| 98 | 98 | return $this->frameHandler; |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | /** |
| 102 | 102 | * @return Handshake |
| 103 | 103 | */ |
| 104 | - public function getHandshake (): Handshake { |
|
| 104 | + public function getHandshake(): Handshake { |
|
| 105 | 105 | return $this->handshake; |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | /** |
| 109 | 109 | * @return WebSocketServer |
| 110 | 110 | */ |
| 111 | - public function getServer (): WebSocketServer { |
|
| 111 | + public function getServer(): WebSocketServer { |
|
| 112 | 112 | return $this->server; |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | /** |
| 116 | 116 | * @return int |
| 117 | 117 | */ |
| 118 | - public function getState (): int { |
|
| 118 | + public function getState(): int { |
|
| 119 | 119 | return $this->state; |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | - final public function isNegotiating (): bool { |
|
| 122 | + final public function isNegotiating(): bool { |
|
| 123 | 123 | return $this->state === self::STATE_HANDSHAKE; |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | /** |
| 127 | 127 | * @return bool |
| 128 | 128 | */ |
| 129 | - final public function isOk (): bool { |
|
| 129 | + final public function isOk(): bool { |
|
| 130 | 130 | return $this->state === self::STATE_OK; |
| 131 | 131 | } |
| 132 | 132 | |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | * @param string $binary |
| 139 | 139 | * @throws WebSocketError |
| 140 | 140 | */ |
| 141 | - public function onBinary (string $binary): void { |
|
| 141 | + public function onBinary(string $binary): void { |
|
| 142 | 142 | unset($binary); |
| 143 | 143 | throw new WebSocketError(Frame::CLOSE_UNHANDLED_DATA, "I don't handle binary data."); |
| 144 | 144 | } |
@@ -149,7 +149,7 @@ discard block |
||
| 149 | 149 | * @param int $code |
| 150 | 150 | * @param string $reason |
| 151 | 151 | */ |
| 152 | - public function onClose (int $code, string $reason): void { |
|
| 152 | + public function onClose(int $code, string $reason): void { |
|
| 153 | 153 | unset($code, $reason); |
| 154 | 154 | $this->close(); |
| 155 | 155 | } |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | * |
| 162 | 162 | * Closes the connection with a protocol-error frame. |
| 163 | 163 | */ |
| 164 | - final public function onOutOfBand (): void { |
|
| 164 | + final public function onOutOfBand(): void { |
|
| 165 | 165 | $this->close(Frame::CLOSE_PROTOCOL_ERROR, "Received out-of-band data."); |
| 166 | 166 | } |
| 167 | 167 | |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | * |
| 173 | 173 | * @param string $message |
| 174 | 174 | */ |
| 175 | - public function onPing (string $message): void { |
|
| 175 | + public function onPing(string $message): void { |
|
| 176 | 176 | $this->frameHandler->writePong($message); |
| 177 | 177 | } |
| 178 | 178 | |
@@ -183,7 +183,7 @@ discard block |
||
| 183 | 183 | * |
| 184 | 184 | * @param string $message |
| 185 | 185 | */ |
| 186 | - public function onPong (string $message): void { |
|
| 186 | + public function onPong(string $message): void { |
|
| 187 | 187 | // stub |
| 188 | 188 | } |
| 189 | 189 | |
@@ -193,7 +193,7 @@ discard block |
||
| 193 | 193 | * @throws WebSocketError |
| 194 | 194 | * @throws Throwable |
| 195 | 195 | */ |
| 196 | - public function onReadable (): void { |
|
| 196 | + public function onReadable(): void { |
|
| 197 | 197 | try { |
| 198 | 198 | if ($this->isNegotiating()) { |
| 199 | 199 | if ($this->handshake->onReadable()) { |
@@ -223,7 +223,7 @@ discard block |
||
| 223 | 223 | * If you have negotiated an extension during {@link Handshake}, |
| 224 | 224 | * claim the RSV bits here via {@link FrameReader::setRsv()} |
| 225 | 225 | */ |
| 226 | - protected function onStateOk (): void { |
|
| 226 | + protected function onStateOk(): void { |
|
| 227 | 227 | // stub |
| 228 | 228 | } |
| 229 | 229 | |
@@ -235,7 +235,7 @@ discard block |
||
| 235 | 235 | * @param string $text |
| 236 | 236 | * @throws WebSocketError |
| 237 | 237 | */ |
| 238 | - public function onText (string $text): void { |
|
| 238 | + public function onText(string $text): void { |
|
| 239 | 239 | unset($text); |
| 240 | 240 | throw new WebSocketError(Frame::CLOSE_UNHANDLED_DATA, "I don't handle text."); |
| 241 | 241 | } |
@@ -245,7 +245,7 @@ discard block |
||
| 245 | 245 | * |
| 246 | 246 | * @param string $binary |
| 247 | 247 | */ |
| 248 | - public function writeBinary (string $binary): void { |
|
| 248 | + public function writeBinary(string $binary): void { |
|
| 249 | 249 | $this->frameHandler->writeBinary($binary); |
| 250 | 250 | } |
| 251 | 251 | |
@@ -254,7 +254,7 @@ discard block |
||
| 254 | 254 | * |
| 255 | 255 | * @param string $text |
| 256 | 256 | */ |
| 257 | - public function writeText (string $text): void { |
|
| 257 | + public function writeText(string $text): void { |
|
| 258 | 258 | $this->frameHandler->writeText($text); |
| 259 | 259 | } |
| 260 | 260 | |
@@ -82,8 +82,7 @@ discard block |
||
| 82 | 82 | if ($code >= 1000 and $this->isOk()) { |
| 83 | 83 | $this->frameHandler->writeClose($code, $reason); |
| 84 | 84 | } |
| 85 | - } |
|
| 86 | - finally { |
|
| 85 | + } finally { |
|
| 87 | 86 | $this->server->remove($this); |
| 88 | 87 | parent::close(); |
| 89 | 88 | $this->state = self::STATE_CLOSED; |
@@ -200,16 +199,13 @@ discard block |
||
| 200 | 199 | $this->state = self::STATE_OK; |
| 201 | 200 | $this->onStateOk(); |
| 202 | 201 | } |
| 203 | - } |
|
| 204 | - elseif ($this->isOk()) { |
|
| 202 | + } elseif ($this->isOk()) { |
|
| 205 | 203 | $this->frameHandler->onReadable(); |
| 206 | 204 | } |
| 207 | - } |
|
| 208 | - catch (WebSocketError $e) { |
|
| 205 | + } catch (WebSocketError $e) { |
|
| 209 | 206 | $this->close($e->getCode(), $e->getMessage()); |
| 210 | 207 | throw $e; |
| 211 | - } |
|
| 212 | - catch (Throwable $e) { |
|
| 208 | + } catch (Throwable $e) { |
|
| 213 | 209 | $this->close(Frame::CLOSE_INTERNAL_ERROR); |
| 214 | 210 | throw $e; |
| 215 | 211 | } |
@@ -43,15 +43,15 @@ discard block |
||
| 43 | 43 | 0x0f => 'RESERVED CONTROL 0x0f', |
| 44 | 44 | ]; |
| 45 | 45 | |
| 46 | - const CLOSE_NORMAL = 1000; // mutual closure |
|
| 47 | - const CLOSE_INTERRUPT = 1001; // abrupt closure due to hangups, reboots, "going away" |
|
| 48 | - const CLOSE_PROTOCOL_ERROR = 1002; // invalid behavior / framing |
|
| 49 | - const CLOSE_UNHANDLED_DATA = 1003; // message handler doesn't want the payload |
|
| 50 | - const CLOSE_BAD_DATA = 1007; // message handler can't understand the payload |
|
| 51 | - const CLOSE_POLICY_VIOLATION = 1008; // generic "access denied" |
|
| 52 | - const CLOSE_TOO_LARGE = 1009; // unacceptable payload size |
|
| 53 | - const CLOSE_EXPECTATION = 1010; // peer closed because it wants extensions (listed in the reason) |
|
| 54 | - const CLOSE_INTERNAL_ERROR = 1011; // like http 500 |
|
| 46 | + const CLOSE_NORMAL = 1000; // mutual closure |
|
| 47 | + const CLOSE_INTERRUPT = 1001; // abrupt closure due to hangups, reboots, "going away" |
|
| 48 | + const CLOSE_PROTOCOL_ERROR = 1002; // invalid behavior / framing |
|
| 49 | + const CLOSE_UNHANDLED_DATA = 1003; // message handler doesn't want the payload |
|
| 50 | + const CLOSE_BAD_DATA = 1007; // message handler can't understand the payload |
|
| 51 | + const CLOSE_POLICY_VIOLATION = 1008; // generic "access denied" |
|
| 52 | + const CLOSE_TOO_LARGE = 1009; // unacceptable payload size |
|
| 53 | + const CLOSE_EXPECTATION = 1010; // peer closed because it wants extensions (listed in the reason) |
|
| 54 | + const CLOSE_INTERNAL_ERROR = 1011; // like http 500 |
|
| 55 | 55 | |
| 56 | 56 | /** |
| 57 | 57 | * @var bool |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | * @param int $opCode |
| 82 | 82 | * @param string $payload |
| 83 | 83 | */ |
| 84 | - public function __construct (bool $final, int $rsv, int $opCode, string $payload) { |
|
| 84 | + public function __construct(bool $final, int $rsv, int $opCode, string $payload) { |
|
| 85 | 85 | $this->final = $final; |
| 86 | 86 | $this->rsv = $rsv; |
| 87 | 87 | $this->opCode = $opCode; |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | * |
| 94 | 94 | * @return string |
| 95 | 95 | */ |
| 96 | - public function __toString () { |
|
| 96 | + public function __toString() { |
|
| 97 | 97 | if ($this->isClose()) { |
| 98 | 98 | return $this->getCloseReason(); |
| 99 | 99 | } |
@@ -107,7 +107,7 @@ discard block |
||
| 107 | 107 | * |
| 108 | 108 | * @return int |
| 109 | 109 | */ |
| 110 | - final public function getCloseCode (): int { |
|
| 110 | + final public function getCloseCode(): int { |
|
| 111 | 111 | $code = substr($this->payload, 0, 2); |
| 112 | 112 | return isset($code[1]) ? unpack('n', $code)[1] : self::CLOSE_NORMAL; |
| 113 | 113 | } |
@@ -119,126 +119,126 @@ discard block |
||
| 119 | 119 | * |
| 120 | 120 | * @return string |
| 121 | 121 | */ |
| 122 | - final public function getCloseReason (): string { |
|
| 122 | + final public function getCloseReason(): string { |
|
| 123 | 123 | return substr($this->payload, 2); |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | /** |
| 127 | 127 | * @return int |
| 128 | 128 | */ |
| 129 | - final public function getLength (): int { |
|
| 129 | + final public function getLength(): int { |
|
| 130 | 130 | return strlen($this->payload); |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | /** |
| 134 | 134 | * @return string |
| 135 | 135 | */ |
| 136 | - public function getName (): string { |
|
| 136 | + public function getName(): string { |
|
| 137 | 137 | return self::NAMES[$this->opCode]; |
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | /** |
| 141 | 141 | * @return int |
| 142 | 142 | */ |
| 143 | - final public function getOpCode (): int { |
|
| 143 | + final public function getOpCode(): int { |
|
| 144 | 144 | return $this->opCode; |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | /** |
| 148 | 148 | * @return string |
| 149 | 149 | */ |
| 150 | - final public function getPayload (): string { |
|
| 150 | + final public function getPayload(): string { |
|
| 151 | 151 | return $this->payload; |
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | /** |
| 155 | 155 | * @return int |
| 156 | 156 | */ |
| 157 | - final public function getRsv (): int { |
|
| 157 | + final public function getRsv(): int { |
|
| 158 | 158 | return $this->rsv; |
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | /** |
| 162 | 162 | * @return bool |
| 163 | 163 | */ |
| 164 | - final public function hasRsv1 (): bool { |
|
| 165 | - return (bool)($this->rsv & self::RSV1); |
|
| 164 | + final public function hasRsv1(): bool { |
|
| 165 | + return (bool) ($this->rsv & self::RSV1); |
|
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | /** |
| 169 | 169 | * @return bool |
| 170 | 170 | */ |
| 171 | - final public function hasRsv2 (): bool { |
|
| 172 | - return (bool)($this->rsv & self::RSV2); |
|
| 171 | + final public function hasRsv2(): bool { |
|
| 172 | + return (bool) ($this->rsv & self::RSV2); |
|
| 173 | 173 | } |
| 174 | 174 | |
| 175 | 175 | /** |
| 176 | 176 | * @return bool |
| 177 | 177 | */ |
| 178 | - final public function hasRsv3 (): bool { |
|
| 179 | - return (bool)($this->rsv & self::RSV3); |
|
| 178 | + final public function hasRsv3(): bool { |
|
| 179 | + return (bool) ($this->rsv & self::RSV3); |
|
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | /** |
| 183 | 183 | * @return bool |
| 184 | 184 | */ |
| 185 | - final public function isBinary (): bool { |
|
| 185 | + final public function isBinary(): bool { |
|
| 186 | 186 | return $this->opCode === self::OP_BINARY; |
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | /** |
| 190 | 190 | * @return bool |
| 191 | 191 | */ |
| 192 | - final public function isClose (): bool { |
|
| 192 | + final public function isClose(): bool { |
|
| 193 | 193 | return $this->opCode === self::OP_CLOSE; |
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | /** |
| 197 | 197 | * @return bool |
| 198 | 198 | */ |
| 199 | - final public function isContinuation (): bool { |
|
| 199 | + final public function isContinuation(): bool { |
|
| 200 | 200 | return $this->opCode === self::OP_CONTINUATION; |
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | /** |
| 204 | 204 | * @return bool |
| 205 | 205 | */ |
| 206 | - final public function isControl (): bool { |
|
| 206 | + final public function isControl(): bool { |
|
| 207 | 207 | return $this->opCode >= self::OP_CLOSE; |
| 208 | 208 | } |
| 209 | 209 | |
| 210 | 210 | /** |
| 211 | 211 | * @return bool |
| 212 | 212 | */ |
| 213 | - final public function isData (): bool { |
|
| 213 | + final public function isData(): bool { |
|
| 214 | 214 | return $this->opCode < self::OP_CLOSE; |
| 215 | 215 | } |
| 216 | 216 | |
| 217 | 217 | /** |
| 218 | 218 | * @return bool |
| 219 | 219 | */ |
| 220 | - final public function isFinal (): bool { |
|
| 220 | + final public function isFinal(): bool { |
|
| 221 | 221 | return $this->final; |
| 222 | 222 | } |
| 223 | 223 | |
| 224 | 224 | /** |
| 225 | 225 | * @return bool |
| 226 | 226 | */ |
| 227 | - final public function isPing (): bool { |
|
| 227 | + final public function isPing(): bool { |
|
| 228 | 228 | return $this->opCode === self::OP_PING; |
| 229 | 229 | } |
| 230 | 230 | |
| 231 | 231 | /** |
| 232 | 232 | * @return bool |
| 233 | 233 | */ |
| 234 | - final public function isPong (): bool { |
|
| 234 | + final public function isPong(): bool { |
|
| 235 | 235 | return $this->opCode === self::OP_PONG; |
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | /** |
| 239 | 239 | * @return bool |
| 240 | 240 | */ |
| 241 | - final public function isText (): bool { |
|
| 241 | + final public function isText(): bool { |
|
| 242 | 242 | return $this->opCode === self::OP_TEXT; |
| 243 | 243 | } |
| 244 | 244 | |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | /** |
| 63 | 63 | * @param WebSocketClient $client |
| 64 | 64 | */ |
| 65 | - public function __construct (WebSocketClient $client) { |
|
| 65 | + public function __construct(WebSocketClient $client) { |
|
| 66 | 66 | $this->client = $client; |
| 67 | 67 | } |
| 68 | 68 | |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | * @return null|Frame |
| 73 | 73 | * @throws WebSocketError |
| 74 | 74 | */ |
| 75 | - protected function getFrame (): ?Frame { |
|
| 75 | + protected function getFrame(): ?Frame { |
|
| 76 | 76 | // wait for the header |
| 77 | 77 | if (!$this->header ??= $this->getFrame_header()) { |
| 78 | 78 | return null; |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | * https://tools.ietf.org/html/rfc6455#section-5.2 |
| 111 | 111 | * @return null|array |
| 112 | 112 | */ |
| 113 | - protected function getFrame_header (): ?array { |
|
| 113 | + protected function getFrame_header(): ?array { |
|
| 114 | 114 | if (!preg_match(self::REGEXP, $this->buffer, $match)) { |
| 115 | 115 | if (strlen($this->buffer) >= 14) { // max head room |
| 116 | 116 | throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, 'Bad frame.'); |
@@ -166,7 +166,7 @@ discard block |
||
| 166 | 166 | * |
| 167 | 167 | * @return Generator|Frame[] |
| 168 | 168 | */ |
| 169 | - public function getFrames () { |
|
| 169 | + public function getFrames() { |
|
| 170 | 170 | // read into the buffer |
| 171 | 171 | $this->buffer .= $bytes = $this->client->recvAll(); |
| 172 | 172 | |
@@ -184,14 +184,14 @@ discard block |
||
| 184 | 184 | /** |
| 185 | 185 | * @return int |
| 186 | 186 | */ |
| 187 | - public function getMaxLength (): int { |
|
| 187 | + public function getMaxLength(): int { |
|
| 188 | 188 | return $this->maxLength; |
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | /** |
| 192 | 192 | * @return int |
| 193 | 193 | */ |
| 194 | - public function getRsv (): int { |
|
| 194 | + public function getRsv(): int { |
|
| 195 | 195 | return $this->rsv; |
| 196 | 196 | } |
| 197 | 197 | |
@@ -201,7 +201,7 @@ discard block |
||
| 201 | 201 | * @param string $payload |
| 202 | 202 | * @return Frame |
| 203 | 203 | */ |
| 204 | - protected function newFrame (string $payload): Frame { |
|
| 204 | + protected function newFrame(string $payload): Frame { |
|
| 205 | 205 | return new Frame($this->header['final'], $this->header['rsv'], $this->header['opCode'], $payload); |
| 206 | 206 | } |
| 207 | 207 | |
@@ -209,7 +209,7 @@ discard block |
||
| 209 | 209 | * @param int $bytes |
| 210 | 210 | * @return $this |
| 211 | 211 | */ |
| 212 | - public function setMaxLength (int $bytes) { |
|
| 212 | + public function setMaxLength(int $bytes) { |
|
| 213 | 213 | $this->maxLength = min(max(125, $bytes), 2 ** 63 - 1); |
| 214 | 214 | return $this; |
| 215 | 215 | } |
@@ -218,7 +218,7 @@ discard block |
||
| 218 | 218 | * @param int $rsv |
| 219 | 219 | * @return $this |
| 220 | 220 | */ |
| 221 | - public function setRsv (int $rsv) { |
|
| 221 | + public function setRsv(int $rsv) { |
|
| 222 | 222 | $this->rsv = $rsv; |
| 223 | 223 | return $this; |
| 224 | 224 | } |
@@ -141,19 +141,15 @@ |
||
| 141 | 141 | if ($badRsv = $rsv & ~$this->rsv) { |
| 142 | 142 | $badRsv = str_pad(base_convert($badRsv >> 4, 10, 2), 3, '0', STR_PAD_LEFT); |
| 143 | 143 | throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, "Received unknown RSV bits: 0b{$badRsv}"); |
| 144 | - } |
|
| 145 | - elseif ($opCode >= Frame::OP_CLOSE) { |
|
| 144 | + } elseif ($opCode >= Frame::OP_CLOSE) { |
|
| 146 | 145 | if ($opCode > Frame::OP_PONG) { |
| 147 | 146 | throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, "Received unsupported control frame ({$opCode})"); |
| 148 | - } |
|
| 149 | - elseif (!$final) { |
|
| 147 | + } elseif (!$final) { |
|
| 150 | 148 | throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, "Received fragmented control frame ({$opCode})"); |
| 151 | 149 | } |
| 152 | - } |
|
| 153 | - elseif ($opCode > Frame::OP_BINARY) { |
|
| 150 | + } elseif ($opCode > Frame::OP_BINARY) { |
|
| 154 | 151 | throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, "Received unsupported data frame ({$opCode})"); |
| 155 | - } |
|
| 156 | - elseif ($length > $this->maxLength) { |
|
| 152 | + } elseif ($length > $this->maxLength) { |
|
| 157 | 153 | throw new WebSocketError(Frame::CLOSE_TOO_LARGE, "Payload would exceed {$this->maxLength} bytes"); |
| 158 | 154 | } |
| 159 | 155 | |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | /** |
| 68 | 68 | * @param WebSocketClient $client |
| 69 | 69 | */ |
| 70 | - public function __construct (WebSocketClient $client) { |
|
| 70 | + public function __construct(WebSocketClient $client) { |
|
| 71 | 71 | $this->client = $client; |
| 72 | 72 | $this->reader = new FrameReader($client); |
| 73 | 73 | } |
@@ -75,21 +75,21 @@ discard block |
||
| 75 | 75 | /** |
| 76 | 76 | * @return int |
| 77 | 77 | */ |
| 78 | - public function getFragmentSize (): int { |
|
| 78 | + public function getFragmentSize(): int { |
|
| 79 | 79 | return $this->fragmentSize; |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | /** |
| 83 | 83 | * @return int |
| 84 | 84 | */ |
| 85 | - public function getMaxLength (): int { |
|
| 85 | + public function getMaxLength(): int { |
|
| 86 | 86 | return $this->maxLength; |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | /** |
| 90 | 90 | * @return bool |
| 91 | 91 | */ |
| 92 | - public function isStream (): bool { |
|
| 92 | + public function isStream(): bool { |
|
| 93 | 93 | return $this->stream; |
| 94 | 94 | } |
| 95 | 95 | |
@@ -102,7 +102,7 @@ discard block |
||
| 102 | 102 | * @param Frame $frame |
| 103 | 103 | * @throws WebSocketError |
| 104 | 104 | */ |
| 105 | - protected function onBinary (Frame $frame): void { |
|
| 105 | + protected function onBinary(Frame $frame): void { |
|
| 106 | 106 | if ($this->stream) { |
| 107 | 107 | $this->client->onBinary($frame->getPayload()); |
| 108 | 108 | } |
@@ -128,7 +128,7 @@ discard block |
||
| 128 | 128 | * |
| 129 | 129 | * @param Frame $frame |
| 130 | 130 | */ |
| 131 | - protected function onClose (Frame $frame): void { |
|
| 131 | + protected function onClose(Frame $frame): void { |
|
| 132 | 132 | $this->client->onClose($frame->getCloseCode(), $frame->getCloseReason()); |
| 133 | 133 | } |
| 134 | 134 | |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | * @param Frame $frame |
| 139 | 139 | * @throws WebSocketError |
| 140 | 140 | */ |
| 141 | - protected function onContinuation (Frame $frame): void { |
|
| 141 | + protected function onContinuation(Frame $frame): void { |
|
| 142 | 142 | if (!$this->continue) { |
| 143 | 143 | throw new WebSocketError( |
| 144 | 144 | Frame::CLOSE_PROTOCOL_ERROR, |
@@ -170,7 +170,7 @@ discard block |
||
| 170 | 170 | * |
| 171 | 171 | * @param Frame $frame |
| 172 | 172 | */ |
| 173 | - protected function onControl (Frame $frame): void { |
|
| 173 | + protected function onControl(Frame $frame): void { |
|
| 174 | 174 | if ($frame->isClose()) { |
| 175 | 175 | $this->onClose($frame); |
| 176 | 176 | } |
@@ -190,7 +190,7 @@ discard block |
||
| 190 | 190 | * |
| 191 | 191 | * @param Frame $frame |
| 192 | 192 | */ |
| 193 | - protected function onData (Frame $frame): void { |
|
| 193 | + protected function onData(Frame $frame): void { |
|
| 194 | 194 | // did we get a continuation? |
| 195 | 195 | if ($frame->isContinuation()) { |
| 196 | 196 | $this->onContinuation($frame); |
@@ -199,7 +199,7 @@ discard block |
||
| 199 | 199 | elseif ($this->continue) { |
| 200 | 200 | throw new WebSocketError( |
| 201 | 201 | Frame::CLOSE_PROTOCOL_ERROR, |
| 202 | - "Received interleaved {$frame->getName()} against existing " . Frame::NAMES[$this->continue], |
|
| 202 | + "Received interleaved {$frame->getName()} against existing ".Frame::NAMES[$this->continue], |
|
| 203 | 203 | $frame |
| 204 | 204 | ); |
| 205 | 205 | } |
@@ -226,7 +226,7 @@ discard block |
||
| 226 | 226 | * @param Frame $frame |
| 227 | 227 | * @throws WebSocketError |
| 228 | 228 | */ |
| 229 | - protected function onData_CheckLength (Frame $frame): void { |
|
| 229 | + protected function onData_CheckLength(Frame $frame): void { |
|
| 230 | 230 | if (strlen($this->buffer) + $frame->getLength() > $this->maxLength) { |
| 231 | 231 | throw new WebSocketError( |
| 232 | 232 | Frame::CLOSE_TOO_LARGE, |
@@ -245,7 +245,7 @@ discard block |
||
| 245 | 245 | * |
| 246 | 246 | * @param Frame $frame |
| 247 | 247 | */ |
| 248 | - public function onFrame (Frame $frame): void { |
|
| 248 | + public function onFrame(Frame $frame): void { |
|
| 249 | 249 | if ($frame->isControl()) { |
| 250 | 250 | $this->onControl($frame); |
| 251 | 251 | } |
@@ -259,7 +259,7 @@ discard block |
||
| 259 | 259 | * |
| 260 | 260 | * @param Frame $frame |
| 261 | 261 | */ |
| 262 | - protected function onPing (Frame $frame): void { |
|
| 262 | + protected function onPing(Frame $frame): void { |
|
| 263 | 263 | $this->client->onPing($frame->getPayload()); |
| 264 | 264 | } |
| 265 | 265 | |
@@ -268,14 +268,14 @@ discard block |
||
| 268 | 268 | * |
| 269 | 269 | * @param Frame $frame |
| 270 | 270 | */ |
| 271 | - protected function onPong (Frame $frame): void { |
|
| 271 | + protected function onPong(Frame $frame): void { |
|
| 272 | 272 | $this->client->onPong($frame->getPayload()); |
| 273 | 273 | } |
| 274 | 274 | |
| 275 | 275 | /** |
| 276 | 276 | * Uses {@link FrameReader} to read frames and passes them off to {@link onFrame()} |
| 277 | 277 | */ |
| 278 | - public function onReadable (): void { |
|
| 278 | + public function onReadable(): void { |
|
| 279 | 279 | foreach ($this->reader->getFrames() as $frame) { |
| 280 | 280 | $this->onFrame($frame); |
| 281 | 281 | } |
@@ -288,7 +288,7 @@ discard block |
||
| 288 | 288 | * @param Frame $frame |
| 289 | 289 | * @throws WebSocketError |
| 290 | 290 | */ |
| 291 | - protected function onText (Frame $frame): void { |
|
| 291 | + protected function onText(Frame $frame): void { |
|
| 292 | 292 | $this->onData_CheckLength($frame); |
| 293 | 293 | $this->buffer .= $frame->getPayload(); |
| 294 | 294 | if ($frame->isFinal()) { |
@@ -305,7 +305,7 @@ discard block |
||
| 305 | 305 | * @param int $bytes |
| 306 | 306 | * @return $this |
| 307 | 307 | */ |
| 308 | - public function setFragmentSize (int $bytes) { |
|
| 308 | + public function setFragmentSize(int $bytes) { |
|
| 309 | 309 | $this->fragmentSize = $bytes; |
| 310 | 310 | return $this; |
| 311 | 311 | } |
@@ -314,7 +314,7 @@ discard block |
||
| 314 | 314 | * @param int $bytes |
| 315 | 315 | * @return $this |
| 316 | 316 | */ |
| 317 | - public function setMaxLength (int $bytes) { |
|
| 317 | + public function setMaxLength(int $bytes) { |
|
| 318 | 318 | $this->maxLength = $bytes; |
| 319 | 319 | return $this; |
| 320 | 320 | } |
@@ -323,7 +323,7 @@ discard block |
||
| 323 | 323 | * @param bool $stream |
| 324 | 324 | * @return $this |
| 325 | 325 | */ |
| 326 | - public function setStream (bool $stream) { |
|
| 326 | + public function setStream(bool $stream) { |
|
| 327 | 327 | $this->stream = $stream; |
| 328 | 328 | return $this; |
| 329 | 329 | } |
@@ -334,7 +334,7 @@ discard block |
||
| 334 | 334 | * @param int $opCode |
| 335 | 335 | * @param string $payload |
| 336 | 336 | */ |
| 337 | - public function write (int $opCode, string $payload): void { |
|
| 337 | + public function write(int $opCode, string $payload): void { |
|
| 338 | 338 | $offset = 0; |
| 339 | 339 | $total = strlen($payload); |
| 340 | 340 | do { |
@@ -350,7 +350,7 @@ discard block |
||
| 350 | 350 | /** |
| 351 | 351 | * @param string $payload |
| 352 | 352 | */ |
| 353 | - public function writeBinary (string $payload): void { |
|
| 353 | + public function writeBinary(string $payload): void { |
|
| 354 | 354 | $this->write(Frame::OP_BINARY, $payload); |
| 355 | 355 | } |
| 356 | 356 | |
@@ -358,8 +358,8 @@ discard block |
||
| 358 | 358 | * @param int $code |
| 359 | 359 | * @param string $reason |
| 360 | 360 | */ |
| 361 | - public function writeClose (int $code = Frame::CLOSE_NORMAL, string $reason = ''): void { |
|
| 362 | - $this->writeFrame(true, Frame::OP_CLOSE, pack('n', $code) . $reason); |
|
| 361 | + public function writeClose(int $code = Frame::CLOSE_NORMAL, string $reason = ''): void { |
|
| 362 | + $this->writeFrame(true, Frame::OP_CLOSE, pack('n', $code).$reason); |
|
| 363 | 363 | } |
| 364 | 364 | |
| 365 | 365 | /** |
@@ -369,7 +369,7 @@ discard block |
||
| 369 | 369 | * @param int $opCode |
| 370 | 370 | * @param string $payload |
| 371 | 371 | */ |
| 372 | - protected function writeFrame (bool $final, int $opCode, string $payload): void { |
|
| 372 | + protected function writeFrame(bool $final, int $opCode, string $payload): void { |
|
| 373 | 373 | if ($opCode & 0x08 and !$final) { |
| 374 | 374 | throw new LogicException("Would have sent a fragmented control frame ({$opCode}) {$payload}"); |
| 375 | 375 | } |
@@ -386,27 +386,27 @@ discard block |
||
| 386 | 386 | else { |
| 387 | 387 | $head .= chr($length); |
| 388 | 388 | } |
| 389 | - $this->client->write($head . $payload); |
|
| 389 | + $this->client->write($head.$payload); |
|
| 390 | 390 | } |
| 391 | 391 | |
| 392 | 392 | /** |
| 393 | 393 | * @param string $payload |
| 394 | 394 | */ |
| 395 | - public function writePing (string $payload = ''): void { |
|
| 395 | + public function writePing(string $payload = ''): void { |
|
| 396 | 396 | $this->writeFrame(true, Frame::OP_PING, $payload); |
| 397 | 397 | } |
| 398 | 398 | |
| 399 | 399 | /** |
| 400 | 400 | * @param string $payload |
| 401 | 401 | */ |
| 402 | - public function writePong (string $payload = ''): void { |
|
| 402 | + public function writePong(string $payload = ''): void { |
|
| 403 | 403 | $this->writeFrame(true, Frame::OP_PONG, $payload); |
| 404 | 404 | } |
| 405 | 405 | |
| 406 | 406 | /** |
| 407 | 407 | * @param string $payload |
| 408 | 408 | */ |
| 409 | - public function writeText (string $payload): void { |
|
| 409 | + public function writeText(string $payload): void { |
|
| 410 | 410 | $this->write(Frame::OP_TEXT, $payload); |
| 411 | 411 | } |
| 412 | 412 | } |
| 413 | 413 | \ No newline at end of file |
@@ -105,8 +105,7 @@ discard block |
||
| 105 | 105 | protected function onBinary (Frame $frame): void { |
| 106 | 106 | if ($this->stream) { |
| 107 | 107 | $this->client->onBinary($frame->getPayload()); |
| 108 | - } |
|
| 109 | - else { |
|
| 108 | + } else { |
|
| 110 | 109 | $this->onData_CheckLength($frame); |
| 111 | 110 | $this->buffer .= $frame->getPayload(); |
| 112 | 111 | if ($frame->isFinal()) { |
@@ -149,12 +148,10 @@ discard block |
||
| 149 | 148 | try { |
| 150 | 149 | if ($this->continue === Frame::OP_TEXT) { |
| 151 | 150 | $this->onText($frame); |
| 152 | - } |
|
| 153 | - else { |
|
| 151 | + } else { |
|
| 154 | 152 | $this->onBinary($frame); |
| 155 | 153 | } |
| 156 | - } |
|
| 157 | - finally { |
|
| 154 | + } finally { |
|
| 158 | 155 | if ($frame->isFinal()) { |
| 159 | 156 | $this->continue = null; |
| 160 | 157 | } |
@@ -173,14 +170,11 @@ discard block |
||
| 173 | 170 | protected function onControl (Frame $frame): void { |
| 174 | 171 | if ($frame->isClose()) { |
| 175 | 172 | $this->onClose($frame); |
| 176 | - } |
|
| 177 | - elseif ($frame->isPing()) { |
|
| 173 | + } elseif ($frame->isPing()) { |
|
| 178 | 174 | $this->onPing($frame); |
| 179 | - } |
|
| 180 | - elseif ($frame->isPong()) { |
|
| 175 | + } elseif ($frame->isPong()) { |
|
| 181 | 176 | $this->onPong($frame); |
| 182 | - } |
|
| 183 | - else { |
|
| 177 | + } else { |
|
| 184 | 178 | throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, "Unsupported control frame.", $frame); |
| 185 | 179 | } |
| 186 | 180 | } |
@@ -248,8 +242,7 @@ discard block |
||
| 248 | 242 | public function onFrame (Frame $frame): void { |
| 249 | 243 | if ($frame->isControl()) { |
| 250 | 244 | $this->onControl($frame); |
| 251 | - } |
|
| 252 | - else { |
|
| 245 | + } else { |
|
| 253 | 246 | $this->onData($frame); |
| 254 | 247 | } |
| 255 | 248 | } |
@@ -378,12 +371,10 @@ discard block |
||
| 378 | 371 | if ($length > 65535) { |
| 379 | 372 | $head .= chr(127); |
| 380 | 373 | $head .= pack('J', $length); |
| 381 | - } |
|
| 382 | - elseif ($length >= 126) { |
|
| 374 | + } elseif ($length >= 126) { |
|
| 383 | 375 | $head .= chr(126); |
| 384 | 376 | $head .= pack('n', $length); |
| 385 | - } |
|
| 386 | - else { |
|
| 377 | + } else { |
|
| 387 | 378 | $head .= chr($length); |
| 388 | 379 | } |
| 389 | 380 | $this->client->write($head . $payload); |