editor_debugger_server.cpp 5.6 KB

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