From e89ddfe7b1bc3653ceac09c941e21bb8af538de0 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Tue, 10 Mar 2020 00:36:29 +0100 Subject: [PATCH] Correct reading chunks They're not actually stored in `data`, but in `*data`. --- src/common/communication.h | 37 ++++++++++++++++--------------------- src/common/logging.cpp | 2 +- src/common/serialization.h | 12 ++++++------ src/plugin/host-bridge.cpp | 2 +- 4 files changed, 24 insertions(+), 29 deletions(-) diff --git a/src/common/communication.h b/src/common/communication.h index 5ca4813e..4d331088 100644 --- a/src/common/communication.h +++ b/src/common/communication.h @@ -225,25 +225,18 @@ void passthrough_event(boost::asio::local::stream_protocol::socket& socket, event.payload, event.option); } - // Some buffer for the event to write to if needed. We only pass around a - // marker struct to indicate that this is indeed the case. - std::vector binary_buffer; 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(); - }, - [&](WantsBinaryBuffer&) -> void* { - // Only allocate when we actually need this, i.e. when - // we're getting a chunk from the plugin - binary_buffer.resize(binary_buffer_size); - return binary_buffer.data(); - }, - [&](WantsString&) -> void* { return string_buffer.data(); }}, + 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, @@ -255,10 +248,12 @@ void passthrough_event(boost::asio::local::stream_protocol::socket& socket, // report some data back? const auto response_data = std::visit( overload{ - [&](WantsBinaryBuffer&) -> std::optional { - // In this case the return value from the event determines how - // much data the plugin has written - return std::string(static_cast(data), return_value); + [&](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)); diff --git a/src/common/logging.cpp b/src/common/logging.cpp index fff8fb38..fbade8cf 100644 --- a/src/common/logging.cpp +++ b/src/common/logging.cpp @@ -164,7 +164,7 @@ void Logger::log_event(bool is_dispatch, [&](const DynamicVstEvents& events) { message << "<" << events.events.size() << " midi_events>"; }, - [&](const WantsBinaryBuffer&) { + [&](const WantsChunkBuffer&) { message << ""; }, [&](const WantsString&) { message << ""; }}, diff --git a/src/common/serialization.h b/src/common/serialization.h index 92b9ffcb..e13e81dd 100644 --- a/src/common/serialization.h +++ b/src/common/serialization.h @@ -111,11 +111,11 @@ class alignas(16) DynamicVstEvents { }; /** - * Marker struct to indicate that that the event needs to write arbitrary data - * to the void pointer, with the return value indicating the amount of data - * actually written. + * Marker struct to indicate that that the event writes arbitrary data into one + * of its own buffers and uses the void pointer to store start of that data, + * with the return value indicating the size of the array. */ -struct WantsBinaryBuffer {}; +struct WantsChunkBuffer {}; /** * Marker struct to indicate that that the event requires some buffer to write @@ -154,7 +154,7 @@ struct WantsString {}; using EventPayload = std::variant; template @@ -170,7 +170,7 @@ void serialize(S& s, EventPayload& payload) { s.container1b(event.dump); }); }, - [](S&, WantsBinaryBuffer&) {}, [](S&, WantsString&) {}}); + [](S&, WantsChunkBuffer&) {}, [](S&, WantsString&) {}}); } /** diff --git a/src/plugin/host-bridge.cpp b/src/plugin/host-bridge.cpp index 9464f23f..3c7e9305 100644 --- a/src/plugin/host-bridge.cpp +++ b/src/plugin/host-bridge.cpp @@ -148,7 +148,7 @@ class DispatchDataConverter : DefaultDataConverter { // TODO: More of these structs switch (opcode) { case effGetChunk: - return WantsBinaryBuffer(); + return WantsChunkBuffer(); case effProcessEvents: return DynamicVstEvents(*static_cast(data)); break;