websocket_peer.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**************************************************************************/
  2. /* websocket_peer.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef WEBSOCKET_PEER_H
  31. #define WEBSOCKET_PEER_H
  32. #include "core/crypto/crypto.h"
  33. #include "core/error/error_list.h"
  34. #include "core/io/packet_peer.h"
  35. class WebSocketPeer : public PacketPeer {
  36. GDCLASS(WebSocketPeer, PacketPeer);
  37. public:
  38. enum State {
  39. STATE_CONNECTING,
  40. STATE_OPEN,
  41. STATE_CLOSING,
  42. STATE_CLOSED
  43. };
  44. enum WriteMode {
  45. WRITE_MODE_TEXT,
  46. WRITE_MODE_BINARY,
  47. };
  48. enum {
  49. DEFAULT_BUFFER_SIZE = 65535,
  50. };
  51. private:
  52. virtual Error _send_bind(const PackedByteArray &p_data, WriteMode p_mode = WRITE_MODE_BINARY);
  53. protected:
  54. static WebSocketPeer *(*_create)(bool p_notify_postinitialize);
  55. static void _bind_methods();
  56. Vector<String> supported_protocols;
  57. Vector<String> handshake_headers;
  58. Vector<String> _get_supported_protocols() const;
  59. Vector<String> _get_handshake_headers() const;
  60. int outbound_buffer_size = DEFAULT_BUFFER_SIZE;
  61. int inbound_buffer_size = DEFAULT_BUFFER_SIZE;
  62. int max_queued_packets = 4096;
  63. uint64_t heartbeat_interval_msec = 0;
  64. public:
  65. static WebSocketPeer *create(bool p_notify_postinitialize = true) {
  66. if (!_create) {
  67. return nullptr;
  68. }
  69. return _create(p_notify_postinitialize);
  70. }
  71. virtual Error connect_to_url(const String &p_url, Ref<TLSOptions> p_options = Ref<TLSOptions>()) = 0;
  72. virtual Error accept_stream(Ref<StreamPeer> p_stream) = 0;
  73. virtual Error send(const uint8_t *p_buffer, int p_buffer_size, WriteMode p_mode) = 0;
  74. virtual void close(int p_code = 1000, String p_reason = "") = 0;
  75. virtual IPAddress get_connected_host() const = 0;
  76. virtual uint16_t get_connected_port() const = 0;
  77. virtual bool was_string_packet() const = 0;
  78. virtual void set_no_delay(bool p_enabled) = 0;
  79. virtual int get_current_outbound_buffered_amount() const = 0;
  80. virtual String get_selected_protocol() const = 0;
  81. virtual String get_requested_url() const = 0;
  82. virtual void poll() = 0;
  83. virtual State get_ready_state() const = 0;
  84. virtual int get_close_code() const = 0;
  85. virtual String get_close_reason() const = 0;
  86. Error send_text(const String &p_text);
  87. void set_supported_protocols(const Vector<String> &p_protocols);
  88. const Vector<String> get_supported_protocols() const;
  89. void set_handshake_headers(const Vector<String> &p_headers);
  90. const Vector<String> get_handshake_headers() const;
  91. void set_outbound_buffer_size(int p_buffer_size);
  92. int get_outbound_buffer_size() const;
  93. void set_inbound_buffer_size(int p_buffer_size);
  94. int get_inbound_buffer_size() const;
  95. void set_max_queued_packets(int p_max_queued_packets);
  96. int get_max_queued_packets() const;
  97. double get_heartbeat_interval() const;
  98. void set_heartbeat_interval(double p_interval);
  99. WebSocketPeer();
  100. ~WebSocketPeer();
  101. };
  102. VARIANT_ENUM_CAST(WebSocketPeer::WriteMode);
  103. VARIANT_ENUM_CAST(WebSocketPeer::State);
  104. #endif // WEBSOCKET_PEER_H