From fe45f5a6d2d8ed3d8ca3c05394bd290dc184e6b9 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 27 Jul 2019 10:53:50 +0200 Subject: [PATCH] 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. --- Libraries/LibCore/CoreIPCServer.h | 33 ++++-------------- Servers/AudioServer/ASClientConnection.cpp | 4 +-- Servers/AudioServer/ASClientConnection.h | 2 +- Servers/AudioServer/ASEventLoop.cpp | 38 ++++++--------------- Servers/AudioServer/ASEventLoop.h | 7 ++-- Servers/WindowServer/WSClientConnection.cpp | 4 +-- Servers/WindowServer/WSClientConnection.h | 2 +- Servers/WindowServer/WSEventLoop.cpp | 38 +++++++-------------- Servers/WindowServer/WSEventLoop.h | 5 ++- 9 files changed, 40 insertions(+), 93 deletions(-) diff --git a/Libraries/LibCore/CoreIPCServer.h b/Libraries/LibCore/CoreIPCServer.h index 97db89a38a..1dd1406c01 100644 --- a/Libraries/LibCore/CoreIPCServer.h +++ b/Libraries/LibCore/CoreIPCServer.h @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -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; }; diff --git a/Servers/AudioServer/ASClientConnection.cpp b/Servers/AudioServer/ASClientConnection.cpp index acb467ddbc..1989c166d3 100644 --- a/Servers/AudioServer/ASClientConnection.cpp +++ b/Servers/AudioServer/ASClientConnection.cpp @@ -13,8 +13,8 @@ #include #include -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) { } diff --git a/Servers/AudioServer/ASClientConnection.h b/Servers/AudioServer/ASClientConnection.h index ecc48477d1..9699267f0c 100644 --- a/Servers/AudioServer/ASClientConnection.h +++ b/Servers/AudioServer/ASClientConnection.h @@ -8,7 +8,7 @@ class ASMixer; class ASClientConnection final : public IPC::Server::Connection { 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; diff --git a/Servers/AudioServer/ASEventLoop.cpp b/Servers/AudioServer/ASEventLoop.cpp index dfb4ce3a2a..46b0fb12f0 100644 --- a/Servers/AudioServer/ASEventLoop.cpp +++ b/Servers/AudioServer/ASEventLoop.cpp @@ -1,37 +1,21 @@ #include "ASEventLoop.h" #include "ASClientConnection.h" - -#include #include +#include #include 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(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(client_fd, s_next_client_id++, m_mixer); - } + int client_id = ++s_next_client_id; + IPC::Server::new_connection_for_client(*client_socket, client_id, m_mixer); + }; } - diff --git a/Servers/AudioServer/ASEventLoop.h b/Servers/AudioServer/ASEventLoop.h index c6a5ed259f..72e0a31758 100644 --- a/Servers/AudioServer/ASEventLoop.h +++ b/Servers/AudioServer/ASEventLoop.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include #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 m_server_notifier; + CLocalServer m_server_sock; ASMixer m_mixer; - - void drain_server(); }; diff --git a/Servers/WindowServer/WSClientConnection.cpp b/Servers/WindowServer/WSClientConnection.cpp index 6d51cdb42d..08bce8656a 100644 --- a/Servers/WindowServer/WSClientConnection.cpp +++ b/Servers/WindowServer/WSClientConnection.cpp @@ -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; diff --git a/Servers/WindowServer/WSClientConnection.h b/Servers/WindowServer/WSClientConnection.h index 9fae7bfccc..e2356f7190 100644 --- a/Servers/WindowServer/WSClientConnection.h +++ b/Servers/WindowServer/WSClientConnection.h @@ -16,7 +16,7 @@ class WSMenuBar; class WSClientConnection final : public IPC::Server::Connection { 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; diff --git a/Servers/WindowServer/WSEventLoop.cpp b/Servers/WindowServer/WSEventLoop.cpp index c8b6e2d5cd..94e6de2109 100644 --- a/Servers/WindowServer/WSEventLoop.cpp +++ b/Servers/WindowServer/WSEventLoop.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -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(*client_socket, client_id); + }; - ASSERT(m_server_sock.fd() >= 0); ASSERT(m_keyboard_fd >= 0); ASSERT(m_mouse_fd >= 0); - m_server_notifier = make(m_server_sock.fd(), CNotifier::Read); - m_server_notifier->on_ready_to_read = [this] { drain_server(); }; - m_keyboard_notifier = make(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(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); } } - diff --git a/Servers/WindowServer/WSEventLoop.h b/Servers/WindowServer/WSEventLoop.h index 6f17f6257e..c7454d9278 100644 --- a/Servers/WindowServer/WSEventLoop.h +++ b/Servers/WindowServer/WSEventLoop.h @@ -2,8 +2,8 @@ #include #include +#include #include -#include 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 m_keyboard_notifier; int m_mouse_fd { -1 }; OwnPtr m_mouse_notifier; - CLocalSocket m_server_sock; + CLocalServer m_server_sock; OwnPtr m_server_notifier; };