1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 07:57:47 +00:00

LibCore: Use clang-format on CoreIPC{Client,Server}.

This commit is contained in:
Andreas Kling 2019-07-27 10:50:26 +02:00
parent 82446ea701
commit c289e49ee5
2 changed files with 376 additions and 380 deletions

View file

@ -1,27 +1,26 @@
#pragma once #pragma once
#include <LibCore/CEventLoop.h>
#include <LibCore/CEvent.h>
#include <LibCore/CLocalSocket.h>
#include <LibCore/CSyscallUtils.h>
#include <LibCore/CNotifier.h>
#include <LibAudio/ASAPI.h> #include <LibAudio/ASAPI.h>
#include <LibCore/CEvent.h>
#include <LibCore/CEventLoop.h>
#include <LibCore/CLocalSocket.h>
#include <LibCore/CNotifier.h>
#include <LibCore/CSyscallUtils.h>
#include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <sys/select.h> #include <sys/select.h>
#include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h>
#include <sys/uio.h> #include <sys/uio.h>
#include <unistd.h>
//#define CIPC_DEBUG //#define CIPC_DEBUG
namespace IPC namespace IPC {
{
namespace Client { namespace Client {
class Event : public CEvent { class Event : public CEvent {
public: public:
enum Type { enum Type {
Invalid = 2000, Invalid = 2000,
PostProcess, PostProcess,
@ -31,10 +30,10 @@ public:
: CEvent(type) : CEvent(type)
{ {
} }
}; };
class PostProcessEvent : public Event { class PostProcessEvent : public Event {
public: public:
explicit PostProcessEvent(int client_id) explicit PostProcessEvent(int client_id)
: Event(PostProcess) : Event(PostProcess)
, m_client_id(client_id) , m_client_id(client_id)
@ -43,14 +42,14 @@ public:
int client_id() const { return m_client_id; } int client_id() const { return m_client_id; }
private: private:
int m_client_id { 0 }; int m_client_id { 0 };
}; };
template <typename ServerMessage, typename ClientMessage> template<typename ServerMessage, typename ClientMessage>
class Connection : public CObject { class Connection : public CObject {
C_OBJECT(Connection) C_OBJECT(Connection)
public: public:
Connection(const StringView& address) Connection(const StringView& address)
: m_notifier(CNotifier(m_connection.fd(), CNotifier::Read)) : m_notifier(CNotifier(m_connection.fd(), CNotifier::Read))
{ {
@ -76,7 +75,6 @@ public:
virtual void handshake() = 0; virtual void handshake() = 0;
virtual void event(CEvent& event) override virtual void event(CEvent& event) override
{ {
if (event.type() == Event::PostProcess) { if (event.type() == Event::PostProcess) {
@ -91,7 +89,7 @@ public:
void set_my_client_id(int id) { m_my_client_id = id; } void set_my_client_id(int id) { m_my_client_id = id; }
int my_client_id() const { return m_my_client_id; } int my_client_id() const { return m_my_client_id; }
template <typename MessageType> template<typename MessageType>
bool wait_for_specific_event(MessageType type, ServerMessage& event) bool wait_for_specific_event(MessageType type, ServerMessage& event)
{ {
// Double check we don't already have the event waiting for us. // Double check we don't already have the event waiting for us.
@ -157,7 +155,7 @@ public:
return true; return true;
} }
template <typename MessageType> template<typename MessageType>
ServerMessage sync_request(const ClientMessage& request, MessageType response_type) ServerMessage sync_request(const ClientMessage& request, MessageType response_type)
{ {
bool success = post_message_to_server(request); bool success = post_message_to_server(request);
@ -169,7 +167,7 @@ public:
return response; return response;
} }
protected: protected:
struct IncomingMessageBundle { struct IncomingMessageBundle {
ServerMessage message; ServerMessage message;
ByteBuffer extra_data; ByteBuffer extra_data;
@ -177,11 +175,12 @@ protected:
virtual void postprocess_bundles(Vector<IncomingMessageBundle>& new_bundles) virtual void postprocess_bundles(Vector<IncomingMessageBundle>& new_bundles)
{ {
dbg() << "Client::Connection: " << " 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();
} }
private: private:
bool drain_messages_from_server() bool drain_messages_from_server()
{ {
for (;;) { for (;;) {
@ -223,8 +222,7 @@ private:
Vector<IncomingMessageBundle> 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 } // Client
} // IPC } // IPC

View file

@ -1,26 +1,25 @@
#pragma once #pragma once
#include <LibCore/CObject.h>
#include <LibCore/CEvent.h> #include <LibCore/CEvent.h>
#include <LibCore/CEventLoop.h> #include <LibCore/CEventLoop.h>
#include <LibCore/CIODevice.h> #include <LibCore/CIODevice.h>
#include <LibCore/CNotifier.h> #include <LibCore/CNotifier.h>
#include <LibCore/CObject.h>
#include <errno.h> #include <errno.h>
#include <unistd.h>
#include <sys/uio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h> #include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
//#define CIPC_DEBUG //#define CIPC_DEBUG
namespace IPC namespace IPC {
{
namespace Server { namespace Server {
class Event : public CEvent { class Event : public CEvent {
public: public:
enum Type { enum Type {
Invalid = 2000, Invalid = 2000,
Disconnected, Disconnected,
@ -30,10 +29,10 @@ public:
: CEvent(type) : CEvent(type)
{ {
} }
}; };
class DisconnectedEvent : public Event { class DisconnectedEvent : public Event {
public: public:
explicit DisconnectedEvent(int client_id) explicit DisconnectedEvent(int client_id)
: Event(Disconnected) : Event(Disconnected)
, m_client_id(client_id) , m_client_id(client_id)
@ -42,22 +41,22 @@ public:
int client_id() const { return m_client_id; } int client_id() const { return m_client_id; }
private: private:
int m_client_id { 0 }; int m_client_id { 0 };
}; };
template <typename T, class... Args> template<typename T, class... Args>
T* new_connection_for_client(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();
return conn; return conn;
}; };
template <typename ServerMessage, typename ClientMessage> template<typename ServerMessage, typename ClientMessage>
class Connection : public CObject { class Connection : public CObject {
C_OBJECT(Connection) C_OBJECT(Connection)
public: public:
Connection(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))
@ -175,7 +174,7 @@ public:
// ### having this public is sad // ### having this public is sad
virtual void send_greeting() = 0; virtual void send_greeting() = 0;
protected: protected:
void event(CEvent& event) void event(CEvent& event)
{ {
if (event.type() == Event::Disconnected) { if (event.type() == Event::Disconnected) {
@ -190,7 +189,7 @@ protected:
virtual bool handle_message(const ClientMessage&, const ByteBuffer&& = {}) = 0; virtual bool handle_message(const ClientMessage&, const ByteBuffer&& = {}) = 0;
private: private:
// TODO: A way to create some kind of CIODevice with an open FD would be nice. // TODO: A way to create some kind of CIODevice with an open FD would be nice.
class COpenedSocket : public CIODevice { class COpenedSocket : public CIODevice {
C_OBJECT(COpenedSocket) C_OBJECT(COpenedSocket)
@ -214,8 +213,7 @@ private:
CNotifier m_notifier; CNotifier m_notifier;
int m_client_id; int m_client_id;
int m_pid; int m_pid;
}; };
} // Server } // Server
} // IPC } // IPC