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