]> git.mxchange.org Git - hub.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Wed, 11 May 2022 00:08:20 +0000 (02:08 +0200)
committerRoland Häder <roland@mxchange.org>
Wed, 11 May 2022 01:52:53 +0000 (03:52 +0200)
- rewrote more assert() calls in a public method to hard exception throws
- ops, $bufferSize is 2nd parameter in this mask
- enabled a debug line

Signed-off-by: Roland Häder <roland@mxchange.org>
application/hub/classes/container/socket/class_SocketContainer.php
application/hub/classes/handler/package/class_NetworkPackageHandler.php
application/hub/interfaces/delivery/class_Deliverable.php

index 94970a1672f6f462deb7f35913a4b2ac4610a4a4..fc7f746aeb8c7bddce8f93f80b2a666fb95c16d8 100644 (file)
@@ -969,7 +969,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita
                $bufferSize = $socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE];
 
                // Is some data still pending or sent all out?
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: bufferSize=%d,socketBuffer[%s]=%d', strtoupper($this->getSocketProtocol()), NetworkPackageHandler::RAW_INDEX_DIFF, $socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF]));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: bufferSize=%d,socketBuffer[%s]=%d', strtoupper($this->getSocketProtocol()), $bufferSize, NetworkPackageHandler::RAW_INDEX_DIFF, $socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF]));
                if ($socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF] >= 0) {
                        // Reduce buffer size by difference
                        $bufferSize = $socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE] - $socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF];
index f88f4f159dff4fd4c59791e1dbd0f86b95823864..8d93f13130372248461054bf30aa02766e663db1 100644 (file)
@@ -51,6 +51,7 @@ use \BadMethodCallException;
 use \InvalidArgumentException;
 use \Iterator;
 use \LogicException;
+use \OutOfBoundsException;
 use \UnexpectedValueException;
 
 /**
@@ -1112,22 +1113,37 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei
         * Sends out encoded data to a socket
         *
         * @return      void
+        * @throws      BadMethodCallException  If this method was unexpectedly called
+        * @throws      OutOfBoundsException    If an important array element was not found
+        * @throws      UnexpectedValueException        If an instance of StorableSocket was expected but not provided
+        * @throws      LogicException  If the socket is somehow invalid but this method was called
         */
        public function sendEncodedData () {
                // Make sure there is pending encoded data
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: CALLED!');
-               assert($this->isEncodedDataPending());
+               if (!$this->isEncodedDataPending()) {
+                       // Throw exception
+                       throw new BadMethodCallException('No encoded data is pending but this method was called.');
+               }
 
                // Pop current data from stack
                $encodedDataArray = $this->getStackInstance()->popNamed(self::STACKER_NAME_OUTGOING_STREAM);
 
                // Init in this round sent bytes
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: encodedDataArray()=%d has been "popped" from stack %s.', count($encodedDataArray), self::STACKER_NAME_OUTGOING_STREAM));
                $sentBytes = 0;
 
-               // Assert on socket instance
-               assert(isset($encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE]));
-               assert($encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE] instanceof StorableSocket);
-               assert($encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE]->isValidSocket());
+               // Check all conditions, throw exceptions if something unexpected happened
+               if (!isset($encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE])) {
+                       // Array element not found
+                       throw new OutOfBoundsException(sprintf('encodedDataArray has no element %s', self::RAW_INDEX_SOCKET_INSTANCE));
+               } elseif (!($encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE] instanceof StorableSocket)) {
+                       // Is not a valid instance
+                       throw new UnexpectedValueException(sprintf('encodedDataArray[%s] is not an instance of %s', self::RAW_INDEX_SOCKET_INSTANCE, StorableSocket::class));
+               } elseif (!$encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE]->isValidSocket()) {
+                       // Invalid socket
+                       throw new LogicException(sprintf('encodedDataArray[%s]->socketResource=%s is invalid for some reason', self::RAW_INDEX_SOCKET_INSTANCE, $encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE]->getSocketResource()));
+               }
 
                // Get socket instance and remove it from array
                $socketInstance = $encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE];
@@ -1618,7 +1634,7 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei
 
                // Generate the hash of comparing it
                //* DEBUG-DIE: */ ApplicationEntryPoint::exitApplication(sprintf('[%s:%d]: packageInstance=%s', __METHOD__, __LINE__, print_r($packageInstance, TRUE)));
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s,senderId=%s,senderPrivateKeyHash=%s', $packageInstance->__toString(), $packageInstance->getSenderId(), $packageInstance->getSenderPrivateKeyHash()));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s,senderId=%s,senderPrivateKeyHash=%s', $packageInstance->__toString(), $packageInstance->getSenderId(), $packageInstance->getSenderPrivateKeyHash()));
                if (empty($packageInstance->getSenderId())) {
                        // Invalid $packageInstance
                        throw new InvalidArgumentException('packageInstance does not contain senderId');
index 30e6603c3e6e45ae5cadb8e0df350395ce2b83e2..18df994b8207f2734d7adec7295db28d8d272530 100644 (file)
@@ -100,6 +100,9 @@ interface Deliverable extends HubInterface {
         * Sends pending encoded (raw) data
         *
         * @return      void
+        * @throws      BadMethodCallException  If this method was unexpectedly called
+        * @throws      OutOfBoundsException    If an important array element was not found
+        * @throws      UnexpectedValueException        If an instance of StorableSocket was expected but not provided
         */
        function sendEncodedData ();