// yabridge: a Wine VST bridge // Copyright (C) 2020 Robbert van der Helm // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . #pragma once #include #include #include #include #ifdef __WINE__ #include "../wine-host/boost-fix.h" #endif #include #include "logging.h" #include "serialization.h" template using OutputAdapter = bitsery::OutputBufferAdapter; template using InputAdapter = bitsery::InputBufferAdapter; /** * Serialize an object using bitsery and write it to a socket. * * @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. This is useful for sending audio and * chunk data since that can vary in size by a lot. * * @relates read_object */ template inline void write_object( Socket& socket, const T& object, std::vector buffer = std::vector(64)) { const size_t size = bitsery::quickSerialization>>( buffer, object); // Tell the other side how large the object is so it can prepare a buffer // large enough before sending the data socket.send(boost::asio::buffer(std::array{size})); socket.send(boost::asio::buffer(buffer, size)); } /** * 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. * * @param socket The Boost.Asio socket to read from. * @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 read into. This is useful for sending audio and * chunk data since that can vary in size by a lot. * * @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, std::vector buffer = std::vector(64)) { std::array message_length; socket.receive(boost::asio::buffer(message_length)); // Make sure the buffer is large enough const size_t size = message_length[0]; buffer.resize(size); const auto actual_size = socket.receive(boost::asio::buffer(buffer)); assert(size == actual_size); auto [_, success] = bitsery::quickDeserialization>>( {buffer.begin(), size}, object); if (BOOST_UNLIKELY(!success)) { throw std::runtime_error("Deserialization failure in call:" + std::string(__PRETTY_FUNCTION__)); } return object; } template inline T read_object(Socket& socket) { T object; return read_object(socket, object); } /** * Encodes the base behavior for reading from and writing to the `data` argument * for event dispatch functions. This is sufficient for host callbacks * (`audioMaster()`). The `dispatch()` function will require some more specific * structs. */ class DefaultDataConverter { public: virtual ~DefaultDataConverter() {}; virtual EventPayload read(const int /*opcode*/, const intptr_t /*value*/, const void* data) { if (data == nullptr) { return nullptr; } // Assume buffers are zeroed out, this is probably not the case const char* c_string = static_cast(data); if (c_string[0] != 0) { return std::string(c_string); } else { return WantsString{}; } } virtual void write(const int /*opcode*/, void* data, const EventResult& response) { if (response.data.has_value()) { char* output = static_cast(data); // For correctness we will copy the entire buffer and add a // terminating null byte ourselves. In practice `response.data` will // only ever contain C-style strings, but this would work with any // other data format that can contain null bytes. std::copy(response.data->begin(), response.data->end(), output); output[response.data->size()] = 0; } } }; /** * 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 * since they follow the same format. See one of those functions for details on * the parameters and return value of this function. * * @param data_converter Some struct that knows how to read data from and write * data back to the `data` void pointer. For host callbacks this parameter * contains either a string or a null pointer while `dispatch()` calls might * contain opcode specific structs. See the documentation for `EventPayload` * for more information. The `DefaultDataConverter` defined above handles the * basic behavior that's sufficient for hsot callbacks. * @param logging A pair containing a logger instance and whether or not this is * for sending `dispatch()` events or host callbacks. Optional since it * doesn't have to be done on both sides. * * @relates passthrough_event */ template intptr_t send_event(boost::asio::local::stream_protocol::socket& socket, D& data_converter, int opcode, int index, intptr_t value, void* data, float option, std::optional> logging) { // Encode the right payload type for this event. Check the documentation for // `EventPayload` for more information. const EventPayload payload = data_converter.read(opcode, value, data); if (logging.has_value()) { auto [logger, is_dispatch] = *logging; logger.log_event(is_dispatch, opcode, index, value, payload, option); } const Event event{opcode, index, value, option, payload}; write_object(socket, event); const auto response = read_object(socket); if (logging.has_value()) { auto [logger, is_dispatch] = *logging; logger.log_event_response(is_dispatch, response.return_value, response.data); } data_converter.write(opcode, data, response); return response.return_value; } /** * Receive an event from a socket and pass it through to some callback function. * This is used for both the host -> plugin 'dispatch' events and the plugin -> * host 'audioMaster' host callbacks. This callback function is either one of * those functions. * * @param socket The socket to receive on and to send the response back to. * @param plugin The `AEffect` instance that should be passed to the callback * function. * @param callback The function to call with the arguments received from the * socket. * @param logging A pair containing a logger instance and whether or not this is * for sending `dispatch()` events or host callbacks. Optional since it * doesn't have to be done on both sides. * * @relates send_event */ template void passthrough_event(boost::asio::local::stream_protocol::socket& socket, AEffect* plugin, F callback, std::optional> logging) { auto event = read_object(socket); if (logging.has_value()) { auto [logger, is_dispatch] = *logging; logger.log_event(is_dispatch, event.opcode, event.index, event.value, event.payload, event.option); } std::array string_buffer; void* data = std::visit( overload{ [&](const std::nullptr_t&) -> void* { return nullptr; }, [&](const std::string& s) -> void* { return const_cast(s.c_str()); }, [&](DynamicVstEvents& events) -> void* { return &events.as_c_events(); }, [&](WantsChunkBuffer&) -> void* { return string_buffer.data(); }, [&](WantsString&) -> void* { return string_buffer.data(); }}, event.payload); const intptr_t return_value = callback(plugin, event.opcode, event.index, event.value, data, event.option); // Only write back data when needed, this depends on the event payload type // XXX: Is it possbile here that we got passed a non empty buffer (i.e. // because it was not zeroed out by the host) for an event that should // report some data back? const auto response_data = std::visit( overload{ [&](WantsChunkBuffer&) -> std::optional { // In this case the plugin will have written its data stored in // an array to which a pointer is stored in `data`, with the // return value from the event determines how much data the // plugin has written return std::string(*static_cast(data), return_value); }, [&](WantsString&) -> std::optional { return std::string(static_cast(data)); }, [&](auto) -> std::optional { return std::nullopt; }}, event.payload); if (logging.has_value()) { auto [logger, is_dispatch] = *logging; logger.log_event_response(is_dispatch, return_value, response_data); } EventResult response{return_value, response_data}; write_object(socket, response); }