From: Roland Häder Date: Thu, 26 Dec 2024 05:38:14 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=eeaa5a13ef26a0a2a18d2c90d0e00e6dca74df3a;p=hub.git Continued: - starting with PHP 8, sockets are now no longer a scalar resource type, but a Socket class - updated 'core' framework --- diff --git a/application/hub/classes/container/socket/class_SocketContainer.php b/application/hub/classes/container/socket/class_SocketContainer.php index 49752835a..8619967bd 100644 --- a/application/hub/classes/container/socket/class_SocketContainer.php +++ b/application/hub/classes/container/socket/class_SocketContainer.php @@ -37,6 +37,7 @@ use Org\Mxchange\CoreFramework\Visitor\Visitor; use \BadMethodCallException; use \InvalidArgumentException; use \LogicException; +use \Socket; use \SplFileInfo; /** @@ -118,9 +119,9 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita private $senderPort; /** - * Socket resource + * Socket instance */ - private $socketResource = false; + private $socketResource = NULL; /** * Protected constructor @@ -148,20 +149,17 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita /** * Creates an instance of this Container class and prepares it for usage * - * @param $socketResource A valid socket resource + * @param $socketResource An instance of a Socket class * @param $socketProtocol Socket protocol (TCP, UDP, file) * @param $packageInstance An instance of a DeliverablePackage class * @param $socketType Socket type (incoming, outgoing, server, file, ...) * @return $socketInstance An instance of a StorableSocket class * @throws InvalidArgumentException If a parameter is not valid */ - public static final function createSocketContainer ($socketResource, string $socketProtocol, DeliverablePackage $packageInstance, string $socketType) { + public static final function createSocketContainer (Socket $socketResource, string $socketProtocol, DeliverablePackage $packageInstance, string $socketType) { // Validate parameter - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-CONTAINER: socketResource[%s]=%s,socketProtocol=%s,packageInstance=%s,socketType=%s - CALLED!', gettype($socketResource), $socketResource, $socketProtocol, $packageInstance->__toString(), $socketType)); - if (!is_resource($socketResource)) { - // Throw exception - throw new InvalidArgumentException(sprintf('socketResource[]=%s is not valid', $socketResource)); - } elseif (empty($socketProtocol)) { + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-CONTAINER: socketResource[]=%s,socketProtocol=%s,packageInstance=%s,socketType=%s - CALLED!', gettype($socketResource), $socketProtocol, $packageInstance->__toString(), $socketType)); + if (empty($socketProtocol)) { // Throw again throw new InvalidArgumentException('Parameter "socketProtocol" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } elseif (empty($socketType)) { @@ -219,13 +217,13 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita /** * Checks whether the given socket matches with stored * - * @param $socketResource A valid socket resource + * @param $socketResource An instance of a Socket class * @return $matches Whether given socket matches */ - public function ifSocketResourceMatches ($socketResource) { + public function ifSocketResourceMatches (Socket $socketResource) { // So, does both match? - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(strtoupper($this->getSocketProtocol()) . '-SOCKET: socketResource[' . gettype($socketResource) . ']=' .$socketResource . ',storedResource[' . gettype($this->getSocketResource()) . ']=' . $this->getSocketResource()); - $matches = ((is_resource($socketResource)) && ($socketResource === $this->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('%s-SOCKET: socketResource[]=%s,storedResource[]=%s - CALLED!', strtoupper($this->getSocketProtocol()), gettype($socketResource)), gettype($this->getSocketResource())); + $matches = ($socketResource === $this->getSocketResource()); // Return result /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('%s-SOCKET: matches=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($matches))); @@ -781,7 +779,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $socketResource = socket_accept($this->getSocketResource()); // Create socket instance from it - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('%s-SOCKET: this->socketResource=%s,serverSocket=%s', strtoupper($this->getSocketProtocol()), $socketResource, $this->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('%s-SOCKET: this->socketResource[]=%s,serverSocket=%s', strtoupper($this->getSocketProtocol()), gettype($socketResource), $this->getSocketResource())); $socketInstance = SocketFactory::createIncomingSocketInstance($socketResource, $this->getSocketProtocol()); // Return accepted socket instance @@ -1525,10 +1523,10 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita /** * Setter for socket resource * - * @param $socketResource A valid socket resource + * @param $socketResource An instance of a Socket class * @return void */ - public final function setSocketResource ($socketResource) { + public final function setSocketResource (Socket $socketResource) { $this->socketResource = $socketResource; } diff --git a/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseFrontend.php b/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseFrontend.php index 746056ef9..8d2d6f905 100644 --- a/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseFrontend.php +++ b/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseFrontend.php @@ -137,7 +137,7 @@ class PeerStateLookupDatabaseFrontend extends BaseHubDatabaseFrontend implements * Registers a new peer with given package data. We use the session id from it. * * @param $packageInstance An instance of a DeliverablePackage class - * @param $socketResource An instance of a StorableSocket class + * @param $socketInstance An instance of a StorableSocket class * @return void * @throws PeerAlreadyRegisteredException If a peer is already registered */ diff --git a/application/hub/classes/discovery/recipient/socket/class_PackageSocketDiscovery.php b/application/hub/classes/discovery/recipient/socket/class_PackageSocketDiscovery.php index dd5aeae34..f62c0ca5c 100644 --- a/application/hub/classes/discovery/recipient/socket/class_PackageSocketDiscovery.php +++ b/application/hub/classes/discovery/recipient/socket/class_PackageSocketDiscovery.php @@ -142,7 +142,7 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera * * @param $packageInstance An instance of a DeliverablePackage class array * @param $connectionType Type of connection, can be either 'incoming' or 'outgoing', but *NEVER* 'server'! - * @return $socketResource A valid socket resource or FALSE if an error occured + * @return $socketInstance An instance of a StorableSocket class * @throws NoListGroupException If the procol group is not found in peer list * @throws NullPointerException If listenerInstance is NULL * @throws InvalidUnlException If the provided UNL cannot be validated by the protocol handler diff --git a/application/hub/classes/factories/socket/class_SocketFactory.php b/application/hub/classes/factories/socket/class_SocketFactory.php index ebc874cbf..4e9ab32f7 100644 --- a/application/hub/classes/factories/socket/class_SocketFactory.php +++ b/application/hub/classes/factories/socket/class_SocketFactory.php @@ -22,6 +22,7 @@ use Org\Mxchange\CoreFramework\Socket\InvalidSocketException; // Import SPL stuff use \InvalidArgumentException; use \LogicException; +use \Socket; use \SplFileInfo; use \UnexpectedValueException; @@ -173,7 +174,7 @@ class SocketFactory extends BaseFactory { /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SOCKET-FACTORY: socketInstance=%s', $socketInstance->__toString())); if (!$socketInstance->isValidSocket()) { // Something bad happened - throw new InvalidSocketException(array($listenerInstance, $socketInstance->getSocketResource()), self::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException([$listenerInstance, $socketInstance->getSocketResource()], self::EXCEPTION_INVALID_SOCKET); } // Check if there was an error else @@ -235,14 +236,8 @@ class SocketFactory extends BaseFactory { /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: packageInstance=%s - CALLED!', $packageInstance->__toString())); $socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); - // Is it a socket resource? - /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SOCKET-FACTORY: socketResource[]=%s', gettype($socketResource))); - if (!is_resource($socketResource)) { - // Throw UVE - throw new UnexpectedValueException(sprintf('socketResource[]=%s is not expected, could not create socket', gettype($socketResource))); - } - // Create socket instance + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SOCKET-FACTORY: socketResource[]=%s', gettype($socketResource))); $socketInstance = ObjectFactory::createObjectByConfiguredName('socket_container_class', [ $socketResource, StorableSocket::SOCKET_PROTOCOL_TCP, @@ -303,12 +298,8 @@ class SocketFactory extends BaseFactory { /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: listenerInstance=%s - CALLED!', $listenerInstance->__toString())); $socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); - // Is it a socket resource? + // Debug message /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SOCKET-FACTORY: socketResource[]=%s', gettype($socketResource))); - if (!is_resource($socketResource)) { - // Throw UVE - throw new UnexpectedValueException(sprintf('socketResource[]=%s is not expected, could not create socket', gettype($socketResource))); - } // Init fake "package" instance, the SocketContainer class requires this $packageInstance = ObjectFactory::createObjectByConfiguredName('package_data_class'); @@ -389,12 +380,8 @@ class SocketFactory extends BaseFactory { /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: listenerInstance=%s - CALLED!', $listenerInstance->__toString())); $socketResource = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); - // Is it a socket resource? + // Debug message /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SOCKET-FACTORY: socketResource[]=%s', gettype($socketResource))); - if (!is_resource($socketResource)) { - // Throw UVE - throw new UnexpectedValueException(sprintf('socketResource[]=%s is not expected, could not create socket', gettype($socketResource))); - } // Init fake "package" instance, the SocketContainer class requires this $packageInstance = ObjectFactory::createObjectByConfiguredName('package_data_class'); @@ -416,7 +403,7 @@ class SocketFactory extends BaseFactory { /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SOCKET-FACTORY: socketInstance=%s', $socketInstance->__toString())); if (!$socketInstance->isValidSocket()) { // Something bad happened - throw new InvalidSocketException(array($listenerInstance, $socketInstance), self::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException([$listenerInstance, $socketInstance], self::EXCEPTION_INVALID_SOCKET); } // Check if there was an error else @@ -532,13 +519,10 @@ class SocketFactory extends BaseFactory { * @return $socketInstance An instance of a StorableSocket class * @throws InvalidArgumentException If a parameter is not valid */ - public static final function createIncomingSocketInstance ($socketResource, string $socketProtocol) { + public static final function createIncomingSocketInstance (Socket $socketResource, string $socketProtocol) { // Validate parameter - /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: socketResource[%s]=%s,socketProtocol=%s - CALLED!', gettype($socketResource), $socketResource, $socketProtocol)); - if (!is_resource($socketResource)) { - // Throw exception - throw new InvalidArgumentException(sprintf('socketResource[]=%s is not valid', gettype($socketResource))); - } elseif (empty($socketProtocol)) { + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: socketResource[]=%s,socketProtocol=%s - CALLED!', gettype($socketResource), $socketProtocol)); + if (empty($socketProtocol)) { // Throw it again throw new InvalidArgumentException('Parameter "socketProtocol" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } diff --git a/application/hub/classes/pools/peer/class_DefaultPeerPool.php b/application/hub/classes/pools/peer/class_DefaultPeerPool.php index 053d419cd..bd2ccd5c9 100644 --- a/application/hub/classes/pools/peer/class_DefaultPeerPool.php +++ b/application/hub/classes/pools/peer/class_DefaultPeerPool.php @@ -83,7 +83,7 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-SYSTEM: socketInstance->socketResource=%s - CALLED!', $socketInstance->getSocketResource())); if (!$socketInstance->isValidSocket()) { // Throw an exception - throw new InvalidSocketException(array($this, $socketInstance->getSocketResource()), self::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException([$this, $socketInstance->getSocketResource()], self::EXCEPTION_INVALID_SOCKET); } // Get error code diff --git a/application/hub/interfaces/container/socket/class_StorableSocket.php b/application/hub/interfaces/container/socket/class_StorableSocket.php index 94013338f..f6754ece3 100644 --- a/application/hub/interfaces/container/socket/class_StorableSocket.php +++ b/application/hub/interfaces/container/socket/class_StorableSocket.php @@ -9,6 +9,7 @@ use Org\Shipsimu\Hub\Information\ShareableInfo; use Org\Mxchange\CoreFramework\Generic\FrameworkInterface; // Import PHP stuff +use \Socket; use \SplFileInfo; /** @@ -224,10 +225,10 @@ interface StorableSocket extends FrameworkInterface { /** * Checks whether the given socket matches with stored * - * @param $socketResource A valid socket resource + * @param $socketResource An instance of a Socket class * @return $matches Whether given socket matches */ - function ifSocketResourceMatches ($socketResource); + function ifSocketResourceMatches (Socket $socketResource); /** * Shuts down a given socket resource. This method does only ease calling diff --git a/core b/core index f993e439e..3ad2578dc 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit f993e439e838c8cea3a98b1254c2cf340d68bc77 +Subproject commit 3ad2578dc223f97de06547be49d66e7adfea677c