2021-12-06 16:54:11 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2021, Andreas Kling <andreas@ladybird.org>
|
2021-12-06 16:54:11 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Error.h>
|
2022-12-09 16:17:52 +00:00
|
|
|
#include <AK/Function.h>
|
2021-12-06 16:54:11 +00:00
|
|
|
#include <LibCore/LocalServer.h>
|
2022-02-25 10:18:30 +00:00
|
|
|
#include <LibIPC/ConnectionFromClient.h>
|
2021-12-06 16:54:11 +00:00
|
|
|
|
|
|
|
namespace IPC {
|
|
|
|
|
2022-02-25 10:18:30 +00:00
|
|
|
template<typename ConnectionFromClientType>
|
2021-12-06 16:54:11 +00:00
|
|
|
class MultiServer {
|
|
|
|
public:
|
2023-12-16 14:19:34 +00:00
|
|
|
static ErrorOr<NonnullOwnPtr<MultiServer>> try_create(Optional<ByteString> socket_path = {})
|
2021-12-06 16:54:11 +00:00
|
|
|
{
|
|
|
|
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)));
|
|
|
|
}
|
|
|
|
|
2024-04-26 21:16:52 +00:00
|
|
|
static ErrorOr<NonnullOwnPtr<MultiServer>> try_create(NonnullRefPtr<Core::LocalServer> server)
|
|
|
|
{
|
|
|
|
return adopt_nonnull_own_or_enomem(new (nothrow) MultiServer(move(server)));
|
|
|
|
}
|
|
|
|
|
2022-12-09 16:17:52 +00:00
|
|
|
Function<void(ConnectionFromClientType&)> on_new_client;
|
|
|
|
|
2021-12-06 16:54:11 +00:00
|
|
|
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;
|
2022-12-09 16:17:52 +00:00
|
|
|
|
2024-10-22 21:47:33 +00:00
|
|
|
auto client = IPC::new_client_connection<ConnectionFromClientType>(IPC::Transport(move(client_socket)), client_id);
|
2022-12-09 16:17:52 +00:00
|
|
|
if (on_new_client)
|
|
|
|
on_new_client(*client);
|
2021-12-06 16:54:11 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
int m_next_client_id { 0 };
|
|
|
|
RefPtr<Core::LocalServer> m_server;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|