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