@@ -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,7 +74,7 @@ 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 | 80 | $this->getFrameHandler()->writeClose($code, $reason); |
@@ -91,49 +91,49 @@ discard block |
||
| 91 | 91 | /** |
| 92 | 92 | * @return FrameHandler |
| 93 | 93 | */ |
| 94 | - public function getFrameHandler () { |
|
| 94 | + public function getFrameHandler() { |
|
| 95 | 95 | return $this->frameHandler ?? $this->frameHandler = new FrameHandler($this); |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | /** |
| 99 | 99 | * @return FrameReader |
| 100 | 100 | */ |
| 101 | - public function getFrameReader () { |
|
| 101 | + public function getFrameReader() { |
|
| 102 | 102 | return $this->frameReader ?? $this->frameReader = new FrameReader($this); |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | /** |
| 106 | 106 | * @return HandShake |
| 107 | 107 | */ |
| 108 | - public function getHandshake () { |
|
| 108 | + public function getHandshake() { |
|
| 109 | 109 | return $this->handshake ?? $this->handshake = new HandShake($this); |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | /** |
| 113 | 113 | * @return MessageHandler |
| 114 | 114 | */ |
| 115 | - public function getMessageHandler () { |
|
| 115 | + public function getMessageHandler() { |
|
| 116 | 116 | return $this->messageHandler ?? $this->messageHandler = new MessageHandler($this); |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | /** |
| 120 | 120 | * @return WebSocketServer |
| 121 | 121 | */ |
| 122 | - public function getServer () { |
|
| 122 | + public function getServer() { |
|
| 123 | 123 | return $this->server; |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | /** |
| 127 | 127 | * @return int |
| 128 | 128 | */ |
| 129 | - public function getState (): int { |
|
| 129 | + public function getState(): int { |
|
| 130 | 130 | return $this->state; |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | /** |
| 134 | 134 | * @return bool |
| 135 | 135 | */ |
| 136 | - final public function isOk (): bool { |
|
| 136 | + final public function isOk(): bool { |
|
| 137 | 137 | return $this->state === self::STATE_OK; |
| 138 | 138 | } |
| 139 | 139 | |
@@ -142,7 +142,7 @@ discard block |
||
| 142 | 142 | * |
| 143 | 143 | * The RFC says the connection must be dropped if any unsupported activity occurs. |
| 144 | 144 | */ |
| 145 | - final public function onOutOfBand (): void { |
|
| 145 | + final public function onOutOfBand(): void { |
|
| 146 | 146 | $this->close(Frame::CLOSE_PROTOCOL_ERROR, "Received out-of-band data."); |
| 147 | 147 | } |
| 148 | 148 | |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | * |
| 152 | 152 | * @throws Exception |
| 153 | 153 | */ |
| 154 | - public function onReadable (): void { |
|
| 154 | + public function onReadable(): void { |
|
| 155 | 155 | if (!strlen($this->recv(1, MSG_PEEK))) { // peer has shut down writing, or closed. |
| 156 | 156 | $this->close(); |
| 157 | 157 | return; |
@@ -187,7 +187,7 @@ discard block |
||
| 187 | 187 | /** |
| 188 | 188 | * Stub. |
| 189 | 189 | */ |
| 190 | - protected function onStateOk (): void { |
|
| 190 | + protected function onStateOk(): void { |
|
| 191 | 191 | |
| 192 | 192 | } |
| 193 | 193 | |
@@ -195,7 +195,7 @@ discard block |
||
| 195 | 195 | * @param FrameHandler $frameHandler |
| 196 | 196 | * @return $this |
| 197 | 197 | */ |
| 198 | - public function setFrameHandler (FrameHandler $frameHandler) { |
|
| 198 | + public function setFrameHandler(FrameHandler $frameHandler) { |
|
| 199 | 199 | $this->frameHandler = $frameHandler; |
| 200 | 200 | return $this; |
| 201 | 201 | } |
@@ -204,7 +204,7 @@ discard block |
||
| 204 | 204 | * @param FrameReader $frameReader |
| 205 | 205 | * @return $this |
| 206 | 206 | */ |
| 207 | - public function setFrameReader (FrameReader $frameReader) { |
|
| 207 | + public function setFrameReader(FrameReader $frameReader) { |
|
| 208 | 208 | $this->frameReader = $frameReader; |
| 209 | 209 | return $this; |
| 210 | 210 | } |
@@ -213,7 +213,7 @@ discard block |
||
| 213 | 213 | * @param MessageHandler $messageHandler |
| 214 | 214 | * @return $this |
| 215 | 215 | */ |
| 216 | - public function setMessageHandler (MessageHandler $messageHandler) { |
|
| 216 | + public function setMessageHandler(MessageHandler $messageHandler) { |
|
| 217 | 217 | $this->messageHandler = $messageHandler; |
| 218 | 218 | return $this; |
| 219 | 219 | } |
@@ -80,8 +80,7 @@ discard block |
||
| 80 | 80 | $this->getFrameHandler()->writeClose($code, $reason); |
| 81 | 81 | $this->shutdown(self::CH_WRITE); |
| 82 | 82 | } |
| 83 | - } |
|
| 84 | - finally { |
|
| 83 | + } finally { |
|
| 85 | 84 | $this->state = self::STATE_CLOSE; |
| 86 | 85 | $this->server->remove($this); |
| 87 | 86 | parent::close(); |
@@ -173,12 +172,10 @@ discard block |
||
| 173 | 172 | case self::STATE_CLOSE: |
| 174 | 173 | return; |
| 175 | 174 | } |
| 176 | - } |
|
| 177 | - catch (WebSocketError $e) { |
|
| 175 | + } catch (WebSocketError $e) { |
|
| 178 | 176 | $this->close($e->getCode(), $e->getMessage()); |
| 179 | 177 | throw $e; |
| 180 | - } |
|
| 181 | - catch (Exception $e) { |
|
| 178 | + } catch (Exception $e) { |
|
| 182 | 179 | $this->close(Frame::CLOSE_INTERNAL_ERROR); |
| 183 | 180 | throw $e; |
| 184 | 181 | } |
@@ -12,14 +12,14 @@ discard block |
||
| 12 | 12 | /** |
| 13 | 13 | * @param WebSocketClient $client |
| 14 | 14 | */ |
| 15 | - public function __construct (WebSocketClient $client) { |
|
| 15 | + public function __construct(WebSocketClient $client) { |
|
| 16 | 16 | $this->client = $client; |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | 20 | * @param string $binary |
| 21 | 21 | */ |
| 22 | - public function onBinary (string $binary): void { |
|
| 22 | + public function onBinary(string $binary): void { |
|
| 23 | 23 | unset($binary); |
| 24 | 24 | throw new WebSocketError(Frame::CLOSE_UNHANDLED_DATA, "I don't handle binary data."); |
| 25 | 25 | } |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | /** |
| 28 | 28 | * @param string $text |
| 29 | 29 | */ |
| 30 | - public function onText (string $text): void { |
|
| 30 | + public function onText(string $text): void { |
|
| 31 | 31 | $this->onText_CheckUtf8($text); |
| 32 | 32 | throw new WebSocketError(Frame::CLOSE_UNHANDLED_DATA, "I don't handle text."); |
| 33 | 33 | } |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | * |
| 38 | 38 | * @param string $text |
| 39 | 39 | */ |
| 40 | - protected function onText_CheckUtf8 (string $text): void { |
|
| 40 | + protected function onText_CheckUtf8(string $text): void { |
|
| 41 | 41 | if (!mb_detect_encoding($text, 'UTF-8', true)) { |
| 42 | 42 | throw new WebSocketError(Frame::CLOSE_BAD_DATA, "Received TEXT is not UTF-8."); |
| 43 | 43 | } |
@@ -46,14 +46,14 @@ discard block |
||
| 46 | 46 | */ |
| 47 | 47 | protected $maxLength = 10 * 1024 * 1024; |
| 48 | 48 | |
| 49 | - public function __construct (WebSocketClient $client) { |
|
| 49 | + public function __construct(WebSocketClient $client) { |
|
| 50 | 50 | $this->client = $client; |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | /** |
| 54 | 54 | * @return Frame|null |
| 55 | 55 | */ |
| 56 | - protected function getFrame () { |
|
| 56 | + protected function getFrame() { |
|
| 57 | 57 | if (!$this->head) { |
| 58 | 58 | if (preg_match(self::REGEXP, $this->buffer, $head)) { |
| 59 | 59 | [, $op, $len] = unpack('C2', $head[0]); |
@@ -95,7 +95,7 @@ discard block |
||
| 95 | 95 | * |
| 96 | 96 | * @return Generator|Frame[] |
| 97 | 97 | */ |
| 98 | - public function getFrames () { |
|
| 98 | + public function getFrames() { |
|
| 99 | 99 | $this->buffer .= $this->client->recvAll(); |
| 100 | 100 | while ($frame = $this->getFrame()) { |
| 101 | 101 | yield $frame; |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | /** |
| 106 | 106 | * @return int |
| 107 | 107 | */ |
| 108 | - public function getMaxLength (): int { |
|
| 108 | + public function getMaxLength(): int { |
|
| 109 | 109 | return $this->maxLength; |
| 110 | 110 | } |
| 111 | 111 | |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | * @param int $bytes |
| 114 | 114 | * @return $this |
| 115 | 115 | */ |
| 116 | - public function setMaxLength (int $bytes) { |
|
| 116 | + public function setMaxLength(int $bytes) { |
|
| 117 | 117 | if ($bytes < self::MAX_LENGTH_RANGE[0] or $bytes > self::MAX_LENGTH_RANGE[1]) { |
| 118 | 118 | throw new InvalidArgumentException('Max length must be within range [125,2^63-1]'); |
| 119 | 119 | } |
@@ -126,7 +126,7 @@ discard block |
||
| 126 | 126 | * |
| 127 | 127 | * @throws WebSocketError |
| 128 | 128 | */ |
| 129 | - protected function validate (): void { |
|
| 129 | + protected function validate(): void { |
|
| 130 | 130 | if ($this->head['length'] > $this->maxLength) { |
| 131 | 131 | throw new WebSocketError(Frame::CLOSE_TOO_LARGE, "Payload would exceed {$this->maxLength} bytes"); |
| 132 | 132 | } |
@@ -67,11 +67,9 @@ discard block |
||
| 67 | 67 | ]; |
| 68 | 68 | $this->buffer = substr($this->buffer, strlen($head[0])); |
| 69 | 69 | $this->validate(); |
| 70 | - } |
|
| 71 | - elseif (strlen($this->buffer) >= 14) { // max head room |
|
| 70 | + } elseif (strlen($this->buffer) >= 14) { // max head room |
|
| 72 | 71 | throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, 'Bad frame.'); |
| 73 | - } |
|
| 74 | - else { |
|
| 72 | + } else { |
|
| 75 | 73 | return null; |
| 76 | 74 | } |
| 77 | 75 | } |
@@ -139,8 +137,7 @@ discard block |
||
| 139 | 137 | if (!$this->head['final']) { |
| 140 | 138 | throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, "Received fragmented {$name}"); |
| 141 | 139 | } |
| 142 | - } |
|
| 143 | - elseif ($opCode > Frame::OP_BINARY) { // data |
|
| 140 | + } elseif ($opCode > Frame::OP_BINARY) { // data |
|
| 144 | 141 | throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, "Received {$name}"); |
| 145 | 142 | } |
| 146 | 143 | } |
@@ -93,7 +93,7 @@ |
||
| 93 | 93 | /** |
| 94 | 94 | * Constructs and yields all available frames from the peer. |
| 95 | 95 | * |
| 96 | - * @return Generator|Frame[] |
|
| 96 | + * @return Generator |
|
| 97 | 97 | */ |
| 98 | 98 | public function getFrames () { |
| 99 | 99 | $this->buffer .= $this->client->recvAll(); |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | * |
| 19 | 19 | * @return string |
| 20 | 20 | */ |
| 21 | - public function __toString () { |
|
| 21 | + public function __toString() { |
|
| 22 | 22 | try { |
| 23 | 23 | return implode(':', $this->getSockName()); |
| 24 | 24 | } |
@@ -40,7 +40,7 @@ discard block |
||
| 40 | 40 | * @return $this |
| 41 | 41 | * @throws SocketError |
| 42 | 42 | */ |
| 43 | - public function bind (string $address, int $port = 0) { |
|
| 43 | + public function bind(string $address, int $port = 0) { |
|
| 44 | 44 | if (!@socket_bind($this->resource, $address, $port)) { |
| 45 | 45 | throw new SocketError($this->resource, SOCKET_EOPNOTSUPP); |
| 46 | 46 | } |
@@ -21,8 +21,7 @@ |
||
| 21 | 21 | public function __toString () { |
| 22 | 22 | try { |
| 23 | 23 | return implode(':', $this->getSockName()); |
| 24 | - } |
|
| 25 | - catch (Throwable $e) { |
|
| 24 | + } catch (Throwable $e) { |
|
| 26 | 25 | return "?{$this->resource}"; |
| 27 | 26 | } |
| 28 | 27 | } |
@@ -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 () { |
|
| 28 | + public function accept() { |
|
| 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) { |
|
| 61 | + protected function newClient($resource) { |
|
| 62 | 62 | return new StreamClient($resource); |
| 63 | 63 | } |
| 64 | 64 | |
@@ -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 = null, int &$port = 0): string { |
|
| 31 | + public function recv(int $length, int $flags = 0, string &$name = null, 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 |
@@ -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 | } |
@@ -46,21 +46,21 @@ discard block |
||
| 46 | 46 | */ |
| 47 | 47 | protected $maxLength = 10 * 1024 * 1024; |
| 48 | 48 | |
| 49 | - public function __construct (WebSocketClient $client) { |
|
| 49 | + public function __construct(WebSocketClient $client) { |
|
| 50 | 50 | $this->client = $client; |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | /** |
| 54 | 54 | * @return int |
| 55 | 55 | */ |
| 56 | - public function getFragmentSize (): int { |
|
| 56 | + public function getFragmentSize(): int { |
|
| 57 | 57 | return $this->fragmentSize; |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | /** |
| 61 | 61 | * @return int |
| 62 | 62 | */ |
| 63 | - public function getMaxLength (): int { |
|
| 63 | + public function getMaxLength(): int { |
|
| 64 | 64 | return $this->maxLength; |
| 65 | 65 | } |
| 66 | 66 | |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | * @param Frame $binary |
| 73 | 73 | * @throws WebSocketError |
| 74 | 74 | */ |
| 75 | - protected function onBinary (Frame $binary): void { |
|
| 75 | + protected function onBinary(Frame $binary): void { |
|
| 76 | 76 | $this->buffer .= $binary->getPayload(); |
| 77 | 77 | if ($binary->isFinal()) { |
| 78 | 78 | $message = $this->buffer; |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | * |
| 93 | 93 | * @param Frame $close |
| 94 | 94 | */ |
| 95 | - protected function onClose (Frame $close): void { |
|
| 95 | + protected function onClose(Frame $close): void { |
|
| 96 | 96 | $this->client->close($close->getCloseCode()); |
| 97 | 97 | } |
| 98 | 98 | |
@@ -102,7 +102,7 @@ discard block |
||
| 102 | 102 | * @param Frame $frame |
| 103 | 103 | * @throws WebSocketError |
| 104 | 104 | */ |
| 105 | - protected function onContinue (Frame $frame): void { |
|
| 105 | + protected function onContinue(Frame $frame): void { |
|
| 106 | 106 | try { |
| 107 | 107 | switch ($this->continue) { |
| 108 | 108 | case Frame::OP_TEXT: |
@@ -135,7 +135,7 @@ discard block |
||
| 135 | 135 | * |
| 136 | 136 | * @param Frame $control |
| 137 | 137 | */ |
| 138 | - protected function onControl (Frame $control): void { |
|
| 138 | + protected function onControl(Frame $control): void { |
|
| 139 | 139 | if ($control->isClose()) { |
| 140 | 140 | $this->onClose($control); |
| 141 | 141 | } |
@@ -152,7 +152,7 @@ discard block |
||
| 152 | 152 | * |
| 153 | 153 | * @param Frame $data |
| 154 | 154 | */ |
| 155 | - protected function onData (Frame $data): void { |
|
| 155 | + protected function onData(Frame $data): void { |
|
| 156 | 156 | $this->onData_SetContinue($data); |
| 157 | 157 | if ($data->isText()) { |
| 158 | 158 | $this->onText($data); |
@@ -162,7 +162,7 @@ discard block |
||
| 162 | 162 | } |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | - protected function onData_SetContinue (Frame $data): void { |
|
| 165 | + protected function onData_SetContinue(Frame $data): void { |
|
| 166 | 166 | if ($this->continue) { |
| 167 | 167 | $existing = Frame::NAMES[$this->continue]; |
| 168 | 168 | throw new WebSocketError( |
@@ -183,7 +183,7 @@ discard block |
||
| 183 | 183 | * |
| 184 | 184 | * @param Frame $frame |
| 185 | 185 | */ |
| 186 | - public function onFrame (Frame $frame): void { |
|
| 186 | + public function onFrame(Frame $frame): void { |
|
| 187 | 187 | $this->onFrame_CheckRsv($frame); |
| 188 | 188 | $this->onFrame_CheckLength($frame); |
| 189 | 189 | if ($frame->isControl()) { |
@@ -200,7 +200,7 @@ discard block |
||
| 200 | 200 | /** |
| 201 | 201 | * @param Frame $frame |
| 202 | 202 | */ |
| 203 | - protected function onFrame_CheckLength (Frame $frame): void { |
|
| 203 | + protected function onFrame_CheckLength(Frame $frame): void { |
|
| 204 | 204 | if ($frame->isData()) { |
| 205 | 205 | $length = strlen($this->buffer); |
| 206 | 206 | if ($length + $frame->getLength() > $this->maxLength) { |
@@ -219,7 +219,7 @@ discard block |
||
| 219 | 219 | * @param Frame $frame |
| 220 | 220 | * @throws WebSocketError |
| 221 | 221 | */ |
| 222 | - protected function onFrame_CheckRsv (Frame $frame): void { |
|
| 222 | + protected function onFrame_CheckRsv(Frame $frame): void { |
|
| 223 | 223 | if ($badRsv = $frame->getRsv() & ~$this->client->getHandshake()->getRsv()) { |
| 224 | 224 | $badRsv = str_pad(base_convert($badRsv >> 4, 10, 2), 3, '0', STR_PAD_LEFT); |
| 225 | 225 | throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, "Received unknown RSV bits: 0b{$badRsv}"); |
@@ -233,7 +233,7 @@ discard block |
||
| 233 | 233 | * |
| 234 | 234 | * @param Frame $ping |
| 235 | 235 | */ |
| 236 | - protected function onPing (Frame $ping): void { |
|
| 236 | + protected function onPing(Frame $ping): void { |
|
| 237 | 237 | $this->writePong($ping->getPayload()); |
| 238 | 238 | } |
| 239 | 239 | |
@@ -244,7 +244,7 @@ discard block |
||
| 244 | 244 | * |
| 245 | 245 | * @param Frame $pong |
| 246 | 246 | */ |
| 247 | - protected function onPong (Frame $pong): void { |
|
| 247 | + protected function onPong(Frame $pong): void { |
|
| 248 | 248 | // stub |
| 249 | 249 | } |
| 250 | 250 | |
@@ -256,7 +256,7 @@ discard block |
||
| 256 | 256 | * @param Frame $text |
| 257 | 257 | * @throws WebSocketError |
| 258 | 258 | */ |
| 259 | - protected function onText (Frame $text): void { |
|
| 259 | + protected function onText(Frame $text): void { |
|
| 260 | 260 | $this->buffer .= $text->getPayload(); |
| 261 | 261 | if ($text->isFinal()) { |
| 262 | 262 | $message = $this->buffer; |
@@ -269,7 +269,7 @@ discard block |
||
| 269 | 269 | * @param int $bytes |
| 270 | 270 | * @return $this |
| 271 | 271 | */ |
| 272 | - public function setFragmentSize (int $bytes) { |
|
| 272 | + public function setFragmentSize(int $bytes) { |
|
| 273 | 273 | $this->fragmentSize = $bytes; |
| 274 | 274 | return $this; |
| 275 | 275 | } |
@@ -278,7 +278,7 @@ discard block |
||
| 278 | 278 | * @param int $bytes |
| 279 | 279 | * @return $this |
| 280 | 280 | */ |
| 281 | - public function setMaxLength (int $bytes) { |
|
| 281 | + public function setMaxLength(int $bytes) { |
|
| 282 | 282 | $this->maxLength = $bytes; |
| 283 | 283 | return $this; |
| 284 | 284 | } |
@@ -289,7 +289,7 @@ discard block |
||
| 289 | 289 | * @param int $opCode |
| 290 | 290 | * @param string $payload |
| 291 | 291 | */ |
| 292 | - public function write (int $opCode, string $payload): void { |
|
| 292 | + public function write(int $opCode, string $payload): void { |
|
| 293 | 293 | $offset = 0; |
| 294 | 294 | $total = strlen($payload); |
| 295 | 295 | do { |
@@ -305,7 +305,7 @@ discard block |
||
| 305 | 305 | /** |
| 306 | 306 | * @param string $payload |
| 307 | 307 | */ |
| 308 | - public function writeBinary (string $payload): void { |
|
| 308 | + public function writeBinary(string $payload): void { |
|
| 309 | 309 | $this->write(Frame::OP_BINARY, $payload); |
| 310 | 310 | } |
| 311 | 311 | |
@@ -313,8 +313,8 @@ discard block |
||
| 313 | 313 | * @param int $code |
| 314 | 314 | * @param string $reason |
| 315 | 315 | */ |
| 316 | - public function writeClose (int $code = Frame::CLOSE_NORMAL, string $reason = ''): void { |
|
| 317 | - $this->writeFrame(true, Frame::OP_CLOSE, pack('n', $code) . $reason); |
|
| 316 | + public function writeClose(int $code = Frame::CLOSE_NORMAL, string $reason = ''): void { |
|
| 317 | + $this->writeFrame(true, Frame::OP_CLOSE, pack('n', $code).$reason); |
|
| 318 | 318 | } |
| 319 | 319 | |
| 320 | 320 | /** |
@@ -324,7 +324,7 @@ discard block |
||
| 324 | 324 | * @param int $opCode |
| 325 | 325 | * @param string $payload |
| 326 | 326 | */ |
| 327 | - protected function writeFrame (bool $final, int $opCode, string $payload): void { |
|
| 327 | + protected function writeFrame(bool $final, int $opCode, string $payload): void { |
|
| 328 | 328 | if ($opCode & 0x08 and !$final) { |
| 329 | 329 | throw new WebSocketError( |
| 330 | 330 | Frame::CLOSE_INTERNAL_ERROR, |
@@ -344,27 +344,27 @@ discard block |
||
| 344 | 344 | else { |
| 345 | 345 | $head .= chr($length); |
| 346 | 346 | } |
| 347 | - $this->client->write($head . $payload); |
|
| 347 | + $this->client->write($head.$payload); |
|
| 348 | 348 | } |
| 349 | 349 | |
| 350 | 350 | /** |
| 351 | 351 | * @param string $payload |
| 352 | 352 | */ |
| 353 | - public function writePing (string $payload = ''): void { |
|
| 353 | + public function writePing(string $payload = ''): void { |
|
| 354 | 354 | $this->writeFrame(true, Frame::OP_PING, $payload); |
| 355 | 355 | } |
| 356 | 356 | |
| 357 | 357 | /** |
| 358 | 358 | * @param string $payload |
| 359 | 359 | */ |
| 360 | - public function writePong (string $payload = ''): void { |
|
| 360 | + public function writePong(string $payload = ''): void { |
|
| 361 | 361 | $this->writeFrame(true, Frame::OP_PONG, $payload); |
| 362 | 362 | } |
| 363 | 363 | |
| 364 | 364 | /** |
| 365 | 365 | * @param string $payload |
| 366 | 366 | */ |
| 367 | - public function writeText (string $payload): void { |
|
| 367 | + public function writeText(string $payload): void { |
|
| 368 | 368 | $this->write(Frame::OP_TEXT, $payload); |
| 369 | 369 | } |
| 370 | 370 | } |
| 371 | 371 | \ No newline at end of file |
@@ -118,8 +118,7 @@ discard block |
||
| 118 | 118 | $frame |
| 119 | 119 | ); |
| 120 | 120 | } |
| 121 | - } |
|
| 122 | - finally { |
|
| 121 | + } finally { |
|
| 123 | 122 | if ($frame->isFinal()) { |
| 124 | 123 | $this->continue = null; |
| 125 | 124 | } |
@@ -138,11 +137,9 @@ discard block |
||
| 138 | 137 | protected function onControl (Frame $control): void { |
| 139 | 138 | if ($control->isClose()) { |
| 140 | 139 | $this->onClose($control); |
| 141 | - } |
|
| 142 | - elseif ($control->isPing()) { |
|
| 140 | + } elseif ($control->isPing()) { |
|
| 143 | 141 | $this->onPing($control); |
| 144 | - } |
|
| 145 | - elseif ($control->isPong()) { |
|
| 142 | + } elseif ($control->isPong()) { |
|
| 146 | 143 | $this->onPong($control); |
| 147 | 144 | } |
| 148 | 145 | } |
@@ -156,8 +153,7 @@ discard block |
||
| 156 | 153 | $this->onData_SetContinue($data); |
| 157 | 154 | if ($data->isText()) { |
| 158 | 155 | $this->onText($data); |
| 159 | - } |
|
| 160 | - elseif ($data->isBinary()) { |
|
| 156 | + } elseif ($data->isBinary()) { |
|
| 161 | 157 | $this->onBinary($data); |
| 162 | 158 | } |
| 163 | 159 | } |
@@ -188,11 +184,9 @@ discard block |
||
| 188 | 184 | $this->onFrame_CheckLength($frame); |
| 189 | 185 | if ($frame->isControl()) { |
| 190 | 186 | $this->onControl($frame); |
| 191 | - } |
|
| 192 | - elseif ($frame->isContinue()) { |
|
| 187 | + } elseif ($frame->isContinue()) { |
|
| 193 | 188 | $this->onContinue($frame); |
| 194 | - } |
|
| 195 | - else { |
|
| 189 | + } else { |
|
| 196 | 190 | $this->onData($frame); |
| 197 | 191 | } |
| 198 | 192 | } |
@@ -336,12 +330,10 @@ discard block |
||
| 336 | 330 | if ($length > 65535) { |
| 337 | 331 | $head .= chr(127); |
| 338 | 332 | $head .= pack('J', $length); |
| 339 | - } |
|
| 340 | - elseif ($length >= 126) { |
|
| 333 | + } elseif ($length >= 126) { |
|
| 341 | 334 | $head .= chr(126); |
| 342 | 335 | $head .= pack('n', $length); |
| 343 | - } |
|
| 344 | - else { |
|
| 336 | + } else { |
|
| 345 | 337 | $head .= chr($length); |
| 346 | 338 | } |
| 347 | 339 | $this->client->write($head . $payload); |