WebSocketClient.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2021, Dex♪ <dexes.ttp@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibProtocol/WebSocket.h>
  7. #include <LibProtocol/WebSocketClient.h>
  8. namespace Protocol {
  9. WebSocketClient::WebSocketClient()
  10. : IPC::ServerConnection<WebSocketClientEndpoint, WebSocketServerEndpoint>(*this, "/tmp/portal/websocket")
  11. {
  12. handshake();
  13. }
  14. void WebSocketClient::handshake()
  15. {
  16. greet();
  17. }
  18. RefPtr<WebSocket> WebSocketClient::connect(const URL& url, const String& origin, const Vector<String>& protocols, const Vector<String>& extensions, const HashMap<String, String>& request_headers)
  19. {
  20. IPC::Dictionary header_dictionary;
  21. for (auto& it : request_headers)
  22. header_dictionary.add(it.key, it.value);
  23. auto connection_id = IPCProxy::connect(url, origin, protocols, extensions, header_dictionary);
  24. if (connection_id < 0)
  25. return nullptr;
  26. auto connection = WebSocket::create_from_id({}, *this, connection_id);
  27. m_connections.set(connection_id, connection);
  28. return connection;
  29. }
  30. u32 WebSocketClient::ready_state(Badge<WebSocket>, WebSocket& connection)
  31. {
  32. if (!m_connections.contains(connection.id()))
  33. return (u32)WebSocket::ReadyState::Closed;
  34. return IPCProxy::ready_state(connection.id());
  35. }
  36. void WebSocketClient::send(Badge<WebSocket>, WebSocket& connection, ByteBuffer data, bool is_text)
  37. {
  38. if (!m_connections.contains(connection.id()))
  39. return;
  40. async_send(connection.id(), is_text, move(data));
  41. }
  42. void WebSocketClient::close(Badge<WebSocket>, WebSocket& connection, u16 code, String message)
  43. {
  44. if (!m_connections.contains(connection.id()))
  45. return;
  46. async_close(connection.id(), code, move(message));
  47. }
  48. bool WebSocketClient::set_certificate(Badge<WebSocket>, WebSocket& connection, String certificate, String key)
  49. {
  50. if (!m_connections.contains(connection.id()))
  51. return false;
  52. return IPCProxy::set_certificate(connection.id(), move(certificate), move(key));
  53. }
  54. void WebSocketClient::connected(i32 connection_id)
  55. {
  56. auto maybe_connection = m_connections.get(connection_id);
  57. if (maybe_connection.has_value())
  58. maybe_connection.value()->did_open({});
  59. }
  60. void WebSocketClient::received(i32 connection_id, bool is_text, ByteBuffer const& data)
  61. {
  62. auto maybe_connection = m_connections.get(connection_id);
  63. if (maybe_connection.has_value())
  64. maybe_connection.value()->did_receive({}, data, is_text);
  65. }
  66. void WebSocketClient::errored(i32 connection_id, i32 message)
  67. {
  68. auto maybe_connection = m_connections.get(connection_id);
  69. if (maybe_connection.has_value())
  70. maybe_connection.value()->did_error({}, message);
  71. }
  72. void WebSocketClient::closed(i32 connection_id, u16 code, String const& reason, bool clean)
  73. {
  74. auto maybe_connection = m_connections.get(connection_id);
  75. if (maybe_connection.has_value())
  76. maybe_connection.value()->did_close({}, code, reason, clean);
  77. }
  78. void WebSocketClient::certificate_requested(i32 connection_id)
  79. {
  80. auto maybe_connection = m_connections.get(connection_id);
  81. if (maybe_connection.has_value())
  82. maybe_connection.value()->did_request_certificates({});
  83. }
  84. }