WebSocketClient.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. }
  17. RefPtr<WebSocket> WebSocketClient::connect(const URL& url, const String& origin, const Vector<String>& protocols, const Vector<String>& extensions, const HashMap<String, String>& request_headers)
  18. {
  19. IPC::Dictionary header_dictionary;
  20. for (auto& it : request_headers)
  21. header_dictionary.add(it.key, it.value);
  22. auto connection_id = IPCProxy::connect(url, origin, protocols, extensions, header_dictionary);
  23. if (connection_id < 0)
  24. return nullptr;
  25. auto connection = WebSocket::create_from_id({}, *this, connection_id);
  26. m_connections.set(connection_id, connection);
  27. return connection;
  28. }
  29. u32 WebSocketClient::ready_state(Badge<WebSocket>, WebSocket& connection)
  30. {
  31. if (!m_connections.contains(connection.id()))
  32. return (u32)WebSocket::ReadyState::Closed;
  33. return IPCProxy::ready_state(connection.id());
  34. }
  35. void WebSocketClient::send(Badge<WebSocket>, WebSocket& connection, ByteBuffer data, bool is_text)
  36. {
  37. if (!m_connections.contains(connection.id()))
  38. return;
  39. async_send(connection.id(), is_text, move(data));
  40. }
  41. void WebSocketClient::close(Badge<WebSocket>, WebSocket& connection, u16 code, String message)
  42. {
  43. if (!m_connections.contains(connection.id()))
  44. return;
  45. async_close(connection.id(), code, move(message));
  46. }
  47. bool WebSocketClient::set_certificate(Badge<WebSocket>, WebSocket& connection, String certificate, String key)
  48. {
  49. if (!m_connections.contains(connection.id()))
  50. return false;
  51. return IPCProxy::set_certificate(connection.id(), move(certificate), move(key));
  52. }
  53. void WebSocketClient::connected(i32 connection_id)
  54. {
  55. auto maybe_connection = m_connections.get(connection_id);
  56. if (maybe_connection.has_value())
  57. maybe_connection.value()->did_open({});
  58. }
  59. void WebSocketClient::received(i32 connection_id, bool is_text, ByteBuffer const& data)
  60. {
  61. auto maybe_connection = m_connections.get(connection_id);
  62. if (maybe_connection.has_value())
  63. maybe_connection.value()->did_receive({}, data, is_text);
  64. }
  65. void WebSocketClient::errored(i32 connection_id, i32 message)
  66. {
  67. auto maybe_connection = m_connections.get(connection_id);
  68. if (maybe_connection.has_value())
  69. maybe_connection.value()->did_error({}, message);
  70. }
  71. void WebSocketClient::closed(i32 connection_id, u16 code, String const& reason, bool clean)
  72. {
  73. auto maybe_connection = m_connections.get(connection_id);
  74. if (maybe_connection.has_value())
  75. maybe_connection.value()->did_close({}, code, reason, clean);
  76. }
  77. void WebSocketClient::certificate_requested(i32 connection_id)
  78. {
  79. auto maybe_connection = m_connections.get(connection_id);
  80. if (maybe_connection.has_value())
  81. maybe_connection.value()->did_request_certificates({});
  82. }
  83. }