editor_debugger_server_websocket.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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_settings.h"
  33. #include "modules/websocket/remote_debugger_peer_websocket.h"
  34. void EditorDebuggerServerWebSocket::_peer_connected(int p_id, String _protocol) {
  35. pending_peers.push_back(p_id);
  36. }
  37. void EditorDebuggerServerWebSocket::_peer_disconnected(int p_id, bool p_was_clean) {
  38. if (pending_peers.find(p_id)) {
  39. pending_peers.erase(p_id);
  40. }
  41. }
  42. void EditorDebuggerServerWebSocket::poll() {
  43. server->poll();
  44. }
  45. Error EditorDebuggerServerWebSocket::start(const String &p_uri) {
  46. int bind_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port");
  47. String bind_host = EditorSettings::get_singleton()->get("network/debug/remote_host");
  48. if (!p_uri.is_empty() && p_uri != "ws://") {
  49. String scheme, path;
  50. Error err = p_uri.parse_url(scheme, bind_host, bind_port, path);
  51. ERR_FAIL_COND_V(err != OK, ERR_INVALID_PARAMETER);
  52. ERR_FAIL_COND_V(!bind_host.is_valid_ip_address() && bind_host != "*", ERR_INVALID_PARAMETER);
  53. }
  54. server->set_bind_ip(bind_host);
  55. Vector<String> compatible_protocols;
  56. compatible_protocols.push_back("binary"); // compatibility with EMSCRIPTEN TCP-to-WebSocket layer.
  57. return server->listen(bind_port, compatible_protocols);
  58. }
  59. void EditorDebuggerServerWebSocket::stop() {
  60. server->stop();
  61. pending_peers.clear();
  62. }
  63. bool EditorDebuggerServerWebSocket::is_active() const {
  64. return server->is_listening();
  65. }
  66. bool EditorDebuggerServerWebSocket::is_connection_available() const {
  67. return pending_peers.size() > 0;
  68. }
  69. Ref<RemoteDebuggerPeer> EditorDebuggerServerWebSocket::take_connection() {
  70. ERR_FAIL_COND_V(!is_connection_available(), Ref<RemoteDebuggerPeer>());
  71. RemoteDebuggerPeer *peer = memnew(RemoteDebuggerPeerWebSocket(server->get_peer(pending_peers[0])));
  72. pending_peers.pop_front();
  73. return peer;
  74. }
  75. EditorDebuggerServerWebSocket::EditorDebuggerServerWebSocket() {
  76. server = Ref<WebSocketServer>(WebSocketServer::create());
  77. int max_pkts = (int)GLOBAL_GET("network/limits/debugger/max_queued_messages");
  78. server->set_buffers(8192, max_pkts, 8192, max_pkts);
  79. server->connect("client_connected", callable_mp(this, &EditorDebuggerServerWebSocket::_peer_connected));
  80. server->connect("client_disconnected", callable_mp(this, &EditorDebuggerServerWebSocket::_peer_disconnected));
  81. }
  82. EditorDebuggerServerWebSocket::~EditorDebuggerServerWebSocket() {
  83. stop();
  84. }
  85. EditorDebuggerServer *EditorDebuggerServerWebSocket::create(const String &p_protocol) {
  86. ERR_FAIL_COND_V(p_protocol != "ws://", nullptr);
  87. return memnew(EditorDebuggerServerWebSocket);
  88. }