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