Skip to content

Commit

Permalink
LibIPC: Port MessageBuffer to Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
stasoid committed Dec 7, 2024
1 parent e0d0904 commit f9b95b6
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 7 deletions.
9 changes: 6 additions & 3 deletions Libraries/LibIPC/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibIPC/Connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class ConnectionBase : public Core::EventReceiver {
RefPtr<Core::Timer> m_responsiveness_timer;

Vector<NonnullOwnPtr<Message>> m_unprocessed_messages;
Queue<IPC::File> m_unprocessed_fds;
Queue<IPC::File> m_unprocessed_fds; // unused on Windows
ByteBuffer m_unprocessed_bytes;

u32 m_local_endpoint_magic { 0 };
Expand Down
9 changes: 6 additions & 3 deletions Libraries/LibIPC/Message.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#include <AK/RefPtr.h>
#include <AK/Vector.h>
#include <LibCore/Forward.h>
#include <LibCore/System.h>
#include <LibIPC/Transport.h>
#include <unistd.h>

namespace IPC {

Expand All @@ -27,7 +27,7 @@ class AutoCloseFileDescriptor : public RefCounted<AutoCloseFileDescriptor> {
~AutoCloseFileDescriptor()
{
if (m_fd != -1)
close(m_fd);
(void)Core::System::close(m_fd);
}

int value() const { return m_fd; }
Expand All @@ -45,11 +45,14 @@ class MessageBuffer {

ErrorOr<void> append_file_descriptor(int fd);

ErrorOr<void> transfer_message(Transport& socket);
ErrorOr<void> transfer_message(Transport& transport);

private:
Vector<u8, 1024> m_data;
Vector<NonnullRefPtr<AutoCloseFileDescriptor>, 1> m_fds;
#ifdef AK_OS_WINDOWS
Vector<size_t> m_handle_offsets;
#endif
};

enum class ErrorCode : u32 {
Expand Down
51 changes: 51 additions & 0 deletions Libraries/LibIPC/MessageWindows.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
* Copyright (c) 2024, stasoid <stasoid@yahoo.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <LibIPC/Message.h>
#include <io.h>

namespace IPC {

using MessageSizeType = u32;

MessageBuffer::MessageBuffer()
{
m_data.resize(sizeof(MessageSizeType));
}

ErrorOr<void> MessageBuffer::extend_data_capacity(size_t capacity)
{
TRY(m_data.try_ensure_capacity(m_data.size() + capacity));
return {};
}

ErrorOr<void> MessageBuffer::append_data(u8 const* values, size_t count)
{
TRY(m_data.try_append(values, count));
return {};
}

ErrorOr<void> 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<void> MessageBuffer::transfer_message(Transport& transport)
{
VERIFY(m_data.size() >= sizeof(MessageSizeType) && m_data.size() < NumericLimits<MessageSizeType>::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 {};
}

}

0 comments on commit f9b95b6

Please sign in to comment.