diff --git a/src/common/communication.h b/src/common/communication.h index b6d7fb8a..2f731d40 100644 --- a/src/common/communication.h +++ b/src/common/communication.h @@ -223,18 +223,27 @@ struct buffer_type { * * @param socket The Boost.Asio socket to write to. * @param object The object to write to the stream. + * @param buffer The buffer to write to. Only needed for audio buffers + * constantly allocating huge vectors would be a waste. * * @relates read_object */ template -inline void write_object(Socket& socket, const T& object) { - typename buffer_type::type buffer; +inline void write_object(Socket& socket, + const T& object, + typename buffer_type::type& buffer) { auto length = bitsery::quickSerialization< OutputAdapter::type>>(buffer, object); socket.send(boost::asio::buffer(buffer, length)); } +template +inline void write_object(Socket& socket, const T& object) { + typename buffer_type::type buffer; + 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. @@ -243,14 +252,17 @@ inline void write_object(Socket& socket, const T& object) { * @param object The object to deserialize to, if given. This can be used to * update an existing `AEffect` struct without losing the pointers set by the * host and the bridge. + * @param buffer The buffer to write to. Only needed for audio buffers + * constantly allocating huge vectors would be a waste. * * @throw std::runtime_error If the conversion to an object was not successful. * * @relates write_object */ template -inline T read_object(Socket& socket, T object = T()) { - typename buffer_type::type buffer; +inline T& read_object(Socket& socket, + T& object, + typename buffer_type::type& buffer) { auto message_length = socket.receive(boost::asio::buffer(buffer)); auto [_, success] = bitsery::quickDeserialization< @@ -265,6 +277,18 @@ inline T read_object(Socket& socket, T object = T()) { return object; } +template +inline T& read_object(Socket& socket, T& object) { + typename buffer_type::type buffer; + return read_object(socket, object, buffer); +} + +template +inline T read_object(Socket& socket) { + T object; + return read_object(socket, object); +} + /** * Serialize and send an event over a socket. This is used for both the host -> * plugin 'dispatch' events and the plugin -> host 'audioMaster' host callbacks