2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2022-03-03 11:37:49 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-08-03 15:29:40 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
2022-02-22 19:22:11 +01:00
|
|
|
#include <AK/Error.h>
|
2021-05-19 14:35:34 +02:00
|
|
|
#include <AK/RefCounted.h>
|
2021-05-02 12:28:20 +02:00
|
|
|
#include <AK/RefPtr.h>
|
2024-01-02 20:14:18 -05:00
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
#include <LibCore/Forward.h>
|
2024-10-22 15:47:33 -06:00
|
|
|
#include <LibIPC/Transport.h>
|
2021-05-02 12:28:20 +02:00
|
|
|
#include <unistd.h>
|
2019-12-30 02:41:45 +01:00
|
|
|
|
2020-02-05 19:57:18 +01:00
|
|
|
namespace IPC {
|
2019-08-03 15:29:40 +02:00
|
|
|
|
2021-05-02 12:28:20 +02:00
|
|
|
class AutoCloseFileDescriptor : public RefCounted<AutoCloseFileDescriptor> {
|
|
|
|
|
public:
|
|
|
|
|
AutoCloseFileDescriptor(int fd)
|
|
|
|
|
: m_fd(fd)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~AutoCloseFileDescriptor()
|
|
|
|
|
{
|
|
|
|
|
if (m_fd != -1)
|
|
|
|
|
close(m_fd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int value() const { return m_fd; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
int m_fd;
|
|
|
|
|
};
|
|
|
|
|
|
2024-01-02 20:27:29 -05:00
|
|
|
class MessageBuffer {
|
|
|
|
|
public:
|
|
|
|
|
MessageBuffer();
|
|
|
|
|
|
|
|
|
|
ErrorOr<void> extend_data_capacity(size_t capacity);
|
|
|
|
|
ErrorOr<void> append_data(u8 const* values, size_t count);
|
|
|
|
|
|
|
|
|
|
ErrorOr<void> append_file_descriptor(int fd);
|
|
|
|
|
|
2024-10-22 15:47:33 -06:00
|
|
|
ErrorOr<void> transfer_message(Transport& socket);
|
2024-01-02 20:14:18 -05:00
|
|
|
|
2024-01-02 20:27:29 -05:00
|
|
|
private:
|
|
|
|
|
Vector<u8, 1024> m_data;
|
|
|
|
|
Vector<NonnullRefPtr<AutoCloseFileDescriptor>, 1> m_fds;
|
2020-11-21 21:59:12 +03:00
|
|
|
};
|
2020-02-05 19:57:18 +01:00
|
|
|
|
2021-05-03 16:51:42 +02:00
|
|
|
enum class ErrorCode : u32 {
|
|
|
|
|
PeerDisconnected
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-22 19:22:11 +01:00
|
|
|
template<typename Value>
|
|
|
|
|
using IPCErrorOr = ErrorOr<Value, ErrorCode>;
|
|
|
|
|
|
2020-02-05 19:57:18 +01:00
|
|
|
class Message {
|
2019-08-03 15:29:40 +02:00
|
|
|
public:
|
2022-03-03 11:37:49 -07:00
|
|
|
virtual ~Message() = default;
|
2019-08-03 15:29:40 +02:00
|
|
|
|
2021-04-25 13:19:53 +02:00
|
|
|
virtual u32 endpoint_magic() const = 0;
|
2019-12-02 11:07:05 +01:00
|
|
|
virtual int message_id() const = 0;
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual char const* message_name() const = 0;
|
2021-05-02 05:20:28 +02:00
|
|
|
virtual bool valid() const = 0;
|
2023-01-01 23:58:49 -05:00
|
|
|
virtual ErrorOr<MessageBuffer> encode() const = 0;
|
2019-08-03 15:29:40 +02:00
|
|
|
|
|
|
|
|
protected:
|
2022-03-03 11:37:49 -07:00
|
|
|
Message() = default;
|
2019-08-03 15:29:40 +02:00
|
|
|
};
|
2020-02-05 19:57:18 +01:00
|
|
|
|
|
|
|
|
}
|