1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:17:36 +00:00

Rename new IPC headers & classes

Sticking these in a namespace allows us to use a more generic
("Connection") term without clashing, which is way easier to understand
than to try to come up with unique names for both.
This commit is contained in:
Robin Burchell 2019-07-17 19:46:06 +02:00 committed by Andreas Kling
parent 2177594c96
commit 9c8dd836fc
12 changed files with 71 additions and 59 deletions

View file

@ -3,7 +3,7 @@
#include "AClientConnection.h" #include "AClientConnection.h"
AClientConnection::AClientConnection() AClientConnection::AClientConnection()
: CIPCClientSideConnection("/tmp/asportal") : Connection("/tmp/asportal")
{ {
} }

View file

@ -2,12 +2,12 @@
#include <LibCore/CLocalSocket.h> #include <LibCore/CLocalSocket.h>
#include <LibCore/CNotifier.h> #include <LibCore/CNotifier.h>
#include <LibCore/CIPCClientSideConnection.h> #include <LibCore/CoreIPCClient.h>
#include <LibAudio/ASAPI.h> #include <LibAudio/ASAPI.h>
class ABuffer; class ABuffer;
class AClientConnection : public CIPCClientSideConnection<ASAPI_ServerMessage, ASAPI_ClientMessage> { class AClientConnection : public IPC::Client::Connection<ASAPI_ServerMessage, ASAPI_ClientMessage> {
public: public:
AClientConnection(); AClientConnection();

View file

@ -15,23 +15,27 @@
//#define CIPC_DEBUG //#define CIPC_DEBUG
class CIPCClientEvent : public CEvent { namespace IPC
{
namespace Client {
class Event : public CEvent {
public: public:
enum Type { enum Type {
Invalid = 2000, Invalid = 2000,
DoPostprocess, PostProcess,
}; };
CIPCClientEvent() {} Event() {}
explicit CIPCClientEvent(Type type) explicit Event(Type type)
: CEvent(type) : CEvent(type)
{ {
} }
}; };
class CIPCClientPostprocessEvent : public CIPCClientEvent { class PostProcessEvent : public Event {
public: public:
explicit CIPCClientPostprocessEvent(int client_id) explicit PostProcessEvent(int client_id)
: CIPCClientEvent(DoPostprocess) : Event(PostProcess)
, m_client_id(client_id) , m_client_id(client_id)
{ {
} }
@ -43,16 +47,16 @@ private:
}; };
template <typename ServerMessage, typename ClientMessage> template <typename ServerMessage, typename ClientMessage>
class CIPCClientSideConnection : public CObject { class Connection : public CObject {
public: public:
CIPCClientSideConnection(const StringView& address) Connection(const StringView& address)
: m_notifier(CNotifier(m_connection.fd(), CNotifier::Read)) : m_notifier(CNotifier(m_connection.fd(), CNotifier::Read))
{ {
// We want to rate-limit our clients // We want to rate-limit our clients
m_connection.set_blocking(true); m_connection.set_blocking(true);
m_notifier.on_ready_to_read = [this] { m_notifier.on_ready_to_read = [this] {
drain_messages_from_server(); drain_messages_from_server();
CEventLoop::current().post_event(*this, make<CIPCClientPostprocessEvent>(m_connection.fd())); CEventLoop::current().post_event(*this, make<PostProcessEvent>(m_connection.fd()));
}; };
int retries = 1000; int retries = 1000;
@ -61,7 +65,7 @@ public:
break; break;
} }
dbgprintf("CIPCClientSideConnection: connect failed: %d, %s\n", errno, strerror(errno)); dbgprintf("Client::Connection: connect failed: %d, %s\n", errno, strerror(errno));
sleep(1); sleep(1);
--retries; --retries;
} }
@ -73,7 +77,7 @@ public:
virtual void event(CEvent& event) override virtual void event(CEvent& event) override
{ {
if (event.type() == CIPCClientEvent::DoPostprocess) { if (event.type() == Event::PostProcess) {
postprocess_bundles(m_unprocessed_bundles); postprocess_bundles(m_unprocessed_bundles);
} else { } else {
CObject::event(event); CObject::event(event);
@ -94,7 +98,7 @@ public:
if (m_unprocessed_bundles[i].message.type == type) { if (m_unprocessed_bundles[i].message.type == type) {
event = move(m_unprocessed_bundles[i].message); event = move(m_unprocessed_bundles[i].message);
m_unprocessed_bundles.remove(i); m_unprocessed_bundles.remove(i);
CEventLoop::current().post_event(*this, make<CIPCClientPostprocessEvent>(m_connection.fd())); CEventLoop::current().post_event(*this, make<PostProcessEvent>(m_connection.fd()));
return true; return true;
} }
} }
@ -115,7 +119,7 @@ public:
if (m_unprocessed_bundles[i].message.type == type) { if (m_unprocessed_bundles[i].message.type == type) {
event = move(m_unprocessed_bundles[i].message); event = move(m_unprocessed_bundles[i].message);
m_unprocessed_bundles.remove(i); m_unprocessed_bundles.remove(i);
CEventLoop::current().post_event(*this, make<CIPCClientPostprocessEvent>(m_connection.fd())); CEventLoop::current().post_event(*this, make<PostProcessEvent>(m_connection.fd()));
return true; return true;
} }
} }
@ -164,14 +168,14 @@ public:
} }
protected: protected:
struct IncomingASMessageBundle { struct IncomingMessageBundle {
ServerMessage message; ServerMessage message;
ByteBuffer extra_data; ByteBuffer extra_data;
}; };
virtual void postprocess_bundles(Vector<IncomingASMessageBundle>& new_bundles) virtual void postprocess_bundles(Vector<IncomingMessageBundle>& new_bundles)
{ {
dbg() << "CIPCClientSideConnection " << " warning: discarding " << new_bundles.size() << " unprocessed bundles; this may not be what you want"; dbg() << "Client::Connection: " << " warning: discarding " << new_bundles.size() << " unprocessed bundles; this may not be what you want";
new_bundles.clear(); new_bundles.clear();
} }
@ -214,7 +218,11 @@ private:
CLocalSocket m_connection; CLocalSocket m_connection;
CNotifier m_notifier; CNotifier m_notifier;
Vector<IncomingASMessageBundle> m_unprocessed_bundles; Vector<IncomingMessageBundle> m_unprocessed_bundles;
int m_server_pid; int m_server_pid;
int m_my_client_id; int m_my_client_id;
}; };
} // Client
} // IPC

View file

@ -15,23 +15,27 @@
//#define CIPC_DEBUG //#define CIPC_DEBUG
class CIPCServerEvent : public CEvent { namespace IPC
{
namespace Server {
class Event : public CEvent {
public: public:
enum Type { enum Type {
Invalid = 2000, Invalid = 2000,
ClientDisconnected, Disconnected,
}; };
CIPCServerEvent() {} Event() {}
explicit CIPCServerEvent(Type type) explicit Event(Type type)
: CEvent(type) : CEvent(type)
{ {
} }
}; };
class ASClientDisconnectedNotification : public CIPCServerEvent { class DisconnectedEvent : public Event {
public: public:
explicit ASClientDisconnectedNotification(int client_id) explicit DisconnectedEvent(int client_id)
: CIPCServerEvent(ClientDisconnected) : Event(Disconnected)
, m_client_id(client_id) , m_client_id(client_id)
{ {
} }
@ -43,7 +47,7 @@ private:
}; };
template <typename T, class... Args> template <typename T, class... Args>
T* CIPCServerSideClientCreator(Args&& ... args) T* new_connection_for_client(Args&& ... args)
{ {
auto conn = new T(AK::forward<Args>(args)...) /* arghs */; auto conn = new T(AK::forward<Args>(args)...) /* arghs */;
conn->send_greeting(); conn->send_greeting();
@ -51,24 +55,24 @@ T* CIPCServerSideClientCreator(Args&& ... args)
}; };
template <typename ServerMessage, typename ClientMessage> template <typename ServerMessage, typename ClientMessage>
class CIPCServerSideClient : public CObject class Connection : public CObject
{ {
public: public:
CIPCServerSideClient(int fd, int client_id) Connection(int fd, int client_id)
: m_socket(fd) : m_socket(fd)
, m_notifier(CNotifier(fd, CNotifier::Read)) , m_notifier(CNotifier(fd, CNotifier::Read))
, m_client_id(client_id) , m_client_id(client_id)
{ {
m_notifier.on_ready_to_read = [this] { drain_client(); }; m_notifier.on_ready_to_read = [this] { drain_client(); };
#if defined(CIPC_DEBUG) #if defined(CIPC_DEBUG)
dbg() << "S: Created new CIPCServerSideClient " << fd << client_id << " and said hello"; dbg() << "S: Created new Connection " << fd << client_id << " and said hello";
#endif #endif
} }
~CIPCServerSideClient() ~Connection()
{ {
#if defined(CIPC_DEBUG) #if defined(CIPC_DEBUG)
dbg() << "S: Destroyed CIPCServerSideClient " << m_socket.fd() << client_id(); dbg() << "S: Destroyed Connection " << m_socket.fd() << client_id();
#endif #endif
} }
@ -96,17 +100,17 @@ public:
if (nwritten < 0) { if (nwritten < 0) {
switch (errno) { switch (errno) {
case EPIPE: case EPIPE:
dbgprintf("WSClientConnection::post_message: Disconnected from peer.\n"); dbgprintf("Connection::post_message: Disconnected from peer.\n");
delete_later(); delete_later();
return; return;
break; break;
case EAGAIN: case EAGAIN:
dbgprintf("WSClientConnection::post_message: Client buffer overflowed.\n"); dbgprintf("Connection::post_message: Client buffer overflowed.\n");
did_misbehave(); did_misbehave();
return; return;
break; break;
default: default:
perror("WSClientConnection::post_message writev"); perror("Connection::post_message writev");
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }
} }
@ -124,7 +128,7 @@ public:
if (nread == 0 || (nread == -1 && errno == EAGAIN)) { if (nread == 0 || (nread == -1 && errno == EAGAIN)) {
if (!messages_received) { if (!messages_received) {
// TODO: is delete_later() sufficient? // TODO: is delete_later() sufficient?
CEventLoop::current().post_event(*this, make<ASClientDisconnectedNotification>(client_id())); CEventLoop::current().post_event(*this, make<DisconnectedEvent>(client_id()));
} }
break; break;
} }
@ -159,12 +163,12 @@ public:
void did_misbehave() void did_misbehave()
{ {
dbgprintf("CIPCServerSideClient{%p} (id=%d, pid=%d) misbehaved, disconnecting.\n", this, client_id(), m_pid); dbgprintf("Connection{%p} (id=%d, pid=%d) misbehaved, disconnecting.\n", this, client_id(), m_pid);
delete_later(); delete_later();
m_notifier.set_enabled(false); m_notifier.set_enabled(false);
} }
const char* class_name() const override { return "CIPCServerSideClient"; } const char* class_name() const override { return "Connection"; }
int client_id() const { return m_client_id; } int client_id() const { return m_client_id; }
pid_t client_pid() const { return m_pid; } pid_t client_pid() const { return m_pid; }
@ -176,9 +180,9 @@ public:
protected: protected:
void event(CEvent& event) void event(CEvent& event)
{ {
if (event.type() == CIPCServerEvent::ClientDisconnected) { if (event.type() == Event::Disconnected) {
int client_id = static_cast<const ASClientDisconnectedNotification&>(event).client_id(); int client_id = static_cast<const DisconnectedEvent&>(event).client_id();
dbgprintf("CIPCServerSideClient: Client disconnected: %d\n", client_id); dbgprintf("Connection: Client disconnected: %d\n", client_id);
delete this; delete this;
return; return;
} }
@ -215,4 +219,6 @@ private:
int m_pid; int m_pid;
}; };
} // Server
} // IPC

View file

@ -188,7 +188,7 @@ void GWindowServerConnection::handle_wm_event(const WSAPI_ServerMessage& event,
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }
void GWindowServerConnection::postprocess_bundles(Vector<CIPCClientSideConnection::IncomingASMessageBundle>& bundles) void GWindowServerConnection::postprocess_bundles(Vector<IncomingMessageBundle>& bundles)
{ {
int coalesced_paints = 0; int coalesced_paints = 0;
int coalesced_resizes = 0; int coalesced_resizes = 0;

View file

@ -1,7 +1,7 @@
#pragma once #pragma once
#include <LibCore/CEventLoop.h> #include <LibCore/CEventLoop.h>
#include <LibCore/CIPCClientSideConnection.h> #include <LibCore/CoreIPCClient.h>
#include <LibGUI/GEvent.h> #include <LibGUI/GEvent.h>
#include <WindowServer/WSAPITypes.h> #include <WindowServer/WSAPITypes.h>
@ -10,16 +10,16 @@ class CObject;
class CNotifier; class CNotifier;
class GWindow; class GWindow;
class GWindowServerConnection : public CIPCClientSideConnection<WSAPI_ServerMessage, WSAPI_ClientMessage> { class GWindowServerConnection : public IPC::Client::Connection<WSAPI_ServerMessage, WSAPI_ClientMessage> {
public: public:
GWindowServerConnection() GWindowServerConnection()
: CIPCClientSideConnection("/tmp/wsportal") : Connection("/tmp/wsportal")
{} {}
void handshake() override; void handshake() override;
private: private:
void postprocess_bundles(Vector<IncomingASMessageBundle>& m_unprocessed_bundles) override; void postprocess_bundles(Vector<IncomingMessageBundle>& m_unprocessed_bundles) override;
void handle_paint_event(const WSAPI_ServerMessage&, GWindow&, const ByteBuffer& extra_data); void handle_paint_event(const WSAPI_ServerMessage&, GWindow&, const ByteBuffer& extra_data);
void handle_resize_event(const WSAPI_ServerMessage&, GWindow&); void handle_resize_event(const WSAPI_ServerMessage&, GWindow&);
void handle_mouse_event(const WSAPI_ServerMessage&, GWindow&); void handle_mouse_event(const WSAPI_ServerMessage&, GWindow&);

View file

@ -14,7 +14,7 @@
#include <stdio.h> #include <stdio.h>
ASClientConnection::ASClientConnection(int fd, int client_id, ASMixer& mixer) ASClientConnection::ASClientConnection(int fd, int client_id, ASMixer& mixer)
: CIPCServerSideClient(fd, client_id) : Connection(fd, client_id)
, m_mixer(mixer) , m_mixer(mixer)
{ {
} }

View file

@ -1,11 +1,11 @@
#pragma once #pragma once
#include <LibCore/CIPCServerSideClient.h> #include <LibCore/CoreIPCServer.h>
#include <LibAudio/ASAPI.h> #include <LibAudio/ASAPI.h>
class ASMixer; class ASMixer;
class ASClientConnection final : public CIPCServerSideClient<ASAPI_ServerMessage, ASAPI_ClientMessage> class ASClientConnection final : public IPC::Server::Connection<ASAPI_ServerMessage, ASAPI_ClientMessage>
{ {
public: public:
explicit ASClientConnection(int fd, int client_id, ASMixer& mixer); explicit ASClientConnection(int fd, int client_id, ASMixer& mixer);

View file

@ -31,8 +31,7 @@ void ASEventLoop::drain_server()
} else { } else {
dbgprintf("AudioServer: accept()ed client %d\n", client_fd); dbgprintf("AudioServer: accept()ed client %d\n", client_fd);
static int s_next_client_id = 0; static int s_next_client_id = 0;
CIPCServerSideClientCreator<ASClientConnection>(client_fd, s_next_client_id++, m_mixer); IPC::Server::new_connection_for_client<ASClientConnection>(client_fd, s_next_client_id++, m_mixer);
//new ASClientConnection(client_fd, s_next_client_id++, m_mixer);
} }
} }

View file

@ -40,7 +40,7 @@ WSClientConnection* WSClientConnection::from_client_id(int client_id)
} }
WSClientConnection::WSClientConnection(int fd, int client_id) WSClientConnection::WSClientConnection(int fd, int client_id)
: CIPCServerSideClient(fd, client_id) : Connection(fd, client_id)
{ {
if (!s_connections) if (!s_connections)
s_connections = new HashMap<int, WSClientConnection*>; s_connections = new HashMap<int, WSClientConnection*>;
@ -88,7 +88,7 @@ void WSClientConnection::event(CEvent& event)
return; return;
} }
CIPCServerSideClient::event(event); Connection::event(event);
} }
static Vector<Rect, 32> get_rects(const WSAPI_ClientMessage& message, const ByteBuffer& extra_data) static Vector<Rect, 32> get_rects(const WSAPI_ClientMessage& message, const ByteBuffer& extra_data)

View file

@ -5,7 +5,7 @@
#include <AK/OwnPtr.h> #include <AK/OwnPtr.h>
#include <AK/WeakPtr.h> #include <AK/WeakPtr.h>
#include <LibCore/CObject.h> #include <LibCore/CObject.h>
#include <LibCore/CIPCServerSideClient.h> #include <LibCore/CoreIPCServer.h>
#include <SharedGraphics/GraphicsBitmap.h> #include <SharedGraphics/GraphicsBitmap.h>
#include <WindowServer/WSEvent.h> #include <WindowServer/WSEvent.h>
@ -13,7 +13,7 @@ class WSWindow;
class WSMenu; class WSMenu;
class WSMenuBar; class WSMenuBar;
class WSClientConnection final : public CIPCServerSideClient<WSAPI_ServerMessage, WSAPI_ClientMessage> { class WSClientConnection final : public IPC::Server::Connection<WSAPI_ServerMessage, WSAPI_ClientMessage> {
public: public:
explicit WSClientConnection(int fd, int client_id); explicit WSClientConnection(int fd, int client_id);
~WSClientConnection() override; ~WSClientConnection() override;

View file

@ -62,8 +62,7 @@ void WSEventLoop::drain_server()
} else { } else {
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;
IPC::Server::new_connection_for_client<WSClientConnection>(client_fd, client_id);
CIPCServerSideClientCreator<WSClientConnection>(client_fd, client_id);
} }
} }