From 9a8bdf84c8321fa57d820af7f9069a4759321f2f Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 23 Oct 2021 22:21:47 +0200 Subject: [PATCH] LibIPC: Move waiting for synchronous responses to ConnectionBase --- Userland/Libraries/LibIPC/Connection.cpp | 23 +++++++++++++++++++++++ Userland/Libraries/LibIPC/Connection.h | 22 +++------------------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/Userland/Libraries/LibIPC/Connection.cpp b/Userland/Libraries/LibIPC/Connection.cpp index 4d2c29ea80..e0dd2a2744 100644 --- a/Userland/Libraries/LibIPC/Connection.cpp +++ b/Userland/Libraries/LibIPC/Connection.cpp @@ -181,4 +181,27 @@ bool ConnectionBase::drain_messages_from_peer(u32 local_endpoint_magic) return true; } +OwnPtr ConnectionBase::wait_for_specific_endpoint_message_impl(u32 endpoint_magic, int message_id, u32 local_endpoint_magic) +{ + for (;;) { + // Double check we don't already have the event waiting for us. + // Otherwise we might end up blocked for a while for no reason. + for (size_t i = 0; i < m_unprocessed_messages.size(); ++i) { + auto& message = m_unprocessed_messages[i]; + if (message.endpoint_magic() != endpoint_magic) + continue; + if (message.message_id() == message_id) + return m_unprocessed_messages.take(i); + } + + if (!m_socket->is_open()) + break; + + wait_for_socket_to_become_readable(); + if (!drain_messages_from_peer(local_endpoint_magic)) + break; + } + return {}; +} + } diff --git a/Userland/Libraries/LibIPC/Connection.h b/Userland/Libraries/LibIPC/Connection.h index 43707f2486..65e2915af7 100644 --- a/Userland/Libraries/LibIPC/Connection.h +++ b/Userland/Libraries/LibIPC/Connection.h @@ -48,6 +48,7 @@ protected: virtual void did_become_responsive() { } virtual void try_parse_messages(Vector const& bytes, size_t& index) = 0; + OwnPtr wait_for_specific_endpoint_message_impl(u32 endpoint_magic, int message_id, u32 local_endpoint_magic); void wait_for_socket_to_become_readable(); Result, bool> read_as_much_as_possible_from_socket_without_blocking(); bool drain_messages_from_peer(u32 local_endpoint_magic); @@ -104,25 +105,8 @@ protected: template OwnPtr wait_for_specific_endpoint_message() { - for (;;) { - // Double check we don't already have the event waiting for us. - // Otherwise we might end up blocked for a while for no reason. - for (size_t i = 0; i < m_unprocessed_messages.size(); ++i) { - auto& message = m_unprocessed_messages[i]; - if (message.endpoint_magic() != Endpoint::static_magic()) - continue; - if (message.message_id() == MessageType::static_message_id()) - return m_unprocessed_messages.take(i).template release_nonnull(); - } - - if (!m_socket->is_open()) - break; - - wait_for_socket_to_become_readable(); - - if (!drain_messages_from_peer(LocalEndpoint::static_magic())) - break; - } + if (auto message = wait_for_specific_endpoint_message_impl(Endpoint::static_magic(), MessageType::static_message_id(), LocalEndpoint::static_magic())) + return message.template release_nonnull(); return {}; }