diff options
author | Andreas Kling <kling@serenityos.org> | 2021-12-06 17:54:11 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-12-06 19:22:16 +0100 |
commit | 6d0f504822bad8baaed79ed55e43e7a253efa187 (patch) | |
tree | 5d0ff3c6fc615addc006edd4c8e0219fcfaead61 | |
parent | 81047d8f9cb092fa3ef83c4e06f1dfd8f65173be (diff) | |
download | serenity-6d0f504822bad8baaed79ed55e43e7a253efa187.zip |
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.
-rw-r--r-- | Userland/Libraries/LibIPC/MultiServer.h | 39 | ||||
-rw-r--r-- | Userland/Services/Clipboard/main.cpp | 12 | ||||
-rw-r--r-- | Userland/Services/ConfigServer/main.cpp | 11 | ||||
-rw-r--r-- | Userland/Services/InspectorServer/main.cpp | 9 | ||||
-rw-r--r-- | Userland/Services/LaunchServer/main.cpp | 11 | ||||
-rw-r--r-- | Userland/Services/LookupServer/LookupServer.cpp | 8 | ||||
-rw-r--r-- | Userland/Services/LookupServer/LookupServer.h | 4 | ||||
-rw-r--r-- | Userland/Services/NotificationServer/main.cpp | 12 | ||||
-rw-r--r-- | Userland/Services/SQLServer/main.cpp | 11 | ||||
-rw-r--r-- | Userland/Services/WindowServer/EventLoop.cpp | 18 | ||||
-rw-r--r-- | Userland/Services/WindowServer/EventLoop.h | 8 |
11 files changed, 62 insertions, 81 deletions
diff --git a/Userland/Libraries/LibIPC/MultiServer.h b/Userland/Libraries/LibIPC/MultiServer.h new file mode 100644 index 0000000000..3a3794fb28 --- /dev/null +++ b/Userland/Libraries/LibIPC/MultiServer.h @@ -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; +}; + +} diff --git a/Userland/Services/Clipboard/main.cpp b/Userland/Services/Clipboard/main.cpp index ea4ddc4855..3e087fcfd7 100644 --- a/Userland/Services/Clipboard/main.cpp +++ b/Userland/Services/Clipboard/main.cpp @@ -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) { diff --git a/Userland/Services/ConfigServer/main.cpp b/Userland/Services/ConfigServer/main.cpp index cde261b8a7..344e940a94 100644 --- a/Userland/Services/ConfigServer/main.cpp +++ b/Userland/Services/ConfigServer/main.cpp @@ -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(); } diff --git a/Userland/Services/InspectorServer/main.cpp b/Userland/Services/InspectorServer/main.cpp index 5029ffdc90..d9770f32fa 100644 --- a/Userland/Services/InspectorServer/main.cpp +++ b/Userland/Services/InspectorServer/main.cpp @@ -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")); diff --git a/Userland/Services/LaunchServer/main.cpp b/Userland/Services/LaunchServer/main.cpp index 00d6137fb2..befe4af84b 100644 --- a/Userland/Services/LaunchServer/main.cpp +++ b/Userland/Services/LaunchServer/main.cpp @@ -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(); } diff --git a/Userland/Services/LookupServer/LookupServer.cpp b/Userland/Services/LookupServer/LookupServer.cpp index 9cc5115117..2018901088 100644 --- a/Userland/Services/LookupServer/LookupServer.cpp +++ b/Userland/Services/LookupServer/LookupServer.cpp @@ -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() diff --git a/Userland/Services/LookupServer/LookupServer.h b/Userland/Services/LookupServer/LookupServer.h index 2e42d5e290..f94b3ad10c 100644 --- a/Userland/Services/LookupServer/LookupServer.h +++ b/Userland/Services/LookupServer/LookupServer.h @@ -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; diff --git a/Userland/Services/NotificationServer/main.cpp b/Userland/Services/NotificationServer/main.cpp index ef74de1a2e..d7e69122f5 100644 --- a/Userland/Services/NotificationServer/main.cpp +++ b/Userland/Services/NotificationServer/main.cpp @@ -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)); diff --git a/Userland/Services/SQLServer/main.cpp b/Userland/Services/SQLServer/main.cpp index 814d3e342c..98ccc1099b 100644 --- a/Userland/Services/SQLServer/main.cpp +++ b/Userland/Services/SQLServer/main.cpp @@ -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(); } diff --git a/Userland/Services/WindowServer/EventLoop.cpp b/Userland/Services/WindowServer/EventLoop.cpp index 15a4f3d701..101740f550 100644 --- a/Userland/Services/WindowServer/EventLoop.cpp +++ b/Userland/Services/WindowServer/EventLoop.cpp @@ -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); diff --git a/Userland/Services/WindowServer/EventLoop.h b/Userland/Services/WindowServer/EventLoop.h index 2964de8408..dfad84b887 100644 --- a/Userland/Services/WindowServer/EventLoop.h +++ b/Userland/Services/WindowServer/EventLoop.h @@ -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; }; } |