mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-02 08:22:55 +00:00
Some checks are pending
CI / Lagom (x86_64, Fuzzers_CI, false, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (arm64, Sanitizer_CI, false, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, false, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, true, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (arm64, macos-15, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (x86_64, ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
Memory stream is a more suitable container for the socket send queue, as using it results in fewer allocations than trying to emulate a stream using a Vector.
121 lines
3.1 KiB
C++
121 lines
3.1 KiB
C++
/*
|
|
* Copyright (c) 2024, Andrew Kaster <andrew@ladybird.org>
|
|
* Copyright (c) 2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/MemoryStream.h>
|
|
#include <AK/Queue.h>
|
|
#include <LibCore/Socket.h>
|
|
#include <LibThreading/ConditionVariable.h>
|
|
#include <LibThreading/MutexProtected.h>
|
|
#include <LibThreading/RWLock.h>
|
|
#include <LibThreading/Thread.h>
|
|
|
|
namespace IPC {
|
|
|
|
class AutoCloseFileDescriptor : public RefCounted<AutoCloseFileDescriptor> {
|
|
public:
|
|
AutoCloseFileDescriptor(int fd)
|
|
: m_fd(fd)
|
|
{
|
|
}
|
|
|
|
~AutoCloseFileDescriptor()
|
|
{
|
|
if (m_fd != -1)
|
|
(void)Core::System::close(m_fd);
|
|
}
|
|
|
|
int value() const { return m_fd; }
|
|
|
|
int take_fd()
|
|
{
|
|
int fd = m_fd;
|
|
m_fd = -1;
|
|
return fd;
|
|
}
|
|
|
|
private:
|
|
int m_fd;
|
|
};
|
|
|
|
class SendQueue : public AtomicRefCounted<SendQueue> {
|
|
public:
|
|
enum class Running {
|
|
No,
|
|
Yes,
|
|
};
|
|
Running block_until_message_enqueued();
|
|
void stop();
|
|
|
|
void enqueue_message(Vector<u8>&& bytes, Vector<int>&& fds);
|
|
struct BytesAndFds {
|
|
Vector<u8> bytes;
|
|
Vector<int> fds;
|
|
};
|
|
BytesAndFds peek(size_t max_bytes);
|
|
void discard(size_t bytes_count, size_t fds_count);
|
|
|
|
private:
|
|
AllocatingMemoryStream m_stream;
|
|
Vector<int> m_fds;
|
|
Threading::Mutex m_mutex;
|
|
Threading::ConditionVariable m_condition { m_mutex };
|
|
bool m_running { true };
|
|
};
|
|
|
|
class TransportSocket {
|
|
AK_MAKE_NONCOPYABLE(TransportSocket);
|
|
AK_MAKE_NONMOVABLE(TransportSocket);
|
|
|
|
public:
|
|
static constexpr socklen_t SOCKET_BUFFER_SIZE = 128 * KiB;
|
|
|
|
explicit TransportSocket(NonnullOwnPtr<Core::LocalSocket> socket);
|
|
~TransportSocket();
|
|
|
|
void set_up_read_hook(Function<void()>);
|
|
bool is_open() const;
|
|
void close();
|
|
|
|
void wait_until_readable();
|
|
|
|
void post_message(Vector<u8> const&, Vector<NonnullRefPtr<AutoCloseFileDescriptor>> const&);
|
|
|
|
enum class ShouldShutdown {
|
|
No,
|
|
Yes,
|
|
};
|
|
struct Message {
|
|
Vector<u8> bytes;
|
|
Queue<File> fds;
|
|
};
|
|
ShouldShutdown read_as_many_messages_as_possible_without_blocking(Function<void(Message&&)>&&);
|
|
|
|
// Obnoxious name to make it clear that this is a dangerous operation.
|
|
ErrorOr<int> release_underlying_transport_for_transfer();
|
|
|
|
ErrorOr<IPC::File> clone_for_transfer();
|
|
|
|
private:
|
|
static ErrorOr<void> send_message(Core::LocalSocket&, ReadonlyBytes& bytes, Vector<int>& unowned_fds);
|
|
|
|
NonnullOwnPtr<Core::LocalSocket> m_socket;
|
|
mutable Threading::RWLock m_socket_rw_lock;
|
|
ByteBuffer m_unprocessed_bytes;
|
|
Queue<File> m_unprocessed_fds;
|
|
|
|
// After file descriptor is sent, it is moved to the wait queue until an acknowledgement is received from the peer.
|
|
// This is necessary to handle a specific behavior of the macOS kernel, which may prematurely garbage-collect the file
|
|
// descriptor contained in the message before the peer receives it. https://openradar.me/9477351
|
|
Queue<NonnullRefPtr<AutoCloseFileDescriptor>> m_fds_retained_until_received_by_peer;
|
|
|
|
RefPtr<Threading::Thread> m_send_thread;
|
|
RefPtr<SendQueue> m_send_queue;
|
|
};
|
|
|
|
}
|