1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:17:44 +00:00

LibIPC: Store local endpoint magic in a ConnectionBase member

This simplifies some of the code, since it's no longer necessary for the
templated code to pass LocalEndpoint::static_magic() everywhere.
This commit is contained in:
Andreas Kling 2021-10-23 22:59:04 +02:00
parent 9a8bdf84c8
commit 24642861af
3 changed files with 20 additions and 17 deletions

View file

@ -30,7 +30,7 @@ public:
, m_client_id(client_id) , m_client_id(client_id)
{ {
VERIFY(this->socket().is_connected()); 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 virtual ~ClientConnection() override

View file

@ -10,10 +10,11 @@
namespace IPC { namespace IPC {
ConnectionBase::ConnectionBase(IPC::Stub& local_stub, NonnullRefPtr<Core::LocalSocket> socket) ConnectionBase::ConnectionBase(IPC::Stub& local_stub, NonnullRefPtr<Core::LocalSocket> socket, u32 local_endpoint_magic)
: m_local_stub(local_stub) : m_local_stub(local_stub)
, m_socket(move(socket)) , m_socket(move(socket))
, m_notifier(Core::Notifier::construct(m_socket->fd(), Core::Notifier::Read, this)) , 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(); }); m_responsiveness_timer = Core::Timer::create_single_shot(3000, [this] { may_have_become_unresponsive(); });
} }
@ -83,11 +84,11 @@ void ConnectionBase::shutdown()
die(); die();
} }
void ConnectionBase::handle_messages(u32 local_endpoint_magic) void ConnectionBase::handle_messages()
{ {
auto messages = move(m_unprocessed_messages); auto messages = move(m_unprocessed_messages);
for (auto& message : 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)) if (auto response = m_local_stub.handle(message))
post_message(*response); post_message(*response);
} }
@ -149,7 +150,7 @@ Result<Vector<u8>, bool> ConnectionBase::read_as_much_as_possible_from_socket_wi
return bytes; 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()); 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()) { if (!m_unprocessed_messages.is_empty()) {
deferred_invoke([this, local_endpoint_magic] { deferred_invoke([this] {
handle_messages(local_endpoint_magic); handle_messages();
}); });
} }
return true; return true;
} }
OwnPtr<IPC::Message> ConnectionBase::wait_for_specific_endpoint_message_impl(u32 endpoint_magic, int message_id, u32 local_endpoint_magic) OwnPtr<IPC::Message> ConnectionBase::wait_for_specific_endpoint_message_impl(u32 endpoint_magic, int message_id)
{ {
for (;;) { for (;;) {
// Double check we don't already have the event waiting for us. // Double check we don't already have the event waiting for us.
@ -198,7 +199,7 @@ OwnPtr<IPC::Message> ConnectionBase::wait_for_specific_endpoint_message_impl(u32
break; break;
wait_for_socket_to_become_readable(); wait_for_socket_to_become_readable();
if (!drain_messages_from_peer(local_endpoint_magic)) if (!drain_messages_from_peer())
break; break;
} }
return {}; return {};

View file

@ -40,7 +40,7 @@ public:
virtual void die() { } virtual void die() { }
protected: protected:
explicit ConnectionBase(IPC::Stub&, NonnullRefPtr<Core::LocalSocket>); explicit ConnectionBase(IPC::Stub&, NonnullRefPtr<Core::LocalSocket>, u32 local_endpoint_magic);
Core::LocalSocket& socket() { return *m_socket; } Core::LocalSocket& socket() { return *m_socket; }
@ -48,13 +48,13 @@ protected:
virtual void did_become_responsive() { } virtual void did_become_responsive() { }
virtual void try_parse_messages(Vector<u8> const& bytes, size_t& index) = 0; virtual void try_parse_messages(Vector<u8> const& bytes, size_t& index) = 0;
OwnPtr<IPC::Message> wait_for_specific_endpoint_message_impl(u32 endpoint_magic, int message_id, u32 local_endpoint_magic); OwnPtr<IPC::Message> wait_for_specific_endpoint_message_impl(u32 endpoint_magic, int message_id);
void wait_for_socket_to_become_readable(); void wait_for_socket_to_become_readable();
Result<Vector<u8>, bool> read_as_much_as_possible_from_socket_without_blocking(); Result<Vector<u8>, 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 post_message(MessageBuffer);
void handle_messages(u32 local_endpoint_magic); void handle_messages();
IPC::Stub& m_local_stub; IPC::Stub& m_local_stub;
@ -64,18 +64,20 @@ protected:
RefPtr<Core::Notifier> m_notifier; RefPtr<Core::Notifier> m_notifier;
NonnullOwnPtrVector<Message> m_unprocessed_messages; NonnullOwnPtrVector<Message> m_unprocessed_messages;
ByteBuffer m_unprocessed_bytes; ByteBuffer m_unprocessed_bytes;
u32 m_local_endpoint_magic { 0 };
}; };
template<typename LocalEndpoint, typename PeerEndpoint> template<typename LocalEndpoint, typename PeerEndpoint>
class Connection : public ConnectionBase { class Connection : public ConnectionBase {
public: public:
Connection(IPC::Stub& local_stub, NonnullRefPtr<Core::LocalSocket> socket) Connection(IPC::Stub& local_stub, NonnullRefPtr<Core::LocalSocket> socket)
: ConnectionBase(local_stub, move(socket)) : ConnectionBase(local_stub, move(socket), LocalEndpoint::static_magic())
{ {
m_notifier->on_ready_to_read = [this] { m_notifier->on_ready_to_read = [this] {
NonnullRefPtr protect = *this; NonnullRefPtr protect = *this;
drain_messages_from_peer(LocalEndpoint::static_magic()); drain_messages_from_peer();
handle_messages(LocalEndpoint::static_magic()); handle_messages();
}; };
} }
@ -105,7 +107,7 @@ protected:
template<typename MessageType, typename Endpoint> template<typename MessageType, typename Endpoint>
OwnPtr<MessageType> wait_for_specific_endpoint_message() OwnPtr<MessageType> 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<MessageType>(); return message.template release_nonnull<MessageType>();
return {}; return {};
} }