@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | * @param bool $final |
26 | 26 | * @return string |
27 | 27 | */ |
28 | - public static function pack (string $payload, int $opCode = self::OP_TEXT, bool $final = true): string { |
|
28 | + public static function pack(string $payload, int $opCode = self::OP_TEXT, bool $final = true): string { |
|
29 | 29 | $frame = chr(0x80 | $opCode); |
30 | 30 | $len = strlen($payload); |
31 | 31 | if ($len > 65535) { |
@@ -39,14 +39,14 @@ discard block |
||
39 | 39 | else { |
40 | 40 | $frame .= chr($len); |
41 | 41 | } |
42 | - return $frame . $payload; |
|
42 | + return $frame.$payload; |
|
43 | 43 | } |
44 | 44 | |
45 | - public function __construct (string $data) { |
|
45 | + public function __construct(string $data) { |
|
46 | 46 | $this->data = $data; |
47 | 47 | } |
48 | 48 | |
49 | - final public function getLength () { |
|
49 | + final public function getLength() { |
|
50 | 50 | switch ($char = ord($this->data[1]) & 0x7f) { |
51 | 51 | case 127: // big-endian long-long |
52 | 52 | return unpack('J', substr($this->data, 2, 8)); |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | /** |
61 | 61 | * @return int[] |
62 | 62 | */ |
63 | - final public function getMask () { |
|
63 | + final public function getMask() { |
|
64 | 64 | if ($this->isMasked()) { |
65 | 65 | switch (ord($this->data[1]) & 0x7f) { |
66 | 66 | case 127: |
@@ -74,11 +74,11 @@ discard block |
||
74 | 74 | return [0, 0, 0, 0]; |
75 | 75 | } |
76 | 76 | |
77 | - final public function getOpCode (): int { |
|
77 | + final public function getOpCode(): int { |
|
78 | 78 | return ord($this->data[0]) & 0x0f; |
79 | 79 | } |
80 | 80 | |
81 | - final public function getPayload (): string { |
|
81 | + final public function getPayload(): string { |
|
82 | 82 | $length = $this->getLength(); |
83 | 83 | if ($length === 0) { |
84 | 84 | return ''; |
@@ -102,27 +102,27 @@ discard block |
||
102 | 102 | return $decoded; |
103 | 103 | } |
104 | 104 | |
105 | - final public function isBinary (): bool { |
|
105 | + final public function isBinary(): bool { |
|
106 | 106 | return $this->getOpCode() === self::OP_BINARY; |
107 | 107 | } |
108 | 108 | |
109 | - final public function isClose (): bool { |
|
109 | + final public function isClose(): bool { |
|
110 | 110 | return $this->getOpCode() === self::OP_CLOSE; |
111 | 111 | } |
112 | 112 | |
113 | - final public function isFinal (): bool { |
|
113 | + final public function isFinal(): bool { |
|
114 | 114 | return (bool)(ord($this->data[0]) & 0x80); |
115 | 115 | } |
116 | 116 | |
117 | - final public function isMasked (): bool { |
|
117 | + final public function isMasked(): bool { |
|
118 | 118 | return (bool)(ord($this->data[1]) & 0x80); |
119 | 119 | } |
120 | 120 | |
121 | - final public function isPong (): bool { |
|
121 | + final public function isPong(): bool { |
|
122 | 122 | return $this->getOpCode() === self::OP_PONG; |
123 | 123 | } |
124 | 124 | |
125 | - final public function isText (): bool { |
|
125 | + final public function isText(): bool { |
|
126 | 126 | return $this->getOpCode() === self::OP_TEXT; |
127 | 127 | } |
128 | 128 |
@@ -31,12 +31,10 @@ |
||
31 | 31 | if ($len > 65535) { |
32 | 32 | $frame .= chr(127); |
33 | 33 | $frame .= pack('J', $len); |
34 | - } |
|
35 | - elseif ($len >= 126) { |
|
34 | + } elseif ($len >= 126) { |
|
36 | 35 | $frame .= chr(126); |
37 | 36 | $frame .= pack('n', $len); |
38 | - } |
|
39 | - else { |
|
37 | + } else { |
|
40 | 38 | $frame .= chr($len); |
41 | 39 | } |
42 | 40 | return $frame . $payload; |
@@ -14,13 +14,13 @@ |
||
14 | 14 | * |
15 | 15 | * @return void |
16 | 16 | */ |
17 | - public function onOutOfBand (); |
|
17 | + public function onOutOfBand(); |
|
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 (); |
|
24 | + public function onReadable(); |
|
25 | 25 | |
26 | 26 | } |
27 | 27 | \ No newline at end of file |
@@ -42,34 +42,34 @@ |
||
42 | 42 | * |
43 | 43 | * @return int `SOCK_*` |
44 | 44 | */ |
45 | - public static function getType (); |
|
45 | + public static function getType(); |
|
46 | 46 | |
47 | 47 | /** |
48 | 48 | * Returns the address family constant of the socket. |
49 | 49 | * |
50 | 50 | * @return int `AF_*` |
51 | 51 | */ |
52 | - public function getDomain (); |
|
52 | + public function getDomain(); |
|
53 | 53 | |
54 | 54 | /** |
55 | 55 | * Returns the underlying socket resource as an integer. |
56 | 56 | * |
57 | 57 | * @return int |
58 | 58 | */ |
59 | - public function getId (); |
|
59 | + public function getId(); |
|
60 | 60 | |
61 | 61 | /** |
62 | 62 | * Returns the protocol constant used by the socket. |
63 | 63 | * |
64 | 64 | * @return int |
65 | 65 | */ |
66 | - public function getProtocol (); |
|
66 | + public function getProtocol(); |
|
67 | 67 | |
68 | 68 | /** |
69 | 69 | * Returns the underlying socket resource. |
70 | 70 | * |
71 | 71 | * @return resource |
72 | 72 | */ |
73 | - public function getResource (); |
|
73 | + public function getResource(); |
|
74 | 74 | |
75 | 75 | } |
76 | 76 | \ No newline at end of file |
@@ -14,7 +14,7 @@ discard block |
||
14 | 14 | * |
15 | 15 | * @return string |
16 | 16 | */ |
17 | - public function __toString () { |
|
17 | + public function __toString() { |
|
18 | 18 | try { |
19 | 19 | return implode(':', $this->getSockName()); |
20 | 20 | } |
@@ -36,7 +36,7 @@ discard block |
||
36 | 36 | * @throws Error |
37 | 37 | * @return $this |
38 | 38 | */ |
39 | - public function bind (string $address, int $port = 0) { |
|
39 | + public function bind(string $address, int $port = 0) { |
|
40 | 40 | if (!@socket_bind($this->resource, $address, $port)) { |
41 | 41 | throw new Error($this->resource, SOCKET_EOPNOTSUPP); |
42 | 42 | } |
@@ -23,7 +23,7 @@ discard block |
||
23 | 23 | * @param $resource |
24 | 24 | * @param Reactor $reactor |
25 | 25 | */ |
26 | - public function __construct ($resource, Reactor $reactor) { |
|
26 | + public function __construct($resource, Reactor $reactor) { |
|
27 | 27 | parent::__construct($resource); |
28 | 28 | $reactor->add($this); |
29 | 29 | $this->reactor = $reactor; |
@@ -35,7 +35,7 @@ discard block |
||
35 | 35 | * https://tools.ietf.org/html/rfc6455#section-4.2 |
36 | 36 | * @return WebSocketClient |
37 | 37 | */ |
38 | - public function accept () { |
|
38 | + public function accept() { |
|
39 | 39 | if (!$resource = @socket_accept($this->resource)) { |
40 | 40 | throw new Error($this->resource); // reliable errno |
41 | 41 | } |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | /** |
51 | 51 | * @param WebSocketClient $client |
52 | 52 | */ |
53 | - protected function acceptHandshake (WebSocketClient $client) { |
|
53 | + protected function acceptHandshake(WebSocketClient $client) { |
|
54 | 54 | $head = $client->await(self::CH_READ)->recv(4096); // todo client->readUntil(string,maxlength) |
55 | 55 | $head = explode("\r\n", $head); |
56 | 56 | $method = array_shift($head); |
@@ -78,17 +78,17 @@ discard block |
||
78 | 78 | "HTTP/1.1 101 Switching Protocols", |
79 | 79 | "Upgrade: websocket", |
80 | 80 | "Connection: Upgrade", |
81 | - "Sec-WebSocket-Accept: " . base64_encode(sha1($base64Key . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11', true)), |
|
81 | + "Sec-WebSocket-Accept: ".base64_encode(sha1($base64Key.'258EAFA5-E914-47DA-95CA-C5AB0DC85B11', true)), |
|
82 | 82 | ]; |
83 | 83 | |
84 | - $client->write(implode("\r\n", $response) . "\r\n\r\n"); |
|
84 | + $client->write(implode("\r\n", $response)."\r\n\r\n"); |
|
85 | 85 | } |
86 | 86 | |
87 | 87 | /** |
88 | 88 | * @param WebSocketClient $client |
89 | 89 | * @param string $connection |
90 | 90 | */ |
91 | - protected function acceptHandshakeConnection (WebSocketClient $client, string $connection) { |
|
91 | + protected function acceptHandshakeConnection(WebSocketClient $client, string $connection) { |
|
92 | 92 | if (!preg_match('/^upgrade$/i', $connection)) { |
93 | 93 | $this->reject($client, "Expected: Connection: Upgrade"); |
94 | 94 | } |
@@ -100,7 +100,7 @@ discard block |
||
100 | 100 | * @param WebSocketClient $client |
101 | 101 | * @param string $host |
102 | 102 | */ |
103 | - protected function acceptHandshakeHost (WebSocketClient $client, string $host) { |
|
103 | + protected function acceptHandshakeHost(WebSocketClient $client, string $host) { |
|
104 | 104 | |
105 | 105 | } |
106 | 106 | |
@@ -108,7 +108,7 @@ discard block |
||
108 | 108 | * @param WebSocketClient $client |
109 | 109 | * @param string $base64Key |
110 | 110 | */ |
111 | - protected function acceptHandshakeKey (WebSocketClient $client, string $base64Key) { |
|
111 | + protected function acceptHandshakeKey(WebSocketClient $client, string $base64Key) { |
|
112 | 112 | $key = base64_decode($base64Key); |
113 | 113 | if ($key === false or strlen($key) !== 16) { |
114 | 114 | $this->reject($client, "Expected Sec-WebSocket-Key"); |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | * @param WebSocketClient $client |
120 | 120 | * @param string $method |
121 | 121 | */ |
122 | - protected function acceptHandshakeMethod (WebSocketClient $client, string $method) { |
|
122 | + protected function acceptHandshakeMethod(WebSocketClient $client, string $method) { |
|
123 | 123 | if (!preg_match('/HTTP\/1\.1$/i', $method)) { |
124 | 124 | $this->reject($client, "Expected: HTTP/1.1"); |
125 | 125 | } |
@@ -131,7 +131,7 @@ discard block |
||
131 | 131 | * @param WebSocketClient $client |
132 | 132 | * @param string $origin |
133 | 133 | */ |
134 | - protected function acceptHandshakeOrigin (WebSocketClient $client, string $origin) { |
|
134 | + protected function acceptHandshakeOrigin(WebSocketClient $client, string $origin) { |
|
135 | 135 | |
136 | 136 | } |
137 | 137 | |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | * @param WebSocketClient $client |
140 | 140 | * @param string $upgrade |
141 | 141 | */ |
142 | - protected function acceptHandshakeUpgrade (WebSocketClient $client, string $upgrade) { |
|
142 | + protected function acceptHandshakeUpgrade(WebSocketClient $client, string $upgrade) { |
|
143 | 143 | if (!preg_match('/^websocket$/i', $upgrade)) { |
144 | 144 | $this->reject($client, "Expected Upgrade: websocket"); |
145 | 145 | } |
@@ -149,7 +149,7 @@ discard block |
||
149 | 149 | * @param WebSocketClient $client |
150 | 150 | * @param string $version |
151 | 151 | */ |
152 | - protected function acceptHandshakeVersion (WebSocketClient $client, string $version) { |
|
152 | + protected function acceptHandshakeVersion(WebSocketClient $client, string $version) { |
|
153 | 153 | if ($version !== '13') { |
154 | 154 | $this->reject($client, "Expected Sec-WebSocket-Version: 13"); |
155 | 155 | } |
@@ -158,7 +158,7 @@ discard block |
||
158 | 158 | /** |
159 | 159 | * @return WebSocketClient[] |
160 | 160 | */ |
161 | - public function getClients () { |
|
161 | + public function getClients() { |
|
162 | 162 | return $this->clients; |
163 | 163 | } |
164 | 164 | |
@@ -166,7 +166,7 @@ discard block |
||
166 | 166 | * @param resource $resource |
167 | 167 | * @return WebSocketClient |
168 | 168 | */ |
169 | - protected function newClient ($resource) { |
|
169 | + protected function newClient($resource) { |
|
170 | 170 | return new WebSocketClient($resource, $this); |
171 | 171 | } |
172 | 172 | |
@@ -175,14 +175,14 @@ discard block |
||
175 | 175 | * |
176 | 176 | * @inheritDoc |
177 | 177 | */ |
178 | - final public function onOutOfBand () { |
|
178 | + final public function onOutOfBand() { |
|
179 | 179 | // do nothing |
180 | 180 | } |
181 | 181 | |
182 | 182 | /** |
183 | 183 | * @inheritDoc |
184 | 184 | */ |
185 | - public function onReadable () { |
|
185 | + public function onReadable() { |
|
186 | 186 | $this->accept(); |
187 | 187 | } |
188 | 188 | |
@@ -193,7 +193,7 @@ discard block |
||
193 | 193 | * @param string $reason |
194 | 194 | * @throws RuntimeException |
195 | 195 | */ |
196 | - protected function reject (WebSocketClient $client, string $reason) { |
|
196 | + protected function reject(WebSocketClient $client, string $reason) { |
|
197 | 197 | throw new RuntimeException("Rejected {$client}: {$reason}"); |
198 | 198 | } |
199 | 199 | |
@@ -203,7 +203,7 @@ discard block |
||
203 | 203 | * @param WebSocketClient $client |
204 | 204 | * @return $this |
205 | 205 | */ |
206 | - public function remove (WebSocketClient $client) { |
|
206 | + public function remove(WebSocketClient $client) { |
|
207 | 207 | unset($this->clients[$client->getId()]); |
208 | 208 | $this->reactor->remove($client->getId()); |
209 | 209 | return $this; |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | * @return static[] Two instances at indices `0` and `1`. |
18 | 18 | * @throws Error |
19 | 19 | */ |
20 | - public static function newUnixPair (...$extra) { |
|
20 | + public static function newUnixPair(...$extra) { |
|
21 | 21 | if (!@socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $fd)) { |
22 | 22 | throw new Error; // reliable errno |
23 | 23 | } |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | * |
33 | 33 | * @return int |
34 | 34 | */ |
35 | - final public static function getType () { |
|
35 | + final public static function getType() { |
|
36 | 36 | return SOCK_STREAM; |
37 | 37 | } |
38 | 38 | |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | * @return string |
47 | 47 | * @throws Error The error's extra data is set to what was partially read. |
48 | 48 | */ |
49 | - public function read ($length) { |
|
49 | + public function read($length) { |
|
50 | 50 | $data = ''; |
51 | 51 | while ($length > 0) { |
52 | 52 | try { |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | * @throws Error |
77 | 77 | * @return string |
78 | 78 | */ |
79 | - public function recv ($length, $flags = 0) { |
|
79 | + public function recv($length, $flags = 0) { |
|
80 | 80 | $count = @socket_recv($this->resource, $data, $length, $flags); |
81 | 81 | if ($count === false) { |
82 | 82 | $error = new Error($this->resource, SOCKET_EINVAL); |
@@ -18,7 +18,7 @@ discard block |
||
18 | 18 | * @param $resource |
19 | 19 | * @param WebSocketServer $server |
20 | 20 | */ |
21 | - public function __construct ($resource, WebSocketServer $server) { |
|
21 | + public function __construct($resource, WebSocketServer $server) { |
|
22 | 22 | parent::__construct($resource); |
23 | 23 | $this->server = $server; |
24 | 24 | } |
@@ -26,33 +26,33 @@ discard block |
||
26 | 26 | /** |
27 | 27 | * Removes the client from the server and reactor. |
28 | 28 | */ |
29 | - protected function onClose () { |
|
29 | + protected function onClose() { |
|
30 | 30 | $this->server->remove($this); |
31 | 31 | } |
32 | 32 | |
33 | 33 | /** |
34 | 34 | * @param string $payload |
35 | 35 | */ |
36 | - protected function onData (string $payload): void { |
|
36 | + protected function onData(string $payload): void { |
|
37 | 37 | } |
38 | 38 | |
39 | 39 | /** |
40 | 40 | * WebSockets do not use the out-of-band channel. |
41 | 41 | */ |
42 | - final public function onOutOfBand () { |
|
42 | + final public function onOutOfBand() { |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | /** |
46 | 46 | * When a browser responds to a ping. |
47 | 47 | */ |
48 | - protected function onPong (): void { |
|
48 | + protected function onPong(): void { |
|
49 | 49 | // stub |
50 | 50 | } |
51 | 51 | |
52 | 52 | /** |
53 | 53 | * Reads framed messages. |
54 | 54 | */ |
55 | - public function onReadable (): void { |
|
55 | + public function onReadable(): void { |
|
56 | 56 | $data = $this->recv(4096); |
57 | 57 | $frame = new WebSocketFrame($data); |
58 | 58 | if ($frame->isText()) { |
@@ -74,17 +74,17 @@ discard block |
||
74 | 74 | * |
75 | 75 | * @param string $text |
76 | 76 | */ |
77 | - protected function onText (string $text): void { |
|
77 | + protected function onText(string $text): void { |
|
78 | 78 | } |
79 | 79 | |
80 | 80 | /** |
81 | 81 | * Pings the browser. |
82 | 82 | */ |
83 | - public function ping () { |
|
83 | + public function ping() { |
|
84 | 84 | $this->write(WebSocketFrame::pack('', WebSocketFrame::OP_PING)); |
85 | 85 | } |
86 | 86 | |
87 | - public function writeText (string $message) { |
|
87 | + public function writeText(string $message) { |
|
88 | 88 | $packed = WebSocketFrame::pack($message); |
89 | 89 | $this->write($packed); |
90 | 90 | } |
@@ -57,14 +57,11 @@ |
||
57 | 57 | $frame = new WebSocketFrame($data); |
58 | 58 | if ($frame->isText()) { |
59 | 59 | $this->onText($frame->getPayload()); |
60 | - } |
|
61 | - elseif ($frame->isBinary()) { |
|
60 | + } elseif ($frame->isBinary()) { |
|
62 | 61 | $this->onData($frame->getPayload()); |
63 | - } |
|
64 | - elseif ($frame->isPong()) { |
|
62 | + } elseif ($frame->isPong()) { |
|
65 | 63 | $this->onPong(); |
66 | - } |
|
67 | - elseif ($frame->isClose()) { |
|
64 | + } elseif ($frame->isClose()) { |
|
68 | 65 | $this->close(); |
69 | 66 | } |
70 | 67 | } |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | * |
13 | 13 | * @return int |
14 | 14 | */ |
15 | - final public static function getType () { |
|
15 | + final public static function getType() { |
|
16 | 16 | return SOCK_STREAM; |
17 | 17 | } |
18 | 18 | |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | * @throws Error |
28 | 28 | * @return StreamClient The accepted connection, as a client instance. |
29 | 29 | */ |
30 | - public function accept () { |
|
30 | + public function accept() { |
|
31 | 31 | if (!$resource = @socket_accept($this->resource)) { |
32 | 32 | throw new Error($this->resource); // reliable errno |
33 | 33 | } |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | * @throws Error |
47 | 47 | * @return $this |
48 | 48 | */ |
49 | - public function listen ($backlog = 0) { |
|
49 | + public function listen($backlog = 0) { |
|
50 | 50 | if (!@socket_listen($this->resource, $backlog)) { |
51 | 51 | throw new Error($this->resource); // reliable errno |
52 | 52 | } |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | * @return StreamClient |
61 | 61 | * @throws Error |
62 | 62 | */ |
63 | - protected function newClient ($resource) { |
|
63 | + protected function newClient($resource) { |
|
64 | 64 | return new StreamClient($resource); |
65 | 65 | } |
66 | 66 | |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | * @param StreamClient $client |
71 | 71 | * @return void |
72 | 72 | */ |
73 | - protected function onAccept (StreamClient $client) { |
|
73 | + protected function onAccept(StreamClient $client) { |
|
74 | 74 | } |
75 | 75 | |
76 | 76 | } |
@@ -19,8 +19,7 @@ discard block |
||
19 | 19 | public function __toString () { |
20 | 20 | try { |
21 | 21 | return implode(':', $this->getPeerName()); |
22 | - } |
|
23 | - catch (Exception $exception) { |
|
22 | + } catch (Exception $exception) { |
|
24 | 23 | return ':'; |
25 | 24 | } |
26 | 25 | } |
@@ -109,8 +108,7 @@ discard block |
||
109 | 108 | try { |
110 | 109 | $total += $count = $this->await(self::CH_WRITE)->send($data); |
111 | 110 | $data = substr($data, $count); // 0 bytes sent = unaltered buffer |
112 | - } |
|
113 | - catch (Error $error) { |
|
111 | + } catch (Error $error) { |
|
114 | 112 | $error->setExtra($total); |
115 | 113 | throw $error; |
116 | 114 | } |