editor_debugger_server_websocket.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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() {
  46. int remote_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port");
  47. Vector<String> protocols;
  48. protocols.push_back("binary"); // compatibility with EMSCRIPTEN TCP-to-WebSocket layer.
  49. return server->listen(remote_port, protocols);
  50. }
  51. void EditorDebuggerServerWebSocket::stop() {
  52. server->stop();
  53. pending_peers.clear();
  54. }
  55. bool EditorDebuggerServerWebSocket::is_active() const {
  56. return server->is_listening();
  57. }
  58. bool EditorDebuggerServerWebSocket::is_connection_available() const {
  59. return pending_peers.size() > 0;
  60. }
  61. Ref<RemoteDebuggerPeer> EditorDebuggerServerWebSocket::take_connection() {
  62. ERR_FAIL_COND_V(!is_connection_available(), Ref<RemoteDebuggerPeer>());
  63. RemoteDebuggerPeer *peer = memnew(RemoteDebuggerPeerWebSocket(server->get_peer(pending_peers[0])));
  64. pending_peers.pop_front();
  65. return peer;
  66. }
  67. EditorDebuggerServerWebSocket::EditorDebuggerServerWebSocket() {
  68. server = Ref<WebSocketServer>(WebSocketServer::create());
  69. int max_pkts = (int)GLOBAL_GET("network/limits/debugger/max_queued_messages");
  70. server->set_buffers(8192, max_pkts, 8192, max_pkts);
  71. server->connect("client_connected", callable_mp(this, &EditorDebuggerServerWebSocket::_peer_connected));
  72. server->connect("client_disconnected", callable_mp(this, &EditorDebuggerServerWebSocket::_peer_disconnected));
  73. }
  74. EditorDebuggerServerWebSocket::~EditorDebuggerServerWebSocket() {
  75. stop();
  76. }
  77. EditorDebuggerServer *EditorDebuggerServerWebSocket::create(const String &p_protocol) {
  78. ERR_FAIL_COND_V(p_protocol != "ws://", nullptr);
  79. return memnew(EditorDebuggerServerWebSocket);
  80. }