Skip to content

Commit

Permalink
Add error code and raise message too big error at sending time
Browse files Browse the repository at this point in the history
CURA-11103
  • Loading branch information
wawanbreton committed Sep 24, 2024
1 parent 0e24426 commit 0b4ae85
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
2 changes: 2 additions & 0 deletions include/Arcus/Error.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

/**
Expand Down
2 changes: 1 addition & 1 deletion include/Arcus/Socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 10 additions & 2 deletions src/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> lock(d->sendQueueMutex);
d->sendQueue.push_back(message);
return true;
}

MessagePtr Socket::takeNextMessage()
Expand Down
8 changes: 1 addition & 7 deletions src/Socket_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -338,20 +338,14 @@ 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)
{
error(ErrorCode::SendFailedError, "Could not send message header");
return;
}

const uint32_t message_size = message->ByteSizeLong();
if (platform_socket.writeUInt32(message_size) == -1)
{
error(ErrorCode::SendFailedError, "Could not send message size");
Expand Down

0 comments on commit 0b4ae85

Please sign in to comment.