/* * Copyright (c) 2018-2020, Andreas Kling * Copyright (c) 2022, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace IPC { // NOTE: This is an abstraction to allow using IPC::Connection without a Core::EventLoop. // FIXME: It's not particularly nice, think of something nicer. struct DeferredInvoker { virtual ~DeferredInvoker() = default; virtual void schedule(Function) = 0; }; class ConnectionBase : public Core::Object { C_OBJECT_ABSTRACT(ConnectionBase); public: virtual ~ConnectionBase() override = default; void set_fd_passing_socket(NonnullOwnPtr); void set_deferred_invoker(NonnullOwnPtr); DeferredInvoker& deferred_invoker() { return *m_deferred_invoker; } bool is_open() const { return m_socket->is_open(); } ErrorOr post_message(Message const&); void shutdown(); virtual void die() { } Core::Stream::LocalSocket& socket() { return *m_socket; } Core::Stream::LocalSocket& fd_passing_socket(); protected: explicit ConnectionBase(IPC::Stub&, NonnullOwnPtr, u32 local_endpoint_magic); virtual void may_have_become_unresponsive() { } virtual void did_become_responsive() { } virtual void try_parse_messages(Vector const& bytes, size_t& index) = 0; virtual void shutdown_with_error(Error const&); OwnPtr wait_for_specific_endpoint_message_impl(u32 endpoint_magic, int message_id); void wait_for_socket_to_become_readable(); ErrorOr> read_as_much_as_possible_from_socket_without_blocking(); ErrorOr drain_messages_from_peer(); ErrorOr post_message(MessageBuffer); void handle_messages(); IPC::Stub& m_local_stub; NonnullOwnPtr m_socket; OwnPtr m_fd_passing_socket; RefPtr m_responsiveness_timer; NonnullOwnPtrVector m_unprocessed_messages; ByteBuffer m_unprocessed_bytes; u32 m_local_endpoint_magic { 0 }; NonnullOwnPtr m_deferred_invoker; }; template class Connection : public ConnectionBase { public: Connection(IPC::Stub& local_stub, NonnullOwnPtr socket) : ConnectionBase(local_stub, move(socket), LocalEndpoint::static_magic()) { m_socket->on_ready_to_read = [this] { NonnullRefPtr protect = *this; // FIXME: Do something about errors. (void)drain_messages_from_peer(); handle_messages(); }; } template OwnPtr wait_for_specific_message() { return wait_for_specific_endpoint_message(); } template NonnullOwnPtr send_sync(Args&&... args) { MUST(post_message(RequestType(forward(args)...))); auto response = wait_for_specific_endpoint_message(); VERIFY(response); return response.release_nonnull(); } template OwnPtr send_sync_but_allow_failure(Args&&... args) { if (post_message(RequestType(forward(args)...)).is_error()) return nullptr; return wait_for_specific_endpoint_message(); } protected: template OwnPtr wait_for_specific_endpoint_message() { if (auto message = wait_for_specific_endpoint_message_impl(Endpoint::static_magic(), MessageType::static_message_id())) return message.template release_nonnull(); return {}; } virtual void try_parse_messages(Vector const& bytes, size_t& index) override { u32 message_size = 0; for (; index + sizeof(message_size) < bytes.size(); index += message_size) { memcpy(&message_size, bytes.data() + index, sizeof(message_size)); if (message_size == 0 || bytes.size() - index - sizeof(uint32_t) < message_size) break; index += sizeof(message_size); auto remaining_bytes = ReadonlyBytes { bytes.data() + index, message_size }; if (auto message = LocalEndpoint::decode_message(remaining_bytes, fd_passing_socket())) { m_unprocessed_messages.append(message.release_nonnull()); } else if (auto message = PeerEndpoint::decode_message(remaining_bytes, fd_passing_socket())) { m_unprocessed_messages.append(message.release_nonnull()); } else { dbgln("Failed to parse a message"); break; } } } }; } template struct AK::Formatter> : Formatter { };