From: Roland Häder Date: Tue, 10 May 2022 23:27:44 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=76ee8489e77523819d5c62fd53299ee6e5f246bc;p=hub.git Continued: - no need for two socket_write() invocations when you can just do the math in the if() block Signed-off-by: Roland Häder --- 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);