editor_debugger_server_websocket.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "editor/editor_log.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_settings.h"
  36. void EditorDebuggerServerWebSocket::poll() {
  37. if (pending_peer.is_null() && tcp_server->is_connection_available()) {
  38. Ref<WebSocketPeer> peer = Ref<WebSocketPeer>(WebSocketPeer::create());
  39. ERR_FAIL_COND(peer.is_null()); // Bug.
  40. Vector<String> ws_protocols;
  41. ws_protocols.push_back("binary"); // Compatibility for emscripten TCP-to-WebSocket.
  42. peer->set_supported_protocols(ws_protocols);
  43. Error err = peer->accept_stream(tcp_server->take_connection());
  44. if (err == OK) {
  45. pending_timer = OS::get_singleton()->get_ticks_msec();
  46. pending_peer = peer;
  47. }
  48. }
  49. if (pending_peer.is_valid() && pending_peer->get_ready_state() != WebSocketPeer::STATE_OPEN) {
  50. pending_peer->poll();
  51. WebSocketPeer::State ready_state = pending_peer->get_ready_state();
  52. if (ready_state != WebSocketPeer::STATE_CONNECTING && ready_state != WebSocketPeer::STATE_OPEN) {
  53. pending_peer.unref(); // Failed.
  54. }
  55. if (ready_state == WebSocketPeer::STATE_CONNECTING && OS::get_singleton()->get_ticks_msec() - pending_timer > 3000) {
  56. pending_peer.unref(); // Timeout.
  57. }
  58. }
  59. }
  60. String EditorDebuggerServerWebSocket::get_uri() const {
  61. return endpoint;
  62. }
  63. Error EditorDebuggerServerWebSocket::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 != "ws://") {
  69. String scheme, path, fragment;
  70. Error err = p_uri.parse_url(scheme, bind_host, bind_port, path, fragment);
  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 = tcp_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("ws://%s:%d", bind_host, bind_port);
  90. return OK;
  91. }
  92. void EditorDebuggerServerWebSocket::stop() {
  93. pending_peer.unref();
  94. tcp_server->stop();
  95. }
  96. bool EditorDebuggerServerWebSocket::is_active() const {
  97. return tcp_server->is_listening();
  98. }
  99. bool EditorDebuggerServerWebSocket::is_connection_available() const {
  100. return pending_peer.is_valid() && pending_peer->get_ready_state() == WebSocketPeer::STATE_OPEN;
  101. }
  102. Ref<RemoteDebuggerPeer> EditorDebuggerServerWebSocket::take_connection() {
  103. ERR_FAIL_COND_V(!is_connection_available(), Ref<RemoteDebuggerPeer>());
  104. RemoteDebuggerPeer *peer = memnew(RemoteDebuggerPeerWebSocket(pending_peer));
  105. pending_peer.unref();
  106. return peer;
  107. }
  108. EditorDebuggerServerWebSocket::EditorDebuggerServerWebSocket() {
  109. tcp_server.instantiate();
  110. }
  111. EditorDebuggerServerWebSocket::~EditorDebuggerServerWebSocket() {
  112. stop();
  113. }
  114. EditorDebuggerServer *EditorDebuggerServerWebSocket::create(const String &p_protocol) {
  115. ERR_FAIL_COND_V(p_protocol != "ws://", nullptr);
  116. return memnew(EditorDebuggerServerWebSocket);
  117. }
  118. #endif // TOOLS_ENABLED