WebSocket.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2021, Dex♪ <dexes.ttp@gmail.com>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Span.h>
  9. #include <LibCore/Object.h>
  10. #include <LibWebSocket/ConnectionInfo.h>
  11. #include <LibWebSocket/Impl/WebSocketImpl.h>
  12. #include <LibWebSocket/Message.h>
  13. namespace WebSocket {
  14. enum class ReadyState {
  15. Connecting = 0,
  16. Open = 1,
  17. Closing = 2,
  18. Closed = 3,
  19. };
  20. class WebSocket final : public Core::Object {
  21. C_OBJECT(WebSocket)
  22. public:
  23. static NonnullRefPtr<WebSocket> create(ConnectionInfo, RefPtr<WebSocketImpl> = nullptr);
  24. virtual ~WebSocket() override = default;
  25. URL const& url() const { return m_connection.url(); }
  26. ReadyState ready_state();
  27. // Call this to start the WebSocket connection.
  28. void start();
  29. // This can only be used if the `ready_state` is `ReadyState::Open`
  30. void send(Message const&);
  31. // This can only be used if the `ready_state` is `ReadyState::Open`
  32. void close(u16 code = 1005, String const& reason = {});
  33. Function<void()> on_open;
  34. Function<void(u16 code, String reason, bool was_clean)> on_close;
  35. Function<void(Message message)> on_message;
  36. enum class Error {
  37. CouldNotEstablishConnection,
  38. ConnectionUpgradeFailed,
  39. ServerClosedSocket,
  40. };
  41. Function<void(Error)> on_error;
  42. private:
  43. WebSocket(ConnectionInfo, RefPtr<WebSocketImpl>);
  44. // As defined in section 5.2
  45. enum class OpCode : u8 {
  46. Continuation = 0x0,
  47. Text = 0x1,
  48. Binary = 0x2,
  49. ConnectionClose = 0x8,
  50. Ping = 0x9,
  51. Pong = 0xA,
  52. };
  53. void drain_read();
  54. void send_client_handshake();
  55. void read_server_handshake();
  56. void read_frame();
  57. void send_frame(OpCode, ReadonlyBytes, bool is_final);
  58. void notify_open();
  59. void notify_close(u16 code, String reason, bool was_clean);
  60. void notify_error(Error);
  61. void notify_message(Message);
  62. void fatal_error(Error);
  63. void discard_connection();
  64. enum class InternalState {
  65. NotStarted,
  66. EstablishingProtocolConnection,
  67. SendingClientHandshake,
  68. WaitingForServerHandshake,
  69. Open,
  70. Closing,
  71. Closed,
  72. Errored,
  73. };
  74. InternalState m_state { InternalState::NotStarted };
  75. String m_websocket_key;
  76. bool m_has_read_server_handshake_first_line { false };
  77. bool m_has_read_server_handshake_upgrade { false };
  78. bool m_has_read_server_handshake_connection { false };
  79. bool m_has_read_server_handshake_accept { false };
  80. u16 m_last_close_code { 1005 };
  81. String m_last_close_message;
  82. ConnectionInfo m_connection;
  83. RefPtr<WebSocketImpl> m_impl;
  84. Vector<u8> m_buffered_data;
  85. };
  86. }