@@ -338,37 +338,41 @@ void Socket::Private::run()
338338// Send a message to the connected socket.
339339void Socket::Private::sendMessage (const MessagePtr& message)
340340{
341- uint32_t header = (ARCUS_SIGNATURE << 16 ) | (VERSION_MAJOR << 8 ) | (VERSION_MINOR);
341+ const uint32_t message_size = message->ByteSizeLong ();
342+ if (message_size > message_size_maximum)
343+ {
344+ error (ErrorCode::SendFailedError, " Message is too big to be sent" );
345+ return ;
346+ }
347+
348+ const uint32_t header = (ARCUS_SIGNATURE << 16 ) | (VERSION_MAJOR << 8 ) | (VERSION_MINOR);
342349 if (platform_socket.writeUInt32 (header) == -1 )
343350 {
344351 error (ErrorCode::SendFailedError, " Could not send message header" );
345352 return ;
346353 }
347354
348- uint32_t message_size = message->ByteSizeLong ();
349355 if (platform_socket.writeUInt32 (message_size) == -1 )
350356 {
351357 error (ErrorCode::SendFailedError, " Could not send message size" );
352358 return ;
353359 }
354360
355- uint32_t type_id = message_types.getMessageTypeId (message);
361+ const uint32_t type_id = message_types.getMessageTypeId (message);
356362 if (platform_socket.writeUInt32 (type_id) == -1 )
357363 {
358364 error (ErrorCode::SendFailedError, " Could not send message type" );
359365 return ;
360366 }
361367
362- std::string data = message->SerializeAsString ();
363- if (data.size () > message_size_maximum)
364- {
365- error (ErrorCode::SendFailedError, " Message is too big to be sent" );
366- }
367- else if (platform_socket.writeBytes (data.size (), data.data ()) == -1 )
368+ const std::string data = message->SerializeAsString ();
369+ if (platform_socket.writeBytes (data.size (), data.data ()) == -1 )
368370 {
369371 error (ErrorCode::SendFailedError, " Could not send message data" );
372+ return ;
370373 }
371- DEBUG (std::string (" Sending message of type " ) + std::to_string (type_id) + " and size " + std::to_string (message_size));
374+
375+ DEBUG (std::string (" Sent message of type " ) + std::to_string (type_id) + " and size " + std::to_string (message_size));
372376}
373377
374378// Handle receiving data until we have a proper message.
0 commit comments