mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:27:44 +00:00
LibIPC: Add IPC::MultiServer convenience class
This encapsulates what our multi-client IPC servers typically do on startup: 1. Create a Core::LocalServer 2. Take over a listening socket file descriptor from SystemServer 3. Set up an accept handler for incoming connections IPC::MultiServer does all this for you! All you have to do is provide the relevant client connection type as a template argument.
This commit is contained in:
parent
81047d8f9c
commit
6d0f504822
11 changed files with 62 additions and 81 deletions
39
Userland/Libraries/LibIPC/MultiServer.h
Normal file
39
Userland/Libraries/LibIPC/MultiServer.h
Normal file
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Error.h>
|
||||
#include <LibCore/LocalServer.h>
|
||||
#include <LibIPC/ClientConnection.h>
|
||||
|
||||
namespace IPC {
|
||||
|
||||
template<typename ClientConnectionType>
|
||||
class MultiServer {
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<MultiServer>> try_create(Optional<String> socket_path = {})
|
||||
{
|
||||
auto server = TRY(Core::LocalServer::try_create());
|
||||
TRY(server->take_over_from_system_server(socket_path.value_or({})));
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) MultiServer(move(server)));
|
||||
}
|
||||
|
||||
private:
|
||||
explicit MultiServer(NonnullRefPtr<Core::LocalServer> server)
|
||||
: m_server(move(server))
|
||||
{
|
||||
m_server->on_accept = [&](auto client_socket) {
|
||||
auto client_id = ++m_next_client_id;
|
||||
(void)IPC::new_client_connection<ClientConnectionType>(move(client_socket), client_id);
|
||||
};
|
||||
}
|
||||
|
||||
int m_next_client_id { 0 };
|
||||
RefPtr<Core::LocalServer> m_server;
|
||||
};
|
||||
|
||||
}
|
|
@ -7,9 +7,8 @@
|
|||
#include <Clipboard/ClientConnection.h>
|
||||
#include <Clipboard/Storage.h>
|
||||
#include <LibCore/EventLoop.h>
|
||||
#include <LibCore/LocalServer.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <LibIPC/ClientConnection.h>
|
||||
#include <LibIPC/MultiServer.h>
|
||||
#include <LibMain/Main.h>
|
||||
|
||||
ErrorOr<int> serenity_main(Main::Arguments)
|
||||
|
@ -18,14 +17,7 @@ ErrorOr<int> serenity_main(Main::Arguments)
|
|||
Core::EventLoop event_loop;
|
||||
TRY(Core::System::unveil(nullptr, nullptr));
|
||||
|
||||
auto server = TRY(Core::LocalServer::try_create());
|
||||
TRY(server->take_over_from_system_server());
|
||||
|
||||
server->on_accept = [&](auto client_socket) {
|
||||
static int s_next_client_id = 0;
|
||||
int client_id = ++s_next_client_id;
|
||||
(void)IPC::new_client_connection<Clipboard::ClientConnection>(move(client_socket), client_id);
|
||||
};
|
||||
auto server = TRY(IPC::MultiServer<Clipboard::ClientConnection>::try_create());
|
||||
|
||||
Clipboard::Storage::the().on_content_change = [&] {
|
||||
Clipboard::ClientConnection::for_each_client([&](auto& client) {
|
||||
|
|
|
@ -5,9 +5,9 @@
|
|||
*/
|
||||
|
||||
#include "ClientConnection.h"
|
||||
#include <LibCore/LocalServer.h>
|
||||
#include <LibCore/StandardPaths.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <LibIPC/MultiServer.h>
|
||||
#include <LibMain/Main.h>
|
||||
|
||||
ErrorOr<int> serenity_main(Main::Arguments)
|
||||
|
@ -18,13 +18,6 @@ ErrorOr<int> serenity_main(Main::Arguments)
|
|||
|
||||
Core::EventLoop event_loop;
|
||||
|
||||
auto server = TRY(Core::LocalServer::try_create());
|
||||
TRY(server->take_over_from_system_server());
|
||||
server->on_accept = [&](auto client_socket) {
|
||||
static int s_next_client_id = 0;
|
||||
int client_id = ++s_next_client_id;
|
||||
(void)IPC::new_client_connection<ConfigServer::ClientConnection>(move(client_socket), client_id);
|
||||
};
|
||||
|
||||
auto server = TRY(IPC::MultiServer<ConfigServer::ClientConnection>::try_create());
|
||||
return event_loop.exec();
|
||||
}
|
||||
|
|
|
@ -10,21 +10,16 @@
|
|||
#include <LibCore/LocalServer.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <LibIPC/ClientConnection.h>
|
||||
#include <LibIPC/MultiServer.h>
|
||||
#include <LibMain/Main.h>
|
||||
|
||||
ErrorOr<int> serenity_main(Main::Arguments)
|
||||
{
|
||||
Core::EventLoop event_loop;
|
||||
auto server = TRY(Core::LocalServer::try_create());
|
||||
|
||||
TRY(Core::System::pledge("stdio unix accept"));
|
||||
|
||||
TRY(server->take_over_from_system_server("/tmp/portal/inspector"));
|
||||
server->on_accept = [&](auto client_socket) {
|
||||
static int s_next_client_id = 0;
|
||||
int client_id = ++s_next_client_id;
|
||||
(void)IPC::new_client_connection<InspectorServer::ClientConnection>(move(client_socket), client_id);
|
||||
};
|
||||
auto server = TRY(IPC::MultiServer<InspectorServer::ClientConnection>::try_create("/tmp/portal/inspector"));
|
||||
|
||||
auto inspectables_server = TRY(Core::LocalServer::try_create());
|
||||
TRY(inspectables_server->take_over_from_system_server("/tmp/portal/inspectables"));
|
||||
|
|
|
@ -8,14 +8,14 @@
|
|||
#include "Launcher.h"
|
||||
#include <LibCore/ConfigFile.h>
|
||||
#include <LibCore/EventLoop.h>
|
||||
#include <LibCore/LocalServer.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <LibIPC/MultiServer.h>
|
||||
#include <LibMain/Main.h>
|
||||
|
||||
ErrorOr<int> serenity_main(Main::Arguments)
|
||||
{
|
||||
Core::EventLoop event_loop;
|
||||
auto server = TRY(Core::LocalServer::try_create());
|
||||
auto server = TRY(IPC::MultiServer<LaunchServer::ClientConnection>::try_create());
|
||||
|
||||
auto launcher = LaunchServer::Launcher();
|
||||
launcher.load_handlers();
|
||||
|
@ -23,12 +23,5 @@ ErrorOr<int> serenity_main(Main::Arguments)
|
|||
|
||||
TRY(Core::System::pledge("stdio accept rpath proc exec"));
|
||||
|
||||
TRY(server->take_over_from_system_server());
|
||||
server->on_accept = [&](auto client_socket) {
|
||||
static int s_next_client_id = 0;
|
||||
int client_id = ++s_next_client_id;
|
||||
(void)IPC::new_client_connection<LaunchServer::ClientConnection>(move(client_socket), client_id);
|
||||
};
|
||||
|
||||
return event_loop.exec();
|
||||
}
|
||||
|
|
|
@ -72,13 +72,7 @@ LookupServer::LookupServer()
|
|||
}
|
||||
m_mdns = MulticastDNS::construct(this);
|
||||
|
||||
m_local_server = Core::LocalServer::construct(this);
|
||||
m_local_server->on_accept = [](auto client_socket) {
|
||||
static int s_next_client_id = 0;
|
||||
int client_id = ++s_next_client_id;
|
||||
(void)IPC::new_client_connection<ClientConnection>(move(client_socket), client_id);
|
||||
};
|
||||
MUST(m_local_server->take_over_from_system_server());
|
||||
m_server = MUST(IPC::MultiServer<ClientConnection>::try_create());
|
||||
}
|
||||
|
||||
void LookupServer::load_etc_hosts()
|
||||
|
|
|
@ -6,12 +6,14 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "ClientConnection.h"
|
||||
#include "DNSName.h"
|
||||
#include "DNSPacket.h"
|
||||
#include "DNSServer.h"
|
||||
#include "MulticastDNS.h"
|
||||
#include <LibCore/FileWatcher.h>
|
||||
#include <LibCore/Object.h>
|
||||
#include <LibIPC/MultiServer.h>
|
||||
|
||||
namespace LookupServer {
|
||||
|
||||
|
@ -32,7 +34,7 @@ private:
|
|||
|
||||
Vector<DNSAnswer> lookup(const DNSName& hostname, const String& nameserver, bool& did_get_response, DNSRecordType record_type, ShouldRandomizeCase = ShouldRandomizeCase::Yes);
|
||||
|
||||
RefPtr<Core::LocalServer> m_local_server;
|
||||
OwnPtr<IPC::MultiServer<ClientConnection>> m_server;
|
||||
RefPtr<DNSServer> m_dns_server;
|
||||
RefPtr<MulticastDNS> m_mdns;
|
||||
Vector<String> m_nameservers;
|
||||
|
|
|
@ -5,10 +5,9 @@
|
|||
*/
|
||||
|
||||
#include "ClientConnection.h"
|
||||
#include <LibCore/LocalServer.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <LibGUI/Application.h>
|
||||
#include <LibGUI/WindowServerConnection.h>
|
||||
#include <LibIPC/MultiServer.h>
|
||||
#include <LibMain/Main.h>
|
||||
|
||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
|
@ -16,14 +15,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
TRY(Core::System::pledge("stdio recvfd sendfd accept rpath unix"));
|
||||
|
||||
auto app = TRY(GUI::Application::try_create(arguments));
|
||||
auto server = TRY(Core::LocalServer::try_create());
|
||||
|
||||
TRY(server->take_over_from_system_server());
|
||||
server->on_accept = [&](auto client_socket) {
|
||||
static int s_next_client_id = 0;
|
||||
int client_id = ++s_next_client_id;
|
||||
(void)IPC::new_client_connection<NotificationServer::ClientConnection>(move(client_socket), client_id);
|
||||
};
|
||||
auto server = TRY(IPC::MultiServer<NotificationServer::ClientConnection>::try_create());
|
||||
|
||||
TRY(Core::System::unveil("/res", "r"));
|
||||
TRY(Core::System::unveil(nullptr, nullptr));
|
||||
|
|
|
@ -5,8 +5,8 @@
|
|||
*/
|
||||
|
||||
#include <LibCore/EventLoop.h>
|
||||
#include <LibCore/LocalServer.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <LibIPC/MultiServer.h>
|
||||
#include <LibMain/Main.h>
|
||||
#include <SQLServer/ClientConnection.h>
|
||||
#include <stdio.h>
|
||||
|
@ -26,13 +26,6 @@ ErrorOr<int> serenity_main(Main::Arguments)
|
|||
|
||||
Core::EventLoop event_loop;
|
||||
|
||||
auto server = TRY(Core::LocalServer::try_create());
|
||||
TRY(server->take_over_from_system_server());
|
||||
server->on_accept = [&](auto client_socket) {
|
||||
static int s_next_client_id = 0;
|
||||
int client_id = ++s_next_client_id;
|
||||
(void)IPC::new_client_connection<SQLServer::ClientConnection>(client_socket, client_id);
|
||||
};
|
||||
|
||||
auto server = TRY(IPC::MultiServer<SQLServer::ClientConnection>::try_create());
|
||||
return event_loop.exec();
|
||||
}
|
||||
|
|
|
@ -19,26 +19,12 @@
|
|||
namespace WindowServer {
|
||||
|
||||
EventLoop::EventLoop()
|
||||
: m_window_server(Core::LocalServer::construct())
|
||||
, m_wm_server(Core::LocalServer::construct())
|
||||
{
|
||||
m_keyboard_fd = open("/dev/keyboard0", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
|
||||
m_mouse_fd = open("/dev/mouse0", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
|
||||
|
||||
MUST(m_window_server->take_over_from_system_server("/tmp/portal/window"));
|
||||
MUST(m_wm_server->take_over_from_system_server("/tmp/portal/wm"));
|
||||
|
||||
m_window_server->on_accept = [&](auto client_socket) {
|
||||
static int s_next_client_id = 0;
|
||||
int client_id = ++s_next_client_id;
|
||||
(void)IPC::new_client_connection<ClientConnection>(move(client_socket), client_id);
|
||||
};
|
||||
|
||||
m_wm_server->on_accept = [&](auto client_socket) {
|
||||
static int s_next_wm_id = 0;
|
||||
int wm_id = ++s_next_wm_id;
|
||||
(void)IPC::new_client_connection<WMClientConnection>(move(client_socket), wm_id);
|
||||
};
|
||||
m_window_server = MUST(IPC::MultiServer<ClientConnection>::try_create("/tmp/portal/window"));
|
||||
m_wm_server = MUST(IPC::MultiServer<WMClientConnection>::try_create("/tmp/portal/wm"));
|
||||
|
||||
if (m_keyboard_fd >= 0) {
|
||||
m_keyboard_notifier = Core::Notifier::construct(m_keyboard_fd, Core::Notifier::Read);
|
||||
|
|
|
@ -6,10 +6,12 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "ClientConnection.h"
|
||||
#include "WMClientConnection.h"
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <LibCore/EventLoop.h>
|
||||
#include <LibCore/LocalServer.h>
|
||||
#include <LibCore/Notifier.h>
|
||||
#include <LibIPC/MultiServer.h>
|
||||
|
||||
namespace WindowServer {
|
||||
|
||||
|
@ -31,8 +33,8 @@ private:
|
|||
RefPtr<Core::Notifier> m_keyboard_notifier;
|
||||
int m_mouse_fd { -1 };
|
||||
RefPtr<Core::Notifier> m_mouse_notifier;
|
||||
RefPtr<Core::LocalServer> m_window_server;
|
||||
RefPtr<Core::LocalServer> m_wm_server;
|
||||
OwnPtr<IPC::MultiServer<ClientConnection>> m_window_server;
|
||||
OwnPtr<IPC::MultiServer<WMClientConnection>> m_wm_server;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue