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