| 1 | /**************************************************************************/ |
| 2 | /* editor_debugger_server.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.h" |
| 32 | |
| 33 | #include "core/io/marshalls.h" |
| 34 | #include "core/io/tcp_server.h" |
| 35 | #include "core/os/mutex.h" |
| 36 | #include "core/os/thread.h" |
| 37 | #include "editor/editor_log.h" |
| 38 | #include "editor/editor_node.h" |
| 39 | #include "editor/editor_settings.h" |
| 40 | |
| 41 | class EditorDebuggerServerTCP : public EditorDebuggerServer { |
| 42 | private: |
| 43 | Ref<TCPServer> server; |
| 44 | String endpoint; |
| 45 | |
| 46 | public: |
| 47 | static EditorDebuggerServer *create(const String &p_protocol); |
| 48 | |
| 49 | virtual void poll() override {} |
| 50 | virtual String get_uri() const override; |
| 51 | virtual Error start(const String &p_uri) override; |
| 52 | virtual void stop() override; |
| 53 | virtual bool is_active() const override; |
| 54 | virtual bool is_connection_available() const override; |
| 55 | virtual Ref<RemoteDebuggerPeer> take_connection() override; |
| 56 | |
| 57 | EditorDebuggerServerTCP(); |
| 58 | }; |
| 59 | |
| 60 | EditorDebuggerServer *EditorDebuggerServerTCP::create(const String &p_protocol) { |
| 61 | ERR_FAIL_COND_V(p_protocol != "tcp://" , nullptr); |
| 62 | return memnew(EditorDebuggerServerTCP); |
| 63 | } |
| 64 | |
| 65 | EditorDebuggerServerTCP::EditorDebuggerServerTCP() { |
| 66 | server.instantiate(); |
| 67 | } |
| 68 | |
| 69 | String EditorDebuggerServerTCP::get_uri() const { |
| 70 | return endpoint; |
| 71 | } |
| 72 | |
| 73 | Error EditorDebuggerServerTCP::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 != "tcp://" ) { |
| 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 = 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("tcp://%s:%d" , bind_host, bind_port); |
| 103 | |
| 104 | return OK; |
| 105 | } |
| 106 | |
| 107 | void EditorDebuggerServerTCP::stop() { |
| 108 | server->stop(); |
| 109 | } |
| 110 | |
| 111 | bool EditorDebuggerServerTCP::is_active() const { |
| 112 | return server->is_listening(); |
| 113 | } |
| 114 | |
| 115 | bool EditorDebuggerServerTCP::is_connection_available() const { |
| 116 | return server->is_listening() && server->is_connection_available(); |
| 117 | } |
| 118 | |
| 119 | Ref<RemoteDebuggerPeer> EditorDebuggerServerTCP::take_connection() { |
| 120 | ERR_FAIL_COND_V(!is_connection_available(), Ref<RemoteDebuggerPeer>()); |
| 121 | return memnew(RemoteDebuggerPeerTCP(server->take_connection())); |
| 122 | } |
| 123 | |
| 124 | /// EditorDebuggerServer |
| 125 | HashMap<StringName, EditorDebuggerServer::CreateServerFunc> EditorDebuggerServer::protocols; |
| 126 | |
| 127 | EditorDebuggerServer *EditorDebuggerServer::create(const String &p_protocol) { |
| 128 | ERR_FAIL_COND_V(!protocols.has(p_protocol), nullptr); |
| 129 | return protocols[p_protocol](p_protocol); |
| 130 | } |
| 131 | |
| 132 | void EditorDebuggerServer::register_protocol_handler(const String &p_protocol, CreateServerFunc p_func) { |
| 133 | ERR_FAIL_COND(protocols.has(p_protocol)); |
| 134 | protocols[p_protocol] = p_func; |
| 135 | } |
| 136 | |
| 137 | void EditorDebuggerServer::initialize() { |
| 138 | register_protocol_handler("tcp://" , EditorDebuggerServerTCP::create); |
| 139 | } |
| 140 | |
| 141 | void EditorDebuggerServer::deinitialize() { |
| 142 | protocols.clear(); |
| 143 | } |
| 144 | |