diff --git a/src/common/communication.h b/src/common/communication.h index 4aa40cee..ea5a46ca 100644 --- a/src/common/communication.h +++ b/src/common/communication.h @@ -17,7 +17,6 @@ #pragma once #include -#include #include #include @@ -55,10 +54,9 @@ using InputAdapter = bitsery::InputBufferAdapter; * @relates read_object */ template -inline void write_object( - Socket& socket, - const T& object, - std::vector buffer = std::vector(64)) { +inline void write_object(Socket& socket, + const T& object, + std::vector& buffer) { const size_t size = bitsery::quickSerialization>>( buffer, object); @@ -77,6 +75,17 @@ inline void write_object( assert(bytes_written == size); } +/** + * `write_object()` with a small default buffer for convenience. + * + * @overload + */ +template +inline void write_object(Socket& socket, const T& object) { + std::vector buffer(64); + write_object(socket, object, buffer); +} + /** * Deserialize an object by reading it from a socket. This should be used * together with `write_object`. This will block until the object is available. @@ -88,12 +97,13 @@ inline void write_object( * @return The deserialized object. * * @throw std::runtime_error If the conversion to an object was not successful. + * @throw boost::system::system_error If the socket is closed or gets closed + * while reading. * * @relates write_object */ template -inline T read_object(Socket& socket, - std::vector buffer = std::vector(64)) { +inline T read_object(Socket& socket, std::vector& buffer) { // 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)); @@ -122,6 +132,17 @@ inline T read_object(Socket& socket, return object; } +/** + * `read_object()` with a small default buffer for convenience. + * + * @overload + */ +template +inline T read_object(Socket& socket) { + std::vector buffer(64); + return read_object(socket, buffer); +} + /** * Encodes the base behavior for reading from and writing to the `data` argument * for event dispatch functions. This provides base functionality for these