Merge events.h into communication.h

This commit is contained in:
Robbert van der Helm
2020-10-25 21:25:31 +01:00
parent 4b53342514
commit 54ed69c408
5 changed files with 407 additions and 410 deletions
+53
View File
@@ -101,6 +101,59 @@ void Sockets::connect() {
}
}
EventPayload DefaultDataConverter::read(const int /*opcode*/,
const int /*index*/,
const intptr_t /*value*/,
const void* data) const {
if (!data) {
return nullptr;
}
// This is a simple fallback that will work in almost every case.
// Because some plugins don't zero out their string buffers when sending
// host callbacks, we will explicitely list all callbacks that expect a
// string in `DispatchDataConverter` adn `HostCallbackDataConverter`.
const char* c_string = static_cast<const char*>(data);
if (c_string[0] != 0) {
return std::string(c_string);
} else {
return WantsString{};
}
}
std::optional<EventPayload> DefaultDataConverter::read_value(
const int /*opcode*/,
const intptr_t /*value*/) const {
return std::nullopt;
}
void DefaultDataConverter::write(const int /*opcode*/,
void* data,
const EventResult& response) const {
// The default behavior is to handle this as a null terminated C-style
// string
std::visit(overload{[&](const auto&) {},
[&](const std::string& s) {
char* output = static_cast<char*>(data);
// We use std::string for easy transport but in
// practice we're always writing null terminated
// C-style strings
std::copy(s.begin(), s.end(), output);
output[s.size()] = 0;
}},
response.payload);
}
void DefaultDataConverter::write_value(const int /*opcode*/,
intptr_t /*value*/,
const EventResult& /*response*/) const {}
intptr_t DefaultDataConverter::return_value(const int /*opcode*/,
const intptr_t original) const {
return original;
}
boost::filesystem::path generate_endpoint_base(const std::string& plugin_name) {
fs::path temp_directory = get_temporary_directory();