| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/crypto/crypto.h" |
| 34 |
#include "core/io/packet_peer.h" |
| 35 |
|
| 36 |
class WebSocketPeer : public PacketPeer { |
| 37 |
GDCLASS(WebSocketPeer, PacketPeer); |
| 38 |
|
| 39 |
public: |
| 40 |
enum State { |
| 41 |
STATE_CONNECTING, |
| 42 |
STATE_OPEN, |
| 43 |
STATE_CLOSING, |
| 44 |
STATE_CLOSED |
| 45 |
}; |
| 46 |
|
| 47 |
enum WriteMode { |
| 48 |
WRITE_MODE_TEXT, |
| 49 |
WRITE_MODE_BINARY, |
| 50 |
}; |
| 51 |
|
| 52 |
enum { |
| 53 |
DEFAULT_BUFFER_SIZE = 65535, |
| 54 |
}; |
| 55 |
|
| 56 |
private: |
| 57 |
virtual Error _send_bind(const PackedByteArray &p_data, WriteMode p_mode = WRITE_MODE_BINARY); |
| 58 |
|
| 59 |
protected: |
| 60 |
static WebSocketPeer *(*_create)(bool p_notify_postinitialize); |
| 61 |
|
| 62 |
static void _bind_methods(); |
| 63 |
|
| 64 |
Vector<String> supported_protocols; |
| 65 |
Vector<String> handshake_headers; |
| 66 |
|
| 67 |
Vector<String> _get_supported_protocols() const; |
| 68 |
Vector<String> _get_handshake_headers() const; |
| 69 |
|
| 70 |
int outbound_buffer_size = DEFAULT_BUFFER_SIZE; |
| 71 |
int inbound_buffer_size = DEFAULT_BUFFER_SIZE; |
| 72 |
int max_queued_packets = 4096; |
| 73 |
uint64_t heartbeat_interval_msec = 0; |
| 74 |
|
| 75 |
public: |
| 76 |
static WebSocketPeer *create(bool p_notify_postinitialize = true) { |
| 77 |
if (!_create) { |
| 78 |
return nullptr; |
| 79 |
} |
| 80 |
return _create(p_notify_postinitialize); |
| 81 |
} |
| 82 |
|
| 83 |
virtual Error connect_to_url(const String &p_url, const Ref<TLSOptions> &p_options = Ref<TLSOptions>()) = 0; |
| 84 |
virtual Error accept_stream(const Ref<StreamPeer> &p_stream) = 0; |
| 85 |
|
| 86 |
virtual Error send(const uint8_t *p_buffer, int p_buffer_size, WriteMode p_mode) = 0; |
| 87 |
virtual void close(int p_code = 1000, const String &p_reason = "") = 0; |
| 88 |
|
| 89 |
virtual IPAddress get_connected_host() const = 0; |
| 90 |
virtual uint16_t get_connected_port() const = 0; |
| 91 |
virtual bool was_string_packet() const = 0; |
| 92 |
virtual void set_no_delay(bool p_enabled) = 0; |
| 93 |
virtual int get_current_outbound_buffered_amount() const = 0; |
| 94 |
virtual String get_selected_protocol() const = 0; |
| 95 |
virtual String get_requested_url() const = 0; |
| 96 |
|
| 97 |
virtual void poll() = 0; |
| 98 |
virtual State get_ready_state() const = 0; |
| 99 |
virtual int get_close_code() const = 0; |
| 100 |
virtual String get_close_reason() const = 0; |
| 101 |
|
| 102 |
Error send_text(const String &p_text); |
| 103 |
|
| 104 |
void set_supported_protocols(const Vector<String> &p_protocols); |
| 105 |
const Vector<String> get_supported_protocols() const; |
| 106 |
|
| 107 |
void set_handshake_headers(const Vector<String> &p_headers); |
| 108 |
const Vector<String> get_handshake_headers() const; |
| 109 |
|
| 110 |
void set_outbound_buffer_size(int p_buffer_size); |
| 111 |
int get_outbound_buffer_size() const; |
| 112 |
|
| 113 |
void set_inbound_buffer_size(int p_buffer_size); |
| 114 |
int get_inbound_buffer_size() const; |
| 115 |
|
| 116 |
void set_max_queued_packets(int p_max_queued_packets); |
| 117 |
int get_max_queued_packets() const; |
| 118 |
|
| 119 |
double get_heartbeat_interval() const; |
| 120 |
void set_heartbeat_interval(double p_interval); |
| 121 |
|
| 122 |
WebSocketPeer(); |
| 123 |
~WebSocketPeer(); |
| 124 |
}; |
| 125 |
|
| 126 |
VARIANT_ENUM_CAST(WebSocketPeer::WriteMode); |
| 127 |
VARIANT_ENUM_CAST(WebSocketPeer::State); |