From 2e9adbe31049711548c0f2e413e7ef4e94363246 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Thu, 30 Apr 2020 12:32:27 +0200 Subject: [PATCH] Always write message sizes in 64-bit integers This will make sure the sizes match on both the 32 and 64 bit host applications. This way only the 32-bit application will have to convert between 32 and 64 bit integers. --- src/common/communication.h | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/common/communication.h b/src/common/communication.h index 54c88ef9..4cc5086c 100644 --- a/src/common/communication.h +++ b/src/common/communication.h @@ -33,7 +33,8 @@ template using InputAdapter = bitsery::InputBufferAdapter; /** - * Serialize an object using bitsery and write it to a socket. + * Serialize an object using bitsery and write it to a socket. This will write + * both the size of the serialized object and the object itself over the socket. * * @param socket The Boost.Asio socket to write to. * @param object The object to write to the stream. @@ -53,8 +54,13 @@ inline void write_object( // Tell the other side how large the object is so it can prepare a buffer // large enough before sending the data + // NOTE: We're writing these sizes as a 64 bit integers, **not** as pointer + // sized integers. This is to provide compatibility with the 32-bit + // bit bridge. This won't make any function difference aside from the + // 32-bit host application having to convert between 64 and 32 bit + // integers. boost::asio::write(socket, - boost::asio::buffer(std::array{size})); + boost::asio::buffer(std::array{size})); const size_t bytes_written = boost::asio::write(socket, boost::asio::buffer(buffer, size)); assert(bytes_written == size); @@ -79,7 +85,8 @@ template inline T& read_object(Socket& socket, T& object, std::vector buffer = std::vector(64)) { - std::array message_length; + // See the note above on the use of `uint64_t` instead of `size_t` + std::array message_length; boost::asio::read(socket, boost::asio::buffer(message_length)); // Make sure the buffer is large enough