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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
/**************************************************************************/
/* emws_peer.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifdef WEB_ENABLED
#include "emws_peer.h"
#include "core/io/ip.h"
void EMWSPeer::_esws_on_connect(void *p_obj, char *p_proto) {
EMWSPeer *peer = static_cast<EMWSPeer *>(p_obj);
peer->ready_state = STATE_OPEN;
peer->selected_protocol.parse_utf8(p_proto);
}
void EMWSPeer::_esws_on_message(void *p_obj, const uint8_t *p_data, int p_data_size, int p_is_string) {
EMWSPeer *peer = static_cast<EMWSPeer *>(p_obj);
uint8_t is_string = p_is_string ? 1 : 0;
peer->in_buffer.write_packet(p_data, p_data_size, &is_string);
}
void EMWSPeer::_esws_on_error(void *p_obj) {
EMWSPeer *peer = static_cast<EMWSPeer *>(p_obj);
peer->ready_state = STATE_CLOSED;
}
void EMWSPeer::_esws_on_close(void *p_obj, int p_code, const char *p_reason, int p_was_clean) {
EMWSPeer *peer = static_cast<EMWSPeer *>(p_obj);
peer->close_code = p_code;
peer->close_reason.parse_utf8(p_reason);
peer->ready_state = STATE_CLOSED;
}
Error EMWSPeer::connect_to_url(const String &p_url, bool p_verify_tls, Ref<X509Certificate> p_tls_certificate) {
ERR_FAIL_COND_V(ready_state != STATE_CLOSED, ERR_ALREADY_IN_USE);
_clear();
String host;
String path;
String scheme;
int port = 0;
Error err = p_url.parse_url(scheme, host, port, path);
ERR_FAIL_COND_V_MSG(err != OK, err, "Invalid URL: " + p_url);
if (scheme.is_empty()) {
scheme = "ws://";
}
ERR_FAIL_COND_V_MSG(scheme != "ws://" && scheme != "wss://", ERR_INVALID_PARAMETER, vformat("Invalid protocol: \"%s\" (must be either \"ws://\" or \"wss://\").", scheme));
String proto_string;
for (int i = 0; i < supported_protocols.size(); i++) {
if (i != 0) {
proto_string += ",";
}
proto_string += supported_protocols[i];
}
if (handshake_headers.size()) {
WARN_PRINT_ONCE("Custom headers are not supported in Web platform.");
}
if (p_tls_certificate.is_valid()) {
WARN_PRINT_ONCE("Custom SSL certificates are not supported in Web platform.");
}
requested_url = scheme + host;
if (port && ((scheme == "ws://" && port != 80) || (scheme == "wss://" && port != 443))) {
requested_url += ":" + String::num(port);
}
if (!path.is_empty()) {
requested_url += path;
}
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);
if (peer_sock == -1) {
return FAILED;
}
in_buffer.resize(nearest_shift(inbound_buffer_size), max_queued_packets);
packet_buffer.resize(inbound_buffer_size);
ready_state = STATE_CONNECTING;
return OK;
}
Error EMWSPeer::accept_stream(Ref<StreamPeer> p_stream) {
WARN_PRINT_ONCE("Acting as WebSocket server is not supported in Web platforms.");
return ERR_UNAVAILABLE;
}
Error EMWSPeer::_send(const uint8_t *p_buffer, int p_buffer_size, bool p_binary) {
ERR_FAIL_COND_V(outbound_buffer_size > 0 && (get_current_outbound_buffered_amount() + p_buffer_size >= outbound_buffer_size), ERR_OUT_OF_MEMORY);
if (godot_js_websocket_send(peer_sock, p_buffer, p_buffer_size, p_binary ? 1 : 0) != 0) {
return FAILED;
}
return OK;
}
Error EMWSPeer::send(const uint8_t *p_buffer, int p_buffer_size, WriteMode p_mode) {
return _send(p_buffer, p_buffer_size, p_mode == WRITE_MODE_BINARY);
}
Error EMWSPeer::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
return _send(p_buffer, p_buffer_size, true);
}
Error EMWSPeer::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
if (in_buffer.packets_left() == 0) {
return ERR_UNAVAILABLE;
}
int read = 0;
Error err = in_buffer.read_packet(packet_buffer.ptrw(), packet_buffer.size(), &was_string, read);
ERR_FAIL_COND_V(err != OK, err);
*r_buffer = packet_buffer.ptr();
r_buffer_size = read;
return OK;
}
int EMWSPeer::get_available_packet_count() const {
return in_buffer.packets_left();
}
int EMWSPeer::get_current_outbound_buffered_amount() const {
if (peer_sock != -1) {
return godot_js_websocket_buffered_amount(peer_sock);
}
return 0;
}
bool EMWSPeer::was_string_packet() const {
return was_string;
}
void EMWSPeer::_clear() {
if (peer_sock != -1) {
godot_js_websocket_destroy(peer_sock);
peer_sock = -1;
}
ready_state = STATE_CLOSED;
was_string = 0;
close_code = -1;
close_reason.clear();
selected_protocol.clear();
requested_url.clear();
in_buffer.clear();
packet_buffer.clear();
}
void EMWSPeer::close(int p_code, String p_reason) {
if (p_code < 0) {
if (peer_sock != -1) {
godot_js_websocket_destroy(peer_sock);
peer_sock = -1;
}
ready_state = STATE_CLOSED;
}
if (ready_state == STATE_CONNECTING || ready_state == STATE_OPEN) {
ready_state = STATE_CLOSING;
if (peer_sock != -1) {
godot_js_websocket_close(peer_sock, p_code, p_reason.utf8().get_data());
} else {
ready_state = STATE_CLOSED;
}
}
in_buffer.clear();
packet_buffer.clear();
}
void EMWSPeer::poll() {
// Automatically polled by the navigator.
}
WebSocketPeer::State EMWSPeer::get_ready_state() const {
return ready_state;
}
int EMWSPeer::get_close_code() const {
return close_code;
}
String EMWSPeer::get_close_reason() const {
return close_reason;
}
String EMWSPeer::get_selected_protocol() const {
return selected_protocol;
}
String EMWSPeer::get_requested_url() const {
return requested_url;
}
IPAddress EMWSPeer::get_connected_host() const {
ERR_FAIL_V_MSG(IPAddress(), "Not supported in Web export.");
}
uint16_t EMWSPeer::get_connected_port() const {
ERR_FAIL_V_MSG(0, "Not supported in Web export.");
}
void EMWSPeer::set_no_delay(bool p_enabled) {
ERR_FAIL_MSG("'set_no_delay' is not supported in Web export.");
}
EMWSPeer::EMWSPeer() {
}
EMWSPeer::~EMWSPeer() {
_clear();
}
#endif // WEB_ENABLED
|