Correct reading chunks

They're not actually stored in `data`, but in `*data`.
This commit is contained in:
Robbert van der Helm
2020-03-10 00:36:29 +01:00
parent 7fcf5abaf2
commit e89ddfe7b1
4 changed files with 24 additions and 29 deletions
+9 -14
View File
@@ -225,24 +225,17 @@ void passthrough_event(boost::asio::local::stream_protocol::socket& socket,
event.payload, event.option); 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<char> binary_buffer;
std::array<char, max_string_length> string_buffer; std::array<char, max_string_length> string_buffer;
void* data = std::visit( void* data = std::visit(
overload{[&](const std::nullptr_t&) -> void* { return nullptr; }, overload{
[&](const std::nullptr_t&) -> void* { return nullptr; },
[&](const std::string& s) -> void* { [&](const std::string& s) -> void* {
return const_cast<char*>(s.c_str()); return const_cast<char*>(s.c_str());
}, },
[&](DynamicVstEvents& events) -> void* { [&](DynamicVstEvents& events) -> void* {
return &events.as_c_events(); return &events.as_c_events();
}, },
[&](WantsBinaryBuffer&) -> void* { [&](WantsChunkBuffer&) -> void* { return string_buffer.data(); },
// 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(); }}, [&](WantsString&) -> void* { return string_buffer.data(); }},
event.payload); event.payload);
@@ -255,10 +248,12 @@ void passthrough_event(boost::asio::local::stream_protocol::socket& socket,
// report some data back? // report some data back?
const auto response_data = std::visit( const auto response_data = std::visit(
overload{ overload{
[&](WantsBinaryBuffer&) -> std::optional<std::string> { [&](WantsChunkBuffer&) -> std::optional<std::string> {
// In this case the return value from the event determines how // In this case the plugin will have written its data stored in
// much data the plugin has written // an array to which a pointer is stored in `data`, with the
return std::string(static_cast<char*>(data), return_value); // return value from the event determines how much data the
// plugin has written
return std::string(*static_cast<char**>(data), return_value);
}, },
[&](WantsString&) -> std::optional<std::string> { [&](WantsString&) -> std::optional<std::string> {
return std::string(static_cast<char*>(data)); return std::string(static_cast<char*>(data));
+1 -1
View File
@@ -164,7 +164,7 @@ void Logger::log_event(bool is_dispatch,
[&](const DynamicVstEvents& events) { [&](const DynamicVstEvents& events) {
message << "<" << events.events.size() << " midi_events>"; message << "<" << events.events.size() << " midi_events>";
}, },
[&](const WantsBinaryBuffer&) { [&](const WantsChunkBuffer&) {
message << "<writable_buffer>"; message << "<writable_buffer>";
}, },
[&](const WantsString&) { message << "<writable_string>"; }}, [&](const WantsString&) { message << "<writable_string>"; }},
+6 -6
View File
@@ -111,11 +111,11 @@ class alignas(16) DynamicVstEvents {
}; };
/** /**
* Marker struct to indicate that that the event needs to write arbitrary data * Marker struct to indicate that that the event writes arbitrary data into one
* to the void pointer, with the return value indicating the amount of data * of its own buffers and uses the void pointer to store start of that data,
* actually written. * 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 * Marker struct to indicate that that the event requires some buffer to write
@@ -154,7 +154,7 @@ struct WantsString {};
using EventPayload = std::variant<std::nullptr_t, using EventPayload = std::variant<std::nullptr_t,
std::string, std::string,
DynamicVstEvents, DynamicVstEvents,
WantsBinaryBuffer, WantsChunkBuffer,
WantsString>; WantsString>;
template <typename S> template <typename S>
@@ -170,7 +170,7 @@ void serialize(S& s, EventPayload& payload) {
s.container1b(event.dump); s.container1b(event.dump);
}); });
}, },
[](S&, WantsBinaryBuffer&) {}, [](S&, WantsString&) {}}); [](S&, WantsChunkBuffer&) {}, [](S&, WantsString&) {}});
} }
/** /**
+1 -1
View File
@@ -148,7 +148,7 @@ class DispatchDataConverter : DefaultDataConverter {
// TODO: More of these structs // TODO: More of these structs
switch (opcode) { switch (opcode) {
case effGetChunk: case effGetChunk:
return WantsBinaryBuffer(); return WantsChunkBuffer();
case effProcessEvents: case effProcessEvents:
return DynamicVstEvents(*static_cast<const VstEvents*>(data)); return DynamicVstEvents(*static_cast<const VstEvents*>(data));
break; break;