editor_debugger_server_websocket.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_websocket.h"
  31. #ifdef TOOLS_ENABLED
  32. #include "../remote_debugger_peer_websocket.h"
  33. #include "core/config/project_settings.h"
  34. #include "editor/editor_log.h"
  35. #include "editor/editor_node.h"
  36. #include "editor/editor_settings.h"
  37. void EditorDebuggerServerWebSocket::poll() {
  38. if (pending_peer.is_null() && tcp_server->is_connection_available()) {
  39. Ref<WebSocketPeer> peer;
  40. Error err = peer->accept_stream(tcp_server->take_connection());
  41. if (err == OK) {
  42. pending_timer = OS::get_singleton()->get_ticks_msec();
  43. pending_peer = peer;
  44. }
  45. }
  46. if (pending_peer.is_valid() && pending_peer->get_ready_state() != WebSocketPeer::STATE_OPEN) {
  47. pending_peer->poll();
  48. WebSocketPeer::State ready_state = pending_peer->get_ready_state();
  49. if (ready_state != WebSocketPeer::STATE_CONNECTING && ready_state != WebSocketPeer::STATE_OPEN) {
  50. pending_peer.unref(); // Failed.
  51. }
  52. if (ready_state == WebSocketPeer::STATE_CONNECTING && OS::get_singleton()->get_ticks_msec() - pending_timer > 3000) {
  53. pending_peer.unref(); // Timeout.
  54. }
  55. }
  56. }
  57. String EditorDebuggerServerWebSocket::get_uri() const {
  58. return endpoint;
  59. }
  60. Error EditorDebuggerServerWebSocket::start(const String &p_uri) {
  61. // Default host and port
  62. String bind_host = (String)EditorSettings::get_singleton()->get("network/debug/remote_host");
  63. int bind_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port");
  64. // Optionally override
  65. if (!p_uri.is_empty() && p_uri != "ws://") {
  66. String scheme, path;
  67. Error err = p_uri.parse_url(scheme, bind_host, bind_port, path);
  68. ERR_FAIL_COND_V(err != OK, ERR_INVALID_PARAMETER);
  69. ERR_FAIL_COND_V(!bind_host.is_valid_ip_address() && bind_host != "*", ERR_INVALID_PARAMETER);
  70. }
  71. // Try listening on ports
  72. const int max_attempts = 5;
  73. for (int attempt = 1;; ++attempt) {
  74. const Error err = tcp_server->listen(bind_port, bind_host);
  75. if (err == OK) {
  76. break;
  77. }
  78. if (attempt >= max_attempts) {
  79. EditorNode::get_log()->add_message(vformat("Cannot listen on port %d, remote debugging unavailable.", bind_port), EditorLog::MSG_TYPE_ERROR);
  80. return err;
  81. }
  82. int last_port = bind_port++;
  83. EditorNode::get_log()->add_message(vformat("Cannot listen on port %d, trying %d instead.", last_port, bind_port), EditorLog::MSG_TYPE_WARNING);
  84. }
  85. // Endpoint that the client should connect to
  86. endpoint = vformat("ws://%s:%d", bind_host, bind_port);
  87. return OK;
  88. }
  89. void EditorDebuggerServerWebSocket::stop() {
  90. pending_peer.unref();
  91. tcp_server->stop();
  92. }
  93. bool EditorDebuggerServerWebSocket::is_active() const {
  94. return tcp_server->is_listening();
  95. }
  96. bool EditorDebuggerServerWebSocket::is_connection_available() const {
  97. return pending_peer.is_valid() && pending_peer->get_ready_state() == WebSocketPeer::STATE_OPEN;
  98. }
  99. Ref<RemoteDebuggerPeer> EditorDebuggerServerWebSocket::take_connection() {
  100. ERR_FAIL_COND_V(!is_connection_available(), Ref<RemoteDebuggerPeer>());
  101. RemoteDebuggerPeer *peer = memnew(RemoteDebuggerPeerWebSocket(pending_peer));
  102. pending_peer.unref();
  103. return peer;
  104. }
  105. EditorDebuggerServerWebSocket::EditorDebuggerServerWebSocket() {
  106. tcp_server.instantiate();
  107. }
  108. EditorDebuggerServerWebSocket::~EditorDebuggerServerWebSocket() {
  109. stop();
  110. }
  111. EditorDebuggerServer *EditorDebuggerServerWebSocket::create(const String &p_protocol) {
  112. ERR_FAIL_COND_V(p_protocol != "ws://", nullptr);
  113. return memnew(EditorDebuggerServerWebSocket);
  114. }
  115. #endif // TOOLS_ENABLED