From 0b4ae857f2bea31cfab79a3ef8e5caaf5ec7a3d1 Mon Sep 17 00:00:00 2001 From: Erwan MATHIEU Date: Tue, 24 Sep 2024 08:50:46 +0200 Subject: [PATCH] Add error code and raise message too big error at sending time CURA-11103 --- include/Arcus/Error.h | 2 ++ include/Arcus/Socket.h | 2 +- src/Socket.cpp | 12 ++++++++++-- src/Socket_p.h | 8 +------- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/include/Arcus/Error.h b/include/Arcus/Error.h index 0260887d..a35968ba 100644 --- a/include/Arcus/Error.h +++ b/include/Arcus/Error.h @@ -28,6 +28,8 @@ enum class ErrorCode InvalidStateError, ///< Socket is in an invalid state. InvalidMessageError, ///< Message being handled is a nullptr or otherwise invalid. Debug, // Debug messages + + // When changing this list, don't forget to apply the same changes on pyArcus/python/Error.sip }; /** diff --git a/include/Arcus/Socket.h b/include/Arcus/Socket.h index 8c6b3d61..dcb83994 100644 --- a/include/Arcus/Socket.h +++ b/include/Arcus/Socket.h @@ -114,7 +114,7 @@ class Socket /** * Send a message across the socket. */ - virtual void sendMessage(MessagePtr message); + virtual bool sendMessage(MessagePtr message); /** * Remove and return the next pending message from the queue with condition blocking. diff --git a/src/Socket.cpp b/src/Socket.cpp index 158c68ea..69487cac 100644 --- a/src/Socket.cpp +++ b/src/Socket.cpp @@ -203,21 +203,29 @@ void Socket::close() delete d->thread; d->thread = nullptr; } + // Notify all in case of closing because the waiting threads need to know // that this socket has been closed and they should not wait any more. d->message_received_condition_variable.notify_all(); } -void Socket::sendMessage(MessagePtr message) +bool Socket::sendMessage(MessagePtr message) { if (! message) { d->error(ErrorCode::InvalidMessageError, "Message cannot be nullptr"); - return; + return false; + } + + if (message->ByteSizeLong() > Private::message_size_maximum) + { + d->error(ErrorCode::MessageTooBigError, "Message is too big to be sent"); + return false; } std::lock_guard lock(d->sendQueueMutex); d->sendQueue.push_back(message); + return true; } MessagePtr Socket::takeNextMessage() diff --git a/src/Socket_p.h b/src/Socket_p.h index 60a46b97..33f5873f 100644 --- a/src/Socket_p.h +++ b/src/Socket_p.h @@ -338,13 +338,6 @@ void Socket::Private::run() // Send a message to the connected socket. void Socket::Private::sendMessage(const MessagePtr& message) { - const uint32_t message_size = message->ByteSizeLong(); - if (message_size > message_size_maximum) - { - error(ErrorCode::MessageTooBigError, "Message is too big to be sent"); - return; - } - const uint32_t header = (ARCUS_SIGNATURE << 16) | (VERSION_MAJOR << 8) | (VERSION_MINOR); if (platform_socket.writeUInt32(header) == -1) { @@ -352,6 +345,7 @@ void Socket::Private::sendMessage(const MessagePtr& message) return; } + const uint32_t message_size = message->ByteSizeLong(); if (platform_socket.writeUInt32(message_size) == -1) { error(ErrorCode::SendFailedError, "Could not send message size");