Skip to content

Commit

Permalink
#20. Fix non-resource closing connection on stream_select
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Sep 2, 2018
1 parent 57b482f commit 80c8376
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/WebSocketServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,30 @@ private function looping($server)
}

if ($this->totalClients !== 0 // avoid 0 process creation
&& $this->totalClients % $this->config->getClientsPerFork() === 0 // only when N is there
&& true === $this->stepRecursion // only once
&& $this->maxClients === $this->totalClients // only if stack grows
&& $this->totalClients % $this->config->getClientsPerFork() === 0 // only when N is there
) {
$this->stepRecursion = false;
$this->eventLoop($server, true);
}

if ($this->totalClients !== 0 && $this->totalClients % $this->config->getClientsPerFork() === 0
&& $this->maxClients > $this->totalClients) { // there is less connection for amount of processes at this moment
if ($this->totalClients !== 0 && $this->maxClients > $this->totalClients
&& $this->totalClients % $this->config->getClientsPerFork() === 0) { // there is less connection for amount of processes at this moment
exit(1);
}

//prepare readable sockets
$readSocks = $this->clients;
$readSocks[] = $server;

// clear socket resources that were closed, thus avoiding (stream_select(): supplied resource is not a valid stream resource)
foreach ($readSocks as $k => $sock) {
if (!is_resource($sock)) {
unset($readSocks[$k]);
}
}

//start reading and use a large timeout
if (!stream_select($readSocks, $write, $except, $this->config->getStreamSelectTimeout())) {
die('something went wrong while selecting');
Expand Down Expand Up @@ -376,6 +383,7 @@ private function setPathParams(string $headers)
$matches = [];
preg_match('/GET\s(.*?)\s/', $headers, $matches);
$left = $matches[1];

foreach ($this->handler->pathParams as $k => $param) {
if (empty($this->handler->pathParams[$k + 1]) && strpos($left, '/', 1) === false) {
// do not eat last char if there is no / at the end
Expand All @@ -385,6 +393,7 @@ private function setPathParams(string $headers)
$this->handler->pathParams[$param] = substr($left, strpos($left, '/') + 1,
strpos($left, '/', 1) - 1);
}

// clear the declaration of parsed param
unset($this->handler->pathParams[array_search($param, $this->handler->pathParams, false)]);
$left = substr($left, strpos($left, '/', 1));
Expand Down

0 comments on commit 80c8376

Please sign in to comment.