diff --git a/Userland/Libraries/LibIPC/ClientConnection.h b/Userland/Libraries/LibIPC/ClientConnection.h index 48767fa4e5..52d8cdcaef 100644 --- a/Userland/Libraries/LibIPC/ClientConnection.h +++ b/Userland/Libraries/LibIPC/ClientConnection.h @@ -30,7 +30,7 @@ public: , m_client_id(client_id) { VERIFY(this->socket().is_connected()); - this->socket().on_ready_to_read = [this] { this->drain_messages_from_peer(ServerEndpoint::static_magic()); }; + this->socket().on_ready_to_read = [this] { this->drain_messages_from_peer(); }; } virtual ~ClientConnection() override diff --git a/Userland/Libraries/LibIPC/Connection.cpp b/Userland/Libraries/LibIPC/Connection.cpp index e0dd2a2744..5780761d24 100644 --- a/Userland/Libraries/LibIPC/Connection.cpp +++ b/Userland/Libraries/LibIPC/Connection.cpp @@ -10,10 +10,11 @@ namespace IPC { -ConnectionBase::ConnectionBase(IPC::Stub& local_stub, NonnullRefPtr socket) +ConnectionBase::ConnectionBase(IPC::Stub& local_stub, NonnullRefPtr socket, u32 local_endpoint_magic) : m_local_stub(local_stub) , m_socket(move(socket)) , m_notifier(Core::Notifier::construct(m_socket->fd(), Core::Notifier::Read, this)) + , m_local_endpoint_magic(local_endpoint_magic) { m_responsiveness_timer = Core::Timer::create_single_shot(3000, [this] { may_have_become_unresponsive(); }); } @@ -83,11 +84,11 @@ void ConnectionBase::shutdown() die(); } -void ConnectionBase::handle_messages(u32 local_endpoint_magic) +void ConnectionBase::handle_messages() { auto messages = move(m_unprocessed_messages); for (auto& message : messages) { - if (message.endpoint_magic() == local_endpoint_magic) + if (message.endpoint_magic() == m_local_endpoint_magic) if (auto response = m_local_stub.handle(message)) post_message(*response); } @@ -149,7 +150,7 @@ Result, bool> ConnectionBase::read_as_much_as_possible_from_socket_wi return bytes; } -bool ConnectionBase::drain_messages_from_peer(u32 local_endpoint_magic) +bool ConnectionBase::drain_messages_from_peer() { auto bytes = TRY(read_as_much_as_possible_from_socket_without_blocking()); @@ -174,14 +175,14 @@ bool ConnectionBase::drain_messages_from_peer(u32 local_endpoint_magic) } if (!m_unprocessed_messages.is_empty()) { - deferred_invoke([this, local_endpoint_magic] { - handle_messages(local_endpoint_magic); + deferred_invoke([this] { + handle_messages(); }); } return true; } -OwnPtr ConnectionBase::wait_for_specific_endpoint_message_impl(u32 endpoint_magic, int message_id, u32 local_endpoint_magic) +OwnPtr ConnectionBase::wait_for_specific_endpoint_message_impl(u32 endpoint_magic, int message_id) { for (;;) { // Double check we don't already have the event waiting for us. @@ -198,7 +199,7 @@ OwnPtr ConnectionBase::wait_for_specific_endpoint_message_impl(u32 break; wait_for_socket_to_become_readable(); - if (!drain_messages_from_peer(local_endpoint_magic)) + if (!drain_messages_from_peer()) break; } return {}; diff --git a/Userland/Libraries/LibIPC/Connection.h b/Userland/Libraries/LibIPC/Connection.h index 65e2915af7..446ba9e151 100644 --- a/Userland/Libraries/LibIPC/Connection.h +++ b/Userland/Libraries/LibIPC/Connection.h @@ -40,7 +40,7 @@ public: virtual void die() { } protected: - explicit ConnectionBase(IPC::Stub&, NonnullRefPtr); + explicit ConnectionBase(IPC::Stub&, NonnullRefPtr, u32 local_endpoint_magic); Core::LocalSocket& socket() { return *m_socket; } @@ -48,13 +48,13 @@ 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); + OwnPtr wait_for_specific_endpoint_message_impl(u32 endpoint_magic, int message_id); 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); + bool drain_messages_from_peer(); void post_message(MessageBuffer); - void handle_messages(u32 local_endpoint_magic); + void handle_messages(); IPC::Stub& m_local_stub; @@ -64,18 +64,20 @@ protected: RefPtr m_notifier; NonnullOwnPtrVector m_unprocessed_messages; ByteBuffer m_unprocessed_bytes; + + u32 m_local_endpoint_magic { 0 }; }; template class Connection : public ConnectionBase { public: Connection(IPC::Stub& local_stub, NonnullRefPtr socket) - : ConnectionBase(local_stub, move(socket)) + : ConnectionBase(local_stub, move(socket), LocalEndpoint::static_magic()) { m_notifier->on_ready_to_read = [this] { NonnullRefPtr protect = *this; - drain_messages_from_peer(LocalEndpoint::static_magic()); - handle_messages(LocalEndpoint::static_magic()); + drain_messages_from_peer(); + handle_messages(); }; } @@ -105,7 +107,7 @@ protected: template OwnPtr wait_for_specific_endpoint_message() { - if (auto message = wait_for_specific_endpoint_message_impl(Endpoint::static_magic(), MessageType::static_message_id(), LocalEndpoint::static_magic())) + if (auto message = wait_for_specific_endpoint_message_impl(Endpoint::static_magic(), MessageType::static_message_id())) return message.template release_nonnull(); return {}; }