Debian Codegraph @

modules/websocket/emws_peer.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* emws_peer.cpp */
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 #include "emws_peer.h"
32
33 #ifdef WEB_ENABLED
34
35 #include "core/io/ip.h"
36
37 void EMWSPeer::_esws_on_connect(void *p_obj, char *p_proto) {
38 EMWSPeer *peer = static_cast<EMWSPeer *>(p_obj);
39 peer->ready_state = STATE_OPEN;
40 peer->selected_protocol.clear();
41 peer->selected_protocol.append_utf8(p_proto);
42 }
43
44 void EMWSPeer::_esws_on_message(void *p_obj, const uint8_t *p_data, int p_data_size, int p_is_string) {
45 EMWSPeer *peer = static_cast<EMWSPeer *>(p_obj);
46 uint8_t is_string = p_is_string ? 1 : 0;
47 peer->in_buffer.write_packet(p_data, p_data_size, &is_string);
48 }
49
50 void EMWSPeer::_esws_on_error(void *p_obj) {
51 EMWSPeer *peer = static_cast<EMWSPeer *>(p_obj);
52 peer->ready_state = STATE_CLOSED;
53 }
54
55 void EMWSPeer::_esws_on_close(void *p_obj, int p_code, const char *p_reason, int p_was_clean) {
56 EMWSPeer *peer = static_cast<EMWSPeer *>(p_obj);
57 peer->close_code = p_code;
58 peer->close_reason.clear();
59 peer->close_reason.append_utf8(p_reason);
60 peer->ready_state = STATE_CLOSED;
61 }
62
63 Error EMWSPeer::connect_to_url(const String &p_url, const Ref<TLSOptions> &p_tls_options) {
64 ERR_FAIL_COND_V(p_url.is_empty(), ERR_INVALID_PARAMETER);
65 ERR_FAIL_COND_V(p_tls_options.is_valid() && p_tls_options->is_server(), ERR_INVALID_PARAMETER);
66 ERR_FAIL_COND_V(ready_state != STATE_CLOSED && ready_state != STATE_CLOSING, ERR_ALREADY_IN_USE);
67
68 _clear();
69
70 String host;
71 String path;
72 String scheme;
73 String fragment;
74 int port = 0;
75 Error err = p_url.parse_url(scheme, host, port, path, fragment);
76 ERR_FAIL_COND_V_MSG(err != OK, err, "Invalid URL: " + p_url);
77
78 if (scheme.is_empty()) {
79 scheme = "ws://";
80 }
81 ERR_FAIL_COND_V_MSG(scheme != "ws://" && scheme != "wss://", ERR_INVALID_PARAMETER, vformat("Invalid protocol: \"%s\" (must be either \"ws://\" or \"wss://\").", scheme));
82
83 String proto_string;
84 for (int i = 0; i < supported_protocols.size(); i++) {
85 if (i != 0) {
86 proto_string += ",";
87 }
88 proto_string += supported_protocols[i];
89 }
90
91 if (handshake_headers.size()) {
92 WARN_PRINT_ONCE("Custom headers are not supported in Web platform.");
93 }
94
95 requested_url = scheme + host;
96
97 if (port && ((scheme == "ws://" && port != 80) || (scheme == "wss://" && port != 443))) {
98 requested_url += ":" + String::num_int64(port);
99 }
100
101 if (!path.is_empty()) {
102 requested_url += path;
103 }
104
105 peer_sock = godot_js_websocket_create(this, requested_url.utf8().get_data(), proto_string.utf8().get_data(), &_esws_on_connect, &_esws_on_message, &_esws_on_error, &_esws_on_close);
106 if (peer_sock == -1) {
107 return FAILED;
108 }
109 in_buffer.resize(nearest_shift((uint32_t)inbound_buffer_size), max_queued_packets);
110 packet_buffer.resize(inbound_buffer_size);
111 ready_state = STATE_CONNECTING;
112 return OK;
113 }
114
115 Error EMWSPeer::accept_stream(const Ref<StreamPeer> &p_stream) {
116 WARN_PRINT_ONCE("Acting as WebSocket server is not supported in Web platforms.");
117 return ERR_UNAVAILABLE;
118 }
119
120 Error EMWSPeer::_send(const uint8_t *p_buffer, int p_buffer_size, bool p_binary) {
121 ERR_FAIL_COND_V(outbound_buffer_size > 0 && (get_current_outbound_buffered_amount() + p_buffer_size >= outbound_buffer_size), ERR_OUT_OF_MEMORY);
122
123 if (godot_js_websocket_send(peer_sock, p_buffer, p_buffer_size, p_binary ? 1 : 0) != 0) {
124 return FAILED;
125 }
126 return OK;
127 }
128
129 Error EMWSPeer::send(const uint8_t *p_buffer, int p_buffer_size, WriteMode p_mode) {
130 return _send(p_buffer, p_buffer_size, p_mode == WRITE_MODE_BINARY);
131 }
132
133 Error EMWSPeer::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
134 return _send(p_buffer, p_buffer_size, true);
135 }
136
137 Error EMWSPeer::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
138 if (in_buffer.packets_left() == 0) {
139 return ERR_UNAVAILABLE;
140 }
141
142 int read = 0;
143 Error err = in_buffer.read_packet(packet_buffer.ptrw(), packet_buffer.size(), &was_string, read);
144 ERR_FAIL_COND_V(err != OK, err);
145
146 *r_buffer = packet_buffer.ptr();
147 r_buffer_size = read;
148
149 return OK;
150 }
151
152 int EMWSPeer::get_available_packet_count() const {
153 return in_buffer.packets_left();
154 }
155
156 int EMWSPeer::get_current_outbound_buffered_amount() const {
157 if (peer_sock != -1) {
158 return godot_js_websocket_buffered_amount(peer_sock);
159 }
160 return 0;
161 }
162
163 bool EMWSPeer::was_string_packet() const {
164 return was_string;
165 }
166
167 void EMWSPeer::_clear() {
168 if (peer_sock != -1) {
169 godot_js_websocket_destroy(peer_sock);
170 peer_sock = -1;
171 }
172 ready_state = STATE_CLOSED;
173 was_string = 0;
174 close_code = -1;
175 close_reason.clear();
176 selected_protocol.clear();
177 requested_url.clear();
178 in_buffer.clear();
179 packet_buffer.clear();
180 }
181
182 void EMWSPeer::close(int p_code, const String &p_reason) {
183 if (p_code < 0) {
184 if (peer_sock != -1) {
185 godot_js_websocket_destroy(peer_sock);
186 peer_sock = -1;
187 }
188 ready_state = STATE_CLOSED;
189 }
190 if (ready_state == STATE_CONNECTING || ready_state == STATE_OPEN) {
191 ready_state = STATE_CLOSING;
192 if (peer_sock != -1) {
193 godot_js_websocket_close(peer_sock, p_code, p_reason.utf8().get_data());
194 } else {
195 ready_state = STATE_CLOSED;
196 }
197 }
198 in_buffer.clear();
199 packet_buffer.clear();
200 }
201
202 void EMWSPeer::poll() {
203 // Automatically polled by the navigator.
204 }
205
206 WebSocketPeer::State EMWSPeer::get_ready_state() const {
207 return ready_state;
208 }
209
210 int EMWSPeer::get_close_code() const {
211 return close_code;
212 }
213
214 String EMWSPeer::get_close_reason() const {
215 return close_reason;
216 }
217
218 String EMWSPeer::get_selected_protocol() const {
219 return selected_protocol;
220 }
221
222 String EMWSPeer::get_requested_url() const {
223 return requested_url;
224 }
225
226 IPAddress EMWSPeer::get_connected_host() const {
227 ERR_FAIL_V_MSG(IPAddress(), "Not supported in Web export.");
228 }
229
230 uint16_t EMWSPeer::get_connected_port() const {
231 ERR_FAIL_V_MSG(0, "Not supported in Web export.");
232 }
233
234 void EMWSPeer::set_no_delay(bool p_enabled) {
235 ERR_FAIL_MSG("'set_no_delay' is not supported in Web export.");
236 }
237
238 EMWSPeer::EMWSPeer() {
239 }
240
241 EMWSPeer::~EMWSPeer() {
242 _clear();
243 }
244
245 #endif // WEB_ENABLED