Add the ability to override writing back data

This commit is contained in:
Robbert van der Helm
2020-03-09 23:36:54 +01:00
parent ec96064cc1
commit 7e75f913fa
3 changed files with 36 additions and 29 deletions
+27 -21
View File
@@ -109,25 +109,38 @@ inline T read_object(Socket& socket) {
}
/**
* Encodes the base behavior for encoding the contents of the `data` argument
* 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.
*/
struct DefaultDataConverter {
EventPayload operator()(int /*opcode*/, void* data) {
EventPayload read(const int /*opcode*/, const void* data) {
if (data == nullptr) {
return nullptr;
}
// Assume buffers are zeroed out, this is probably not the case
char* c_string = static_cast<char*>(data);
const char* c_string = static_cast<const char*>(data);
if (c_string[0] != 0) {
return std::string(c_string);
} else {
return WantsString{};
}
}
void write(const int /*opcode*/, void* data, const EventResult& response) {
if (response.data.has_value()) {
char* output = static_cast<char*>(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;
}
}
};
/**
@@ -136,21 +149,21 @@ struct DefaultDataConverter {
* since they follow the same format. See one of those functions for details on
* the parameters and return value of this function.
*
* @tparam DataConverter how the `data` void pointer should be converted to a
* serializable type. 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 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 <typename DataConverter>
template <typename D>
intptr_t send_event(boost::asio::local::stream_protocol::socket& socket,
D& data_converter,
int opcode,
int index,
intptr_t value,
@@ -159,7 +172,7 @@ intptr_t send_event(boost::asio::local::stream_protocol::socket& socket,
std::optional<std::pair<Logger&, bool>> logging) {
// Encode the right payload type for this event. Check the documentation for
// `EventPayload` for more information.
EventPayload payload = DataConverter{}(opcode, data);
const EventPayload payload = data_converter.read(opcode, data);
if (logging.has_value()) {
auto [logger, is_dispatch] = *logging;
@@ -170,21 +183,14 @@ intptr_t send_event(boost::asio::local::stream_protocol::socket& socket,
write_object(socket, event);
const auto response = read_object<EventResult>(socket);
if (logging.has_value()) {
auto [logger, is_dispatch] = *logging;
logger.log_event_response(is_dispatch, response.return_value,
response.data);
}
if (response.data.has_value()) {
char* output = static_cast<char*>(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;
}
data_converter.write(opcode, data, response);
return response.return_value;
}
+6 -6
View File
@@ -138,16 +138,16 @@ HostBridge::HostBridge(audioMasterCallback host_callback)
}
struct DispatchDataConverter : DefaultDataConverter {
EventPayload operator()(int opcode, void* data) {
EventPayload read(const int opcode, const void* data) {
// There are some events that need specific structs that we can't simply
// serialize as a string because they might contain null bytes
// TODO: More of these structs
switch (opcode) {
case effProcessEvents:
return DynamicVstEvents(*static_cast<VstEvents*>(data));
return DynamicVstEvents(*static_cast<const VstEvents*>(data));
break;
default:
return DefaultDataConverter{}(opcode, data);
return DefaultDataConverter::read(opcode, data);
break;
}
}
@@ -183,9 +183,9 @@ intptr_t HostBridge::dispatch(AEffect* /*plugin*/,
}
// TODO: Maybe reuse buffers here when dealing with chunk data
return send_event<DispatchDataConverter>(
host_vst_dispatch, opcode, index, value, data, option,
std::pair<Logger&, bool>(logger, true));
DispatchDataConverter converter;
return send_event(host_vst_dispatch, converter, opcode, index, value, data,
option, std::pair<Logger&, bool>(logger, true));
}
void HostBridge::process_replacing(AEffect* /*plugin*/,
+3 -2
View File
@@ -195,8 +195,9 @@ intptr_t PluginBridge::host_callback(AEffect* /*plugin*/,
intptr_t value,
void* data,
float option) {
return send_event<DefaultDataConverter>(vst_host_callback, opcode, index,
value, data, option, std::nullopt);
DefaultDataConverter converter;
return send_event(vst_host_callback, converter, opcode, index, value, data,
option, std::nullopt);
}
intptr_t VST_CALL_CONV host_callback_proxy(AEffect* effect,