1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:47:44 +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

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

View file

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