LibCore: Change Core::LocalServer::on_ready_to_accept => on_accept

Everyone used this hook in the same way: immediately accept() on the
socket and then do something with the newly accepted fd.

This patch simplifies the hook by having LocalServer do the accepting
automatically.
This commit is contained in:
Andreas Kling 2021-11-29 18:42:01 +01:00
parent 6cb3092b42
commit fe00393941
Notes: sideshowbarker 2024-07-17 23:17:22 +09:00
11 changed files with 28 additions and 80 deletions

View file

@ -83,8 +83,10 @@ void LocalServer::setup_notifier()
{
m_notifier = Notifier::construct(m_fd, Notifier::Event::Read, this);
m_notifier->on_ready_to_read = [this] {
if (on_ready_to_accept)
on_ready_to_accept();
if (on_accept) {
if (auto client_socket = accept())
on_accept(client_socket.release_nonnull());
}
};
}

View file

@ -22,6 +22,7 @@ public:
RefPtr<LocalSocket> accept();
Function<void(NonnullRefPtr<Core::LocalSocket>)> on_accept;
Function<void()> on_ready_to_accept;
private:

View file

@ -34,15 +34,11 @@ int main(int, char**)
auto server = Core::LocalServer::construct();
bool ok = server->take_over_from_system_server();
VERIFY(ok);
server->on_ready_to_accept = [&] {
auto client_socket = server->accept();
if (!client_socket) {
dbgln("AudioServer: accept failed.");
return;
}
server->on_accept = [&](NonnullRefPtr<Core::LocalSocket> client_socket) {
static int s_next_client_id = 0;
int client_id = ++s_next_client_id;
IPC::new_client_connection<AudioServer::ClientConnection>(client_socket.release_nonnull(), client_id, *mixer);
IPC::new_client_connection<AudioServer::ClientConnection>(move(client_socket), client_id, *mixer);
};
if (pledge("stdio recvfd thread accept cpath rpath wpath", nullptr) < 0) {

View file

@ -22,15 +22,10 @@ ErrorOr<int> serenity_main(Main::Arguments)
bool ok = server->take_over_from_system_server();
VERIFY(ok);
server->on_ready_to_accept = [&] {
auto client_socket = server->accept();
if (!client_socket) {
dbgln("Clipboard: accept failed.");
return;
}
server->on_accept = [&](auto client_socket) {
static int s_next_client_id = 0;
int client_id = ++s_next_client_id;
IPC::new_client_connection<Clipboard::ClientConnection>(client_socket.release_nonnull(), client_id);
IPC::new_client_connection<Clipboard::ClientConnection>(move(client_socket), client_id);
};
Clipboard::Storage::the().on_content_change = [&] {

View file

@ -21,15 +21,10 @@ ErrorOr<int> serenity_main(Main::Arguments)
bool ok = server->take_over_from_system_server();
VERIFY(ok);
server->on_ready_to_accept = [&] {
auto client_socket = server->accept();
if (!client_socket) {
dbgln("ConfigServer: accept failed.");
return;
}
server->on_accept = [&](auto client_socket) {
static int s_next_client_id = 0;
int client_id = ++s_next_client_id;
IPC::new_client_connection<ConfigServer::ClientConnection>(client_socket.release_nonnull(), client_id);
IPC::new_client_connection<ConfigServer::ClientConnection>(move(client_socket), client_id);
};
return event_loop.exec();

View file

@ -22,30 +22,19 @@ int main(int, char**)
bool ok = server->take_over_from_system_server("/tmp/portal/inspector");
VERIFY(ok);
server->on_ready_to_accept = [&] {
auto client_socket = server->accept();
if (!client_socket) {
dbgln("accept failed.");
return;
}
server->on_accept = [&](auto client_socket) {
static int s_next_client_id = 0;
int client_id = ++s_next_client_id;
IPC::new_client_connection<InspectorServer::ClientConnection>(client_socket.release_nonnull(), client_id);
IPC::new_client_connection<InspectorServer::ClientConnection>(move(client_socket), client_id);
};
auto inspectables_server = Core::LocalServer::construct();
if (!inspectables_server->take_over_from_system_server("/tmp/portal/inspectables"))
VERIFY_NOT_REACHED();
inspectables_server->on_ready_to_accept = [&] {
auto client_socket = inspectables_server->accept();
if (!client_socket) {
dbgln("backdoor accept failed.");
return;
}
inspectables_server->on_accept = [&](auto client_socket) {
auto pid = client_socket->peer_pid();
InspectorServer::g_processes.set(pid, make<InspectorServer::InspectableProcess>(pid, client_socket.release_nonnull()));
InspectorServer::g_processes.set(pid, make<InspectorServer::InspectableProcess>(pid, move(client_socket)));
};
return event_loop.exec();

View file

@ -29,15 +29,10 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv)
bool ok = server->take_over_from_system_server();
VERIFY(ok);
server->on_ready_to_accept = [&] {
auto client_socket = server->accept();
if (!client_socket) {
dbgln("LaunchServer: accept failed.");
return;
}
server->on_accept = [&](auto client_socket) {
static int s_next_client_id = 0;
int client_id = ++s_next_client_id;
IPC::new_client_connection<LaunchServer::ClientConnection>(client_socket.release_nonnull(), client_id);
IPC::new_client_connection<LaunchServer::ClientConnection>(move(client_socket), client_id);
};
return event_loop.exec();

View file

@ -73,15 +73,10 @@ LookupServer::LookupServer()
m_mdns = MulticastDNS::construct(this);
m_local_server = Core::LocalServer::construct(this);
m_local_server->on_ready_to_accept = [this]() {
auto socket = m_local_server->accept();
if (!socket) {
dbgln("Failed to accept a client connection");
return;
}
m_local_server->on_accept = [this](auto client_socket) {
static int s_next_client_id = 0;
int client_id = ++s_next_client_id;
IPC::new_client_connection<ClientConnection>(socket.release_nonnull(), client_id);
IPC::new_client_connection<ClientConnection>(move(client_socket), client_id);
};
bool ok = m_local_server->take_over_from_system_server();
VERIFY(ok);

View file

@ -20,15 +20,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
bool ok = server->take_over_from_system_server();
VERIFY(ok);
server->on_ready_to_accept = [&] {
auto client_socket = server->accept();
if (!client_socket) {
dbgln("NotificationServer: accept failed.");
return;
}
server->on_accept = [&](auto client_socket) {
static int s_next_client_id = 0;
int client_id = ++s_next_client_id;
IPC::new_client_connection<NotificationServer::ClientConnection>(client_socket.release_nonnull(), client_id);
IPC::new_client_connection<NotificationServer::ClientConnection>(move(client_socket), client_id);
};
TRY(Core::System::unveil("/res", "r"));

View file

@ -37,15 +37,10 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv)
bool ok = server->take_over_from_system_server();
VERIFY(ok);
server->on_ready_to_accept = [&] {
auto client_socket = server->accept();
if (!client_socket) {
dbgln("SQLServer: accept failed.");
return;
}
server->on_accept = [&](auto client_socket) {
static int s_next_client_id = 0;
int client_id = ++s_next_client_id;
IPC::new_client_connection<SQLServer::ClientConnection>(client_socket.release_nonnull(), client_id);
IPC::new_client_connection<SQLServer::ClientConnection>(client_socket, client_id);
};
return event_loop.exec();

View file

@ -30,26 +30,16 @@ EventLoop::EventLoop()
ok = m_wm_server->take_over_from_system_server("/tmp/portal/wm");
VERIFY(ok);
m_window_server->on_ready_to_accept = [this] {
auto client_socket = m_window_server->accept();
if (!client_socket) {
dbgln("WindowServer: accept failed.");
return;
}
m_window_server->on_accept = [&](auto client_socket) {
static int s_next_client_id = 0;
int client_id = ++s_next_client_id;
IPC::new_client_connection<ClientConnection>(client_socket.release_nonnull(), client_id);
IPC::new_client_connection<ClientConnection>(move(client_socket), client_id);
};
m_wm_server->on_ready_to_accept = [this] {
auto client_socket = m_wm_server->accept();
if (!client_socket) {
dbgln("WindowServer: WM accept failed.");
return;
}
m_wm_server->on_accept = [&](auto client_socket) {
static int s_next_wm_id = 0;
int wm_id = ++s_next_wm_id;
IPC::new_client_connection<WMClientConnection>(client_socket.release_nonnull(), wm_id);
IPC::new_client_connection<WMClientConnection>(move(client_socket), wm_id);
};
if (m_keyboard_fd >= 0) {