From: Roland Häder Date: Sun, 12 Dec 2021 20:48:53 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=67174ab94dcb721d9589315f58da6a8dee38019f;p=hub.git Continued: - converted array to static - "cached" configuration entry Signed-off-by: Roland Häder --- diff --git a/application/hub/classes/container/socket/class_SocketContainer.php b/application/hub/classes/container/socket/class_SocketContainer.php index 555142052..e574a84b8 100644 --- a/application/hub/classes/container/socket/class_SocketContainer.php +++ b/application/hub/classes/container/socket/class_SocketContainer.php @@ -64,21 +64,21 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita use HelperTrait; /** - * Socket protocol: - * - 'tcp' for TCP/IPv4 connections - * - 'file' for Uni* file-based sockets + * "Cached" configuration entries */ - private $socketProtocol = StorableSocket::SOCKET_PROTOCOL_INVALID; + private static $configEntries = []; /** * An array with all valid connection types */ - private $connectionTypes = []; + private static $connectionTypes = []; /** - * Timeout in usec for socket_select() + * Socket protocol: + * - 'tcp' for TCP/IPv4 connections + * - 'file' for Uni* file-based sockets */ - private $socketSelectTimeout = 0; + private $socketProtocol = StorableSocket::SOCKET_PROTOCOL_INVALID; /** * Package data instance @@ -129,15 +129,18 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita // Call parent constructor parent::__construct(__CLASS__); - // Init value - $this->socketSelectTimeout = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('socket_select_timeout_usec'); - - // Init array of connection types - $this->connectionTypes = array( - StorableSocket::CONNECTION_TYPE_INCOMING, - StorableSocket::CONNECTION_TYPE_OUTGOING, - StorableSocket::CONNECTION_TYPE_SERVER - ); + // Is the "cache" there? + if (count(self::$configEntries) == 0) { + // Init value + self::$configEntries['socket_select_timeout'] = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('socket_select_timeout_usec'); + + // Init array of connection types + self::$connectionTypes = [ + StorableSocket::CONNECTION_TYPE_INCOMING, + StorableSocket::CONNECTION_TYPE_OUTGOING, + StorableSocket::CONNECTION_TYPE_SERVER, + ]; + } } /** @@ -297,7 +300,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: peerAddress[%s]=%s,peerPort[%s]=%d - CALLED!', strtoupper($this->getSocketProtocol()), gettype($peerAddress), $peerAddress, gettype($peerPort), $peerPort)); if (!$this->isValidSocket()) { // Throw exception - throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET); } // Init result @@ -343,7 +346,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception - throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET); } // Get package data @@ -365,7 +368,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception - throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET); } // Get recipient @@ -394,7 +397,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception - throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET); } // Get recipient UNL @@ -439,7 +442,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception - throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET); } // Get error code from socket @@ -461,7 +464,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception - throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET); } // Get error code and message @@ -503,7 +506,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception - throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET); } elseif (is_null($this->getSocketFile())) { // Throw exception throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); @@ -532,7 +535,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception - throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET); } elseif (is_null($this->getListenerInstance())) { // Required listener not set throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); @@ -562,7 +565,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception - throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET); } // Try to listen on socket @@ -584,7 +587,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception - throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET); } // Try to set non-blocking I/O @@ -606,7 +609,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception - throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET); } // Tries to set option @@ -628,7 +631,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception - throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET); } // Get recipient UNL @@ -713,7 +716,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita } // Is it valid? - $isValid = in_array($connectionType, $this->connectionTypes, TRUE); + $isValid = in_array($connectionType, self::$connectionTypes, TRUE); // Return result /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: isValid=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($isValid))); @@ -731,7 +734,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception - throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET); } // Init all arrays, at least readers @@ -741,13 +744,13 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $excepts = []; // Check if we have some peers left - /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Calling socket_select(%d,%d,%d,0%d) ...', strtoupper($this->getSocketProtocol()), count($readers), count($writers), count($excepts), $this->socketSelectTimeout)); + /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Calling socket_select(%d,%d,%d,0%d) ...', strtoupper($this->getSocketProtocol()), count($readers), count($writers), count($excepts), self::$configEntries['socket_select_timeout'])); $left = socket_select( $readers, $writers, $excepts, 0, - $this->socketSelectTimeout + self::$configEntries['socket_select_timeout'] ); // Some new peers found? @@ -791,7 +794,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception - throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET); } /* @@ -881,7 +884,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Clearing socket error, this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception - throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET); } elseif ($this->getLastSocketErrorCode() === 0) { // Nothing to clear throw new BadMethodCallException(sprintf('Socket "%s" has no error reported, but method is called.', $this->getSocketResource())); @@ -943,7 +946,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: socketBuffer=%s', strtoupper($this->getSocketProtocol()), print_r($socketBuffer, TRUE))); if (!$this->isValidSocket()) { // Throw exception - throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET); } elseif (!isset($socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF])) { // Opps, should not happen throw new InvalidArgumentException(sprintf('socketBuffer[%s] is not set.', NetworkPackageHandler::RAW_INDEX_DIFF)); @@ -1021,7 +1024,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: method=%s,line=%d,lastSocketErrorCode=%d - CALLED!', strtoupper($this->getSocketProtocol()), $method, $line, $this->getLastSocketErrorCode())); if (!$this->isValidSocket()) { // No resource, abort here - throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET); } // Get error code for first validation (0 is not an error) @@ -1030,7 +1033,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita // 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), BaseListener::EXCEPTION_NO_SOCKET_ERROR); + throw new NoSocketErrorDetectedException([$this], BaseListener::EXCEPTION_NO_SOCKET_ERROR); } // Get handler (method) name @@ -1356,7 +1359,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: level=%d,optionName=%d,optionValue[]=%s - CALLED!', strtoupper($this->getSocketProtocol()), $level, $optionName, gettype($optionValue))); if (!$this->isValidSocket()) { // Throw exception - throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET); } // Set it