Skip to content

Commit 0b4ae85

Browse files
committed
Add error code and raise message too big error at sending time
CURA-11103
1 parent 0e24426 commit 0b4ae85

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

include/Arcus/Error.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ enum class ErrorCode
2828
InvalidStateError, ///< Socket is in an invalid state.
2929
InvalidMessageError, ///< Message being handled is a nullptr or otherwise invalid.
3030
Debug, // Debug messages
31+
32+
// When changing this list, don't forget to apply the same changes on pyArcus/python/Error.sip
3133
};
3234

3335
/**

include/Arcus/Socket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class Socket
114114
/**
115115
* Send a message across the socket.
116116
*/
117-
virtual void sendMessage(MessagePtr message);
117+
virtual bool sendMessage(MessagePtr message);
118118

119119
/**
120120
* Remove and return the next pending message from the queue with condition blocking.

src/Socket.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,21 +203,29 @@ void Socket::close()
203203
delete d->thread;
204204
d->thread = nullptr;
205205
}
206+
206207
// Notify all in case of closing because the waiting threads need to know
207208
// that this socket has been closed and they should not wait any more.
208209
d->message_received_condition_variable.notify_all();
209210
}
210211

211-
void Socket::sendMessage(MessagePtr message)
212+
bool Socket::sendMessage(MessagePtr message)
212213
{
213214
if (! message)
214215
{
215216
d->error(ErrorCode::InvalidMessageError, "Message cannot be nullptr");
216-
return;
217+
return false;
218+
}
219+
220+
if (message->ByteSizeLong() > Private::message_size_maximum)
221+
{
222+
d->error(ErrorCode::MessageTooBigError, "Message is too big to be sent");
223+
return false;
217224
}
218225

219226
std::lock_guard<std::mutex> lock(d->sendQueueMutex);
220227
d->sendQueue.push_back(message);
228+
return true;
221229
}
222230

223231
MessagePtr Socket::takeNextMessage()

src/Socket_p.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -338,20 +338,14 @@ void Socket::Private::run()
338338
// Send a message to the connected socket.
339339
void Socket::Private::sendMessage(const MessagePtr& message)
340340
{
341-
const uint32_t message_size = message->ByteSizeLong();
342-
if (message_size > message_size_maximum)
343-
{
344-
error(ErrorCode::MessageTooBigError, "Message is too big to be sent");
345-
return;
346-
}
347-
348341
const uint32_t header = (ARCUS_SIGNATURE << 16) | (VERSION_MAJOR << 8) | (VERSION_MINOR);
349342
if (platform_socket.writeUInt32(header) == -1)
350343
{
351344
error(ErrorCode::SendFailedError, "Could not send message header");
352345
return;
353346
}
354347

348+
const uint32_t message_size = message->ByteSizeLong();
355349
if (platform_socket.writeUInt32(message_size) == -1)
356350
{
357351
error(ErrorCode::SendFailedError, "Could not send message size");

0 commit comments

Comments
 (0)