editor_debugger_server.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #include "editor_debugger_server.h"
  31. #include "core/io/tcp_server.h"
  32. #include "core/io/uds_server.h"
  33. #include "core/os/thread.h"
  34. #include "editor/editor_log.h"
  35. #include "editor/editor_node.h"
  36. #include "editor/settings/editor_settings.h"
  37. template <typename T>
  38. class EditorDebuggerServerSocket : public EditorDebuggerServer {
  39. GDSOFTCLASS(EditorDebuggerServerSocket, EditorDebuggerServer);
  40. protected:
  41. Ref<T> server;
  42. String endpoint;
  43. public:
  44. virtual void poll() override {}
  45. virtual String get_uri() const override;
  46. virtual void stop() override;
  47. virtual bool is_active() const override;
  48. virtual bool is_connection_available() const override;
  49. virtual Ref<RemoteDebuggerPeer> take_connection() override;
  50. EditorDebuggerServerSocket();
  51. };
  52. class EditorDebuggerServerTCP : public EditorDebuggerServerSocket<TCPServer> {
  53. public:
  54. static EditorDebuggerServer *create(const String &p_protocol);
  55. virtual Error start(const String &p_uri) override;
  56. };
  57. EditorDebuggerServer *EditorDebuggerServerTCP::create(const String &p_protocol) {
  58. ERR_FAIL_COND_V(p_protocol != "tcp://", nullptr);
  59. return memnew(EditorDebuggerServerTCP);
  60. }
  61. template <typename T>
  62. EditorDebuggerServerSocket<T>::EditorDebuggerServerSocket() {
  63. server.instantiate();
  64. }
  65. template <typename T>
  66. String EditorDebuggerServerSocket<T>::get_uri() const {
  67. return endpoint;
  68. }
  69. Error EditorDebuggerServerTCP::start(const String &p_uri) {
  70. // Default host and port
  71. String bind_host = (String)EDITOR_GET("network/debug/remote_host");
  72. int bind_port = (int)EDITOR_GET("network/debug/remote_port");
  73. // Optionally override
  74. if (!p_uri.is_empty() && p_uri != "tcp://") {
  75. String scheme, path, fragment;
  76. Error err = p_uri.parse_url(scheme, bind_host, bind_port, path, fragment);
  77. ERR_FAIL_COND_V(err != OK, ERR_INVALID_PARAMETER);
  78. ERR_FAIL_COND_V(!bind_host.is_valid_ip_address() && bind_host != "*", ERR_INVALID_PARAMETER);
  79. }
  80. // Try listening on ports
  81. const int max_attempts = 5;
  82. for (int attempt = 1;; ++attempt) {
  83. const Error err = server->listen(bind_port, bind_host);
  84. if (err == OK) {
  85. break;
  86. }
  87. if (attempt >= max_attempts) {
  88. EditorNode::get_log()->add_message(vformat("Cannot listen on port %d, remote debugging unavailable.", bind_port), EditorLog::MSG_TYPE_ERROR);
  89. return err;
  90. }
  91. int last_port = bind_port++;
  92. EditorNode::get_log()->add_message(vformat("Cannot listen on port %d, trying %d instead.", last_port, bind_port), EditorLog::MSG_TYPE_WARNING);
  93. }
  94. // Endpoint that the client should connect to
  95. endpoint = vformat("tcp://%s:%d", bind_host, bind_port);
  96. return OK;
  97. }
  98. template <typename T>
  99. void EditorDebuggerServerSocket<T>::stop() {
  100. server->stop();
  101. }
  102. template <typename T>
  103. bool EditorDebuggerServerSocket<T>::is_active() const {
  104. return server->is_listening();
  105. }
  106. template <typename T>
  107. bool EditorDebuggerServerSocket<T>::is_connection_available() const {
  108. return server->is_listening() && server->is_connection_available();
  109. }
  110. template <typename T>
  111. Ref<RemoteDebuggerPeer> EditorDebuggerServerSocket<T>::take_connection() {
  112. const Ref<RemoteDebuggerPeer> out;
  113. ERR_FAIL_COND_V(!is_connection_available(), out);
  114. Ref<StreamPeerSocket> stream = server->take_socket_connection();
  115. ERR_FAIL_COND_V(stream.is_null(), out);
  116. return memnew(RemoteDebuggerPeerTCP(stream));
  117. }
  118. class EditorDebuggerServerUDS : public EditorDebuggerServerSocket<UDSServer> {
  119. public:
  120. static EditorDebuggerServer *create(const String &p_protocol);
  121. virtual Error start(const String &p_uri) override;
  122. };
  123. EditorDebuggerServer *EditorDebuggerServerUDS::create(const String &p_protocol) {
  124. ERR_FAIL_COND_V(p_protocol != "unix://", nullptr);
  125. return memnew(EditorDebuggerServerUDS);
  126. }
  127. Error EditorDebuggerServerUDS::start(const String &p_uri) {
  128. String bind_path = p_uri.is_empty() ? String("/tmp/godot_debugger.sock") : p_uri.replace("unix://", "");
  129. const Error err = server->listen(bind_path);
  130. if (err != OK) {
  131. EditorNode::get_log()->add_message(vformat("Cannot listen at path %s, remote debugging unavailable.", bind_path), EditorLog::MSG_TYPE_ERROR);
  132. return err;
  133. }
  134. endpoint = "unix://" + bind_path;
  135. return OK;
  136. }
  137. /// EditorDebuggerServer
  138. HashMap<StringName, EditorDebuggerServer::CreateServerFunc> EditorDebuggerServer::protocols;
  139. EditorDebuggerServer *EditorDebuggerServer::create(const String &p_protocol) {
  140. CreateServerFunc *create_fn = protocols.getptr(p_protocol);
  141. ERR_FAIL_NULL_V(create_fn, nullptr);
  142. return (*create_fn)(p_protocol);
  143. }
  144. void EditorDebuggerServer::register_protocol_handler(const String &p_protocol, CreateServerFunc p_func) {
  145. ERR_FAIL_COND(protocols.has(p_protocol));
  146. protocols[p_protocol] = p_func;
  147. }
  148. void EditorDebuggerServer::initialize() {
  149. register_protocol_handler("tcp://", EditorDebuggerServerTCP::create);
  150. #if defined(UNIX_ENABLED)
  151. register_protocol_handler("unix://", EditorDebuggerServerUDS::create);
  152. #endif
  153. }
  154. void EditorDebuggerServer::deinitialize() {
  155. protocols.clear();
  156. }