ClientConnection.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 <LibIPC/Connection.h>
  8. namespace IPC {
  9. template<typename T, class... Args>
  10. NonnullRefPtr<T> new_client_connection(Args&&... args)
  11. {
  12. return T::construct(forward<Args>(args)...) /* arghs */;
  13. }
  14. template<typename ClientEndpoint, typename ServerEndpoint>
  15. class ClientConnection : public Connection<ServerEndpoint, ClientEndpoint>
  16. , public ServerEndpoint::Stub
  17. , public ClientEndpoint::template Proxy<ServerEndpoint> {
  18. public:
  19. using ServerStub = typename ServerEndpoint::Stub;
  20. using IPCProxy = typename ClientEndpoint::template Proxy<ServerEndpoint>;
  21. ClientConnection(ServerStub& stub, NonnullRefPtr<Core::LocalSocket> socket, int client_id)
  22. : IPC::Connection<ServerEndpoint, ClientEndpoint>(stub, move(socket))
  23. , ClientEndpoint::template Proxy<ServerEndpoint>(*this, {})
  24. , m_client_id(client_id)
  25. {
  26. VERIFY(this->socket().is_connected());
  27. this->socket().on_ready_to_read = [this] { this->drain_messages_from_peer(); };
  28. }
  29. virtual ~ClientConnection() override
  30. {
  31. }
  32. void did_misbehave()
  33. {
  34. dbgln("{} (id={}) misbehaved, disconnecting.", *this, m_client_id);
  35. this->shutdown();
  36. }
  37. void did_misbehave(const char* message)
  38. {
  39. dbgln("{} (id={}) misbehaved ({}), disconnecting.", *this, m_client_id, message);
  40. this->shutdown();
  41. }
  42. int client_id() const { return m_client_id; }
  43. virtual void die() = 0;
  44. private:
  45. int m_client_id { -1 };
  46. };
  47. }
  48. template<typename ClientEndpoint, typename ServerEndpoint>
  49. struct AK::Formatter<IPC::ClientConnection<ClientEndpoint, ServerEndpoint>> : Formatter<Core::Object> {
  50. };