From 76ee8489e77523819d5c62fd53299ee6e5f246bc Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Wed, 11 May 2022 01:27:44 +0200 Subject: [PATCH] Continued: - no need for two socket_write() invocations when you can just do the math in the if() block MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../container/socket/class_SocketContainer.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/application/hub/classes/container/socket/class_SocketContainer.php b/application/hub/classes/container/socket/class_SocketContainer.php index 43cebca34..7e72cfe8a 100644 --- a/application/hub/classes/container/socket/class_SocketContainer.php +++ b/application/hub/classes/container/socket/class_SocketContainer.php @@ -965,18 +965,20 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita throw new InvalidArgumentException(sprintf('socketBuffer[%s] is not set.', NetworkPackageHandler::RAW_INDEX_SENT_BYTES)); } + // Initial buffer size + $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: Sending out %d bytes,rawBufferSize=%d,diff=%d to this->socketResource=%s', strtoupper($this->getSocketProtocol()), strlen($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA]), $socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE], $socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF], $this->getSocketResource())); + /* 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])); if ($socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF] >= 0) { - // Send all out (encodedData is smaller than or equal buffer size) - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: MD5=%s', strtoupper($this->getSocketProtocol()), md5(substr($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA], 0, ($socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE] - $socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF]))))); - $sentBytes = socket_write($this->getSocketResource(), $socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA], ($socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE] - $socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF])); - } else { - // Send buffer size out - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: MD5=%s', strtoupper($this->getSocketProtocol()), md5(substr($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA], 0, $socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE])))); - $sentBytes = socket_write($this->getSocketResource(), $socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA], $socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE]); + // Reduce buffer size by difference + $bufferSize = $socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE] - $socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF]; } + // Send buffered raw data + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Invoking socket_write(%s,()=%d,%d) ...', strtoupper($this->getSocketProtocol()), $this->getSocketResource(), strlen($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA]), $bufferSize)); + $sentBytes = socket_write($this->getSocketResource(), $socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA], $bufferSize); + // Check if the operation was okay // @TODO Maybe check more? $result = ($sentBytes !== FALSE); -- 2.39.5