From 1afb6b2f3fdbaa410eeab5ee51d387aa6ac4eee0 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Mon, 18 May 2020 07:42:48 +0000 Subject: [PATCH] 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 --- tcpserver.php | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/tcpserver.php b/tcpserver.php index 96d9600..3e867a2 100644 --- a/tcpserver.php +++ b/tcpserver.php @@ -512,20 +512,20 @@ class tcpserver throw new \Exception ("Can not read data in parent mode", 500); if ($this->socket === null) throw new \Exception ("Can not read from client : not connected", 500); + stream_set_timeout ($this->socket, $this->timeout); if ($this->readMode === "text") - { $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 - { $read = @fread ($this->socket, $maxLength); - if ($read === false) - throw new \Exception ("Can not read from client : ". - error_get_last ()["message"], 500); + $meta = stream_get_meta_data ($this->socket); + if (isset ($meta["timed_out"]) && $meta["timed_out"] === true) + { + $this->timeoutHandler(); + return false; } + if ($read === false) + throw new \Exception ("Can not read from client : ". + error_get_last ()["message"], 500); $this->logReceive ($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 // /////////////////////////