Permalink
Browse files

Various fixes and improvements.

  • Loading branch information...
cubiclesoft committed Oct 1, 2018
1 parent da6d314 commit bde8fca372de900115643487d3d6234e89961fdd
Showing with 68 additions and 14 deletions.
  1. +11 −0 docs/web_server.md
  2. +11 −0 docs/websocket_server.md
  3. +12 −3 support/websocket.php
  4. +29 −11 web_server.php
  5. +5 −0 websocket_server.php
View
@@ -281,6 +281,17 @@ Returns: The internal array of active clients.
This function retrieves the internal array of active clients. These are the clients that have made it past the initialization states.
WebServer::NumClients()
-----------------------
Access: public
Parameters: None.
Returns: The number of active clients.
This function returns the number active clients that have made it past the initialization states. It's more efficient to call this function than to get a copy of the clients array just to `count()` them.
WebServer::GetClient($id)
-------------------------
View
@@ -210,6 +210,17 @@ Returns: An array of all of the active clients.
This function makes it easy to retrieve the entire list of clients currently connected to the server. Note that this may include clients that are in the process of connecting and upgrading to the WebSocket protocol.
WebSocketServer::NumClients()
-----------------------------
Access: public
Parameters: None.
Returns: The number of active clients.
This function returns the number clients currently connected to the server. It's more efficient to call this function than to get a copy of the clients array just to `count()` them.
WebSocketServer::GetClient($id)
-------------------------------
View
@@ -297,6 +297,9 @@ public function Wait($timeout = false)
{
if ($this->fp === false || $this->state === self::STATE_CONNECTING) return array("success" => false, "error" => self::WSTranslate("Connection not established."), "errorcode" => "no_connection");
$result = $this->ProcessReadData();
if (!$result["success"]) return $result;
$this->FillWriteData();
$readfp = array($this->fp);
@@ -340,10 +343,16 @@ public function ProcessQueuesAndTimeoutState($read, $write, $readsize = 65536)
if ($write)
{
$result = @fwrite($this->fp, $this->writedata);
if ($result === false || ($result === "" && feof($this->fp))) return array("success" => false, "error" => self::WSTranslate("ProcessQueuesAndTimeoutState() failed due to fwrite() failure. Most likely cause: Connection failure."), "errorcode" => "fwrite_failed");
if ($result === false || ($this->writedata === "" && feof($this->fp))) return array("success" => false, "error" => self::WSTranslate("ProcessQueuesAndTimeoutState() failed due to fwrite() failure. Most likely cause: Connection failure."), "errorcode" => "fwrite_failed");
if ($result)
{
$this->rawsendsize += $result;
$this->writedata = (string)substr($this->writedata, $result);
$this->rawsendsize += strlen($result);
$this->writedata = (string)substr($this->writedata, $result);
$this->lastkeepalive = time();
$this->keepalivesent = false;
}
}
// Handle timeout state.
View
@@ -10,7 +10,7 @@
// Compression support requires the CubicleSoft PHP DeflateStream class.
class WebServer
{
private $fp, $ssl, $initclients, $clients, $readyclients, $nextclientid;
private $fp, $ssl, $initclients, $clients, $readreadyclients, $writereadyclients, $nextclientid;
private $defaulttimeout, $defaultclienttimeout, $maxrequests, $defaultclientoptions, $usegzip, $cachedir;
public function __construct()
@@ -26,7 +26,8 @@ public function Reset()
$this->ssl = false;
$this->initclients = array();
$this->clients = array();
$this->readyclients = array();
$this->readreadyclients = array();
$this->writereadyclients = array();
$this->nextclientid = 1;
$this->defaulttimeout = 30;
@@ -136,7 +137,8 @@ public function Stop()
$this->initclients = array();
$this->clients = array();
$this->readyclients = array();
$this->readreadyclients = array();
$this->writereadyclients = array();
$this->fp = false;
$this->ssl = false;
}
@@ -464,7 +466,7 @@ public function UpdateStreamsAndTimeout($prefix, &$timeout, &$readfps, &$writefp
}
}
if (count($this->readyclients)) $timeout = 0;
if (count($this->readreadyclients)) $timeout = 0;
foreach ($this->clients as $id => $client)
{
@@ -478,6 +480,12 @@ public function UpdateStreamsAndTimeout($prefix, &$timeout, &$readfps, &$writefp
}
else if (HTTP::WantRead($client->httpstate)) $readfps[$prefix . "http_c_" . $id] = $client->fp;
else if ($client->mode !== "init_response" && ($client->writedata !== "" || $client->httpstate["data"] !== "")) $writefps[$prefix . "http_c_" . $id] = $client->fp;
else if ($client->responsefinalized)
{
$this->writereadyclients[$id] = $client->fp;
$timeout = 0;
}
}
}
}
@@ -615,8 +623,8 @@ public function Wait($timeout = false, $readfps = array(), $writefps = array(),
$this->HandleNewConnections($readfps, $writefps);
// Handle ready clients.
foreach ($this->readyclients as $id => $fp) $readfps["http_c_" . $id] = $fp;
$this->readyclients = array();
foreach ($this->readreadyclients as $id => $fp) $readfps["http_c_" . $id] = $fp;
$this->readreadyclients = array();
// Handle clients in the read queue.
foreach ($readfps as $cid => $fp)
@@ -707,14 +715,18 @@ public function Wait($timeout = false, $readfps = array(), $writefps = array(),
else if ($client->requestcomplete === false && $client->httpstate["state"] !== "request_line" && $client->httpstate["state"] !== "headers")
{
// Allows the caller an opportunity to adjust some client options based on inputs on a per-client basis (e.g. recvlimit).
$this->readyclients[$id] = $fp;
$this->readreadyclients[$id] = $fp;
$result["clients"][$id] = $client;
}
}
unset($readfps[$cid]);
}
// Handle ready clients.
foreach ($this->writereadyclients as $id => $fp) $writefps["http_c_" . $id] = $fp;
$this->writereadyclients = array();
// Handle clients in the write queue.
foreach ($writefps as $cid => $fp)
{
@@ -760,6 +772,7 @@ public function Wait($timeout = false, $readfps = array(), $writefps = array(),
$client->responseheaders = false;
$client->httpstate["data"] .= "\r\n";
$client->httpstate["result"]["rawsendheadersize"] = strlen($client->httpstate["data"]);
$client->mode = "handle_response";
}
@@ -802,7 +815,7 @@ public function Wait($timeout = false, $readfps = array(), $writefps = array(),
$this->initclients[$id] = $client;
unset($this->clients[$id]);
if ($client->readdata !== "") $this->readyclients[$id] = $fp;
if ($client->readdata !== "") $this->readreadyclients[$id] = $fp;
}
else
{
@@ -921,6 +934,11 @@ public function GetClients()
return $this->clients;
}
public function NumClients()
{
return count($this->clients);
}
public function GetClient($id)
{
return (isset($this->clients[$id]) ? $this->clients[$id] : false);
@@ -933,7 +951,7 @@ public function DetachClient($id)
$client = $this->clients[$id];
unset($this->clients[$id]);
unset($this->readyclients[$id]);
unset($this->readreadyclients[$id]);
return $client;
}
@@ -953,7 +971,7 @@ public function RemoveClient($id)
if ($client->fp !== false) @fclose($client->fp);
unset($this->clients[$id]);
unset($this->readyclients[$id]);
unset($this->readreadyclients[$id]);
}
}
}
@@ -1102,7 +1120,7 @@ public function AddResponseHeader($name, $val, $replace = false)
$name = preg_replace('/\s+/', "-", trim(preg_replace('/[^A-Za-z0-9 ]/', " ", $name)));
if (!isset($this->responseheaders[$name]) || $replace) $this->responseheaders[$name] = array();
$this->responseheaders[$name][] = $val;
$this->responseheaders[$name][] = str_replace(array("\r", "\n"), array("", ""), $val);
}
}
View
@@ -447,6 +447,11 @@ public function GetClients()
return $this->clients;
}
public function NumClients()
{
return count($this->clients);
}
public function GetClient($id)
{
return (isset($this->clients[$id]) ? $this->clients[$id] : false);

0 comments on commit bde8fca

Please sign in to comment.