editor_debugger_server_websocket.cpp 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*************************************************************************/
  2. /* script_editor_debugger_websocket.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/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. void EditorDebuggerServerWebSocket::poll() {
  42. server->poll();
  43. }
  44. Error EditorDebuggerServerWebSocket::start() {
  45. int remote_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port");
  46. Vector<String> protocols;
  47. protocols.push_back("binary"); // compatibility with EMSCRIPTEN TCP-to-WebSocket layer.
  48. return server->listen(remote_port, protocols);
  49. }
  50. void EditorDebuggerServerWebSocket::stop() {
  51. server->stop();
  52. pending_peers.clear();
  53. }
  54. bool EditorDebuggerServerWebSocket::is_active() const {
  55. return server->is_listening();
  56. }
  57. bool EditorDebuggerServerWebSocket::is_connection_available() const {
  58. return pending_peers.size() > 0;
  59. }
  60. Ref<RemoteDebuggerPeer> EditorDebuggerServerWebSocket::take_connection() {
  61. ERR_FAIL_COND_V(!is_connection_available(), Ref<RemoteDebuggerPeer>());
  62. RemoteDebuggerPeer *peer = memnew(RemoteDebuggerPeerWebSocket(server->get_peer(pending_peers[0])));
  63. pending_peers.pop_front();
  64. return peer;
  65. }
  66. EditorDebuggerServerWebSocket::EditorDebuggerServerWebSocket() {
  67. server = Ref<WebSocketServer>(WebSocketServer::create());
  68. int max_pkts = (int)GLOBAL_GET("network/limits/debugger/max_queued_messages");
  69. server->set_buffers(8192, max_pkts, 8192, max_pkts);
  70. server->connect("client_connected", callable_mp(this, &EditorDebuggerServerWebSocket::_peer_connected));
  71. server->connect("client_disconnected", callable_mp(this, &EditorDebuggerServerWebSocket::_peer_disconnected));
  72. }
  73. EditorDebuggerServerWebSocket::~EditorDebuggerServerWebSocket() {
  74. stop();
  75. }
  76. EditorDebuggerServer *EditorDebuggerServerWebSocket::create(const String &p_protocol) {
  77. ERR_FAIL_COND_V(p_protocol != "ws://", nullptr);
  78. return memnew(EditorDebuggerServerWebSocket);
  79. }