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 * for event dispatch functions. This is sufficient for host callbacks
* (`audioMaster()`). The `dispatch()` function will require some more specific * (`audioMaster()`). The `dispatch()` function will require some more specific
* structs. * structs.
*/ */
struct DefaultDataConverter { struct DefaultDataConverter {
EventPayload operator()(int /*opcode*/, void* data) { EventPayload read(const int /*opcode*/, const void* data) {
if (data == nullptr) { if (data == nullptr) {
return nullptr; return nullptr;
} }
// Assume buffers are zeroed out, this is probably not the case // 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) { if (c_string[0] != 0) {
return std::string(c_string); return std::string(c_string);
} else { } else {
return WantsString{}; 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 * since they follow the same format. See one of those functions for details on
* the parameters and return value of this function. * the parameters and return value of this function.
* *
* @tparam DataConverter how the `data` void pointer should be converted to a * @param data_converter Some struct that knows how to read data from and write
* serializable type. For host callbacks this parameter contains either a * data back to the `data` void pointer. For host callbacks this parameter
* string or a null pointer while `dispatch()` calls might contain opcode * contains either a string or a null pointer while `dispatch()` calls might
* specific structs. See the documentation for `EventPayload` for more * contain opcode specific structs. See the documentation for `EventPayload`
* information. The `DefaultDataConverter` defined above handles the basic * for more information. The `DefaultDataConverter` defined above handles the
* behavior that's sufficient for hsot callbacks. * basic behavior that's sufficient for hsot callbacks.
*
* @param logging A pair containing a logger instance and whether or not this is * @param logging A pair containing a logger instance and whether or not this is
* for sending `dispatch()` events or host callbacks. Optional since it * for sending `dispatch()` events or host callbacks. Optional since it
* doesn't have to be done on both sides. * doesn't have to be done on both sides.
* *
* @relates passthrough_event * @relates passthrough_event
*/ */
template <typename DataConverter> template <typename D>
intptr_t send_event(boost::asio::local::stream_protocol::socket& socket, intptr_t send_event(boost::asio::local::stream_protocol::socket& socket,
D& data_converter,
int opcode, int opcode,
int index, int index,
intptr_t value, 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) { std::optional<std::pair<Logger&, bool>> logging) {
// Encode the right payload type for this event. Check the documentation for // Encode the right payload type for this event. Check the documentation for
// `EventPayload` for more information. // `EventPayload` for more information.
EventPayload payload = DataConverter{}(opcode, data); const EventPayload payload = data_converter.read(opcode, data);
if (logging.has_value()) { if (logging.has_value()) {
auto [logger, is_dispatch] = *logging; auto [logger, is_dispatch] = *logging;
@@ -170,21 +183,14 @@ intptr_t send_event(boost::asio::local::stream_protocol::socket& socket,
write_object(socket, event); write_object(socket, event);
const auto response = read_object<EventResult>(socket); const auto response = read_object<EventResult>(socket);
if (logging.has_value()) { if (logging.has_value()) {
auto [logger, is_dispatch] = *logging; auto [logger, is_dispatch] = *logging;
logger.log_event_response(is_dispatch, response.return_value, logger.log_event_response(is_dispatch, response.return_value,
response.data); 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 data_converter.write(opcode, data, response);
// 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;
}
return response.return_value; return response.return_value;
} }
+6 -6
View File
@@ -138,16 +138,16 @@ HostBridge::HostBridge(audioMasterCallback host_callback)
} }
struct DispatchDataConverter : DefaultDataConverter { 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 // There are some events that need specific structs that we can't simply
// serialize as a string because they might contain null bytes // serialize as a string because they might contain null bytes
// TODO: More of these structs // TODO: More of these structs
switch (opcode) { switch (opcode) {
case effProcessEvents: case effProcessEvents:
return DynamicVstEvents(*static_cast<VstEvents*>(data)); return DynamicVstEvents(*static_cast<const VstEvents*>(data));
break; break;
default: default:
return DefaultDataConverter{}(opcode, data); return DefaultDataConverter::read(opcode, data);
break; break;
} }
} }
@@ -183,9 +183,9 @@ intptr_t HostBridge::dispatch(AEffect* /*plugin*/,
} }
// TODO: Maybe reuse buffers here when dealing with chunk data // TODO: Maybe reuse buffers here when dealing with chunk data
return send_event<DispatchDataConverter>( DispatchDataConverter converter;
host_vst_dispatch, opcode, index, value, data, option, return send_event(host_vst_dispatch, converter, opcode, index, value, data,
std::pair<Logger&, bool>(logger, true)); option, std::pair<Logger&, bool>(logger, true));
} }
void HostBridge::process_replacing(AEffect* /*plugin*/, void HostBridge::process_replacing(AEffect* /*plugin*/,
+3 -2
View File
@@ -195,8 +195,9 @@ intptr_t PluginBridge::host_callback(AEffect* /*plugin*/,
intptr_t value, intptr_t value,
void* data, void* data,
float option) { float option) {
return send_event<DefaultDataConverter>(vst_host_callback, opcode, index, DefaultDataConverter converter;
value, data, option, std::nullopt); return send_event(vst_host_callback, converter, opcode, index, value, data,
option, std::nullopt);
} }
intptr_t VST_CALL_CONV host_callback_proxy(AEffect* effect, intptr_t VST_CALL_CONV host_callback_proxy(AEffect* effect,