Total Complexity | 57 |
Total Lines | 385 |
Duplicated Lines | 0 % |
Changes | 26 | ||
Bugs | 2 | Features | 0 |
Complex classes like WebSocketServer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WebSocketServer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class WebSocketServer extends WssMain implements WebSocketServerContract |
||
20 | { |
||
21 | private const MAX_BYTES_READ = 8192; |
||
22 | private const HEADER_BYTES_READ = 1024; |
||
23 | |||
24 | /** |
||
25 | * @var ServerConfig |
||
26 | */ |
||
27 | protected ServerConfig $config; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | private array $clients = []; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | private array $headersUpgrade = []; |
||
38 | |||
39 | /** |
||
40 | * @var int |
||
41 | */ |
||
42 | private int $maxClients = 1; |
||
43 | |||
44 | /** |
||
45 | * @var WebSocket |
||
46 | */ |
||
47 | private WebSocket $handler; |
||
48 | |||
49 | /** |
||
50 | * @var bool |
||
51 | */ |
||
52 | private bool $stepRecursion = true; |
||
53 | |||
54 | /* |
||
55 | * @var bool |
||
56 | */ |
||
57 | private bool $printException = true; |
||
58 | |||
59 | /** |
||
60 | * WebSocketServer constructor. |
||
61 | * |
||
62 | * @param WebSocket $handler |
||
63 | * @param ServerConfig $config |
||
64 | */ |
||
65 | public function __construct( |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Configure if error exceptions should be printed |
||
79 | * |
||
80 | * @return self |
||
81 | */ |
||
82 | public function printException(bool $printException): self |
||
83 | { |
||
84 | $this->printException = $printException; |
||
85 | |||
86 | return $this; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Runs main process - Anscestor with server socket on TCP |
||
91 | * |
||
92 | * @throws WebSocketException |
||
93 | * @throws ConnectionException |
||
94 | */ |
||
95 | public function run(): void |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Recursive event loop that input intu recusion by remainder = 0 - thus when N users, |
||
130 | * and when forks equals true which prevents it from infinite recursive iterations |
||
131 | * |
||
132 | * @param resource $server server connection |
||
133 | * @param bool $fork flag to fork or run event loop |
||
134 | * @throws WebSocketException |
||
135 | * @throws ConnectionException |
||
136 | */ |
||
137 | private function eventLoop($server, bool $fork = false): void |
||
148 | } |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @param resource $server |
||
153 | * @throws WebSocketException |
||
154 | * @throws ConnectionException |
||
155 | */ |
||
156 | private function looping($server): void |
||
157 | { |
||
158 | $loopingDelay = $this->config->getLoopingDelay(); |
||
159 | |||
160 | while (true) { |
||
161 | // usleep require microseconds |
||
162 | if ($loopingDelay) { |
||
163 | usleep($loopingDelay * 1000); |
||
164 | } |
||
165 | |||
166 | $totalClients = count($this->clients) + 1; |
||
167 | |||
168 | // maxClients prevents process fork on count down |
||
169 | if ($totalClients > $this->maxClients) { |
||
170 | $this->maxClients = $totalClients; |
||
171 | } |
||
172 | |||
173 | $doFork = $this->config->isForking() === true |
||
174 | && $totalClients !== 0 // avoid 0 process creation |
||
175 | && $this->stepRecursion === true // only once |
||
176 | && $this->maxClients === $totalClients // only if stack grows |
||
177 | && $totalClients % $this->config->getClientsPerFork() === 0; // only when N is there |
||
178 | if ($doFork) { |
||
179 | $this->stepRecursion = false; |
||
180 | $this->eventLoop($server, true); |
||
181 | } |
||
182 | $this->lessConnThanProc($totalClients, $this->maxClients); |
||
183 | |||
184 | //prepare readable sockets |
||
185 | $readSocks = $this->clients; |
||
186 | $readSocks[] = $server; |
||
187 | $this->cleanSocketResources($readSocks); |
||
188 | |||
189 | //start reading and use a large timeout |
||
190 | if (!stream_select($readSocks, $write, $except, $this->config->getStreamSelectTimeout())) { |
||
191 | throw new WebSocketException('something went wrong while selecting', |
||
192 | CommonsContract::SERVER_SELECT_ERROR); |
||
193 | } |
||
194 | |||
195 | //new client |
||
196 | if (in_array($server, $readSocks, false)) { |
||
197 | $this->acceptNewClient($server, $readSocks); |
||
198 | if ($this->config->isCheckOrigin() && $this->config->isOriginHeader() === false) { |
||
199 | continue; |
||
200 | } |
||
201 | } |
||
202 | |||
203 | //message from existing client |
||
204 | $this->messagesWorker($readSocks); |
||
205 | } |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * @param resource $server |
||
210 | * @param array $readSocks |
||
211 | * @throws ConnectionException |
||
212 | */ |
||
213 | private function acceptNewClient($server, array &$readSocks): void |
||
214 | { |
||
215 | $newClient = stream_socket_accept($server, -1); // must be 0 to non-block |
||
216 | if ($newClient) { |
||
217 | if ($this->config->isSsl() === true) { |
||
218 | $isEnabled = stream_socket_enable_crypto($newClient, true, $this->config->getCryptoType()); |
||
219 | if ($isEnabled === false) { // couldn't enable crypto - let's try one more time |
||
220 | return; |
||
221 | } |
||
222 | } |
||
223 | |||
224 | // important to read from headers here coz later client will change and there will be only msgs on pipe |
||
225 | $headers = fread($newClient, self::HEADER_BYTES_READ); |
||
226 | if ($this->config->isCheckOrigin()) { |
||
227 | $hasOrigin = (new OriginComponent($this->config, $newClient))->checkOrigin($headers); |
||
228 | $this->config->setOriginHeader($hasOrigin); |
||
229 | if ($hasOrigin === false) { |
||
230 | return; |
||
231 | } |
||
232 | } |
||
233 | |||
234 | if (empty($this->handler->pathParams[0]) === false) { |
||
235 | $this->setPathParams($headers); |
||
236 | } |
||
237 | |||
238 | $this->clients[] = $newClient; |
||
239 | $this->stepRecursion = true; // set on new client - remainder % is always 0 |
||
240 | |||
241 | // trigger OPEN event |
||
242 | $this->handler->onOpen(new Connection($newClient, $this->clients)); |
||
243 | $this->handshake($newClient, $headers); |
||
244 | } |
||
245 | |||
246 | //delete the server socket from the read sockets |
||
247 | unset($readSocks[array_search($server, $readSocks, false)]); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * @param array $readSocks |
||
252 | * @uses onPing |
||
253 | * @uses onPong |
||
254 | * @uses onMessage |
||
255 | */ |
||
256 | private function messagesWorker(array $readSocks): void |
||
257 | { |
||
258 | foreach ($readSocks as $kSock => $sock) { |
||
259 | $data = $this->decode(fread($sock, self::MAX_BYTES_READ)); |
||
260 | if ($data !== null) { |
||
261 | $dataType = null; |
||
262 | $dataPayload = null; |
||
263 | if ($data !== false) { // payload is too large - waiting for remained data |
||
264 | $dataType = $data['type']; |
||
265 | $dataPayload = $data['payload']; |
||
266 | } |
||
267 | |||
268 | // to manipulate connection through send/close methods via handler, specified in IConnection |
||
269 | $cureentConn = new Connection($sock, $this->clients); |
||
270 | if (empty($data) || $dataType === self::EVENT_TYPE_CLOSE) { // close event triggered from client - browser tab or close socket event |
||
271 | // trigger CLOSE event |
||
272 | try { |
||
273 | $this->handler->onClose($cureentConn); |
||
274 | } catch (WebSocketException $e) { |
||
275 | $this->handleMessagesWorkerException($cureentConn, $e); |
||
276 | } |
||
277 | |||
278 | // to avoid event leaks |
||
279 | unset($this->clients[array_search($sock, $this->clients)], $readSocks[$kSock]); |
||
280 | continue; |
||
281 | } |
||
282 | |||
283 | $isSupportedMethod = empty(self::MAP_EVENT_TYPE_TO_METHODS[$dataType]) === false |
||
284 | && method_exists($this->handler, self::MAP_EVENT_TYPE_TO_METHODS[$dataType]); |
||
285 | if ($isSupportedMethod) { |
||
286 | try { |
||
287 | // dynamic call: onMessage, onPing, onPong |
||
288 | $this->handler->{self::MAP_EVENT_TYPE_TO_METHODS[$dataType]}($cureentConn, $dataPayload); |
||
289 | } catch (WebSocketException $e) { |
||
290 | $this->handleMessagesWorkerException($cureentConn, $e); |
||
291 | } |
||
292 | } |
||
293 | } |
||
294 | } |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Handshakes/upgrade and key parse |
||
299 | * |
||
300 | * @param resource $client Source client socket to write |
||
301 | * @param string $headers Headers that client has been sent |
||
302 | * @return string socket handshake key (Sec-WebSocket-Key)| false on parse error |
||
303 | * @throws ConnectionException |
||
304 | */ |
||
305 | private function handshake($client, string $headers): string |
||
306 | { |
||
307 | $match = []; |
||
308 | preg_match(self::SEC_WEBSOCKET_KEY_PTRN, $headers, $match); |
||
309 | if (empty($match[1])) { |
||
310 | return ''; |
||
311 | } |
||
312 | |||
313 | $key = $match[1]; |
||
314 | // sending header according to WebSocket Protocol |
||
315 | $secWebSocketAccept = base64_encode(sha1(trim($key) . self::HEADER_WEBSOCKET_ACCEPT_HASH, true)); |
||
316 | $this->setHeadersUpgrade($secWebSocketAccept); |
||
317 | $upgradeHeaders = $this->getHeadersUpgrade(); |
||
318 | |||
319 | fwrite($client, $upgradeHeaders); |
||
320 | |||
321 | return $key; |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Sets an array of headers needed to upgrade server/client connection |
||
326 | * |
||
327 | * @param string $secWebSocketAccept base64 encoded Sec-WebSocket-Accept header |
||
328 | */ |
||
329 | private function setHeadersUpgrade(string $secWebSocketAccept): void |
||
330 | { |
||
331 | $this->headersUpgrade = [ |
||
332 | self::HEADERS_UPGRADE_KEY => self::HEADERS_UPGRADE_VALUE, |
||
333 | self::HEADERS_CONNECTION_KEY => self::HEADERS_CONNECTION_VALUE, |
||
334 | self::HEADERS_SEC_WEBSOCKET_ACCEPT_KEY => ' ' . $secWebSocketAccept |
||
335 | // the space before key is really important |
||
336 | ]; |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * Retreives headers from an array of headers to upgrade server/client connection |
||
341 | * |
||
342 | * @return string Headers to Upgrade communication connection |
||
343 | * @throws ConnectionException |
||
344 | */ |
||
345 | private function getHeadersUpgrade(): string |
||
346 | { |
||
347 | $handShakeHeaders = self::HEADER_HTTP1_1 . self::HEADERS_EOL; |
||
348 | if (empty($this->headersUpgrade)) { |
||
349 | throw new ConnectionException('Headers for upgrade handshake are not set' . PHP_EOL, |
||
350 | CommonsContract::SERVER_HEADERS_NOT_SET); |
||
351 | } |
||
352 | |||
353 | foreach ($this->headersUpgrade as $key => $header) { |
||
354 | $handShakeHeaders .= $key . ':' . $header . self::HEADERS_EOL; |
||
355 | if ($key === self::HEADERS_SEC_WEBSOCKET_ACCEPT_KEY) { // add additional EOL fo Sec-WebSocket-Accept |
||
356 | $handShakeHeaders .= self::HEADERS_EOL; |
||
357 | } |
||
358 | } |
||
359 | |||
360 | return $handShakeHeaders; |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * Parses parameters from GET on web-socket client connection before handshake |
||
365 | * |
||
366 | * @param string $headers |
||
367 | */ |
||
368 | private function setPathParams(string $headers): void |
||
388 | } |
||
389 | } |
||
390 | } |
||
391 | |||
392 | /** |
||
393 | * Manage messagesWorker Exceptions |
||
394 | * |
||
395 | * @param Connection $connection |
||
396 | * @param WebSocketException $e |
||
397 | */ |
||
398 | private function handleMessagesWorkerException(Connection $connection, WebSocketException $e): void |
||
404 | } |
||
405 | } |
||
406 | } |
||
407 |
If you suppress an error, we recommend checking for the error condition explicitly: