]> git.mxchange.org Git - hub.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Sun, 21 May 2017 21:26:22 +0000 (23:26 +0200)
committerRoland Häder <roland@mxchange.org>
Fri, 21 Aug 2020 16:50:07 +0000 (18:50 +0200)
- Socket protocol names being used are now "abstracted" into constants (interface)
- no need to get last socket error code and then use it only once (per method)
- handle over only array values of packageData array to socket-error handler
- used these new constants more

Signed-off-by: Roland Häder <roland@mxchange.org>
application/hub/classes/container/socket/class_SocketContainer.php
application/hub/classes/factories/socket/class_SocketFactory.php
application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php
application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php
application/hub/classes/helper/connection/ipv4/udp/class_UdpConnectionHelper.php
application/hub/classes/listener/socket/class_SocketFileListener.php
application/hub/classes/resolver/state/peer/class_PeerStateResolver.php
application/hub/interfaces/container/socket/class_StorableSocket.php

index a0907188988849a2f6256f2dd127e5a7881541b7..5f95620d8d5738e21ad10b3fbe734978482755e0 100644 (file)
@@ -51,7 +51,7 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable
         * - 'tcp' for TCP/IPv4 connections
         * - 'file' for Unix* file-based sockets
         */
-       private $socketProtocol = 'invalid';
+       private $socketProtocol = StorableSocket::SOCKET_PROTOCOL_INVALID;
 
        /**
         * Protected constructor
@@ -162,6 +162,7 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable
         * Checks whether the stored socket resource is a server socket
         *
         * @return      $isServerSocket         Whether the stored socket resource is a server socket
+        * @throws      LogicException  If SOCKET_ARRAY_INDEX_FILE is not set in packageData
         */
        public function isServerSocketResource () {
                // Trace message
@@ -171,6 +172,21 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable
                $peerAddress = '0.0.0.0';
                $peerPort    = '0';
 
+               // Is socket file?
+               if ($this->getSocketProtocol() == StorableSocket::SOCKET_PROTOCOL_FILE) {
+                       // Get package data
+                       $packageData = $this->getPackageData();
+
+                       // Is it there?
+                       if (!isset($packageData[StorableSocket::SOCKET_ARRAY_INDEX_FILE])) {
+                               // Is not set
+                               throw new LogicException(sprintf('packageData[%s] is not set.', StorableSocket::SOCKET_ARRAY_INDEX_FILE));
+                       } // END - if
+
+                       // Set file as peer address
+                       $peerAddress = $packageData[StorableSocket::SOCKET_ARRAY_INDEX_FILE];
+               } // END - if
+
                // Check it
                $isServerSocket = (($this->isValidSocket()) && ($this->getSocketPeerName($peerAddress, $peerPort) === FALSE));
 
@@ -445,7 +461,7 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable
                return;
 
                // Clear any previous errors
-               socket_clear_error($this->getSocketResource());
+               $this->clearLastSocketError();
 
                // Call the shutdown function on the currently set socket
                if (!socket_shutdown($this->getSocketResource())) {
@@ -456,7 +472,7 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable
                        } // END - if
                } // END - if
 
-               // Try to make blocking IO for socket_close()
+               // Try to make blocking I/O for socket_close()
                socket_set_block($this->getSocketResource());
 
                // Drop all data (don't sent any on socket closure)
index 19301d24a01abc0b50c1fbc321821416d8c852e4..b057981e1ade4aafec949882d4443d1581c5ee67 100644 (file)
@@ -114,8 +114,8 @@ class SocketFactory extends ObjectFactory {
 
                // Init package data
                $packageData = array(
-                       $socketFile,
-                       '0',
+                       StorableSocket::SOCKET_ARRAY_INDEX_FILE => $socketFile,
+                       '__fake_port' => '0',
                );
 
                // Init main socket
@@ -123,7 +123,7 @@ class SocketFactory extends ObjectFactory {
 
                // Get container from it
                // @TODO Somehow handle $infoInstance to this factory
-               $socketInstance = self::createObjectByConfiguredName('socket_container_class', array($socketResource, 'file', $packageData, NULL));
+               $socketInstance = self::createObjectByConfiguredName('socket_container_class', array($socketResource, StorableSocket::SOCKET_PROTOCOL_FILE, $packageData, NULL));
 
                // Debug message
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FILE_LISTENER: socketInstance[]=%s', gettype($socketInstance)));
@@ -134,19 +134,16 @@ class SocketFactory extends ObjectFactory {
                        throw new InvalidSocketException(array($listenerInstance, $socketInstance->getSocketResource()), self::EXCEPTION_INVALID_SOCKET);
                } // END - if
 
-               // Get socket error code for verification
-               $socketError = $socketInstance->getLastSocketErrorCode();
-
                // Check if there was an error else
-               if ($socketError > 0) {
+               if ($socketInstance->getLastSocketErrorCode() > 0) {
                        // Handle this socket error with a faked recipientData array
-                       $socketInstance->handleSocketError(__METHOD__, __LINE__, array('null', '0'));
+                       $socketInstance->handleSocketError(__METHOD__, __LINE__, array_values($packageData));
                } // END - if
 
                // Is the file there?
-               if ((FrameworkBootstrap::isReachableFilePath($packageData[0])) && (file_exists($packageData[0]))) {
+               if ((FrameworkBootstrap::isReachableFilePath($packageData[StorableSocket::SOCKET_ARRAY_INDEX_FILE])) && (file_exists($packageData[StorableSocket::SOCKET_ARRAY_INDEX_FILE]))) {
                        // Old socket found
-                       self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: WARNING: Old socket at ' . $packageData[0] . ' found. Will not start.');
+                       self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: WARNING: Old socket at ' . $packageData[StorableSocket::SOCKET_ARRAY_INDEX_FILE] . ' found. Will not start.');
 
                        // Shutdown this socket
                        $socketInstance->shutdownSocket();
@@ -156,26 +153,26 @@ class SocketFactory extends ObjectFactory {
                } // END - if
 
                // Debug message
-               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: Binding to ' . $packageData[0] . ' ...');
+               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: Binding to ' . $packageData[StorableSocket::SOCKET_ARRAY_INDEX_FILE] . ' ...');
 
                // Try to bind to it
-               if (!$socketInstance->bindSocketTo($packageData[0])) {
+               if (!$socketInstance->bindSocketTo($packageData[StorableSocket::SOCKET_ARRAY_INDEX_FILE])) {
                        // Handle error here
-                       $socketInstance->handleSocketError(__METHOD__, __LINE__, $packageData);
+                       $socketInstance->handleSocketError(__METHOD__, __LINE__, array_values($packageData));
                } // END - if
 
                // Start listen for connections
                self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: Listening for connections.');
                if (!$socketInstance->listenOnSocket()) {
                        // Handle this socket error with a faked recipientData array
-                       $socketInstance->handleSocketError(__METHOD__, __LINE__, $packageData);
+                       $socketInstance->handleSocketError(__METHOD__, __LINE__, array_values($packageData));
                } // END - if
 
                // Allow non-blocking I/O
                self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: Setting non-blocking mode.');
                if (!$socketInstance->enableSocketNonBlocking()) {
                        // Handle this socket error with a faked recipientData array
-                       $socketInstance->handleSocketError(__METHOD__, __LINE__, $packageData);
+                       $socketInstance->handleSocketError(__METHOD__, __LINE__, array_values($packageData));
                } // END - if
 
                // Return socket instance
@@ -200,7 +197,7 @@ class SocketFactory extends ObjectFactory {
 
                // Construct container class, this won't be reached if an exception is thrown
                // @TODO Somehow handle $infoInstance to this factory
-               $socketInstance = ObjectFactory::createObjectByConfiguredName('socket_container_class', array($socketResource, 'tcp', $packageData, NULL));
+               $socketInstance = ObjectFactory::createObjectByConfiguredName('socket_container_class', array($socketResource, StorableSocket::SOCKET_PROTOCOL_TCP, $packageData, NULL));
 
                // Is the socket resource valid?
                if (!$socketInstance->isValidSocket()) {
@@ -212,11 +209,8 @@ class SocketFactory extends ObjectFactory {
                        throw new SocketCreationException(array($factoryInstance, $socketInstance->getSocketResource()), StorableSocket::EXCEPTION_SOCKET_CREATION_FAILED);
                } // END - if
 
-               // Get socket error code for verification
-               $socketError = $socketInstance->getLastSocketErrorCode();
-
                // Check if there was an error else
-               if ($socketError > 0) {
+               if ($socketInstance->getLastSocketErrorCode() > 0) {
                        // Handle this socket error with a faked recipientData array
                        $helperInstance->handleSocketError(__METHOD__, __LINE__, $socketInstance, array('0.0.0.0', '0'));
                } // END - if
@@ -257,7 +251,7 @@ class SocketFactory extends ObjectFactory {
                );
 
                // Create socket instance
-               $socketInstance = self::createObjectByConfiguredName('socket_container_class', array($socketResource, 'file', $packageData, NULL));
+               $socketInstance = self::createObjectByConfiguredName('socket_container_class', array($socketResource, StorableSocket::SOCKET_PROTOCOL_TCP, $packageData, NULL));
 
                // Is the socket resource valid?
                if (!$socketInstance->isValidSocket()) {
@@ -265,11 +259,8 @@ class SocketFactory extends ObjectFactory {
                        throw new InvalidSocketException(array($listenerInstance, $socketInstance), self::EXCEPTION_INVALID_SOCKET);
                } // END - if
 
-               // Get socket error code for verification
-               $socketError = $socketInstance->getLastSocketErrorCode();
-
                // Check if there was an error else
-               if ($socketError > 0) {
+               if ($socketInstance->getLastSocketErrorCode() > 0) {
                        // Handle this socket error with a faked recipientData array
                        $socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0'));
                } // END - if
index 09dfafda259a2abc5458293c0e7c9ba53e339949..ca374da4724618aef98989563986b58e6f29e02d 100644 (file)
@@ -85,7 +85,7 @@ class BaseIpV4ConnectionHelper extends BaseConnectionHelper {
                $timeout = $this->getConfigInstance()->getConfigEntry('socket_timeout_seconds');
 
                // Debug output
-               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: Trying to connect to ' . $unlInstance->getUnlAddress() . ':' . $unlInstance->getUnlPort() . ' with socketResource[' . gettype($this->getSocketInstance()->getSocketResource() . ']=' . $this->getSocketInstance()->getSocketResource() . ' ...');
+               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: Trying to connect to ' . $unlInstance->getUnlAddress() . ':' . $unlInstance->getUnlPort() . ' with socketResource[' . gettype($this->getSocketInstance()->getSocketResource()) . ']=' . $this->getSocketInstance()->getSocketResource() . ' ...');
 
                // Get current time
                $hasTimedOut = FALSE;
index cd6bf3fd0b6dab557954daf9cdb9678532a460b0..da33a3f54983b27f64d7715a9b6034fc6bd0d7bf 100644 (file)
@@ -3,6 +3,7 @@
 namespace Hub\Helper\Connection\Tcp;
 
 // Import application-specific stuff
+use Hub\Container\Socket\StorableSocket;
 use Hub\Factory\Socket\SocketFactory;
 use Hub\Helper\Connection\ConnectionHelper;
 use Hub\Locator\Node\LocateableNode;
@@ -44,7 +45,7 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection
                parent::__construct(__CLASS__);
 
                // Set protocol
-               $this->setProtocolName('tcp');
+               $this->setProtocolName(StorableSocket::SOCKET_PROTOCOL_TCP);
        }
 
        /**
@@ -60,7 +61,7 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection
                $helperInstance = new TcpConnectionHelper();
 
                // Construct container class, this won't be reached if an exception is thrown
-               $socketInstance = SocketFactory::createTcpOutgoingSocketFromPackageData($packageData));
+               $socketInstance = SocketFactory::createTcpOutgoingSocketFromPackageData($packageData);
 
                // Set the resource
                $helperInstance->setSocketInstance($socketInstance);
index 6630711d30f5ee68b6c5e6a4c129c92927cc70bf..bad59079faad0325d2c662a2dfe4081a11dfced2 100644 (file)
@@ -3,6 +3,7 @@
 namespace Hub\Helper\Connection\Udp;
 
 // Import application-specific stuff
+use Hub\Container\Socket\StorableSocket;
 use Hub\Helper\Connection\ConnectionHelper;
 
 /**
@@ -39,7 +40,7 @@ class UdpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection
                parent::__construct(__CLASS__);
 
                // Set protocol
-               $this->setProtocolName('udp');
+               $this->setProtocolName(StorableSocket::SOCKET_PROTOCOL_UDP);
        }
 
        /**
index 62a086ae33fc7985ba87b8ac895905a8dcb10f34..aec290c23eba515d01d86d0746afabe009b3736b 100644 (file)
@@ -3,6 +3,7 @@
 namespace Hub\Listener\Socket;
 
 // Import application-specific stuff
+use Hub\Container\Socket\StorableSocket;
 use Hub\Factory\Socket\SocketFactory;
 use Hub\Helper\Connection\BaseConnectionHelper;
 use Hub\Listener\BaseListener;
@@ -45,7 +46,7 @@ class SocketFileListener extends BaseListener implements Listenable {
                parent::__construct(__CLASS__);
 
                // Set the protocol to file
-               $this->setProtocolName('file');
+               $this->setProtocolName(StorableSocket::SOCKET_PROTOCOL_FILE);
        }
 
        /**
index 315afcd9747fcd004bcbf26804bd6a1a73b6c13c..bf689f6371b0ce0361d30ead81f14b9a8ee0cfce 100644 (file)
@@ -82,7 +82,7 @@ class PeerStateResolver extends BaseStateResolver implements StateResolver {
                        $socketInstance = $helperInstance->getSocketInstance();
 
                        // Still no socket resource?
-                       if (!$socketInstance->isValidSocket())) {
+                       if (!$socketInstance->isValidSocket()) {
                                // Then abort here with an exception (may happen after socket_shutdown())
                                throw new InvalidSocketException(array($helperInstance, $socketInstance->getSocketResource()), self::EXCEPTION_INVALID_SOCKET);
                        } // END - if
index e48a80439b733fe0fc18bc4d36fa13da61129102..13c8997cc464a782af758fa5849ecd89df516fe5 100644 (file)
@@ -47,6 +47,17 @@ interface StorableSocket extends FrameworkInterface {
        const SOCKET_ERROR_OPERATION_NOT_SUPPORTED    = 'operation_not_supported';    // 'Operation not supported'
        const SOCKET_CONNECTED                        = 'connected';                  // Nothing errorous happens, socket is connected
 
+       /**
+        * Socket file array index
+        */
+       const SOCKET_ARRAY_INDEX_FILE = 'socket_file';
+
+       // Socket protocols
+       const SOCKET_PROTOCOL_INVALID = 'invalid';
+       const SOCKET_PROTOCOL_FILE    = 'file';
+       const SOCKET_PROTOCOL_TCP     = 'tcp';
+       const SOCKET_PROTOCOL_UDP     = 'udp';
+
        /**
         * Tries to bind the socket.
         *