mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 08:17:35 +00:00
LibIPC: Put all classes in the IPC namespace and remove the leading I
This commit is contained in:
parent
a894a799c3
commit
d264e8fcc5
22 changed files with 108 additions and 80 deletions
|
@ -40,23 +40,25 @@
|
|||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
class IEvent : public Core::Event {
|
||||
namespace IPC {
|
||||
|
||||
class Event : public Core::Event {
|
||||
public:
|
||||
enum Type {
|
||||
Invalid = 2000,
|
||||
Disconnected,
|
||||
};
|
||||
IEvent() {}
|
||||
explicit IEvent(Type type)
|
||||
Event() {}
|
||||
explicit Event(Type type)
|
||||
: Core::Event(type)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
class IDisconnectedEvent : public IEvent {
|
||||
class DisconnectedEvent : public Event {
|
||||
public:
|
||||
explicit IDisconnectedEvent(int client_id)
|
||||
: IEvent(Disconnected)
|
||||
explicit DisconnectedEvent(int client_id)
|
||||
: Event(Disconnected)
|
||||
, m_client_id(client_id)
|
||||
{
|
||||
}
|
||||
|
@ -74,9 +76,9 @@ NonnullRefPtr<T> new_client_connection(Args&&... args)
|
|||
}
|
||||
|
||||
template<typename Endpoint>
|
||||
class IClientConnection : public Core::Object {
|
||||
class ClientConnection : public Core::Object {
|
||||
public:
|
||||
IClientConnection(Endpoint& endpoint, Core::LocalSocket& socket, int client_id)
|
||||
ClientConnection(Endpoint& endpoint, Core::LocalSocket& socket, int client_id)
|
||||
: m_endpoint(endpoint)
|
||||
, m_socket(socket)
|
||||
, m_client_id(client_id)
|
||||
|
@ -92,11 +94,11 @@ public:
|
|||
m_socket->on_ready_to_read = [this] { drain_messages_from_client(); };
|
||||
}
|
||||
|
||||
virtual ~IClientConnection() override
|
||||
virtual ~ClientConnection() override
|
||||
{
|
||||
}
|
||||
|
||||
void post_message(const IMessage& message)
|
||||
void post_message(const Message& message)
|
||||
{
|
||||
// NOTE: If this connection is being shut down, but has not yet been destroyed,
|
||||
// the socket will be closed. Don't try to send more messages.
|
||||
|
@ -109,11 +111,11 @@ public:
|
|||
if (nwritten < 0) {
|
||||
switch (errno) {
|
||||
case EPIPE:
|
||||
dbg() << "Connection::post_message: Disconnected from peer";
|
||||
dbg() << *this << "::post_message: Disconnected from peer";
|
||||
shutdown();
|
||||
return;
|
||||
case EAGAIN:
|
||||
dbg() << "Connection::post_message: Client buffer overflowed.";
|
||||
dbg() << *this << "::post_message: Client buffer overflowed.";
|
||||
did_misbehave();
|
||||
return;
|
||||
default:
|
||||
|
@ -133,7 +135,7 @@ public:
|
|||
ssize_t nread = recv(m_socket->fd(), buffer, sizeof(buffer), MSG_DONTWAIT);
|
||||
if (nread == 0 || (nread == -1 && errno == EAGAIN)) {
|
||||
if (bytes.is_empty()) {
|
||||
Core::EventLoop::current().post_event(*this, make<IDisconnectedEvent>(client_id()));
|
||||
Core::EventLoop::current().post_event(*this, make<DisconnectedEvent>(client_id()));
|
||||
return;
|
||||
}
|
||||
break;
|
||||
|
@ -162,13 +164,13 @@ public:
|
|||
|
||||
void did_misbehave()
|
||||
{
|
||||
dbg() << "Connection{" << this << "} (id=" << m_client_id << ", pid=" << m_client_pid << ") misbehaved, disconnecting.";
|
||||
dbg() << *this << " (id=" << m_client_id << ", pid=" << m_client_pid << ") misbehaved, disconnecting.";
|
||||
shutdown();
|
||||
}
|
||||
|
||||
void did_misbehave(const char* message)
|
||||
{
|
||||
dbg() << "Connection{" << this << "} (id=" << m_client_id << ", pid=" << m_client_pid << ") misbehaved (" << message << "), disconnecting.";
|
||||
dbg() << *this << " (id=" << m_client_id << ", pid=" << m_client_pid << ") misbehaved (" << message << "), disconnecting.";
|
||||
shutdown();
|
||||
}
|
||||
|
||||
|
@ -186,9 +188,9 @@ public:
|
|||
protected:
|
||||
void event(Core::Event& event) override
|
||||
{
|
||||
if (event.type() == IEvent::Disconnected) {
|
||||
int client_id = static_cast<const IDisconnectedEvent&>(event).client_id();
|
||||
dbgprintf("Connection: Client disconnected: %d\n", client_id);
|
||||
if (event.type() == Event::Disconnected) {
|
||||
int client_id = static_cast<const DisconnectedEvent&>(event).client_id();
|
||||
dbg() << *this << ": Client disconnected: " << client_id;
|
||||
die();
|
||||
return;
|
||||
}
|
||||
|
@ -202,3 +204,5 @@ private:
|
|||
int m_client_id { -1 };
|
||||
int m_client_pid { -1 };
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -28,25 +28,27 @@
|
|||
|
||||
#include <LibIPC/IMessage.h>
|
||||
|
||||
class IEncoder {
|
||||
namespace IPC {
|
||||
|
||||
class Encoder {
|
||||
public:
|
||||
explicit IEncoder(IMessageBuffer& buffer)
|
||||
explicit Encoder(MessageBuffer& buffer)
|
||||
: m_buffer(buffer)
|
||||
{
|
||||
}
|
||||
|
||||
IEncoder& operator<<(bool value)
|
||||
Encoder& operator<<(bool value)
|
||||
{
|
||||
return *this << (u8)value;
|
||||
}
|
||||
|
||||
IEncoder& operator<<(u8 value)
|
||||
Encoder& operator<<(u8 value)
|
||||
{
|
||||
m_buffer.append(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
IEncoder& operator<<(u16 value)
|
||||
Encoder& operator<<(u16 value)
|
||||
{
|
||||
m_buffer.ensure_capacity(m_buffer.size() + 2);
|
||||
m_buffer.unchecked_append((u8)value);
|
||||
|
@ -54,7 +56,7 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
IEncoder& operator<<(u32 value)
|
||||
Encoder& operator<<(u32 value)
|
||||
{
|
||||
m_buffer.ensure_capacity(m_buffer.size() + 4);
|
||||
m_buffer.unchecked_append((u8)value);
|
||||
|
@ -64,7 +66,7 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
IEncoder& operator<<(u64 value)
|
||||
Encoder& operator<<(u64 value)
|
||||
{
|
||||
m_buffer.ensure_capacity(m_buffer.size() + 8);
|
||||
m_buffer.unchecked_append((u8)value);
|
||||
|
@ -78,13 +80,13 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
IEncoder& operator<<(i8 value)
|
||||
Encoder& operator<<(i8 value)
|
||||
{
|
||||
m_buffer.append((u8)value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
IEncoder& operator<<(i16 value)
|
||||
Encoder& operator<<(i16 value)
|
||||
{
|
||||
m_buffer.ensure_capacity(m_buffer.size() + 2);
|
||||
m_buffer.unchecked_append((u8)value);
|
||||
|
@ -92,7 +94,7 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
IEncoder& operator<<(i32 value)
|
||||
Encoder& operator<<(i32 value)
|
||||
{
|
||||
m_buffer.ensure_capacity(m_buffer.size() + 4);
|
||||
m_buffer.unchecked_append((u8)value);
|
||||
|
@ -102,7 +104,7 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
IEncoder& operator<<(i64 value)
|
||||
Encoder& operator<<(i64 value)
|
||||
{
|
||||
m_buffer.ensure_capacity(m_buffer.size() + 8);
|
||||
m_buffer.unchecked_append((u8)value);
|
||||
|
@ -116,27 +118,27 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
IEncoder& operator<<(size_t value)
|
||||
Encoder& operator<<(size_t value)
|
||||
{
|
||||
if constexpr(sizeof(size_t) == 4)
|
||||
if constexpr (sizeof(size_t) == 4)
|
||||
return *this << (u32)value;
|
||||
else if constexpr(sizeof(size_t) == 8)
|
||||
else if constexpr (sizeof(size_t) == 8)
|
||||
return *this << (u64)value;
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
#ifndef __i386__
|
||||
IEncoder& operator<<(ssize_t value)
|
||||
Encoder& operator<<(ssize_t value)
|
||||
{
|
||||
if constexpr(sizeof(ssize_t) == 4)
|
||||
if constexpr (sizeof(ssize_t) == 4)
|
||||
return *this << (i32)value;
|
||||
else if constexpr(sizeof(ssize_t) == 8)
|
||||
else if constexpr (sizeof(ssize_t) == 8)
|
||||
return *this << (i64)value;
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
#endif
|
||||
|
||||
IEncoder& operator<<(float value)
|
||||
Encoder& operator<<(float value)
|
||||
{
|
||||
union bits {
|
||||
float as_float;
|
||||
|
@ -146,17 +148,19 @@ public:
|
|||
return *this << u.as_u32;
|
||||
}
|
||||
|
||||
IEncoder& operator<<(const char* value)
|
||||
Encoder& operator<<(const char* value)
|
||||
{
|
||||
return *this << StringView(value);
|
||||
}
|
||||
|
||||
IEncoder& operator<<(const StringView& value)
|
||||
Encoder& operator<<(const StringView& value)
|
||||
{
|
||||
m_buffer.append((const u8*)value.characters_without_null_termination(), value.length());
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
IMessageBuffer& m_buffer;
|
||||
MessageBuffer& m_buffer;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -26,10 +26,14 @@
|
|||
|
||||
#include <LibIPC/IEndpoint.h>
|
||||
|
||||
IEndpoint::IEndpoint()
|
||||
namespace IPC {
|
||||
|
||||
Endpoint::Endpoint()
|
||||
{
|
||||
}
|
||||
|
||||
IEndpoint::~IEndpoint()
|
||||
Endpoint::~Endpoint()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -26,26 +26,30 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/String.h>
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <AK/String.h>
|
||||
|
||||
namespace AK {
|
||||
class BufferStream;
|
||||
}
|
||||
|
||||
class IMessage;
|
||||
namespace IPC {
|
||||
|
||||
class IEndpoint {
|
||||
class Message;
|
||||
|
||||
class Endpoint {
|
||||
public:
|
||||
virtual ~IEndpoint();
|
||||
virtual ~Endpoint();
|
||||
|
||||
virtual int magic() const = 0;
|
||||
virtual String name() const = 0;
|
||||
virtual OwnPtr<IMessage> handle(const IMessage&) = 0;
|
||||
virtual OwnPtr<Message> handle(const Message&) = 0;
|
||||
|
||||
protected:
|
||||
IEndpoint();
|
||||
Endpoint();
|
||||
|
||||
private:
|
||||
String m_name;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -26,10 +26,14 @@
|
|||
|
||||
#include <LibIPC/IMessage.h>
|
||||
|
||||
IMessage::IMessage()
|
||||
namespace IPC {
|
||||
|
||||
Message::Message()
|
||||
{
|
||||
}
|
||||
|
||||
IMessage::~IMessage()
|
||||
Message::~Message()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -28,17 +28,21 @@
|
|||
|
||||
#include <AK/String.h>
|
||||
|
||||
typedef Vector<u8, 1024> IMessageBuffer;
|
||||
namespace IPC {
|
||||
|
||||
class IMessage {
|
||||
typedef Vector<u8, 1024> MessageBuffer;
|
||||
|
||||
class Message {
|
||||
public:
|
||||
virtual ~IMessage();
|
||||
virtual ~Message();
|
||||
|
||||
virtual int endpoint_magic() const = 0;
|
||||
virtual int message_id() const = 0;
|
||||
virtual String message_name() const = 0;
|
||||
virtual IMessageBuffer encode() const = 0;
|
||||
virtual MessageBuffer encode() const = 0;
|
||||
|
||||
protected:
|
||||
IMessage();
|
||||
Message();
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -40,10 +40,12 @@
|
|||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace IPC {
|
||||
|
||||
template<typename LocalEndpoint, typename PeerEndpoint>
|
||||
class IServerConnection : public Core::Object {
|
||||
class ServerConnection : public Core::Object {
|
||||
public:
|
||||
IServerConnection(LocalEndpoint& local_endpoint, const StringView& address)
|
||||
ServerConnection(LocalEndpoint& local_endpoint, const StringView& address)
|
||||
: m_local_endpoint(local_endpoint)
|
||||
, m_connection(Core::LocalSocket::construct(this))
|
||||
, m_notifier(Core::Notifier::construct(m_connection->fd(), Core::Notifier::Read, this))
|
||||
|
@ -116,7 +118,7 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
bool post_message(const IMessage& message)
|
||||
bool post_message(const Message& message)
|
||||
{
|
||||
auto buffer = message.encode();
|
||||
int nwritten = write(m_connection->fd(), buffer.data(), (size_t)buffer.size());
|
||||
|
@ -195,7 +197,9 @@ private:
|
|||
LocalEndpoint& m_local_endpoint;
|
||||
RefPtr<Core::LocalSocket> m_connection;
|
||||
RefPtr<Core::Notifier> m_notifier;
|
||||
Vector<OwnPtr<IMessage>> m_unprocessed_messages;
|
||||
Vector<OwnPtr<Message>> m_unprocessed_messages;
|
||||
int m_server_pid { -1 };
|
||||
int m_my_client_id { -1 };
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue