2020-09-12 05:44:00 -04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-03-03 13:37:49 -05:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-09-12 05:44:00 -04:00
|
|
|
*
|
2021-04-22 04:24:48 -04:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-09-12 05:44:00 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/ByteBuffer.h>
|
|
|
|
#include <AK/NonnullOwnPtrVector.h>
|
2021-10-23 15:48:42 -04:00
|
|
|
#include <AK/Try.h>
|
2020-09-12 05:44:00 -04:00
|
|
|
#include <LibCore/Event.h>
|
2020-09-12 07:03:39 -04:00
|
|
|
#include <LibCore/EventLoop.h>
|
2020-09-12 05:44:00 -04:00
|
|
|
#include <LibCore/Notifier.h>
|
2022-01-14 08:12:49 -05:00
|
|
|
#include <LibCore/Stream.h>
|
2020-09-12 05:44:00 -04:00
|
|
|
#include <LibCore/Timer.h>
|
2021-10-23 15:48:42 -04:00
|
|
|
#include <LibIPC/Forward.h>
|
2020-09-12 05:44:00 -04:00
|
|
|
#include <LibIPC/Message.h>
|
2021-04-26 13:09:41 -04:00
|
|
|
#include <errno.h>
|
2020-11-21 13:55:00 -05:00
|
|
|
#include <stdint.h>
|
2020-09-12 05:44:00 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
namespace IPC {
|
|
|
|
|
2021-10-23 15:48:42 -04:00
|
|
|
class ConnectionBase : public Core::Object {
|
|
|
|
C_OBJECT_ABSTRACT(ConnectionBase);
|
|
|
|
|
2020-09-12 05:44:00 -04:00
|
|
|
public:
|
2022-03-03 13:37:49 -05:00
|
|
|
virtual ~ConnectionBase() override = default;
|
2021-10-23 15:48:42 -04:00
|
|
|
|
|
|
|
bool is_open() const { return m_socket->is_open(); }
|
2021-11-28 03:59:36 -05:00
|
|
|
ErrorOr<void> post_message(Message const&);
|
2021-10-23 15:48:42 -04:00
|
|
|
|
|
|
|
void shutdown();
|
|
|
|
virtual void die() { }
|
|
|
|
|
|
|
|
protected:
|
2022-01-14 08:12:49 -05:00
|
|
|
explicit ConnectionBase(IPC::Stub&, NonnullOwnPtr<Core::Stream::LocalSocket>, u32 local_endpoint_magic);
|
2021-10-23 15:48:42 -04:00
|
|
|
|
2022-01-14 08:12:49 -05:00
|
|
|
Core::Stream::LocalSocket& socket() { return *m_socket; }
|
2021-10-23 15:48:42 -04:00
|
|
|
|
|
|
|
virtual void may_have_become_unresponsive() { }
|
|
|
|
virtual void did_become_responsive() { }
|
2021-10-23 16:16:53 -04:00
|
|
|
virtual void try_parse_messages(Vector<u8> const& bytes, size_t& index) = 0;
|
2022-06-10 11:21:00 -04:00
|
|
|
virtual void shutdown_with_error(Error const&);
|
2021-10-23 15:48:42 -04:00
|
|
|
|
2021-10-23 16:59:04 -04:00
|
|
|
OwnPtr<IPC::Message> wait_for_specific_endpoint_message_impl(u32 endpoint_magic, int message_id);
|
2021-10-23 15:48:42 -04:00
|
|
|
void wait_for_socket_to_become_readable();
|
2021-11-07 05:59:43 -05:00
|
|
|
ErrorOr<Vector<u8>> read_as_much_as_possible_from_socket_without_blocking();
|
|
|
|
ErrorOr<void> drain_messages_from_peer();
|
2021-10-23 16:16:53 -04:00
|
|
|
|
2021-11-28 03:59:36 -05:00
|
|
|
ErrorOr<void> post_message(MessageBuffer);
|
2021-10-23 16:59:04 -04:00
|
|
|
void handle_messages();
|
2021-10-23 15:48:42 -04:00
|
|
|
|
|
|
|
IPC::Stub& m_local_stub;
|
|
|
|
|
2022-01-14 08:12:49 -05:00
|
|
|
NonnullOwnPtr<Core::Stream::LocalSocket> m_socket;
|
2021-10-23 15:48:42 -04:00
|
|
|
RefPtr<Core::Timer> m_responsiveness_timer;
|
|
|
|
|
|
|
|
NonnullOwnPtrVector<Message> m_unprocessed_messages;
|
|
|
|
ByteBuffer m_unprocessed_bytes;
|
2021-10-23 16:59:04 -04:00
|
|
|
|
|
|
|
u32 m_local_endpoint_magic { 0 };
|
2021-10-23 15:48:42 -04:00
|
|
|
};
|
2021-05-03 02:46:40 -04:00
|
|
|
|
2021-10-23 15:48:42 -04:00
|
|
|
template<typename LocalEndpoint, typename PeerEndpoint>
|
|
|
|
class Connection : public ConnectionBase {
|
|
|
|
public:
|
2022-01-14 08:12:49 -05:00
|
|
|
Connection(IPC::Stub& local_stub, NonnullOwnPtr<Core::Stream::LocalSocket> socket)
|
2021-10-23 16:59:04 -04:00
|
|
|
: ConnectionBase(local_stub, move(socket), LocalEndpoint::static_magic())
|
2020-09-12 05:44:00 -04:00
|
|
|
{
|
2022-01-14 08:12:49 -05:00
|
|
|
m_socket->on_ready_to_read = [this] {
|
2021-02-20 05:07:29 -05:00
|
|
|
NonnullRefPtr protect = *this;
|
2021-11-07 05:59:43 -05:00
|
|
|
// FIXME: Do something about errors.
|
|
|
|
(void)drain_messages_from_peer();
|
2021-10-23 16:59:04 -04:00
|
|
|
handle_messages();
|
2020-09-12 07:03:39 -04:00
|
|
|
};
|
2020-09-12 05:44:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename MessageType>
|
|
|
|
OwnPtr<MessageType> wait_for_specific_message()
|
|
|
|
{
|
|
|
|
return wait_for_specific_endpoint_message<MessageType, LocalEndpoint>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename RequestType, typename... Args>
|
2021-04-20 03:53:31 -04:00
|
|
|
NonnullOwnPtr<typename RequestType::ResponseType> send_sync(Args&&... args)
|
2020-09-12 05:44:00 -04:00
|
|
|
{
|
2021-11-28 03:59:36 -05:00
|
|
|
MUST(post_message(RequestType(forward<Args>(args)...)));
|
2020-09-12 05:44:00 -04:00
|
|
|
auto response = wait_for_specific_endpoint_message<typename RequestType::ResponseType, PeerEndpoint>();
|
2021-02-23 14:42:32 -05:00
|
|
|
VERIFY(response);
|
2021-04-20 03:53:31 -04:00
|
|
|
return response.release_nonnull();
|
2020-09-12 05:44:00 -04:00
|
|
|
}
|
|
|
|
|
2021-02-20 05:07:29 -05:00
|
|
|
template<typename RequestType, typename... Args>
|
|
|
|
OwnPtr<typename RequestType::ResponseType> send_sync_but_allow_failure(Args&&... args)
|
|
|
|
{
|
2021-11-28 03:59:36 -05:00
|
|
|
if (post_message(RequestType(forward<Args>(args)...)).is_error())
|
|
|
|
return nullptr;
|
2021-02-20 05:07:29 -05:00
|
|
|
return wait_for_specific_endpoint_message<typename RequestType::ResponseType, PeerEndpoint>();
|
|
|
|
}
|
|
|
|
|
2020-09-12 05:44:00 -04:00
|
|
|
protected:
|
|
|
|
template<typename MessageType, typename Endpoint>
|
|
|
|
OwnPtr<MessageType> wait_for_specific_endpoint_message()
|
|
|
|
{
|
2021-10-23 16:59:04 -04:00
|
|
|
if (auto message = wait_for_specific_endpoint_message_impl(Endpoint::static_magic(), MessageType::static_message_id()))
|
2021-10-23 16:21:47 -04:00
|
|
|
return message.template release_nonnull<MessageType>();
|
2021-01-10 18:29:28 -05:00
|
|
|
return {};
|
2020-09-12 05:44:00 -04:00
|
|
|
}
|
|
|
|
|
2021-10-23 16:16:53 -04:00
|
|
|
virtual void try_parse_messages(Vector<u8> const& bytes, size_t& index) override
|
2020-09-12 05:44:00 -04:00
|
|
|
{
|
2021-05-24 03:04:22 -04:00
|
|
|
u32 message_size = 0;
|
2020-11-21 13:55:00 -05:00
|
|
|
for (; index + sizeof(message_size) < bytes.size(); index += message_size) {
|
2021-05-24 03:04:22 -04:00
|
|
|
memcpy(&message_size, bytes.data() + index, sizeof(message_size));
|
2020-11-21 13:55:00 -05:00
|
|
|
if (message_size == 0 || bytes.size() - index - sizeof(uint32_t) < message_size)
|
|
|
|
break;
|
|
|
|
index += sizeof(message_size);
|
2021-08-11 05:31:58 -04:00
|
|
|
auto remaining_bytes = ReadonlyBytes { bytes.data() + index, message_size };
|
2022-01-14 08:12:49 -05:00
|
|
|
if (auto message = LocalEndpoint::decode_message(remaining_bytes, *m_socket)) {
|
2020-09-12 05:44:00 -04:00
|
|
|
m_unprocessed_messages.append(message.release_nonnull());
|
2022-01-14 08:12:49 -05:00
|
|
|
} else if (auto message = PeerEndpoint::decode_message(remaining_bytes, *m_socket)) {
|
2020-09-12 05:44:00 -04:00
|
|
|
m_unprocessed_messages.append(message.release_nonnull());
|
|
|
|
} else {
|
2020-11-21 13:55:00 -05:00
|
|
|
dbgln("Failed to parse a message");
|
2020-10-25 06:37:42 -04:00
|
|
|
break;
|
2020-09-12 05:44:00 -04:00
|
|
|
}
|
2020-11-21 13:55:00 -05:00
|
|
|
}
|
2020-09-12 05:44:00 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2021-01-11 15:13:30 -05:00
|
|
|
|
|
|
|
template<typename LocalEndpoint, typename PeerEndpoint>
|
|
|
|
struct AK::Formatter<IPC::Connection<LocalEndpoint, PeerEndpoint>> : Formatter<Core::Object> {
|
|
|
|
};
|