1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:28:12 +00:00

LibIPC+Services: Make ClientConnection take socket as NonnullRefPtr

This avoids getting into the awkward situation where the socket is
still part-owned by main() in multi-instance service. Also it just
reads nicer.
This commit is contained in:
Andreas Kling 2020-07-06 13:23:39 +02:00
parent f9d3055691
commit 94ddb07e58
25 changed files with 40 additions and 40 deletions

View file

@ -48,7 +48,7 @@ public:
Invalid = 2000, Invalid = 2000,
Disconnected, Disconnected,
}; };
Event() {} Event() { }
explicit Event(Type type) explicit Event(Type type)
: Core::Event(type) : Core::Event(type)
{ {
@ -78,19 +78,18 @@ NonnullRefPtr<T> new_client_connection(Args&&... args)
template<typename Endpoint> template<typename Endpoint>
class ClientConnection : public Core::Object { class ClientConnection : public Core::Object {
public: public:
ClientConnection(Endpoint& endpoint, Core::LocalSocket& socket, int client_id) ClientConnection(Endpoint& endpoint, NonnullRefPtr<Core::LocalSocket> socket, int client_id)
: m_endpoint(endpoint) : m_endpoint(endpoint)
, m_socket(socket) , m_socket(move(socket))
, m_client_id(client_id) , m_client_id(client_id)
{ {
ASSERT(socket.is_connected()); ASSERT(m_socket->is_connected());
ucred creds; ucred creds;
socklen_t creds_size = sizeof(creds); socklen_t creds_size = sizeof(creds);
if (getsockopt(m_socket->fd(), SOL_SOCKET, SO_PEERCRED, &creds, &creds_size) < 0) { if (getsockopt(m_socket->fd(), SOL_SOCKET, SO_PEERCRED, &creds, &creds_size) < 0) {
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }
m_client_pid = creds.pid; m_client_pid = creds.pid;
add_child(socket);
m_socket->on_ready_to_read = [this] { drain_messages_from_client(); }; m_socket->on_ready_to_read = [this] { drain_messages_from_client(); };
m_responsiveness_timer = Core::Timer::create_single_shot(3000, [this] { may_have_become_unresponsive(); }); m_responsiveness_timer = Core::Timer::create_single_shot(3000, [this] { may_have_become_unresponsive(); });
@ -100,8 +99,8 @@ public:
{ {
} }
virtual void may_have_become_unresponsive() {} virtual void may_have_become_unresponsive() { }
virtual void did_become_responsive() {} virtual void did_become_responsive() { }
void post_message(const Message& message) void post_message(const Message& message)
{ {
@ -223,7 +222,7 @@ protected:
private: private:
Endpoint& m_endpoint; Endpoint& m_endpoint;
RefPtr<Core::LocalSocket> m_socket; NonnullRefPtr<Core::LocalSocket> m_socket;
RefPtr<Core::Timer> m_responsiveness_timer; RefPtr<Core::Timer> m_responsiveness_timer;
int m_client_id { -1 }; int m_client_id { -1 };
int m_client_pid { -1 }; int m_client_pid { -1 };

View file

@ -50,8 +50,8 @@ void ClientConnection::for_each(Function<void(ClientConnection&)> callback)
callback(connection); callback(connection);
} }
ClientConnection::ClientConnection(Core::LocalSocket& client_socket, int client_id, Mixer& mixer) ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> client_socket, int client_id, Mixer& mixer)
: IPC::ClientConnection<AudioServerEndpoint>(*this, client_socket, client_id) : IPC::ClientConnection<AudioServerEndpoint>(*this, move(client_socket), client_id)
, m_mixer(mixer) , m_mixer(mixer)
{ {
s_connections.set(client_id, *this); s_connections.set(client_id, *this);

View file

@ -43,7 +43,7 @@ class ClientConnection final : public IPC::ClientConnection<AudioServerEndpoint>
, public AudioServerEndpoint { , public AudioServerEndpoint {
C_OBJECT(ClientConnection) C_OBJECT(ClientConnection)
public: public:
explicit ClientConnection(Core::LocalSocket&, int client_id, Mixer& mixer); explicit ClientConnection(NonnullRefPtr<Core::LocalSocket>, int client_id, Mixer& mixer);
~ClientConnection() override; ~ClientConnection() override;
void did_finish_playing_buffer(Badge<BufferQueue>, int buffer_id); void did_finish_playing_buffer(Badge<BufferQueue>, int buffer_id);

View file

@ -49,7 +49,7 @@ int main(int, char**)
} }
static int s_next_client_id = 0; static int s_next_client_id = 0;
int client_id = ++s_next_client_id; int client_id = ++s_next_client_id;
IPC::new_client_connection<AudioServer::ClientConnection>(*client_socket, client_id, mixer); IPC::new_client_connection<AudioServer::ClientConnection>(client_socket.release_nonnull(), client_id, mixer);
}; };
if (pledge("stdio thread shared_buffer accept", nullptr) < 0) { if (pledge("stdio thread shared_buffer accept", nullptr) < 0) {

View file

@ -41,8 +41,8 @@ void ClientConnection::for_each_client(Function<void(ClientConnection&)> callbac
} }
} }
ClientConnection::ClientConnection(Core::LocalSocket& socket, int client_id) ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
: IPC::ClientConnection<ClipboardServerEndpoint>(*this, socket, client_id) : IPC::ClientConnection<ClipboardServerEndpoint>(*this, move(socket), client_id)
{ {
s_connections.set(client_id, *this); s_connections.set(client_id, *this);
} }

View file

@ -37,7 +37,7 @@ class ClientConnection final : public IPC::ClientConnection<ClipboardServerEndpo
C_OBJECT(ClientConnection); C_OBJECT(ClientConnection);
public: public:
explicit ClientConnection(Core::LocalSocket&, int client_id); explicit ClientConnection(NonnullRefPtr<Core::LocalSocket>, int client_id);
virtual ~ClientConnection() override; virtual ~ClientConnection() override;
virtual void die() override; virtual void die() override;

View file

@ -63,7 +63,7 @@ int main(int, char**)
} }
static int s_next_client_id = 0; static int s_next_client_id = 0;
int client_id = ++s_next_client_id; int client_id = ++s_next_client_id;
IPC::new_client_connection<Clipboard::ClientConnection>(*client_socket, client_id); IPC::new_client_connection<Clipboard::ClientConnection>(client_socket.release_nonnull(), client_id);
}; };
Clipboard::Storage::the().on_content_change = [&] { Clipboard::Storage::the().on_content_change = [&] {

View file

@ -9,4 +9,4 @@ set(SOURCES
) )
serenity_bin(ImageDecoder) serenity_bin(ImageDecoder)
target_link_libraries(ImageDecoder LibIPC LibGfx) target_link_libraries(ImageDecoder LibGfx LibIPC)

View file

@ -36,8 +36,8 @@ namespace ImageDecoder {
static HashMap<int, RefPtr<ClientConnection>> s_connections; static HashMap<int, RefPtr<ClientConnection>> s_connections;
ClientConnection::ClientConnection(Core::LocalSocket& socket, int client_id) ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
: IPC::ClientConnection<ImageDecoderServerEndpoint>(*this, socket, client_id) : IPC::ClientConnection<ImageDecoderServerEndpoint>(*this, move(socket), client_id)
{ {
s_connections.set(client_id, *this); s_connections.set(client_id, *this);
} }

View file

@ -40,7 +40,7 @@ class ClientConnection final
C_OBJECT(ClientConnection); C_OBJECT(ClientConnection);
public: public:
explicit ClientConnection(Core::LocalSocket&, int client_id); explicit ClientConnection(NonnullRefPtr<Core::LocalSocket>, int client_id);
~ClientConnection() override; ~ClientConnection() override;
virtual void die() override; virtual void die() override;

View file

@ -42,7 +42,7 @@ int main(int, char**)
} }
auto socket = Core::LocalSocket::take_over_accepted_socket_from_system_server(); auto socket = Core::LocalSocket::take_over_accepted_socket_from_system_server();
IPC::new_client_connection<ImageDecoder::ClientConnection>(*socket, 1); IPC::new_client_connection<ImageDecoder::ClientConnection>(socket.release_nonnull(), 1);
if (pledge("stdio shared_buffer", nullptr) < 0) { if (pledge("stdio shared_buffer", nullptr) < 0) {
perror("pledge"); perror("pledge");
return 1; return 1;

View file

@ -33,8 +33,8 @@
namespace LaunchServer { namespace LaunchServer {
static HashMap<int, RefPtr<ClientConnection>> s_connections; static HashMap<int, RefPtr<ClientConnection>> s_connections;
ClientConnection::ClientConnection(Core::LocalSocket& client_socket, int client_id) ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> client_socket, int client_id)
: IPC::ClientConnection<LaunchServerEndpoint>(*this, client_socket, client_id) : IPC::ClientConnection<LaunchServerEndpoint>(*this, move(client_socket), client_id)
{ {
s_connections.set(client_id, *this); s_connections.set(client_id, *this);
} }

View file

@ -40,7 +40,7 @@ public:
virtual void die() override; virtual void die() override;
private: private:
explicit ClientConnection(Core::LocalSocket&, int client_id); explicit ClientConnection(NonnullRefPtr<Core::LocalSocket>, int client_id);
virtual OwnPtr<Messages::LaunchServer::GreetResponse> handle(const Messages::LaunchServer::Greet&) override; virtual OwnPtr<Messages::LaunchServer::GreetResponse> handle(const Messages::LaunchServer::Greet&) override;
virtual OwnPtr<Messages::LaunchServer::OpenURLResponse> handle(const Messages::LaunchServer::OpenURL&) override; virtual OwnPtr<Messages::LaunchServer::OpenURLResponse> handle(const Messages::LaunchServer::OpenURL&) override;

View file

@ -61,7 +61,7 @@ int main(int argc, char** argv)
static int s_next_client_id = 0; static int s_next_client_id = 0;
int client_id = ++s_next_client_id; int client_id = ++s_next_client_id;
dbg() << "Received connection"; dbg() << "Received connection";
IPC::new_client_connection<LaunchServer::ClientConnection>(*client_socket, client_id); IPC::new_client_connection<LaunchServer::ClientConnection>(client_socket.release_nonnull(), client_id);
}; };
return event_loop.exec(); return event_loop.exec();

View file

@ -33,8 +33,8 @@ namespace NotificationServer {
static HashMap<int, RefPtr<ClientConnection>> s_connections; static HashMap<int, RefPtr<ClientConnection>> s_connections;
ClientConnection::ClientConnection(Core::LocalSocket& client_socket, int client_id) ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> client_socket, int client_id)
: IPC::ClientConnection<NotificationServerEndpoint>(*this, client_socket, client_id) : IPC::ClientConnection<NotificationServerEndpoint>(*this, move(client_socket), client_id)
{ {
s_connections.set(client_id, *this); s_connections.set(client_id, *this);
} }

View file

@ -40,7 +40,7 @@ public:
virtual void die() override; virtual void die() override;
private: private:
explicit ClientConnection(Core::LocalSocket&, int client_id); explicit ClientConnection(NonnullRefPtr<Core::LocalSocket>, int client_id);
virtual OwnPtr<Messages::NotificationServer::GreetResponse> handle(const Messages::NotificationServer::Greet&) override; virtual OwnPtr<Messages::NotificationServer::GreetResponse> handle(const Messages::NotificationServer::Greet&) override;
virtual OwnPtr<Messages::NotificationServer::ShowNotificationResponse> handle(const Messages::NotificationServer::ShowNotification&) override; virtual OwnPtr<Messages::NotificationServer::ShowNotificationResponse> handle(const Messages::NotificationServer::ShowNotification&) override;

View file

@ -52,7 +52,7 @@ int main(int argc, char** argv)
} }
static int s_next_client_id = 0; static int s_next_client_id = 0;
int client_id = ++s_next_client_id; int client_id = ++s_next_client_id;
IPC::new_client_connection<NotificationServer::ClientConnection>(*client_socket, client_id); IPC::new_client_connection<NotificationServer::ClientConnection>(client_socket.release_nonnull(), client_id);
}; };
if (unveil("/res", "r") < 0) { if (unveil("/res", "r") < 0) {

View file

@ -35,8 +35,8 @@ namespace ProtocolServer {
static HashMap<int, RefPtr<ClientConnection>> s_connections; static HashMap<int, RefPtr<ClientConnection>> s_connections;
ClientConnection::ClientConnection(Core::LocalSocket& socket, int client_id) ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
: IPC::ClientConnection<ProtocolServerEndpoint>(*this, socket, client_id) : IPC::ClientConnection<ProtocolServerEndpoint>(*this, move(socket), client_id)
{ {
s_connections.set(client_id, *this); s_connections.set(client_id, *this);
} }

View file

@ -39,7 +39,7 @@ class ClientConnection final
C_OBJECT(ClientConnection); C_OBJECT(ClientConnection);
public: public:
explicit ClientConnection(Core::LocalSocket&, int client_id); explicit ClientConnection(NonnullRefPtr<Core::LocalSocket>, int client_id);
~ClientConnection() override; ~ClientConnection() override;
virtual void die() override; virtual void die() override;

View file

@ -36,8 +36,8 @@ namespace WebContent {
static HashMap<int, RefPtr<ClientConnection>> s_connections; static HashMap<int, RefPtr<ClientConnection>> s_connections;
ClientConnection::ClientConnection(Core::LocalSocket& socket, int client_id) ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
: IPC::ClientConnection<WebContentServerEndpoint>(*this, socket, client_id) : IPC::ClientConnection<WebContentServerEndpoint>(*this, move(socket), client_id)
, m_page_host(PageHost::create(*this)) , m_page_host(PageHost::create(*this))
{ {
s_connections.set(client_id, *this); s_connections.set(client_id, *this);

View file

@ -40,7 +40,7 @@ class ClientConnection final
C_OBJECT(ClientConnection); C_OBJECT(ClientConnection);
public: public:
explicit ClientConnection(Core::LocalSocket&, int client_id); explicit ClientConnection(NonnullRefPtr<Core::LocalSocket>, int client_id);
~ClientConnection() override; ~ClientConnection() override;
virtual void die() override; virtual void die() override;

View file

@ -54,6 +54,7 @@ int main(int, char**)
} }
auto socket = Core::LocalSocket::take_over_accepted_socket_from_system_server(); auto socket = Core::LocalSocket::take_over_accepted_socket_from_system_server();
IPC::new_client_connection<WebContent::ClientConnection>(*socket, 1); ASSERT(socket);
IPC::new_client_connection<WebContent::ClientConnection>(socket.release_nonnull(), 1);
return event_loop.exec(); return event_loop.exec();
} }

View file

@ -77,8 +77,8 @@ ClientConnection* ClientConnection::from_client_id(int client_id)
return (*it).value.ptr(); return (*it).value.ptr();
} }
ClientConnection::ClientConnection(Core::LocalSocket& client_socket, int client_id) ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> client_socket, int client_id)
: IPC::ClientConnection<WindowServerEndpoint>(*this, client_socket, client_id) : IPC::ClientConnection<WindowServerEndpoint>(*this, move(client_socket), client_id)
{ {
if (!s_connections) if (!s_connections)
s_connections = new HashMap<int, NonnullRefPtr<ClientConnection>>; s_connections = new HashMap<int, NonnullRefPtr<ClientConnection>>;

View file

@ -84,7 +84,7 @@ public:
void notify_display_link(Badge<Compositor>); void notify_display_link(Badge<Compositor>);
private: private:
explicit ClientConnection(Core::LocalSocket&, int client_id); explicit ClientConnection(NonnullRefPtr<Core::LocalSocket>, int client_id);
// ^ClientConnection // ^ClientConnection
virtual void die() override; virtual void die() override;

View file

@ -63,7 +63,7 @@ EventLoop::EventLoop()
} }
static int s_next_client_id = 0; static int s_next_client_id = 0;
int client_id = ++s_next_client_id; int client_id = ++s_next_client_id;
IPC::new_client_connection<ClientConnection>(*client_socket, client_id); IPC::new_client_connection<ClientConnection>(client_socket.release_nonnull(), client_id);
}; };
ASSERT(m_keyboard_fd >= 0); ASSERT(m_keyboard_fd >= 0);