Debian Codegraph @

modules/websocket/editor/editor_debugger_server_websocket.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* editor_debugger_server_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 "editor_debugger_server_websocket.h"
32
33 #include "../remote_debugger_peer_websocket.h"
34
35 #include "editor/editor_log.h"
36 #include "editor/editor_node.h"
37 #include "editor/settings/editor_settings.h"
38
39 void EditorDebuggerServerWebSocket::poll() {
40 if (pending_peer.is_null() && tcp_server->is_connection_available()) {
41 Ref<WebSocketPeer> peer = Ref<WebSocketPeer>(WebSocketPeer::create());
42 ERR_FAIL_COND(peer.is_null()); // Bug.
43
44 Vector<String> ws_protocols;
45 ws_protocols.push_back("binary"); // Compatibility for emscripten TCP-to-WebSocket.
46 peer->set_supported_protocols(ws_protocols);
47
48 Error err = peer->accept_stream(tcp_server->take_connection());
49 if (err == OK) {
50 pending_timer = OS::get_singleton()->get_ticks_msec();
51 pending_peer = peer;
52 }
53 }
54 if (pending_peer.is_valid() && pending_peer->get_ready_state() != WebSocketPeer::STATE_OPEN) {
55 pending_peer->poll();
56 WebSocketPeer::State ready_state = pending_peer->get_ready_state();
57 if (ready_state != WebSocketPeer::STATE_CONNECTING && ready_state != WebSocketPeer::STATE_OPEN) {
58 pending_peer.unref(); // Failed.
59 }
60 if (ready_state == WebSocketPeer::STATE_CONNECTING && OS::get_singleton()->get_ticks_msec() - pending_timer > 3000) {
61 pending_peer.unref(); // Timeout.
62 }
63 }
64 }
65
66 String EditorDebuggerServerWebSocket::get_uri() const {
67 return endpoint;
68 }
69
70 Error EditorDebuggerServerWebSocket::start(const String &p_uri) {
71 // Default host and port
72 String bind_host = (String)EDITOR_GET("network/debug/remote_host");
73 int bind_port = (int)EDITOR_GET("network/debug/remote_port");
74
75 // Optionally override
76 if (!p_uri.is_empty() && p_uri != "ws://") {
77 String scheme, path, fragment;
78 Error err = p_uri.parse_url(scheme, bind_host, bind_port, path, fragment);
79 ERR_FAIL_COND_V(err != OK, ERR_INVALID_PARAMETER);
80 ERR_FAIL_COND_V(!bind_host.is_valid_ip_address() && bind_host != "*", ERR_INVALID_PARAMETER);
81 }
82
83 // Try listening on ports
84 const int max_attempts = 5;
85 for (int attempt = 1;; ++attempt) {
86 const Error err = tcp_server->listen(bind_port, bind_host);
87 if (err == OK) {
88 break;
89 }
90 if (attempt >= max_attempts) {
91 EditorNode::get_log()->add_message(vformat("Cannot listen on port %d, remote debugging unavailable.", bind_port), EditorLog::MSG_TYPE_ERROR);
92 return err;
93 }
94 int last_port = bind_port++;
95 EditorNode::get_log()->add_message(vformat("Cannot listen on port %d, trying %d instead.", last_port, bind_port), EditorLog::MSG_TYPE_WARNING);
96 }
97
98 // Endpoint that the client should connect to
99 endpoint = vformat("ws://%s:%d", bind_host, bind_port);
100
101 return OK;
102 }
103
104 void EditorDebuggerServerWebSocket::stop() {
105 pending_peer.unref();
106 tcp_server->stop();
107 }
108
109 bool EditorDebuggerServerWebSocket::is_active() const {
110 return tcp_server->is_listening();
111 }
112
113 bool EditorDebuggerServerWebSocket::is_connection_available() const {
114 return pending_peer.is_valid() && pending_peer->get_ready_state() == WebSocketPeer::STATE_OPEN;
115 }
116
117 Ref<RemoteDebuggerPeer> EditorDebuggerServerWebSocket::take_connection() {
118 ERR_FAIL_COND_V(!is_connection_available(), Ref<RemoteDebuggerPeer>());
119 RemoteDebuggerPeer *peer = memnew(RemoteDebuggerPeerWebSocket(pending_peer));
120 pending_peer.unref();
121 return peer;
122 }
123
124 EditorDebuggerServerWebSocket::EditorDebuggerServerWebSocket() {
125 tcp_server.instantiate();
126 }
127
128 EditorDebuggerServerWebSocket::~EditorDebuggerServerWebSocket() {
129 stop();
130 }
131
132 EditorDebuggerServer *EditorDebuggerServerWebSocket::create(const String &p_protocol) {
133 ERR_FAIL_COND_V(p_protocol != "ws://", nullptr);
134 return memnew(EditorDebuggerServerWebSocket);
135 }