ConnectionToServer.h 2.2 KB

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