editor_debugger_server_websocket.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. #include "core/config/project_settings.h"
  32. #include "editor/editor_log.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_settings.h"
  35. #include "modules/websocket/remote_debugger_peer_websocket.h"
  36. void EditorDebuggerServerWebSocket::_peer_connected(int p_id, String _protocol) {
  37. pending_peers.push_back(p_id);
  38. }
  39. void EditorDebuggerServerWebSocket::_peer_disconnected(int p_id, bool p_was_clean) {
  40. if (pending_peers.find(p_id)) {
  41. pending_peers.erase(p_id);
  42. }
  43. }
  44. void EditorDebuggerServerWebSocket::poll() {
  45. server->poll();
  46. }
  47. String EditorDebuggerServerWebSocket::get_uri() const {
  48. return endpoint;
  49. }
  50. Error EditorDebuggerServerWebSocket::start(const String &p_uri) {
  51. // Default host and port
  52. String bind_host = (String)EditorSettings::get_singleton()->get("network/debug/remote_host");
  53. int bind_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port");
  54. // Optionally override
  55. if (!p_uri.is_empty() && p_uri != "ws://") {
  56. String scheme, path;
  57. Error err = p_uri.parse_url(scheme, bind_host, bind_port, path);
  58. ERR_FAIL_COND_V(err != OK, ERR_INVALID_PARAMETER);
  59. ERR_FAIL_COND_V(!bind_host.is_valid_ip_address() && bind_host != "*", ERR_INVALID_PARAMETER);
  60. }
  61. // Set up the server
  62. server->set_bind_ip(bind_host);
  63. Vector<String> compatible_protocols;
  64. compatible_protocols.push_back("binary"); // compatibility with EMSCRIPTEN TCP-to-WebSocket layer.
  65. // Try listening on ports
  66. const int max_attempts = 5;
  67. for (int attempt = 1;; ++attempt) {
  68. const Error err = server->listen(bind_port, compatible_protocols);
  69. if (err == OK) {
  70. break;
  71. }
  72. if (attempt >= max_attempts) {
  73. EditorNode::get_log()->add_message(vformat("Cannot listen on port %d, remote debugging unavailable.", bind_port), EditorLog::MSG_TYPE_ERROR);
  74. return err;
  75. }
  76. int last_port = bind_port++;
  77. EditorNode::get_log()->add_message(vformat("Cannot listen on port %d, trying %d instead.", last_port, bind_port), EditorLog::MSG_TYPE_WARNING);
  78. }
  79. // Endpoint that the client should connect to
  80. endpoint = vformat("ws://%s:%d", bind_host, bind_port);
  81. return OK;
  82. }
  83. void EditorDebuggerServerWebSocket::stop() {
  84. server->stop();
  85. pending_peers.clear();
  86. }
  87. bool EditorDebuggerServerWebSocket::is_active() const {
  88. return server->is_listening();
  89. }
  90. bool EditorDebuggerServerWebSocket::is_connection_available() const {
  91. return pending_peers.size() > 0;
  92. }
  93. Ref<RemoteDebuggerPeer> EditorDebuggerServerWebSocket::take_connection() {
  94. ERR_FAIL_COND_V(!is_connection_available(), Ref<RemoteDebuggerPeer>());
  95. RemoteDebuggerPeer *peer = memnew(RemoteDebuggerPeerWebSocket(server->get_peer(pending_peers[0])));
  96. pending_peers.pop_front();
  97. return peer;
  98. }
  99. EditorDebuggerServerWebSocket::EditorDebuggerServerWebSocket() {
  100. server = Ref<WebSocketServer>(WebSocketServer::create());
  101. int max_pkts = (int)GLOBAL_GET("network/limits/debugger/max_queued_messages");
  102. server->set_buffers(8192, max_pkts, 8192, max_pkts);
  103. server->connect("client_connected", callable_mp(this, &EditorDebuggerServerWebSocket::_peer_connected));
  104. server->connect("client_disconnected", callable_mp(this, &EditorDebuggerServerWebSocket::_peer_disconnected));
  105. }
  106. EditorDebuggerServerWebSocket::~EditorDebuggerServerWebSocket() {
  107. stop();
  108. }
  109. EditorDebuggerServer *EditorDebuggerServerWebSocket::create(const String &p_protocol) {
  110. ERR_FAIL_COND_V(p_protocol != "ws://", nullptr);
  111. return memnew(EditorDebuggerServerWebSocket);
  112. }