Allow reusing buffers when reading and writing

This commit is contained in:
Robbert van der Helm
2020-03-05 19:18:34 +01:00
parent 620ba5b756
commit 9754909930
+28 -4
View File
@@ -223,18 +223,27 @@ struct buffer_type<AEffect> {
* *
* @param socket The Boost.Asio socket to write to. * @param socket The Boost.Asio socket to write to.
* @param object The object to write to the stream. * @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 * @relates read_object
*/ */
template <typename T, typename Socket> template <typename T, typename Socket>
inline void write_object(Socket& socket, const T& object) { inline void write_object(Socket& socket,
typename buffer_type<T>::type buffer; const T& object,
typename buffer_type<T>::type& buffer) {
auto length = bitsery::quickSerialization< auto length = bitsery::quickSerialization<
OutputAdapter<typename buffer_type<T>::type>>(buffer, object); OutputAdapter<typename buffer_type<T>::type>>(buffer, object);
socket.send(boost::asio::buffer(buffer, length)); socket.send(boost::asio::buffer(buffer, length));
} }
template <typename T, typename Socket>
inline void write_object(Socket& socket, const T& object) {
typename buffer_type<T>::type buffer;
write_object(socket, object, buffer);
}
/** /**
* Deserialize an object by reading it from a socket. This should be used * 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. * 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 * @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 * update an existing `AEffect` struct without losing the pointers set by the
* host and the bridge. * 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. * @throw std::runtime_error If the conversion to an object was not successful.
* *
* @relates write_object * @relates write_object
*/ */
template <typename T, typename Socket> template <typename T, typename Socket>
inline T read_object(Socket& socket, T object = T()) { inline T& read_object(Socket& socket,
typename buffer_type<T>::type buffer; T& object,
typename buffer_type<T>::type& buffer) {
auto message_length = socket.receive(boost::asio::buffer(buffer)); auto message_length = socket.receive(boost::asio::buffer(buffer));
auto [_, success] = bitsery::quickDeserialization< auto [_, success] = bitsery::quickDeserialization<
@@ -265,6 +277,18 @@ inline T read_object(Socket& socket, T object = T()) {
return object; return object;
} }
template <typename T, typename Socket>
inline T& read_object(Socket& socket, T& object) {
typename buffer_type<T>::type buffer;
return read_object(socket, object, buffer);
}
template <typename T, typename Socket>
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 -> * 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 * plugin 'dispatch' events and the plugin -> host 'audioMaster' host callbacks