1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 14:18:12 +00:00

LibIPC: Put all classes in the IPC namespace and remove the leading I

This commit is contained in:
Andreas Kling 2020-02-05 19:57:18 +01:00
parent a894a799c3
commit d264e8fcc5
22 changed files with 108 additions and 80 deletions

View file

@ -285,7 +285,7 @@ int main(int argc, char** argv)
}; };
auto do_message = [&](const String& name, const Vector<Parameter>& parameters, const String& response_type = {}) { auto do_message = [&](const String& name, const Vector<Parameter>& parameters, const String& response_type = {}) {
dbg() << "class " << name << " final : public IMessage {"; dbg() << "class " << name << " final : public IPC::Message {";
dbg() << "public:"; dbg() << "public:";
if (!response_type.is_null()) if (!response_type.is_null())
dbg() << " typedef class " << response_type << " ResponseType;"; dbg() << " typedef class " << response_type << " ResponseType;";
@ -385,10 +385,10 @@ int main(int argc, char** argv)
dbg() << " size_in_bytes = stream.offset();"; dbg() << " size_in_bytes = stream.offset();";
dbg() << " return make<" << name << ">(" << builder.to_string() << ");"; dbg() << " return make<" << name << ">(" << builder.to_string() << ");";
dbg() << " }"; dbg() << " }";
dbg() << " virtual IMessageBuffer encode() const override"; dbg() << " virtual IPC::MessageBuffer encode() const override";
dbg() << " {"; dbg() << " {";
dbg() << " IMessageBuffer buffer;"; dbg() << " IPC::MessageBuffer buffer;";
dbg() << " IEncoder stream(buffer);"; dbg() << " IPC::Encoder stream(buffer);";
dbg() << " stream << endpoint_magic();"; dbg() << " stream << endpoint_magic();";
dbg() << " stream << (int)MessageID::" << name << ";"; dbg() << " stream << (int)MessageID::" << name << ";";
for (auto& parameter : parameters) { for (auto& parameter : parameters) {
@ -447,7 +447,7 @@ int main(int argc, char** argv)
dbg() << "} // namespace " << endpoint.name; dbg() << "} // namespace " << endpoint.name;
dbg(); dbg();
dbg() << "class " << endpoint.name << "Endpoint : public IEndpoint {"; dbg() << "class " << endpoint.name << "Endpoint : public IPC::Endpoint {";
dbg() << "public:"; dbg() << "public:";
dbg() << " " << endpoint.name << "Endpoint() {}"; dbg() << " " << endpoint.name << "Endpoint() {}";
dbg() << " virtual ~" << endpoint.name << "Endpoint() override {}"; dbg() << " virtual ~" << endpoint.name << "Endpoint() override {}";
@ -455,7 +455,7 @@ int main(int argc, char** argv)
dbg() << " virtual int magic() const override { return " << endpoint.magic << "; }"; dbg() << " virtual int magic() const override { return " << endpoint.magic << "; }";
dbg() << " static String static_name() { return \"" << endpoint.name << "\"; };"; dbg() << " static String static_name() { return \"" << endpoint.name << "\"; };";
dbg() << " virtual String name() const override { return \"" << endpoint.name << "\"; };"; dbg() << " virtual String name() const override { return \"" << endpoint.name << "\"; };";
dbg() << " static OwnPtr<IMessage> decode_message(const ByteBuffer& buffer, size_t& size_in_bytes)"; dbg() << " static OwnPtr<IPC::Message> decode_message(const ByteBuffer& buffer, size_t& size_in_bytes)";
dbg() << " {"; dbg() << " {";
dbg() << " BufferStream stream(const_cast<ByteBuffer&>(buffer));"; dbg() << " BufferStream stream(const_cast<ByteBuffer&>(buffer));";
dbg() << " i32 message_endpoint_magic = 0;"; dbg() << " i32 message_endpoint_magic = 0;";
@ -487,7 +487,7 @@ int main(int argc, char** argv)
dbg() << " }"; dbg() << " }";
dbg() << " }"; dbg() << " }";
dbg(); dbg();
dbg() << " virtual OwnPtr<IMessage> handle(const IMessage& message) override"; dbg() << " virtual OwnPtr<IPC::Message> handle(const IPC::Message& message) override";
dbg() << " {"; dbg() << " {";
dbg() << " switch (message.message_id()) {"; dbg() << " switch (message.message_id()) {";
for (auto& message : endpoint.messages) { for (auto& message : endpoint.messages) {

View file

@ -29,7 +29,7 @@
#include <LibAudio/AClientConnection.h> #include <LibAudio/AClientConnection.h>
AClientConnection::AClientConnection() AClientConnection::AClientConnection()
: IServerConnection(*this, "/tmp/portal/audio") : IPC::ServerConnection<AudioClientEndpoint, AudioServerEndpoint>(*this, "/tmp/portal/audio")
{ {
} }

View file

@ -32,7 +32,7 @@
class ABuffer; class ABuffer;
class AClientConnection : public IServerConnection<AudioClientEndpoint, AudioServerEndpoint> class AClientConnection : public IPC::ServerConnection<AudioClientEndpoint, AudioServerEndpoint>
, public AudioClientEndpoint { , public AudioClientEndpoint {
C_OBJECT(AClientConnection) C_OBJECT(AClientConnection)
public: public:

View file

@ -33,12 +33,12 @@
namespace GUI { namespace GUI {
class WindowServerConnection class WindowServerConnection
: public IServerConnection<WindowClientEndpoint, WindowServerEndpoint> : public IPC::ServerConnection<WindowClientEndpoint, WindowServerEndpoint>
, public WindowClientEndpoint { , public WindowClientEndpoint {
C_OBJECT(WindowServerConnection) C_OBJECT(WindowServerConnection)
public: public:
WindowServerConnection() WindowServerConnection()
: IServerConnection(*this, "/tmp/portal/window") : IPC::ServerConnection<WindowClientEndpoint, WindowServerEndpoint>(*this, "/tmp/portal/window")
{ {
handshake(); handshake();
} }

View file

@ -40,23 +40,25 @@
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
class IEvent : public Core::Event { namespace IPC {
class Event : public Core::Event {
public: public:
enum Type { enum Type {
Invalid = 2000, Invalid = 2000,
Disconnected, Disconnected,
}; };
IEvent() {} Event() {}
explicit IEvent(Type type) explicit Event(Type type)
: Core::Event(type) : Core::Event(type)
{ {
} }
}; };
class IDisconnectedEvent : public IEvent { class DisconnectedEvent : public Event {
public: public:
explicit IDisconnectedEvent(int client_id) explicit DisconnectedEvent(int client_id)
: IEvent(Disconnected) : Event(Disconnected)
, m_client_id(client_id) , m_client_id(client_id)
{ {
} }
@ -74,9 +76,9 @@ NonnullRefPtr<T> new_client_connection(Args&&... args)
} }
template<typename Endpoint> template<typename Endpoint>
class IClientConnection : public Core::Object { class ClientConnection : public Core::Object {
public: public:
IClientConnection(Endpoint& endpoint, Core::LocalSocket& socket, int client_id) ClientConnection(Endpoint& endpoint, Core::LocalSocket& socket, int client_id)
: m_endpoint(endpoint) : m_endpoint(endpoint)
, m_socket(socket) , m_socket(socket)
, m_client_id(client_id) , m_client_id(client_id)
@ -92,11 +94,11 @@ public:
m_socket->on_ready_to_read = [this] { drain_messages_from_client(); }; 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, // 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. // the socket will be closed. Don't try to send more messages.
@ -109,11 +111,11 @@ public:
if (nwritten < 0) { if (nwritten < 0) {
switch (errno) { switch (errno) {
case EPIPE: case EPIPE:
dbg() << "Connection::post_message: Disconnected from peer"; dbg() << *this << "::post_message: Disconnected from peer";
shutdown(); shutdown();
return; return;
case EAGAIN: case EAGAIN:
dbg() << "Connection::post_message: Client buffer overflowed."; dbg() << *this << "::post_message: Client buffer overflowed.";
did_misbehave(); did_misbehave();
return; return;
default: default:
@ -133,7 +135,7 @@ public:
ssize_t nread = recv(m_socket->fd(), buffer, sizeof(buffer), MSG_DONTWAIT); ssize_t nread = recv(m_socket->fd(), buffer, sizeof(buffer), MSG_DONTWAIT);
if (nread == 0 || (nread == -1 && errno == EAGAIN)) { if (nread == 0 || (nread == -1 && errno == EAGAIN)) {
if (bytes.is_empty()) { 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; return;
} }
break; break;
@ -162,13 +164,13 @@ public:
void did_misbehave() 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(); shutdown();
} }
void did_misbehave(const char* message) 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(); shutdown();
} }
@ -186,9 +188,9 @@ public:
protected: protected:
void event(Core::Event& event) override void event(Core::Event& event) override
{ {
if (event.type() == IEvent::Disconnected) { if (event.type() == Event::Disconnected) {
int client_id = static_cast<const IDisconnectedEvent&>(event).client_id(); int client_id = static_cast<const DisconnectedEvent&>(event).client_id();
dbgprintf("Connection: Client disconnected: %d\n", client_id); dbg() << *this << ": Client disconnected: " << client_id;
die(); die();
return; return;
} }
@ -202,3 +204,5 @@ private:
int m_client_id { -1 }; int m_client_id { -1 };
int m_client_pid { -1 }; int m_client_pid { -1 };
}; };
}

View file

@ -28,25 +28,27 @@
#include <LibIPC/IMessage.h> #include <LibIPC/IMessage.h>
class IEncoder { namespace IPC {
class Encoder {
public: public:
explicit IEncoder(IMessageBuffer& buffer) explicit Encoder(MessageBuffer& buffer)
: m_buffer(buffer) : m_buffer(buffer)
{ {
} }
IEncoder& operator<<(bool value) Encoder& operator<<(bool value)
{ {
return *this << (u8)value; return *this << (u8)value;
} }
IEncoder& operator<<(u8 value) Encoder& operator<<(u8 value)
{ {
m_buffer.append(value); m_buffer.append(value);
return *this; return *this;
} }
IEncoder& operator<<(u16 value) Encoder& operator<<(u16 value)
{ {
m_buffer.ensure_capacity(m_buffer.size() + 2); m_buffer.ensure_capacity(m_buffer.size() + 2);
m_buffer.unchecked_append((u8)value); m_buffer.unchecked_append((u8)value);
@ -54,7 +56,7 @@ public:
return *this; return *this;
} }
IEncoder& operator<<(u32 value) Encoder& operator<<(u32 value)
{ {
m_buffer.ensure_capacity(m_buffer.size() + 4); m_buffer.ensure_capacity(m_buffer.size() + 4);
m_buffer.unchecked_append((u8)value); m_buffer.unchecked_append((u8)value);
@ -64,7 +66,7 @@ public:
return *this; return *this;
} }
IEncoder& operator<<(u64 value) Encoder& operator<<(u64 value)
{ {
m_buffer.ensure_capacity(m_buffer.size() + 8); m_buffer.ensure_capacity(m_buffer.size() + 8);
m_buffer.unchecked_append((u8)value); m_buffer.unchecked_append((u8)value);
@ -78,13 +80,13 @@ public:
return *this; return *this;
} }
IEncoder& operator<<(i8 value) Encoder& operator<<(i8 value)
{ {
m_buffer.append((u8)value); m_buffer.append((u8)value);
return *this; return *this;
} }
IEncoder& operator<<(i16 value) Encoder& operator<<(i16 value)
{ {
m_buffer.ensure_capacity(m_buffer.size() + 2); m_buffer.ensure_capacity(m_buffer.size() + 2);
m_buffer.unchecked_append((u8)value); m_buffer.unchecked_append((u8)value);
@ -92,7 +94,7 @@ public:
return *this; return *this;
} }
IEncoder& operator<<(i32 value) Encoder& operator<<(i32 value)
{ {
m_buffer.ensure_capacity(m_buffer.size() + 4); m_buffer.ensure_capacity(m_buffer.size() + 4);
m_buffer.unchecked_append((u8)value); m_buffer.unchecked_append((u8)value);
@ -102,7 +104,7 @@ public:
return *this; return *this;
} }
IEncoder& operator<<(i64 value) Encoder& operator<<(i64 value)
{ {
m_buffer.ensure_capacity(m_buffer.size() + 8); m_buffer.ensure_capacity(m_buffer.size() + 8);
m_buffer.unchecked_append((u8)value); m_buffer.unchecked_append((u8)value);
@ -116,27 +118,27 @@ public:
return *this; 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; return *this << (u32)value;
else if constexpr(sizeof(size_t) == 8) else if constexpr (sizeof(size_t) == 8)
return *this << (u64)value; return *this << (u64)value;
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }
#ifndef __i386__ #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; return *this << (i32)value;
else if constexpr(sizeof(ssize_t) == 8) else if constexpr (sizeof(ssize_t) == 8)
return *this << (i64)value; return *this << (i64)value;
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }
#endif #endif
IEncoder& operator<<(float value) Encoder& operator<<(float value)
{ {
union bits { union bits {
float as_float; float as_float;
@ -146,17 +148,19 @@ public:
return *this << u.as_u32; return *this << u.as_u32;
} }
IEncoder& operator<<(const char* value) Encoder& operator<<(const char* value)
{ {
return *this << StringView(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()); m_buffer.append((const u8*)value.characters_without_null_termination(), value.length());
return *this; return *this;
} }
private: private:
IMessageBuffer& m_buffer; MessageBuffer& m_buffer;
}; };
}

View file

@ -26,10 +26,14 @@
#include <LibIPC/IEndpoint.h> #include <LibIPC/IEndpoint.h>
IEndpoint::IEndpoint() namespace IPC {
Endpoint::Endpoint()
{ {
} }
IEndpoint::~IEndpoint() Endpoint::~Endpoint()
{ {
} }
}

View file

@ -26,26 +26,30 @@
#pragma once #pragma once
#include <AK/String.h>
#include <AK/OwnPtr.h> #include <AK/OwnPtr.h>
#include <AK/String.h>
namespace AK { namespace AK {
class BufferStream; class BufferStream;
} }
class IMessage; namespace IPC {
class IEndpoint { class Message;
class Endpoint {
public: public:
virtual ~IEndpoint(); virtual ~Endpoint();
virtual int magic() const = 0; virtual int magic() const = 0;
virtual String name() const = 0; virtual String name() const = 0;
virtual OwnPtr<IMessage> handle(const IMessage&) = 0; virtual OwnPtr<Message> handle(const Message&) = 0;
protected: protected:
IEndpoint(); Endpoint();
private: private:
String m_name; String m_name;
}; };
}

View file

@ -26,10 +26,14 @@
#include <LibIPC/IMessage.h> #include <LibIPC/IMessage.h>
IMessage::IMessage() namespace IPC {
Message::Message()
{ {
} }
IMessage::~IMessage() Message::~Message()
{ {
} }
}

View file

@ -28,17 +28,21 @@
#include <AK/String.h> #include <AK/String.h>
typedef Vector<u8, 1024> IMessageBuffer; namespace IPC {
class IMessage { typedef Vector<u8, 1024> MessageBuffer;
class Message {
public: public:
virtual ~IMessage(); virtual ~Message();
virtual int endpoint_magic() const = 0; virtual int endpoint_magic() const = 0;
virtual int message_id() const = 0; virtual int message_id() const = 0;
virtual String message_name() const = 0; virtual String message_name() const = 0;
virtual IMessageBuffer encode() const = 0; virtual MessageBuffer encode() const = 0;
protected: protected:
IMessage(); Message();
}; };
}

View file

@ -40,10 +40,12 @@
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
namespace IPC {
template<typename LocalEndpoint, typename PeerEndpoint> template<typename LocalEndpoint, typename PeerEndpoint>
class IServerConnection : public Core::Object { class ServerConnection : public Core::Object {
public: public:
IServerConnection(LocalEndpoint& local_endpoint, const StringView& address) ServerConnection(LocalEndpoint& local_endpoint, const StringView& address)
: m_local_endpoint(local_endpoint) : m_local_endpoint(local_endpoint)
, m_connection(Core::LocalSocket::construct(this)) , m_connection(Core::LocalSocket::construct(this))
, m_notifier(Core::Notifier::construct(m_connection->fd(), Core::Notifier::Read, 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(); auto buffer = message.encode();
int nwritten = write(m_connection->fd(), buffer.data(), (size_t)buffer.size()); int nwritten = write(m_connection->fd(), buffer.data(), (size_t)buffer.size());
@ -195,7 +197,9 @@ private:
LocalEndpoint& m_local_endpoint; LocalEndpoint& m_local_endpoint;
RefPtr<Core::LocalSocket> m_connection; RefPtr<Core::LocalSocket> m_connection;
RefPtr<Core::Notifier> m_notifier; RefPtr<Core::Notifier> m_notifier;
Vector<OwnPtr<IMessage>> m_unprocessed_messages; Vector<OwnPtr<Message>> m_unprocessed_messages;
int m_server_pid { -1 }; int m_server_pid { -1 };
int m_my_client_id { -1 }; int m_my_client_id { -1 };
}; };
}

View file

@ -31,7 +31,7 @@
namespace Protocol { namespace Protocol {
Client::Client() Client::Client()
: IServerConnection(*this, "/tmp/portal/protocol") : IPC::ServerConnection<ProtocolClientEndpoint, ProtocolServerEndpoint>(*this, "/tmp/portal/protocol")
{ {
handshake(); handshake();
} }

View file

@ -34,7 +34,7 @@ namespace Protocol {
class Download; class Download;
class Client : public IServerConnection<ProtocolClientEndpoint, ProtocolServerEndpoint> class Client : public IPC::ServerConnection<ProtocolClientEndpoint, ProtocolServerEndpoint>
, public ProtocolClientEndpoint { , public ProtocolClientEndpoint {
C_OBJECT(Client) C_OBJECT(Client)
public: public:

View file

@ -49,7 +49,7 @@ void ASClientConnection::for_each(Function<void(ASClientConnection&)> callback)
} }
ASClientConnection::ASClientConnection(Core::LocalSocket& client_socket, int client_id, ASMixer& mixer) ASClientConnection::ASClientConnection(Core::LocalSocket& client_socket, int client_id, ASMixer& mixer)
: IClientConnection(*this, client_socket, client_id) : IPC::ClientConnection<AudioServerEndpoint>(*this, client_socket, client_id)
, m_mixer(mixer) , m_mixer(mixer)
{ {
s_connections.set(client_id, *this); s_connections.set(client_id, *this);

View file

@ -33,7 +33,7 @@ class ABuffer;
class ASBufferQueue; class ASBufferQueue;
class ASMixer; class ASMixer;
class ASClientConnection final : public IClientConnection<AudioServerEndpoint> class ASClientConnection final : public IPC::ClientConnection<AudioServerEndpoint>
, public AudioServerEndpoint { , public AudioServerEndpoint {
C_OBJECT(ASClientConnection) C_OBJECT(ASClientConnection)
public: public:

View file

@ -43,6 +43,6 @@ ASEventLoop::ASEventLoop()
} }
static int s_next_client_id = 0; static int s_next_client_id = 0;
int client_id = ++s_next_client_id; int client_id = ++s_next_client_id;
new_client_connection<ASClientConnection>(*client_socket, client_id, m_mixer); IPC::new_client_connection<ASClientConnection>(*client_socket, client_id, m_mixer);
}; };
} }

View file

@ -33,7 +33,7 @@
static HashMap<int, RefPtr<PSClientConnection>> s_connections; static HashMap<int, RefPtr<PSClientConnection>> s_connections;
PSClientConnection::PSClientConnection(Core::LocalSocket& socket, int client_id) PSClientConnection::PSClientConnection(Core::LocalSocket& socket, int client_id)
: IClientConnection(*this, socket, client_id) : IPC::ClientConnection<ProtocolServerEndpoint>(*this, socket, client_id)
{ {
s_connections.set(client_id, *this); s_connections.set(client_id, *this);
} }

View file

@ -36,7 +36,7 @@ class SharedBuffer;
class Download; class Download;
class PSClientConnection final : public IClientConnection<ProtocolServerEndpoint> class PSClientConnection final : public IPC::ClientConnection<ProtocolServerEndpoint>
, public ProtocolServerEndpoint { , public ProtocolServerEndpoint {
C_OBJECT(PSClientConnection) C_OBJECT(PSClientConnection)
public: public:

View file

@ -54,7 +54,7 @@ int main(int, char**)
} }
static int s_next_client_id = 0; static int s_next_client_id = 0;
int client_id = ++s_next_client_id; int client_id = ++s_next_client_id;
new_client_connection<PSClientConnection>(*client_socket, client_id); IPC::new_client_connection<PSClientConnection>(*client_socket, client_id);
}; };
return event_loop.exec(); return event_loop.exec();
} }

View file

@ -66,7 +66,7 @@ WSClientConnection* WSClientConnection::from_client_id(int client_id)
} }
WSClientConnection::WSClientConnection(Core::LocalSocket& client_socket, int client_id) WSClientConnection::WSClientConnection(Core::LocalSocket& client_socket, int client_id)
: IClientConnection(*this, client_socket, client_id) : IPC::ClientConnection<WindowServerEndpoint>(*this, client_socket, client_id)
{ {
if (!s_connections) if (!s_connections)
s_connections = new HashMap<int, NonnullRefPtr<WSClientConnection>>; s_connections = new HashMap<int, NonnullRefPtr<WSClientConnection>>;

View file

@ -41,7 +41,7 @@ class WSMenu;
class WSMenuBar; class WSMenuBar;
class WSClientConnection final class WSClientConnection final
: public IClientConnection<WindowServerEndpoint> : public IPC::ClientConnection<WindowServerEndpoint>
, public WindowServerEndpoint { , public WindowServerEndpoint {
C_OBJECT(WSClientConnection) C_OBJECT(WSClientConnection)
public: public:

View file

@ -63,7 +63,7 @@ WSEventLoop::WSEventLoop()
} }
static int s_next_client_id = 0; static int s_next_client_id = 0;
int client_id = ++s_next_client_id; int client_id = ++s_next_client_id;
new_client_connection<WSClientConnection>(*client_socket, client_id); IPC::new_client_connection<WSClientConnection>(*client_socket, client_id);
}; };
ASSERT(m_keyboard_fd >= 0); ASSERT(m_keyboard_fd >= 0);