ConnectionFromClient.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <LibIPC/Connection.h>
  9. #include <LibIPC/Stub.h>
  10. namespace IPC {
  11. template<typename T, class... Args>
  12. NonnullRefPtr<T> new_client_connection(Args&&... args)
  13. {
  14. return T::construct(forward<Args>(args)...) /* arghs */;
  15. }
  16. template<typename ClientEndpoint, typename ServerEndpoint>
  17. class ConnectionFromClient : public Connection<ServerEndpoint, ClientEndpoint>
  18. , public ServerEndpoint::Stub
  19. , public ClientEndpoint::template Proxy<ServerEndpoint> {
  20. public:
  21. using ServerStub = typename ServerEndpoint::Stub;
  22. using IPCProxy = typename ClientEndpoint::template Proxy<ServerEndpoint>;
  23. ConnectionFromClient(ServerStub& stub, NonnullOwnPtr<Core::LocalSocket> socket, int client_id)
  24. : IPC::Connection<ServerEndpoint, ClientEndpoint>(stub, move(socket))
  25. , ClientEndpoint::template Proxy<ServerEndpoint>(*this, {})
  26. , m_client_id(client_id)
  27. {
  28. VERIFY(this->socket().is_open());
  29. this->socket().on_ready_to_read = [this] {
  30. // FIXME: Do something about errors.
  31. (void)this->drain_messages_from_peer();
  32. };
  33. }
  34. virtual ~ConnectionFromClient() override = default;
  35. void did_misbehave()
  36. {
  37. dbgln("{} (id={}) misbehaved, disconnecting.", *this, m_client_id);
  38. this->shutdown();
  39. }
  40. void did_misbehave(char const* message)
  41. {
  42. dbgln("{} (id={}) misbehaved ({}), disconnecting.", *this, m_client_id, message);
  43. this->shutdown();
  44. }
  45. virtual void shutdown_with_error(Error const& error) override
  46. {
  47. dbgln("{} (id={}) had an error ({}), disconnecting.", *this, m_client_id, error);
  48. this->shutdown();
  49. }
  50. int client_id() const { return m_client_id; }
  51. virtual void die() override = 0;
  52. private:
  53. int m_client_id { -1 };
  54. };
  55. }
  56. template<typename ClientEndpoint, typename ServerEndpoint>
  57. struct AK::Formatter<IPC::ConnectionFromClient<ClientEndpoint, ServerEndpoint>> : Formatter<Core::EventReceiver> {
  58. };