From f40943be102e4f91537b037cb408be43240697e3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 21 May 2017 23:26:22 +0200 Subject: [PATCH] Continued: - 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 MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../socket/class_SocketContainer.php | 22 ++++++++-- .../factories/socket/class_SocketFactory.php | 41 ++++++++----------- .../ipv4/class_BaseIpV4ConnectionHelper.php | 2 +- .../ipv4/tcp/class_TcpConnectionHelper.php | 5 ++- .../ipv4/udp/class_UdpConnectionHelper.php | 3 +- .../socket/class_SocketFileListener.php | 3 +- .../state/peer/class_PeerStateResolver.php | 2 +- .../container/socket/class_StorableSocket.php | 11 +++++ 8 files changed, 55 insertions(+), 34 deletions(-) diff --git a/application/hub/classes/container/socket/class_SocketContainer.php b/application/hub/classes/container/socket/class_SocketContainer.php index a09071889..5f95620d8 100644 --- a/application/hub/classes/container/socket/class_SocketContainer.php +++ b/application/hub/classes/container/socket/class_SocketContainer.php @@ -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) diff --git a/application/hub/classes/factories/socket/class_SocketFactory.php b/application/hub/classes/factories/socket/class_SocketFactory.php index 19301d24a..b057981e1 100644 --- a/application/hub/classes/factories/socket/class_SocketFactory.php +++ b/application/hub/classes/factories/socket/class_SocketFactory.php @@ -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 diff --git a/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php b/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php index 09dfafda2..ca374da47 100644 --- a/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php +++ b/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php @@ -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; diff --git a/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php b/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php index cd6bf3fd0..da33a3f54 100644 --- a/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php +++ b/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php @@ -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); diff --git a/application/hub/classes/helper/connection/ipv4/udp/class_UdpConnectionHelper.php b/application/hub/classes/helper/connection/ipv4/udp/class_UdpConnectionHelper.php index 6630711d3..bad59079f 100644 --- a/application/hub/classes/helper/connection/ipv4/udp/class_UdpConnectionHelper.php +++ b/application/hub/classes/helper/connection/ipv4/udp/class_UdpConnectionHelper.php @@ -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); } /** diff --git a/application/hub/classes/listener/socket/class_SocketFileListener.php b/application/hub/classes/listener/socket/class_SocketFileListener.php index 62a086ae3..aec290c23 100644 --- a/application/hub/classes/listener/socket/class_SocketFileListener.php +++ b/application/hub/classes/listener/socket/class_SocketFileListener.php @@ -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); } /** diff --git a/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php b/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php index 315afcd97..bf689f637 100644 --- a/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php +++ b/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php @@ -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 diff --git a/application/hub/interfaces/container/socket/class_StorableSocket.php b/application/hub/interfaces/container/socket/class_StorableSocket.php index e48a80439..13c8997cc 100644 --- a/application/hub/interfaces/container/socket/class_StorableSocket.php +++ b/application/hub/interfaces/container/socket/class_StorableSocket.php @@ -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. * -- 2.39.5