From d264e8fcc599204aa5f8ee9aa00d52ec53b7215b Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 5 Feb 2020 19:57:18 +0100 Subject: [PATCH] LibIPC: Put all classes in the IPC namespace and remove the leading I --- DevTools/IPCCompiler/main.cpp | 14 +++--- Libraries/LibAudio/AClientConnection.cpp | 2 +- Libraries/LibAudio/AClientConnection.h | 2 +- Libraries/LibGUI/GWindowServerConnection.h | 4 +- Libraries/LibIPC/IClientConnection.h | 40 ++++++++-------- Libraries/LibIPC/IEncoder.h | 46 ++++++++++--------- Libraries/LibIPC/IEndpoint.cpp | 8 +++- Libraries/LibIPC/IEndpoint.h | 16 ++++--- Libraries/LibIPC/IMessage.cpp | 8 +++- Libraries/LibIPC/IMessage.h | 14 ++++-- Libraries/LibIPC/IServerConnection.h | 12 +++-- Libraries/LibProtocol/Client.cpp | 2 +- Libraries/LibProtocol/Client.h | 2 +- Servers/AudioServer/ASClientConnection.cpp | 2 +- Servers/AudioServer/ASClientConnection.h | 2 +- Servers/AudioServer/ASEventLoop.cpp | 2 +- Servers/ProtocolServer/PSClientConnection.cpp | 2 +- Servers/ProtocolServer/PSClientConnection.h | 2 +- Servers/ProtocolServer/main.cpp | 2 +- Servers/WindowServer/WSClientConnection.cpp | 2 +- Servers/WindowServer/WSClientConnection.h | 2 +- Servers/WindowServer/WSEventLoop.cpp | 2 +- 22 files changed, 108 insertions(+), 80 deletions(-) diff --git a/DevTools/IPCCompiler/main.cpp b/DevTools/IPCCompiler/main.cpp index 78e50612dc..f7bf82957b 100644 --- a/DevTools/IPCCompiler/main.cpp +++ b/DevTools/IPCCompiler/main.cpp @@ -285,7 +285,7 @@ int main(int argc, char** argv) }; auto do_message = [&](const String& name, const Vector& parameters, const String& response_type = {}) { - dbg() << "class " << name << " final : public IMessage {"; + dbg() << "class " << name << " final : public IPC::Message {"; dbg() << "public:"; if (!response_type.is_null()) dbg() << " typedef class " << response_type << " ResponseType;"; @@ -385,10 +385,10 @@ int main(int argc, char** argv) dbg() << " size_in_bytes = stream.offset();"; dbg() << " return make<" << name << ">(" << builder.to_string() << ");"; dbg() << " }"; - dbg() << " virtual IMessageBuffer encode() const override"; + dbg() << " virtual IPC::MessageBuffer encode() const override"; dbg() << " {"; - dbg() << " IMessageBuffer buffer;"; - dbg() << " IEncoder stream(buffer);"; + dbg() << " IPC::MessageBuffer buffer;"; + dbg() << " IPC::Encoder stream(buffer);"; dbg() << " stream << endpoint_magic();"; dbg() << " stream << (int)MessageID::" << name << ";"; for (auto& parameter : parameters) { @@ -447,7 +447,7 @@ int main(int argc, char** argv) dbg() << "} // namespace " << endpoint.name; dbg(); - dbg() << "class " << endpoint.name << "Endpoint : public IEndpoint {"; + dbg() << "class " << endpoint.name << "Endpoint : public IPC::Endpoint {"; dbg() << "public:"; dbg() << " " << endpoint.name << "Endpoint() {}"; 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() << " static String static_name() { return \"" << endpoint.name << "\"; };"; dbg() << " virtual String name() const override { return \"" << endpoint.name << "\"; };"; - dbg() << " static OwnPtr decode_message(const ByteBuffer& buffer, size_t& size_in_bytes)"; + dbg() << " static OwnPtr decode_message(const ByteBuffer& buffer, size_t& size_in_bytes)"; dbg() << " {"; dbg() << " BufferStream stream(const_cast(buffer));"; dbg() << " i32 message_endpoint_magic = 0;"; @@ -487,7 +487,7 @@ int main(int argc, char** argv) dbg() << " }"; dbg() << " }"; dbg(); - dbg() << " virtual OwnPtr handle(const IMessage& message) override"; + dbg() << " virtual OwnPtr handle(const IPC::Message& message) override"; dbg() << " {"; dbg() << " switch (message.message_id()) {"; for (auto& message : endpoint.messages) { diff --git a/Libraries/LibAudio/AClientConnection.cpp b/Libraries/LibAudio/AClientConnection.cpp index 6c90a1cbb8..0738a822b2 100644 --- a/Libraries/LibAudio/AClientConnection.cpp +++ b/Libraries/LibAudio/AClientConnection.cpp @@ -29,7 +29,7 @@ #include AClientConnection::AClientConnection() - : IServerConnection(*this, "/tmp/portal/audio") + : IPC::ServerConnection(*this, "/tmp/portal/audio") { } diff --git a/Libraries/LibAudio/AClientConnection.h b/Libraries/LibAudio/AClientConnection.h index 75807a1b3d..147e431f1b 100644 --- a/Libraries/LibAudio/AClientConnection.h +++ b/Libraries/LibAudio/AClientConnection.h @@ -32,7 +32,7 @@ class ABuffer; -class AClientConnection : public IServerConnection +class AClientConnection : public IPC::ServerConnection , public AudioClientEndpoint { C_OBJECT(AClientConnection) public: diff --git a/Libraries/LibGUI/GWindowServerConnection.h b/Libraries/LibGUI/GWindowServerConnection.h index 6355c6ab95..7ae007626d 100644 --- a/Libraries/LibGUI/GWindowServerConnection.h +++ b/Libraries/LibGUI/GWindowServerConnection.h @@ -33,12 +33,12 @@ namespace GUI { class WindowServerConnection - : public IServerConnection + : public IPC::ServerConnection , public WindowClientEndpoint { C_OBJECT(WindowServerConnection) public: WindowServerConnection() - : IServerConnection(*this, "/tmp/portal/window") + : IPC::ServerConnection(*this, "/tmp/portal/window") { handshake(); } diff --git a/Libraries/LibIPC/IClientConnection.h b/Libraries/LibIPC/IClientConnection.h index 7542d7b78c..85e1722b91 100644 --- a/Libraries/LibIPC/IClientConnection.h +++ b/Libraries/LibIPC/IClientConnection.h @@ -40,23 +40,25 @@ #include #include -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 new_client_connection(Args&&... args) } template -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(client_id())); + Core::EventLoop::current().post_event(*this, make(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(event).client_id(); - dbgprintf("Connection: Client disconnected: %d\n", client_id); + if (event.type() == Event::Disconnected) { + int client_id = static_cast(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 }; }; + +} diff --git a/Libraries/LibIPC/IEncoder.h b/Libraries/LibIPC/IEncoder.h index 43c634ba64..a99c842f58 100644 --- a/Libraries/LibIPC/IEncoder.h +++ b/Libraries/LibIPC/IEncoder.h @@ -28,25 +28,27 @@ #include -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; }; + +} diff --git a/Libraries/LibIPC/IEndpoint.cpp b/Libraries/LibIPC/IEndpoint.cpp index 5ef8516604..f42721f3f4 100644 --- a/Libraries/LibIPC/IEndpoint.cpp +++ b/Libraries/LibIPC/IEndpoint.cpp @@ -26,10 +26,14 @@ #include -IEndpoint::IEndpoint() +namespace IPC { + +Endpoint::Endpoint() { } -IEndpoint::~IEndpoint() +Endpoint::~Endpoint() { } + +} diff --git a/Libraries/LibIPC/IEndpoint.h b/Libraries/LibIPC/IEndpoint.h index ef237f8934..65ea70a5e8 100644 --- a/Libraries/LibIPC/IEndpoint.h +++ b/Libraries/LibIPC/IEndpoint.h @@ -26,26 +26,30 @@ #pragma once -#include #include +#include 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 handle(const IMessage&) = 0; + virtual OwnPtr handle(const Message&) = 0; protected: - IEndpoint(); + Endpoint(); private: String m_name; }; + +} diff --git a/Libraries/LibIPC/IMessage.cpp b/Libraries/LibIPC/IMessage.cpp index d3d28a9d37..d535515829 100644 --- a/Libraries/LibIPC/IMessage.cpp +++ b/Libraries/LibIPC/IMessage.cpp @@ -26,10 +26,14 @@ #include -IMessage::IMessage() +namespace IPC { + +Message::Message() { } -IMessage::~IMessage() +Message::~Message() { } + +} diff --git a/Libraries/LibIPC/IMessage.h b/Libraries/LibIPC/IMessage.h index 9bb07cd77d..a8f4bccd5b 100644 --- a/Libraries/LibIPC/IMessage.h +++ b/Libraries/LibIPC/IMessage.h @@ -28,17 +28,21 @@ #include -typedef Vector IMessageBuffer; +namespace IPC { -class IMessage { +typedef Vector 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(); }; + +} diff --git a/Libraries/LibIPC/IServerConnection.h b/Libraries/LibIPC/IServerConnection.h index 90dfbe7730..dfa9a3cea8 100644 --- a/Libraries/LibIPC/IServerConnection.h +++ b/Libraries/LibIPC/IServerConnection.h @@ -40,10 +40,12 @@ #include #include +namespace IPC { + template -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 m_connection; RefPtr m_notifier; - Vector> m_unprocessed_messages; + Vector> m_unprocessed_messages; int m_server_pid { -1 }; int m_my_client_id { -1 }; }; + +} diff --git a/Libraries/LibProtocol/Client.cpp b/Libraries/LibProtocol/Client.cpp index e35187de3c..af0d4a6266 100644 --- a/Libraries/LibProtocol/Client.cpp +++ b/Libraries/LibProtocol/Client.cpp @@ -31,7 +31,7 @@ namespace Protocol { Client::Client() - : IServerConnection(*this, "/tmp/portal/protocol") + : IPC::ServerConnection(*this, "/tmp/portal/protocol") { handshake(); } diff --git a/Libraries/LibProtocol/Client.h b/Libraries/LibProtocol/Client.h index 5d6ebc3a1f..f92e9d0cd1 100644 --- a/Libraries/LibProtocol/Client.h +++ b/Libraries/LibProtocol/Client.h @@ -34,7 +34,7 @@ namespace Protocol { class Download; -class Client : public IServerConnection +class Client : public IPC::ServerConnection , public ProtocolClientEndpoint { C_OBJECT(Client) public: diff --git a/Servers/AudioServer/ASClientConnection.cpp b/Servers/AudioServer/ASClientConnection.cpp index cdcfe1c088..a231e1b91c 100644 --- a/Servers/AudioServer/ASClientConnection.cpp +++ b/Servers/AudioServer/ASClientConnection.cpp @@ -49,7 +49,7 @@ void ASClientConnection::for_each(Function callback) } ASClientConnection::ASClientConnection(Core::LocalSocket& client_socket, int client_id, ASMixer& mixer) - : IClientConnection(*this, client_socket, client_id) + : IPC::ClientConnection(*this, client_socket, client_id) , m_mixer(mixer) { s_connections.set(client_id, *this); diff --git a/Servers/AudioServer/ASClientConnection.h b/Servers/AudioServer/ASClientConnection.h index 29f8d695cc..a55880eaed 100644 --- a/Servers/AudioServer/ASClientConnection.h +++ b/Servers/AudioServer/ASClientConnection.h @@ -33,7 +33,7 @@ class ABuffer; class ASBufferQueue; class ASMixer; -class ASClientConnection final : public IClientConnection +class ASClientConnection final : public IPC::ClientConnection , public AudioServerEndpoint { C_OBJECT(ASClientConnection) public: diff --git a/Servers/AudioServer/ASEventLoop.cpp b/Servers/AudioServer/ASEventLoop.cpp index 62e70a675c..67abab6be7 100644 --- a/Servers/AudioServer/ASEventLoop.cpp +++ b/Servers/AudioServer/ASEventLoop.cpp @@ -43,6 +43,6 @@ ASEventLoop::ASEventLoop() } static int s_next_client_id = 0; int client_id = ++s_next_client_id; - new_client_connection(*client_socket, client_id, m_mixer); + IPC::new_client_connection(*client_socket, client_id, m_mixer); }; } diff --git a/Servers/ProtocolServer/PSClientConnection.cpp b/Servers/ProtocolServer/PSClientConnection.cpp index 29a3679fee..fbdfe378c5 100644 --- a/Servers/ProtocolServer/PSClientConnection.cpp +++ b/Servers/ProtocolServer/PSClientConnection.cpp @@ -33,7 +33,7 @@ static HashMap> s_connections; PSClientConnection::PSClientConnection(Core::LocalSocket& socket, int client_id) - : IClientConnection(*this, socket, client_id) + : IPC::ClientConnection(*this, socket, client_id) { s_connections.set(client_id, *this); } diff --git a/Servers/ProtocolServer/PSClientConnection.h b/Servers/ProtocolServer/PSClientConnection.h index 81bc4f32d8..aa4c8a909c 100644 --- a/Servers/ProtocolServer/PSClientConnection.h +++ b/Servers/ProtocolServer/PSClientConnection.h @@ -36,7 +36,7 @@ class SharedBuffer; class Download; -class PSClientConnection final : public IClientConnection +class PSClientConnection final : public IPC::ClientConnection , public ProtocolServerEndpoint { C_OBJECT(PSClientConnection) public: diff --git a/Servers/ProtocolServer/main.cpp b/Servers/ProtocolServer/main.cpp index dd56305145..e1001a3462 100644 --- a/Servers/ProtocolServer/main.cpp +++ b/Servers/ProtocolServer/main.cpp @@ -54,7 +54,7 @@ int main(int, char**) } static int s_next_client_id = 0; int client_id = ++s_next_client_id; - new_client_connection(*client_socket, client_id); + IPC::new_client_connection(*client_socket, client_id); }; return event_loop.exec(); } diff --git a/Servers/WindowServer/WSClientConnection.cpp b/Servers/WindowServer/WSClientConnection.cpp index 7185d3ab9e..785c1b7e54 100644 --- a/Servers/WindowServer/WSClientConnection.cpp +++ b/Servers/WindowServer/WSClientConnection.cpp @@ -66,7 +66,7 @@ WSClientConnection* WSClientConnection::from_client_id(int client_id) } WSClientConnection::WSClientConnection(Core::LocalSocket& client_socket, int client_id) - : IClientConnection(*this, client_socket, client_id) + : IPC::ClientConnection(*this, client_socket, client_id) { if (!s_connections) s_connections = new HashMap>; diff --git a/Servers/WindowServer/WSClientConnection.h b/Servers/WindowServer/WSClientConnection.h index 3f871760fc..d9eae8479d 100644 --- a/Servers/WindowServer/WSClientConnection.h +++ b/Servers/WindowServer/WSClientConnection.h @@ -41,7 +41,7 @@ class WSMenu; class WSMenuBar; class WSClientConnection final - : public IClientConnection + : public IPC::ClientConnection , public WindowServerEndpoint { C_OBJECT(WSClientConnection) public: diff --git a/Servers/WindowServer/WSEventLoop.cpp b/Servers/WindowServer/WSEventLoop.cpp index 3c9d10e125..93358b1718 100644 --- a/Servers/WindowServer/WSEventLoop.cpp +++ b/Servers/WindowServer/WSEventLoop.cpp @@ -63,7 +63,7 @@ WSEventLoop::WSEventLoop() } static int s_next_client_id = 0; int client_id = ++s_next_client_id; - new_client_connection(*client_socket, client_id); + IPC::new_client_connection(*client_socket, client_id); }; ASSERT(m_keyboard_fd >= 0);