remote_debugger_peer_websocket.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**************************************************************************/
  2. /* remote_debugger_peer_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 "remote_debugger_peer_websocket.h"
  31. #include "core/config/project_settings.h"
  32. Error RemoteDebuggerPeerWebSocket::connect_to_host(const String &p_uri) {
  33. ws_peer = Ref<WebSocketPeer>(WebSocketPeer::create());
  34. ERR_FAIL_COND_V(ws_peer.is_null(), ERR_BUG);
  35. Vector<String> protocols;
  36. protocols.push_back("binary"); // Compatibility for emscripten TCP-to-WebSocket.
  37. ws_peer->set_supported_protocols(protocols);
  38. ws_peer->set_max_queued_packets(max_queued_messages);
  39. ws_peer->set_inbound_buffer_size((1 << 23) - 1);
  40. ws_peer->set_outbound_buffer_size((1 << 23) - 1);
  41. Error err = ws_peer->connect_to_url(p_uri);
  42. ERR_FAIL_COND_V(err != OK, err);
  43. ws_peer->poll();
  44. WebSocketPeer::State ready_state = ws_peer->get_ready_state();
  45. if (ready_state != WebSocketPeer::STATE_CONNECTING && ready_state != WebSocketPeer::STATE_OPEN) {
  46. ERR_PRINT(vformat("Remote Debugger: Unable to connect. State: %s.", ws_peer->get_ready_state()));
  47. return FAILED;
  48. }
  49. return OK;
  50. }
  51. bool RemoteDebuggerPeerWebSocket::is_peer_connected() {
  52. return ws_peer.is_valid() && (ws_peer->get_ready_state() == WebSocketPeer::STATE_OPEN || ws_peer->get_ready_state() == WebSocketPeer::STATE_CONNECTING);
  53. }
  54. void RemoteDebuggerPeerWebSocket::poll() {
  55. ERR_FAIL_COND(ws_peer.is_null());
  56. ws_peer->poll();
  57. while (ws_peer->get_ready_state() == WebSocketPeer::STATE_OPEN && ws_peer->get_available_packet_count() > 0 && in_queue.size() < max_queued_messages) {
  58. Variant var;
  59. Error err = ws_peer->get_var(var);
  60. ERR_CONTINUE(err != OK);
  61. ERR_CONTINUE(var.get_type() != Variant::ARRAY);
  62. in_queue.push_back(var);
  63. }
  64. while (ws_peer->get_ready_state() == WebSocketPeer::STATE_OPEN && out_queue.size() > 0) {
  65. Array var = out_queue.front()->get();
  66. Error err = ws_peer->put_var(var);
  67. ERR_BREAK(err != OK); // Peer buffer full?
  68. out_queue.pop_front();
  69. }
  70. }
  71. int RemoteDebuggerPeerWebSocket::get_max_message_size() const {
  72. ERR_FAIL_COND_V(ws_peer.is_null(), 0);
  73. return ws_peer->get_max_packet_size();
  74. }
  75. bool RemoteDebuggerPeerWebSocket::has_message() {
  76. return in_queue.size() > 0;
  77. }
  78. Array RemoteDebuggerPeerWebSocket::get_message() {
  79. ERR_FAIL_COND_V(in_queue.is_empty(), Array());
  80. Array msg = in_queue.front()->get();
  81. in_queue.pop_front();
  82. return msg;
  83. }
  84. Error RemoteDebuggerPeerWebSocket::put_message(const Array &p_arr) {
  85. if (out_queue.size() >= max_queued_messages) {
  86. return ERR_OUT_OF_MEMORY;
  87. }
  88. out_queue.push_back(p_arr);
  89. return OK;
  90. }
  91. void RemoteDebuggerPeerWebSocket::close() {
  92. if (ws_peer.is_valid()) {
  93. ws_peer.unref();
  94. }
  95. }
  96. bool RemoteDebuggerPeerWebSocket::can_block() const {
  97. #ifdef WEB_ENABLED
  98. return false;
  99. #else
  100. return true;
  101. #endif
  102. }
  103. RemoteDebuggerPeerWebSocket::RemoteDebuggerPeerWebSocket(Ref<WebSocketPeer> p_peer) {
  104. max_queued_messages = (int)GLOBAL_GET("network/limits/debugger/max_queued_messages");
  105. ws_peer = p_peer;
  106. if (ws_peer.is_valid()) {
  107. ws_peer->set_max_queued_packets(max_queued_messages);
  108. ws_peer->set_inbound_buffer_size((1 << 23) - 1);
  109. ws_peer->set_outbound_buffer_size((1 << 23) - 1);
  110. }
  111. }
  112. RemoteDebuggerPeer *RemoteDebuggerPeerWebSocket::create(const String &p_uri) {
  113. ERR_FAIL_COND_V(!p_uri.begins_with("ws://") && !p_uri.begins_with("wss://"), nullptr);
  114. RemoteDebuggerPeerWebSocket *peer = memnew(RemoteDebuggerPeerWebSocket);
  115. Error err = peer->connect_to_host(p_uri);
  116. if (err != OK) {
  117. memdelete(peer);
  118. return nullptr;
  119. }
  120. return peer;
  121. }