mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-20 11:36:10 +00:00
LibCore: Port CoreIPCServer to using CLocalServer.
Use CLocalServer to listen for connections in WindowServer and AudioServer. This allows us to accept incoming CLocalSocket objects from the CLocalServer and construct client connections based on those. Removed COpenedSocket since it's replaced by CLocalSocket.
This commit is contained in:
parent
c289e49ee5
commit
fe45f5a6d2
Notes:
sideshowbarker
2024-07-19 13:02:06 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/fe45f5a6d2d
9 changed files with 40 additions and 93 deletions
|
@ -3,6 +3,7 @@
|
|||
#include <LibCore/CEvent.h>
|
||||
#include <LibCore/CEventLoop.h>
|
||||
#include <LibCore/CIODevice.h>
|
||||
#include <LibCore/CLocalSocket.h>
|
||||
#include <LibCore/CNotifier.h>
|
||||
#include <LibCore/CObject.h>
|
||||
|
||||
|
@ -57,12 +58,12 @@ namespace Server {
|
|||
class Connection : public CObject {
|
||||
C_OBJECT(Connection)
|
||||
public:
|
||||
Connection(int fd, int client_id)
|
||||
: m_socket(fd)
|
||||
, m_notifier(CNotifier(fd, CNotifier::Read))
|
||||
Connection(CLocalSocket& socket, int client_id)
|
||||
: m_socket(socket)
|
||||
, m_client_id(client_id)
|
||||
{
|
||||
m_notifier.on_ready_to_read = [this] { drain_client(); };
|
||||
add_child(socket);
|
||||
m_socket.on_ready_to_read = [this] { drain_client(); };
|
||||
#if defined(CIPC_DEBUG)
|
||||
dbg() << "S: Created new Connection " << fd << client_id << " and said hello";
|
||||
#endif
|
||||
|
@ -163,8 +164,8 @@ namespace Server {
|
|||
void did_misbehave()
|
||||
{
|
||||
dbgprintf("Connection{%p} (id=%d, pid=%d) misbehaved, disconnecting.\n", this, client_id(), m_pid);
|
||||
m_socket.close();
|
||||
delete_later();
|
||||
m_notifier.set_enabled(false);
|
||||
}
|
||||
|
||||
int client_id() const { return m_client_id; }
|
||||
|
@ -190,27 +191,7 @@ namespace Server {
|
|||
virtual bool handle_message(const ClientMessage&, const ByteBuffer&& = {}) = 0;
|
||||
|
||||
private:
|
||||
// TODO: A way to create some kind of CIODevice with an open FD would be nice.
|
||||
class COpenedSocket : public CIODevice {
|
||||
C_OBJECT(COpenedSocket)
|
||||
public:
|
||||
COpenedSocket(int fd)
|
||||
{
|
||||
set_fd(fd);
|
||||
set_mode(CIODevice::OpenMode::ReadWrite);
|
||||
}
|
||||
|
||||
bool open(CIODevice::OpenMode) override
|
||||
{
|
||||
ASSERT_NOT_REACHED();
|
||||
return true;
|
||||
};
|
||||
|
||||
int fd() const { return CIODevice::fd(); }
|
||||
};
|
||||
|
||||
COpenedSocket m_socket;
|
||||
CNotifier m_notifier;
|
||||
CLocalSocket& m_socket;
|
||||
int m_client_id;
|
||||
int m_pid;
|
||||
};
|
||||
|
|
|
@ -13,8 +13,8 @@
|
|||
#include <sys/socket.h>
|
||||
#include <stdio.h>
|
||||
|
||||
ASClientConnection::ASClientConnection(int fd, int client_id, ASMixer& mixer)
|
||||
: Connection(fd, client_id)
|
||||
ASClientConnection::ASClientConnection(CLocalSocket& client_socket, int client_id, ASMixer& mixer)
|
||||
: Connection(client_socket, client_id)
|
||||
, m_mixer(mixer)
|
||||
{
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ class ASMixer;
|
|||
class ASClientConnection final : public IPC::Server::Connection<ASAPI_ServerMessage, ASAPI_ClientMessage>
|
||||
{
|
||||
public:
|
||||
explicit ASClientConnection(int fd, int client_id, ASMixer& mixer);
|
||||
explicit ASClientConnection(CLocalSocket&, int client_id, ASMixer& mixer);
|
||||
~ASClientConnection() override;
|
||||
void send_greeting() override;
|
||||
bool handle_message(const ASAPI_ClientMessage&, const ByteBuffer&& = {}) override;
|
||||
|
|
|
@ -1,37 +1,21 @@
|
|||
#include "ASEventLoop.h"
|
||||
#include "ASClientConnection.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
ASEventLoop::ASEventLoop()
|
||||
{
|
||||
unlink("/tmp/asportal");
|
||||
|
||||
sockaddr_un address;
|
||||
address.sun_family = AF_LOCAL;
|
||||
strcpy(address.sun_path, "/tmp/asportal");
|
||||
int rc = bind(m_server_sock.fd(), (const sockaddr*)&address, sizeof(address));
|
||||
ASSERT(rc == 0);
|
||||
rc = listen(m_server_sock.fd(), 5);
|
||||
ASSERT(rc == 0);
|
||||
|
||||
m_server_notifier = make<CNotifier>(m_server_sock.fd(), CNotifier::Read);
|
||||
m_server_notifier->on_ready_to_read = [this] { drain_server(); };
|
||||
}
|
||||
|
||||
void ASEventLoop::drain_server()
|
||||
{
|
||||
sockaddr_un address;
|
||||
socklen_t address_size = sizeof(address);
|
||||
int client_fd = accept(m_server_sock.fd(), (sockaddr*)&address, &address_size);
|
||||
if (client_fd < 0) {
|
||||
dbgprintf("AudioServer: accept() failed: %s\n", strerror(errno));
|
||||
} else {
|
||||
dbgprintf("AudioServer: accept()ed client %d\n", client_fd);
|
||||
m_server_sock.listen("/tmp/asportal");
|
||||
m_server_sock.on_ready_to_accept = [this] {
|
||||
auto* client_socket = m_server_sock.accept();
|
||||
if (!client_socket) {
|
||||
dbg() << "AudioServer: accept failed.";
|
||||
return;
|
||||
}
|
||||
static int s_next_client_id = 0;
|
||||
IPC::Server::new_connection_for_client<ASClientConnection>(client_fd, s_next_client_id++, m_mixer);
|
||||
}
|
||||
int client_id = ++s_next_client_id;
|
||||
IPC::Server::new_connection_for_client<ASClientConnection>(*client_socket, client_id, m_mixer);
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <LibCore/CEventLoop.h>
|
||||
#include <LibCore/CLocalSocket.h>
|
||||
#include <LibCore/CLocalServer.h>
|
||||
#include <LibCore/CNotifier.h>
|
||||
#include "ASMixer.h"
|
||||
|
||||
|
@ -12,9 +12,6 @@ public:
|
|||
int exec() { return m_event_loop.exec(); }
|
||||
private:
|
||||
CEventLoop m_event_loop;
|
||||
CLocalSocket m_server_sock;
|
||||
OwnPtr<CNotifier> m_server_notifier;
|
||||
CLocalServer m_server_sock;
|
||||
ASMixer m_mixer;
|
||||
|
||||
void drain_server();
|
||||
};
|
||||
|
|
|
@ -39,8 +39,8 @@ WSClientConnection* WSClientConnection::from_client_id(int client_id)
|
|||
return (*it).value;
|
||||
}
|
||||
|
||||
WSClientConnection::WSClientConnection(int fd, int client_id)
|
||||
: Connection(fd, client_id)
|
||||
WSClientConnection::WSClientConnection(CLocalSocket& client_socket, int client_id)
|
||||
: Connection(client_socket, client_id)
|
||||
{
|
||||
if (!s_connections)
|
||||
s_connections = new HashMap<int, WSClientConnection*>;
|
||||
|
|
|
@ -16,7 +16,7 @@ class WSMenuBar;
|
|||
|
||||
class WSClientConnection final : public IPC::Server::Connection<WSAPI_ServerMessage, WSAPI_ClientMessage> {
|
||||
public:
|
||||
explicit WSClientConnection(int fd, int client_id);
|
||||
explicit WSClientConnection(CLocalSocket&, int client_id);
|
||||
~WSClientConnection() override;
|
||||
void send_greeting() override;
|
||||
bool handle_message(const WSAPI_ClientMessage&, const ByteBuffer&& = {}) override;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include <Kernel/KeyCode.h>
|
||||
#include <Kernel/MousePacket.h>
|
||||
#include <LibCore/CLocalSocket.h>
|
||||
#include <LibCore/CObject.h>
|
||||
#include <WindowServer/WSAPITypes.h>
|
||||
#include <WindowServer/WSClientConnection.h>
|
||||
|
@ -25,22 +26,22 @@ WSEventLoop::WSEventLoop()
|
|||
m_mouse_fd = open("/dev/psaux", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
|
||||
|
||||
unlink("/tmp/wsportal");
|
||||
m_server_sock.listen("/tmp/wsportal");
|
||||
|
||||
sockaddr_un address;
|
||||
address.sun_family = AF_LOCAL;
|
||||
strcpy(address.sun_path, "/tmp/wsportal");
|
||||
int rc = bind(m_server_sock.fd(), (const sockaddr*)&address, sizeof(address));
|
||||
ASSERT(rc == 0);
|
||||
rc = listen(m_server_sock.fd(), 5);
|
||||
ASSERT(rc == 0);
|
||||
m_server_sock.on_ready_to_accept = [this] {
|
||||
auto* client_socket = m_server_sock.accept();
|
||||
if (!client_socket) {
|
||||
dbg() << "WindowServer: accept failed.";
|
||||
return;
|
||||
}
|
||||
static int s_next_client_id = 0;
|
||||
int client_id = ++s_next_client_id;
|
||||
IPC::Server::new_connection_for_client<WSClientConnection>(*client_socket, client_id);
|
||||
};
|
||||
|
||||
ASSERT(m_server_sock.fd() >= 0);
|
||||
ASSERT(m_keyboard_fd >= 0);
|
||||
ASSERT(m_mouse_fd >= 0);
|
||||
|
||||
m_server_notifier = make<CNotifier>(m_server_sock.fd(), CNotifier::Read);
|
||||
m_server_notifier->on_ready_to_read = [this] { drain_server(); };
|
||||
|
||||
m_keyboard_notifier = make<CNotifier>(m_keyboard_fd, CNotifier::Read);
|
||||
m_keyboard_notifier->on_ready_to_read = [this] { drain_keyboard(); };
|
||||
|
||||
|
@ -52,20 +53,6 @@ WSEventLoop::~WSEventLoop()
|
|||
{
|
||||
}
|
||||
|
||||
void WSEventLoop::drain_server()
|
||||
{
|
||||
sockaddr_un address;
|
||||
socklen_t address_size = sizeof(address);
|
||||
int client_fd = accept(m_server_sock.fd(), (sockaddr*)&address, &address_size);
|
||||
if (client_fd < 0) {
|
||||
dbgprintf("WindowServer: accept() failed: %s\n", strerror(errno));
|
||||
} else {
|
||||
static int s_next_client_id = 0;
|
||||
int client_id = ++s_next_client_id;
|
||||
IPC::Server::new_connection_for_client<WSClientConnection>(client_fd, client_id);
|
||||
}
|
||||
}
|
||||
|
||||
void WSEventLoop::drain_mouse()
|
||||
{
|
||||
auto& screen = WSScreen::the();
|
||||
|
@ -109,4 +96,3 @@ void WSEventLoop::drain_keyboard()
|
|||
screen.on_receive_keyboard_data(event);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <LibCore/CEventLoop.h>
|
||||
#include <LibCore/CLocalServer.h>
|
||||
#include <LibCore/CNotifier.h>
|
||||
#include <LibCore/CLocalSocket.h>
|
||||
|
||||
class WSClientConnection;
|
||||
struct WSAPI_ClientMessage;
|
||||
|
@ -16,7 +16,6 @@ public:
|
|||
int exec() { return m_event_loop.exec(); }
|
||||
|
||||
private:
|
||||
void drain_server();
|
||||
void drain_mouse();
|
||||
void drain_keyboard();
|
||||
|
||||
|
@ -25,6 +24,6 @@ private:
|
|||
OwnPtr<CNotifier> m_keyboard_notifier;
|
||||
int m_mouse_fd { -1 };
|
||||
OwnPtr<CNotifier> m_mouse_notifier;
|
||||
CLocalSocket m_server_sock;
|
||||
CLocalServer m_server_sock;
|
||||
OwnPtr<CNotifier> m_server_notifier;
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue