editor_debugger_server_websocket.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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) 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_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 = Ref<WebSocketPeer>(WebSocketPeer::create());
  40. ERR_FAIL_COND(peer.is_null()); // Bug.
  41. Vector<String> ws_protocols;
  42. ws_protocols.push_back("binary"); // Compatibility for emscripten TCP-to-WebSocket.
  43. peer->set_supported_protocols(ws_protocols);
  44. Error err = peer->accept_stream(tcp_server->take_connection());
  45. if (err == OK) {
  46. pending_timer = OS::get_singleton()->get_ticks_msec();
  47. pending_peer = peer;
  48. }
  49. }
  50. if (pending_peer.is_valid() && pending_peer->get_ready_state() != WebSocketPeer::STATE_OPEN) {
  51. pending_peer->poll();
  52. WebSocketPeer::State ready_state = pending_peer->get_ready_state();
  53. if (ready_state != WebSocketPeer::STATE_CONNECTING && ready_state != WebSocketPeer::STATE_OPEN) {
  54. pending_peer.unref(); // Failed.
  55. }
  56. if (ready_state == WebSocketPeer::STATE_CONNECTING && OS::get_singleton()->get_ticks_msec() - pending_timer > 3000) {
  57. pending_peer.unref(); // Timeout.
  58. }
  59. }
  60. }
  61. String EditorDebuggerServerWebSocket::get_uri() const {
  62. return endpoint;
  63. }
  64. Error EditorDebuggerServerWebSocket::start(const String &p_uri) {
  65. // Default host and port
  66. String bind_host = (String)EDITOR_GET("network/debug/remote_host");
  67. int bind_port = (int)EDITOR_GET("network/debug/remote_port");
  68. // Optionally override
  69. if (!p_uri.is_empty() && p_uri != "ws://") {
  70. String scheme, path, fragment;
  71. Error err = p_uri.parse_url(scheme, bind_host, bind_port, path, fragment);
  72. ERR_FAIL_COND_V(err != OK, ERR_INVALID_PARAMETER);
  73. ERR_FAIL_COND_V(!bind_host.is_valid_ip_address() && bind_host != "*", ERR_INVALID_PARAMETER);
  74. }
  75. // Try listening on ports
  76. const int max_attempts = 5;
  77. for (int attempt = 1;; ++attempt) {
  78. const Error err = tcp_server->listen(bind_port, bind_host);
  79. if (err == OK) {
  80. break;
  81. }
  82. if (attempt >= max_attempts) {
  83. EditorNode::get_log()->add_message(vformat("Cannot listen on port %d, remote debugging unavailable.", bind_port), EditorLog::MSG_TYPE_ERROR);
  84. return err;
  85. }
  86. int last_port = bind_port++;
  87. EditorNode::get_log()->add_message(vformat("Cannot listen on port %d, trying %d instead.", last_port, bind_port), EditorLog::MSG_TYPE_WARNING);
  88. }
  89. // Endpoint that the client should connect to
  90. endpoint = vformat("ws://%s:%d", bind_host, bind_port);
  91. return OK;
  92. }
  93. void EditorDebuggerServerWebSocket::stop() {
  94. pending_peer.unref();
  95. tcp_server->stop();
  96. }
  97. bool EditorDebuggerServerWebSocket::is_active() const {
  98. return tcp_server->is_listening();
  99. }
  100. bool EditorDebuggerServerWebSocket::is_connection_available() const {
  101. return pending_peer.is_valid() && pending_peer->get_ready_state() == WebSocketPeer::STATE_OPEN;
  102. }
  103. Ref<RemoteDebuggerPeer> EditorDebuggerServerWebSocket::take_connection() {
  104. ERR_FAIL_COND_V(!is_connection_available(), Ref<RemoteDebuggerPeer>());
  105. RemoteDebuggerPeer *peer = memnew(RemoteDebuggerPeerWebSocket(pending_peer));
  106. pending_peer.unref();
  107. return peer;
  108. }
  109. EditorDebuggerServerWebSocket::EditorDebuggerServerWebSocket() {
  110. tcp_server.instantiate();
  111. }
  112. EditorDebuggerServerWebSocket::~EditorDebuggerServerWebSocket() {
  113. stop();
  114. }
  115. EditorDebuggerServer *EditorDebuggerServerWebSocket::create(const String &p_protocol) {
  116. ERR_FAIL_COND_V(p_protocol != "ws://", nullptr);
  117. return memnew(EditorDebuggerServerWebSocket);
  118. }
  119. #endif // TOOLS_ENABLED