From 4862a24aef5fd28fdb9c2254bbb052cd012ca91f Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Tue, 6 Oct 2015 20:40:49 +0200 Subject: [PATCH] Maybe right place here? Maybe total rewrite! MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../classes/class_BaseFrameworkSystem.php | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/inc/main/classes/class_BaseFrameworkSystem.php b/inc/main/classes/class_BaseFrameworkSystem.php index 74d92235..66473a4d 100644 --- a/inc/main/classes/class_BaseFrameworkSystem.php +++ b/inc/main/classes/class_BaseFrameworkSystem.php @@ -3436,6 +3436,73 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { // Return result return $stateName; } + + /** + * Handles socket error for given socket resource and peer data. This method + * validates $socketResource if it is a valid resource (see is_resource()) + * but assumes valid data in array $recipientData, except that + * count($recipientData) is always 2. + * + * @param $method Value of __METHOD__ from calling method + * @param $line Value of __LINE__ from calling method + * @param $socketResource A valid socket resource + * @param $socketData A valid socket data array (0 = IP/file name, 1 = port) + * @return void + * @throws InvalidSocketException If $socketResource is no socket resource + * @throws NoSocketErrorDetectedException If socket_last_error() gives zero back + */ + protected final function handleSocketError ($method, $line, $socketResource, array $socketData) { + // This method handles only socket resources + if (!is_resource($socketResource)) { + // No resource, abort here + throw new InvalidSocketException(array($this, $socketResource), BaseListener::EXCEPTION_INVALID_SOCKET); + } // END - if + + // Check socket array, 1st element is mostly IP address (or file name), 2nd is port number + //* DEBUG-DIE: */ die(__METHOD__ . ':socketData=' . print_r($socketData, TRUE)); + assert(isset($socketData[0])); + assert(isset($socketData[1])); + + // Get error code for first validation (0 is not an error) + $errorCode = socket_last_error($socketResource); + + // If the error code is zero, someone called this method without an error + if ($errorCode == 0) { + // No error detected (or previously cleared outside this method) + throw new NoSocketErrorDetectedException(array($this, $socketResource), BaseListener::EXCEPTION_NO_SOCKET_ERROR); + } // END - if + + // Get handler (method) name + $handlerName = $this->getSocketErrorHandlerFromCode($errorCode); + + // Call-back the error handler method + call_user_func_array(array($this, $handlerName), array($socketResource, $socketData)); + + // Finally clear the error because it has been handled + socket_clear_error($socketResource); + } + + /** + * Constructs a callable method name from given socket error code. If the + * method is not found, a generic one is used. + * + * @param $errorCode Error code from socket_last_error() + * @return $handlerName Call-back method name for the error handler + * @throws UnsupportedSocketErrorHandlerException If the error handler is not implemented + */ + protected function getSocketErrorHandlerFromCode ($errorCode) { + // Create a name from translated error code + $handlerName = 'socketError' . self::convertToClassName($this->translateSocketErrorCodeToName($errorCode)) . 'Handler'; + + // Is the call-back method there? + if (!method_exists($this, $handlerName)) { + // Please implement this + throw new UnsupportedSocketErrorHandlerException(array($this, $handlerName, $errorCode), BaseConnectionHelper::EXCEPTION_UNSUPPORTED_ERROR_HANDLER); + } // END - if + + // Return it + return $handlerName; + } } // [EOF] -- 2.39.2