editor_debugger_server.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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/marshalls.h"
  32. #include "core/io/tcp_server.h"
  33. #include "core/os/mutex.h"
  34. #include "core/os/thread.h"
  35. #include "editor/editor_log.h"
  36. #include "editor/editor_node.h"
  37. #include "editor/editor_settings.h"
  38. class EditorDebuggerServerTCP : public EditorDebuggerServer {
  39. private:
  40. Ref<TCPServer> server;
  41. String endpoint;
  42. public:
  43. static EditorDebuggerServer *create(const String &p_protocol);
  44. virtual void poll() override {}
  45. virtual String get_uri() const override;
  46. virtual Error start(const String &p_uri) override;
  47. virtual void stop() override;
  48. virtual bool is_active() const override;
  49. virtual bool is_connection_available() const override;
  50. virtual Ref<RemoteDebuggerPeer> take_connection() override;
  51. EditorDebuggerServerTCP();
  52. };
  53. EditorDebuggerServer *EditorDebuggerServerTCP::create(const String &p_protocol) {
  54. ERR_FAIL_COND_V(p_protocol != "tcp://", nullptr);
  55. return memnew(EditorDebuggerServerTCP);
  56. }
  57. EditorDebuggerServerTCP::EditorDebuggerServerTCP() {
  58. server.instantiate();
  59. }
  60. String EditorDebuggerServerTCP::get_uri() const {
  61. return endpoint;
  62. }
  63. Error EditorDebuggerServerTCP::start(const String &p_uri) {
  64. // Default host and port
  65. String bind_host = (String)EDITOR_GET("network/debug/remote_host");
  66. int bind_port = (int)EDITOR_GET("network/debug/remote_port");
  67. // Optionally override
  68. if (!p_uri.is_empty() && p_uri != "tcp://") {
  69. String scheme, path;
  70. Error err = p_uri.parse_url(scheme, bind_host, bind_port, path);
  71. ERR_FAIL_COND_V(err != OK, ERR_INVALID_PARAMETER);
  72. ERR_FAIL_COND_V(!bind_host.is_valid_ip_address() && bind_host != "*", ERR_INVALID_PARAMETER);
  73. }
  74. // Try listening on ports
  75. const int max_attempts = 5;
  76. for (int attempt = 1;; ++attempt) {
  77. const Error err = server->listen(bind_port, bind_host);
  78. if (err == OK) {
  79. break;
  80. }
  81. if (attempt >= max_attempts) {
  82. EditorNode::get_log()->add_message(vformat("Cannot listen on port %d, remote debugging unavailable.", bind_port), EditorLog::MSG_TYPE_ERROR);
  83. return err;
  84. }
  85. int last_port = bind_port++;
  86. EditorNode::get_log()->add_message(vformat("Cannot listen on port %d, trying %d instead.", last_port, bind_port), EditorLog::MSG_TYPE_WARNING);
  87. }
  88. // Endpoint that the client should connect to
  89. endpoint = vformat("tcp://%s:%d", bind_host, bind_port);
  90. return OK;
  91. }
  92. void EditorDebuggerServerTCP::stop() {
  93. server->stop();
  94. }
  95. bool EditorDebuggerServerTCP::is_active() const {
  96. return server->is_listening();
  97. }
  98. bool EditorDebuggerServerTCP::is_connection_available() const {
  99. return server->is_listening() && server->is_connection_available();
  100. }
  101. Ref<RemoteDebuggerPeer> EditorDebuggerServerTCP::take_connection() {
  102. ERR_FAIL_COND_V(!is_connection_available(), Ref<RemoteDebuggerPeer>());
  103. return memnew(RemoteDebuggerPeerTCP(server->take_connection()));
  104. }
  105. /// EditorDebuggerServer
  106. HashMap<StringName, EditorDebuggerServer::CreateServerFunc> EditorDebuggerServer::protocols;
  107. EditorDebuggerServer *EditorDebuggerServer::create(const String &p_protocol) {
  108. ERR_FAIL_COND_V(!protocols.has(p_protocol), nullptr);
  109. return protocols[p_protocol](p_protocol);
  110. }
  111. void EditorDebuggerServer::register_protocol_handler(const String &p_protocol, CreateServerFunc p_func) {
  112. ERR_FAIL_COND(protocols.has(p_protocol));
  113. protocols[p_protocol] = p_func;
  114. }
  115. void EditorDebuggerServer::initialize() {
  116. register_protocol_handler("tcp://", EditorDebuggerServerTCP::create);
  117. }
  118. void EditorDebuggerServer::deinitialize() {
  119. protocols.clear();
  120. }