Add IPC::TransportHandle as an abstraction for passing IPC transports through .ipc messages. This replaces IPC::File at all sites where a transport (not a generic file) is being transferred between processes. TransportHandle provides from_transport(), clone_from_transport(), and create_transport() methods that encapsulate the fd-to-socket-to-transport conversion in one place. This is preparatory work for Mach port support on macOS -- when that lands, only TransportHandle's internals need to change while all .ipc definitions and call sites remain untouched.
63 lines
1.4 KiB
C++
63 lines
1.4 KiB
C++
/*
|
|
* Copyright (c) 2026, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <LibCore/Socket.h>
|
|
#include <LibIPC/Decoder.h>
|
|
#include <LibIPC/Encoder.h>
|
|
#include <LibIPC/File.h>
|
|
#include <LibIPC/Transport.h>
|
|
#include <LibIPC/TransportHandle.h>
|
|
|
|
namespace IPC {
|
|
|
|
TransportHandle::TransportHandle(File file)
|
|
: m_file(move(file))
|
|
{
|
|
}
|
|
|
|
ErrorOr<TransportHandle> TransportHandle::from_transport(Transport& transport)
|
|
{
|
|
auto fd = TRY(transport.release_underlying_transport_for_transfer());
|
|
return TransportHandle { File::adopt_fd(fd) };
|
|
}
|
|
|
|
ErrorOr<TransportHandle> TransportHandle::clone_from_transport(Transport& transport)
|
|
{
|
|
auto file = TRY(transport.clone_for_transfer());
|
|
return TransportHandle { move(file) };
|
|
}
|
|
|
|
ErrorOr<NonnullOwnPtr<Transport>> TransportHandle::create_transport() const
|
|
{
|
|
auto socket = TRY(Core::LocalSocket::adopt_fd(m_file.take_fd()));
|
|
TRY(socket->set_blocking(true));
|
|
return make<Transport>(move(socket));
|
|
}
|
|
|
|
int TransportHandle::fd() const
|
|
{
|
|
return m_file.fd();
|
|
}
|
|
|
|
ErrorOr<void> TransportHandle::clear_close_on_exec()
|
|
{
|
|
return m_file.clear_close_on_exec();
|
|
}
|
|
|
|
template<>
|
|
ErrorOr<void> encode(Encoder& encoder, TransportHandle const& handle)
|
|
{
|
|
return encoder.encode(handle.m_file);
|
|
}
|
|
|
|
template<>
|
|
ErrorOr<TransportHandle> decode(Decoder& decoder)
|
|
{
|
|
auto file = TRY(decoder.decode<File>());
|
|
return TransportHandle { move(file) };
|
|
}
|
|
|
|
}
|