ClientConnection.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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] {
  28. // FIXME: Do something about errors.
  29. (void)this->drain_messages_from_peer();
  30. };
  31. }
  32. virtual ~ClientConnection() override
  33. {
  34. }
  35. void did_misbehave()
  36. {
  37. dbgln("{} (id={}) misbehaved, disconnecting.", *this, m_client_id);
  38. this->shutdown();
  39. }
  40. void did_misbehave(const char* message)
  41. {
  42. dbgln("{} (id={}) misbehaved ({}), disconnecting.", *this, m_client_id, message);
  43. this->shutdown();
  44. }
  45. void shutdown_with_error(Error const& error)
  46. {
  47. dbgln("{} (id={}) had 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::ClientConnection<ClientEndpoint, ServerEndpoint>> : Formatter<Core::Object> {
  58. };