remote_debugger_peer_websocket.cpp 5.4 KB

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