From 43eac6a156761f82e7d02314bde1e0a7fc07ebe2 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Wed, 13 May 2020 20:34:48 +0000 Subject: [PATCH] tcpserver : add exception catch, not log for "stream_select(): unable to select [4]: Interrupted system call (max_fd=0)" error, more tests git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@5992 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- tcpserver.php | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/tcpserver.php b/tcpserver.php index 098919e..96d9600 100644 --- a/tcpserver.php +++ b/tcpserver.php @@ -176,6 +176,18 @@ class tcpserver // {{{ { $this->logMethods (__METHOD__, func_get_args ()); + if (empty ($this->addresses)) + throw new \Exception (dgettext ("domframework", + "Can not start TCP server loop without addresses initialized"), 500); + if (empty ($this->ports)) + throw new \Exception (dgettext ("domframework", + "Can not start TCP server loop without port initialized"), 500); + foreach ($this->ports as $port) + { + if ($port < 1024 && posix_getuid() !== 0) + throw new \Exception (dgettext ("domframework", + "Can not start TCP server on port $port without root user"), 500); + } $this->logDebug ("Initialize the signal managers"); //declare (ticks = 1); pcntl_async_signals (true); @@ -359,7 +371,8 @@ class tcpserver ob_start (); // Catch the error messages from the application to not hang if triggered // An other handler can be set in function to execute - set_error_handler (function () {}); + set_error_handler ([$this, "errorHandler"]); + set_exception_handler ([$this, "exceptionHandler"]); @fclose (STDIN); @fclose (STDOUT); @fclose (STDERR); @@ -476,9 +489,7 @@ class tcpserver if ($this->socket === null) throw new \Exception ("Can not send to client : not connected", 500); $length = strlen ($data); -ob_start (); $sentLen = fwrite ($this->socket, $data); -$this->debug (ob_get_flush ()); if ($sentLen < $length) throw new \Exception ("Can not send data to client", 500); $this->logSend ($data); @@ -515,7 +526,7 @@ $this->debug (ob_get_flush ()); throw new \Exception ("Can not read from client : ". error_get_last ()["message"], 500); } - $this->logReceive ($data); + $this->logReceive ($read); return $read; } // }}} @@ -639,10 +650,25 @@ $this->debug (ob_get_flush ()); public function errorHandler ($errNo, $errMsg, $file, $line) // {{{ { + // @-operator : error suppressed + if (0 === error_reporting()) + return false; $this->logError ("line $line : $errMsg"); } // }}} + /** Exception catcher + * By default do nothing, but can be overrided by the user + * @param object $exception The exception to catch + */ + public function exceptionHandler ($exception) + // {{{ + { + $this->logError ("Exception ".$exception->getMessage () . " (". + $exception->getFile ().":".$exception->getLine().")"); + } + // }}} + ///////////////////////// // PRIVATE METHODS // /////////////////////////