/src/serenity/Userland/Libraries/LibIPC/ConnectionToServer.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <LibCore/SessionManagement.h> |
10 | | #include <LibIPC/Connection.h> |
11 | | |
12 | | namespace IPC { |
13 | | |
14 | | #define IPC_CLIENT_CONNECTION(klass, socket_path) \ |
15 | | C_OBJECT_ABSTRACT(klass) \ |
16 | | public: \ |
17 | | template<typename Klass = klass, class... Args> \ |
18 | | static ErrorOr<NonnullRefPtr<klass>> try_create(Args&&... args) \ |
19 | | { \ |
20 | | auto parsed_socket_path = TRY(Core::SessionManagement::parse_path_with_sid(socket_path)); \ |
21 | | auto socket = TRY(Core::LocalSocket::connect(move(parsed_socket_path))); \ |
22 | | /* We want to rate-limit our clients */ \ |
23 | | TRY(socket->set_blocking(true)); \ |
24 | | \ |
25 | | return adopt_nonnull_ref_or_enomem(new (nothrow) Klass(move(socket), forward<Args>(args)...)); \ |
26 | | } |
27 | | |
28 | | template<typename ClientEndpoint, typename ServerEndpoint> |
29 | | class ConnectionToServer : public IPC::Connection<ClientEndpoint, ServerEndpoint> |
30 | | , public ClientEndpoint::Stub |
31 | | , public ServerEndpoint::template Proxy<ClientEndpoint> { |
32 | | public: |
33 | | using ClientStub = typename ClientEndpoint::Stub; |
34 | | using IPCProxy = typename ServerEndpoint::template Proxy<ClientEndpoint>; |
35 | | |
36 | | ConnectionToServer(ClientStub& local_endpoint, NonnullOwnPtr<Core::LocalSocket> socket) |
37 | 0 | : Connection<ClientEndpoint, ServerEndpoint>(local_endpoint, move(socket)) |
38 | 0 | , ServerEndpoint::template Proxy<ClientEndpoint>(*this, {}) |
39 | 0 | { |
40 | 0 | } |
41 | | |
42 | | virtual void die() override |
43 | 0 | { |
44 | | // Override this function if you don't want your app to exit if it loses the connection. |
45 | 0 | exit(0); |
46 | 0 | } |
47 | | }; |
48 | | |
49 | | } |