Simplify EventHandler::receive_events

This commit is contained in:
Robbert van der Helm
2020-11-06 17:49:00 +01:00
parent 1c7e866609
commit 5087b49cc4
+26 -40
View File
@@ -393,9 +393,6 @@ class DefaultDataConverter {
* *
* @tparam Thread The thread implementation to use. On the Linux side this * @tparam Thread The thread implementation to use. On the Linux side this
* should be `std::jthread` and on the Wine side this should be `Win32Thread`. * should be `std::jthread` and on the Wine side this should be `Win32Thread`.
*
* TODO: Factor the listening logic out to a `MultiSocketHandler`, and then
* recreate this on top of that
*/ */
template <typename Thread> template <typename Thread>
class EventHandler { class EventHandler {
@@ -590,6 +587,30 @@ class EventHandler {
template <typename F> template <typename F>
void receive_events(std::optional<std::pair<Logger&, bool>> logging, void receive_events(std::optional<std::pair<Logger&, bool>> logging,
F callback) { F callback) {
// Reading, processing, and writing back event data from the sockets
// works in the same way regardless of which socket we're using
auto process_event =
[&](boost::asio::local::stream_protocol::socket& socket,
bool on_main_thread) {
auto event = read_object<Event>(socket);
if (logging) {
auto [logger, is_dispatch] = *logging;
logger.log_event(is_dispatch, event.opcode, event.index,
event.value, event.payload, event.option,
event.value_payload);
}
EventResult response = callback(event, on_main_thread);
if (logging) {
auto [logger, is_dispatch] = *logging;
logger.log_event_response(
is_dispatch, event.opcode, response.return_value,
response.payload, response.value_payload);
}
write_object(socket, response);
};
// As described above we'll handle incoming requests for `socket` on // As described above we'll handle incoming requests for `socket` on
// this thread. We'll also listen for incoming connections on `endpoint` // this thread. We'll also listen for incoming connections on `endpoint`
// on another thread. For any incoming connection we'll spawn a new // on another thread. For any incoming connection we'll spawn a new
@@ -618,26 +639,7 @@ class EventHandler {
active_secondary_requests[request_id] = Thread( active_secondary_requests[request_id] = Thread(
[&, request_id](boost::asio::local::stream_protocol::socket [&, request_id](boost::asio::local::stream_protocol::socket
secondary_socket) { secondary_socket) {
// TODO: Factor this out process_event(secondary_socket, false);
auto event = read_object<Event>(secondary_socket);
if (logging) {
auto [logger, is_dispatch] = *logging;
logger.log_event(is_dispatch, event.opcode,
event.index, event.value,
event.payload, event.option,
event.value_payload);
}
EventResult response = callback(event, false);
if (logging) {
auto [logger, is_dispatch] = *logging;
logger.log_event_response(is_dispatch, event.opcode,
response.return_value,
response.payload,
response.value_payload);
}
write_object(secondary_socket, response);
// When we have processed this request, we'll join the // When we have processed this request, we'll join the
// thread again with the thread that's handling // thread again with the thread that's handling
@@ -658,23 +660,7 @@ class EventHandler {
while (true) { while (true) {
try { try {
auto event = read_object<Event>(socket); process_event(socket, true);
if (logging) {
auto [logger, is_dispatch] = *logging;
logger.log_event(is_dispatch, event.opcode, event.index,
event.value, event.payload, event.option,
event.value_payload);
}
EventResult response = callback(event, true);
if (logging) {
auto [logger, is_dispatch] = *logging;
logger.log_event_response(
is_dispatch, event.opcode, response.return_value,
response.payload, response.value_payload);
}
write_object(socket, response);
} catch (const boost::system::system_error&) { } catch (const boost::system::system_error&) {
// This happens when the sockets got closed because the plugin // This happens when the sockets got closed because the plugin
// is being shut down // is being shut down