LibIPC: Move waiting for synchronous responses to ConnectionBase

This commit is contained in:
Andreas Kling 2021-10-23 22:21:47 +02:00
parent 8728d36dd0
commit 9a8bdf84c8
2 changed files with 26 additions and 19 deletions

View file

@ -181,4 +181,27 @@ bool ConnectionBase::drain_messages_from_peer(u32 local_endpoint_magic)
return true;
}
OwnPtr<IPC::Message> 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 {};
}
}

View file

@ -48,6 +48,7 @@ protected:
virtual void did_become_responsive() { }
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);
void wait_for_socket_to_become_readable();
Result<Vector<u8>, 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<typename MessageType, typename Endpoint>
OwnPtr<MessageType> 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<MessageType>();
}
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<MessageType>();
return {};
}