| 1 | /**************************************************************************/ |
| 2 | /* remote_debugger_peer_websocket.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 "remote_debugger_peer_websocket.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | |
| 35 | Error RemoteDebuggerPeerWebSocket::connect_to_host(const String &p_uri) { |
| 36 | ws_peer = Ref<WebSocketPeer>(WebSocketPeer::create()); |
| 37 | ERR_FAIL_COND_V(ws_peer.is_null(), ERR_BUG); |
| 38 | |
| 39 | Vector<String> protocols; |
| 40 | protocols.push_back("binary" ); // Compatibility for emscripten TCP-to-WebSocket. |
| 41 | |
| 42 | ws_peer->set_supported_protocols(protocols); |
| 43 | ws_peer->set_max_queued_packets(max_queued_messages); |
| 44 | ws_peer->set_inbound_buffer_size((1 << 23) - 1); |
| 45 | ws_peer->set_outbound_buffer_size((1 << 23) - 1); |
| 46 | |
| 47 | Error err = ws_peer->connect_to_url(p_uri); |
| 48 | ERR_FAIL_COND_V(err != OK, err); |
| 49 | |
| 50 | ws_peer->poll(); |
| 51 | WebSocketPeer::State ready_state = ws_peer->get_ready_state(); |
| 52 | if (ready_state != WebSocketPeer::STATE_CONNECTING && ready_state != WebSocketPeer::STATE_OPEN) { |
| 53 | ERR_PRINT(vformat("Remote Debugger: Unable to connect. State: %s." , ws_peer->get_ready_state())); |
| 54 | return FAILED; |
| 55 | } |
| 56 | |
| 57 | return OK; |
| 58 | } |
| 59 | |
| 60 | bool RemoteDebuggerPeerWebSocket::is_peer_connected() { |
| 61 | return ws_peer.is_valid() && (ws_peer->get_ready_state() == WebSocketPeer::STATE_OPEN || ws_peer->get_ready_state() == WebSocketPeer::STATE_CONNECTING); |
| 62 | } |
| 63 | |
| 64 | void RemoteDebuggerPeerWebSocket::poll() { |
| 65 | ERR_FAIL_COND(ws_peer.is_null()); |
| 66 | ws_peer->poll(); |
| 67 | |
| 68 | while (ws_peer->get_ready_state() == WebSocketPeer::STATE_OPEN && ws_peer->get_available_packet_count() > 0 && in_queue.size() < max_queued_messages) { |
| 69 | Variant var; |
| 70 | Error err = ws_peer->get_var(var); |
| 71 | ERR_CONTINUE(err != OK); |
| 72 | ERR_CONTINUE(var.get_type() != Variant::ARRAY); |
| 73 | in_queue.push_back(var); |
| 74 | } |
| 75 | |
| 76 | while (ws_peer->get_ready_state() == WebSocketPeer::STATE_OPEN && out_queue.size() > 0) { |
| 77 | Array var = out_queue[0]; |
| 78 | Error err = ws_peer->put_var(var); |
| 79 | ERR_BREAK(err != OK); // Peer buffer full? |
| 80 | out_queue.pop_front(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | int RemoteDebuggerPeerWebSocket::get_max_message_size() const { |
| 85 | ERR_FAIL_COND_V(ws_peer.is_null(), 0); |
| 86 | return ws_peer->get_max_packet_size(); |
| 87 | } |
| 88 | |
| 89 | bool RemoteDebuggerPeerWebSocket::has_message() { |
| 90 | return in_queue.size() > 0; |
| 91 | } |
| 92 | |
| 93 | Array RemoteDebuggerPeerWebSocket::get_message() { |
| 94 | ERR_FAIL_COND_V(in_queue.size() < 1, Array()); |
| 95 | Array msg = in_queue[0]; |
| 96 | in_queue.pop_front(); |
| 97 | return msg; |
| 98 | } |
| 99 | |
| 100 | Error RemoteDebuggerPeerWebSocket::put_message(const Array &p_arr) { |
| 101 | if (out_queue.size() >= max_queued_messages) { |
| 102 | return ERR_OUT_OF_MEMORY; |
| 103 | } |
| 104 | out_queue.push_back(p_arr); |
| 105 | return OK; |
| 106 | } |
| 107 | |
| 108 | void RemoteDebuggerPeerWebSocket::close() { |
| 109 | if (ws_peer.is_valid()) { |
| 110 | ws_peer.unref(); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | bool RemoteDebuggerPeerWebSocket::can_block() const { |
| 115 | #ifdef WEB_ENABLED |
| 116 | return false; |
| 117 | #else |
| 118 | return true; |
| 119 | #endif |
| 120 | } |
| 121 | |
| 122 | RemoteDebuggerPeerWebSocket::RemoteDebuggerPeerWebSocket(Ref<WebSocketPeer> p_peer) { |
| 123 | max_queued_messages = (int)GLOBAL_GET("network/limits/debugger/max_queued_messages" ); |
| 124 | ws_peer = p_peer; |
| 125 | if (ws_peer.is_valid()) { |
| 126 | ws_peer->set_max_queued_packets(max_queued_messages); |
| 127 | ws_peer->set_inbound_buffer_size((1 << 23) - 1); |
| 128 | ws_peer->set_outbound_buffer_size((1 << 23) - 1); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | RemoteDebuggerPeer *RemoteDebuggerPeerWebSocket::create(const String &p_uri) { |
| 133 | ERR_FAIL_COND_V(!p_uri.begins_with("ws://" ) && !p_uri.begins_with("wss://" ), nullptr); |
| 134 | RemoteDebuggerPeerWebSocket *peer = memnew(RemoteDebuggerPeerWebSocket); |
| 135 | Error err = peer->connect_to_host(p_uri); |
| 136 | if (err != OK) { |
| 137 | memdelete(peer); |
| 138 | return nullptr; |
| 139 | } |
| 140 | return peer; |
| 141 | } |
| 142 | |