From 00447db0aafe5cf811146fec9796bca227ece715 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Wed, 11 May 2022 02:08:20 +0200 Subject: [PATCH] Continued: - rewrote more assert() calls in a public method to hard exception throws - ops, $bufferSize is 2nd parameter in this mask - enabled a debug line MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../socket/class_SocketContainer.php | 2 +- .../package/class_NetworkPackageHandler.php | 28 +++++++++++++++---- .../interfaces/delivery/class_Deliverable.php | 3 ++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/application/hub/classes/container/socket/class_SocketContainer.php b/application/hub/classes/container/socket/class_SocketContainer.php index 94970a167..fc7f746ae 100644 --- a/application/hub/classes/container/socket/class_SocketContainer.php +++ b/application/hub/classes/container/socket/class_SocketContainer.php @@ -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]; diff --git a/application/hub/classes/handler/package/class_NetworkPackageHandler.php b/application/hub/classes/handler/package/class_NetworkPackageHandler.php index f88f4f159..8d93f1313 100644 --- a/application/hub/classes/handler/package/class_NetworkPackageHandler.php +++ b/application/hub/classes/handler/package/class_NetworkPackageHandler.php @@ -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'); diff --git a/application/hub/interfaces/delivery/class_Deliverable.php b/application/hub/interfaces/delivery/class_Deliverable.php index 30e6603c3..18df994b8 100644 --- a/application/hub/interfaces/delivery/class_Deliverable.php +++ b/application/hub/interfaces/delivery/class_Deliverable.php @@ -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 (); -- 2.39.5