From f9b95b6e137019ce6c2610d63809cbc29c779151 Mon Sep 17 00:00:00 2001 From: stasoid Date: Fri, 29 Nov 2024 23:03:56 +0500 Subject: [PATCH] LibIPC: Port MessageBuffer to Windows --- Libraries/LibIPC/CMakeLists.txt | 9 +++-- Libraries/LibIPC/Connection.h | 2 +- Libraries/LibIPC/Message.h | 9 +++-- Libraries/LibIPC/MessageWindows.cpp | 51 +++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 Libraries/LibIPC/MessageWindows.cpp diff --git a/Libraries/LibIPC/CMakeLists.txt b/Libraries/LibIPC/CMakeLists.txt index f2089fd82e0de..cd8e259a71de4 100644 --- a/Libraries/LibIPC/CMakeLists.txt +++ b/Libraries/LibIPC/CMakeLists.txt @@ -3,13 +3,16 @@ set(SOURCES Decoder.cpp Encoder.cpp File.cpp - Message.cpp ) if (UNIX) - list(APPEND SOURCES TransportSocket.cpp) + list(APPEND SOURCES + Message.cpp + TransportSocket.cpp) else() - list(APPEND SOURCES TransportSocketWindows.cpp) + list(APPEND SOURCES + MessageWindows.cpp + TransportSocketWindows.cpp) endif() serenity_lib(LibIPC ipc) diff --git a/Libraries/LibIPC/Connection.h b/Libraries/LibIPC/Connection.h index 229227c7c1421..9bbf65b0c3074 100644 --- a/Libraries/LibIPC/Connection.h +++ b/Libraries/LibIPC/Connection.h @@ -57,7 +57,7 @@ class ConnectionBase : public Core::EventReceiver { RefPtr m_responsiveness_timer; Vector> m_unprocessed_messages; - Queue m_unprocessed_fds; + Queue m_unprocessed_fds; // unused on Windows ByteBuffer m_unprocessed_bytes; u32 m_local_endpoint_magic { 0 }; diff --git a/Libraries/LibIPC/Message.h b/Libraries/LibIPC/Message.h index 413cb03dde988..c0b9b39291706 100644 --- a/Libraries/LibIPC/Message.h +++ b/Libraries/LibIPC/Message.h @@ -12,8 +12,8 @@ #include #include #include +#include #include -#include namespace IPC { @@ -27,7 +27,7 @@ class AutoCloseFileDescriptor : public RefCounted { ~AutoCloseFileDescriptor() { if (m_fd != -1) - close(m_fd); + (void)Core::System::close(m_fd); } int value() const { return m_fd; } @@ -45,11 +45,14 @@ class MessageBuffer { ErrorOr append_file_descriptor(int fd); - ErrorOr transfer_message(Transport& socket); + ErrorOr transfer_message(Transport& transport); private: Vector m_data; Vector, 1> m_fds; +#ifdef AK_OS_WINDOWS + Vector m_handle_offsets; +#endif }; enum class ErrorCode : u32 { diff --git a/Libraries/LibIPC/MessageWindows.cpp b/Libraries/LibIPC/MessageWindows.cpp new file mode 100644 index 0000000000000..4a3ee6a73beec --- /dev/null +++ b/Libraries/LibIPC/MessageWindows.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2024, Tim Flynn + * Copyright (c) 2024, stasoid + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +namespace IPC { + +using MessageSizeType = u32; + +MessageBuffer::MessageBuffer() +{ + m_data.resize(sizeof(MessageSizeType)); +} + +ErrorOr MessageBuffer::extend_data_capacity(size_t capacity) +{ + TRY(m_data.try_ensure_capacity(m_data.size() + capacity)); + return {}; +} + +ErrorOr MessageBuffer::append_data(u8 const* values, size_t count) +{ + TRY(m_data.try_append(values, count)); + return {}; +} + +ErrorOr MessageBuffer::append_file_descriptor(int fd) +{ + m_fds.append(adopt_ref(*new AutoCloseFileDescriptor(fd))); + m_handle_offsets.append(m_data.size()); + intptr_t handle = _get_osfhandle(fd); + m_data.append((u8*)&handle, sizeof(handle)); + return {}; +} + +ErrorOr MessageBuffer::transfer_message(Transport& transport) +{ + VERIFY(m_data.size() >= sizeof(MessageSizeType) && m_data.size() < NumericLimits::max()); + size_t message_size = m_data.size() - sizeof(MessageSizeType); + *(MessageSizeType*)m_data.data() = message_size; + + TRY(transport.transfer(m_data.span(), m_handle_offsets)); + return {}; +} + +}