tcpserver : manage the timeout : disconnect the user and generate an exception by default. Can be overloaded by the developper

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@5993 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2020-05-18 07:42:48 +00:00
parent 43eac6a156
commit 1afb6b2f3f

View File

@@ -512,20 +512,20 @@ class tcpserver
throw new \Exception ("Can not read data in parent mode", 500); throw new \Exception ("Can not read data in parent mode", 500);
if ($this->socket === null) if ($this->socket === null)
throw new \Exception ("Can not read from client : not connected", 500); throw new \Exception ("Can not read from client : not connected", 500);
stream_set_timeout ($this->socket, $this->timeout);
if ($this->readMode === "text") if ($this->readMode === "text")
{
$read = stream_get_line ($this->socket, $maxLength, "\r\n"); $read = stream_get_line ($this->socket, $maxLength, "\r\n");
if ($read === false)
throw new \Exception ("Can not read from client : ".
error_get_last ()["message"], 500);
}
else else
{
$read = @fread ($this->socket, $maxLength); $read = @fread ($this->socket, $maxLength);
if ($read === false) $meta = stream_get_meta_data ($this->socket);
throw new \Exception ("Can not read from client : ". if (isset ($meta["timed_out"]) && $meta["timed_out"] === true)
error_get_last ()["message"], 500); {
$this->timeoutHandler();
return false;
} }
if ($read === false)
throw new \Exception ("Can not read from client : ".
error_get_last ()["message"], 500);
$this->logReceive ($read); $this->logReceive ($read);
return $read; return $read;
} }
@@ -669,6 +669,18 @@ class tcpserver
} }
// }}} // }}}
/** Manage the timeout exception handler
* By default, disconnect and generate an exception
*/
public function timeoutHandler ()
// {{{
{
$this->disconnect ();
throw new \Exception (dgettext ("domframework",
"Disconnected for inactivity"), 500);
}
// }}}
///////////////////////// /////////////////////////
// PRIVATE METHODS // // PRIVATE METHODS //
///////////////////////// /////////////////////////