remote_debugger_peer.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*************************************************************************/
  2. /* remote_debugger_peer.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.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/io/marshalls.h"
  33. #include "core/os/os.h"
  34. bool RemoteDebuggerPeerTCP::is_peer_connected() {
  35. return connected;
  36. }
  37. bool RemoteDebuggerPeerTCP::has_message() {
  38. return in_queue.size() > 0;
  39. }
  40. Array RemoteDebuggerPeerTCP::get_message() {
  41. MutexLock lock(mutex);
  42. ERR_FAIL_COND_V(!has_message(), Array());
  43. Array out = in_queue[0];
  44. in_queue.pop_front();
  45. return out;
  46. }
  47. Error RemoteDebuggerPeerTCP::put_message(const Array &p_arr) {
  48. MutexLock lock(mutex);
  49. if (out_queue.size() >= max_queued_messages) {
  50. return ERR_OUT_OF_MEMORY;
  51. }
  52. out_queue.push_back(p_arr);
  53. return OK;
  54. }
  55. int RemoteDebuggerPeerTCP::get_max_message_size() const {
  56. return 8 << 20; // 8 MiB
  57. }
  58. void RemoteDebuggerPeerTCP::close() {
  59. running = false;
  60. thread.wait_to_finish();
  61. tcp_client->disconnect_from_host();
  62. out_buf.clear();
  63. in_buf.clear();
  64. }
  65. RemoteDebuggerPeerTCP::RemoteDebuggerPeerTCP(Ref<StreamPeerTCP> p_tcp) {
  66. // This means remote debugger takes 16 MiB just because it exists...
  67. in_buf.resize((8 << 20) + 4); // 8 MiB should be way more than enough (need 4 extra bytes for encoding packet size).
  68. out_buf.resize(8 << 20); // 8 MiB should be way more than enough
  69. tcp_client = p_tcp;
  70. if (tcp_client.is_valid()) { // Attaching to an already connected stream.
  71. connected = true;
  72. running = true;
  73. thread.start(_thread_func, this);
  74. } else {
  75. tcp_client.instantiate();
  76. }
  77. }
  78. RemoteDebuggerPeerTCP::~RemoteDebuggerPeerTCP() {
  79. close();
  80. }
  81. void RemoteDebuggerPeerTCP::_write_out() {
  82. while (tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED && tcp_client->wait(NetSocket::POLL_TYPE_OUT) == OK) {
  83. uint8_t *buf = out_buf.ptrw();
  84. if (out_left <= 0) {
  85. if (out_queue.size() == 0) {
  86. break; // Nothing left to send
  87. }
  88. mutex.lock();
  89. Variant var = out_queue[0];
  90. out_queue.pop_front();
  91. mutex.unlock();
  92. int size = 0;
  93. Error err = encode_variant(var, nullptr, size);
  94. ERR_CONTINUE(err != OK || size > out_buf.size() - 4); // 4 bytes separator.
  95. encode_uint32(size, buf);
  96. encode_variant(var, buf + 4, size);
  97. out_left = size + 4;
  98. out_pos = 0;
  99. }
  100. int sent = 0;
  101. tcp_client->put_partial_data(buf + out_pos, out_left, sent);
  102. out_left -= sent;
  103. out_pos += sent;
  104. }
  105. }
  106. void RemoteDebuggerPeerTCP::_read_in() {
  107. while (tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED && tcp_client->wait(NetSocket::POLL_TYPE_IN) == OK) {
  108. uint8_t *buf = in_buf.ptrw();
  109. if (in_left <= 0) {
  110. if (in_queue.size() > max_queued_messages) {
  111. break; // Too many messages already in queue.
  112. }
  113. if (tcp_client->get_available_bytes() < 4) {
  114. break; // Need 4 more bytes.
  115. }
  116. uint32_t size = 0;
  117. int read = 0;
  118. Error err = tcp_client->get_partial_data((uint8_t *)&size, 4, read);
  119. ERR_CONTINUE(read != 4 || err != OK || size > (uint32_t)in_buf.size());
  120. in_left = size;
  121. in_pos = 0;
  122. }
  123. int read = 0;
  124. tcp_client->get_partial_data(buf + in_pos, in_left, read);
  125. in_left -= read;
  126. in_pos += read;
  127. if (in_left == 0) {
  128. Variant var;
  129. Error err = decode_variant(var, buf, in_pos, &read);
  130. ERR_CONTINUE(read != in_pos || err != OK);
  131. ERR_CONTINUE_MSG(var.get_type() != Variant::ARRAY, "Malformed packet received, not an Array.");
  132. mutex.lock();
  133. in_queue.push_back(var);
  134. mutex.unlock();
  135. }
  136. }
  137. }
  138. Error RemoteDebuggerPeerTCP::connect_to_host(const String &p_host, uint16_t p_port) {
  139. IPAddress ip;
  140. if (p_host.is_valid_ip_address()) {
  141. ip = p_host;
  142. } else {
  143. ip = IP::get_singleton()->resolve_hostname(p_host);
  144. }
  145. int port = p_port;
  146. const int tries = 6;
  147. const int waits[tries] = { 1, 10, 100, 1000, 1000, 1000 };
  148. tcp_client->connect_to_host(ip, port);
  149. for (int i = 0; i < tries; i++) {
  150. tcp_client->poll();
  151. if (tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED) {
  152. print_verbose("Remote Debugger: Connected!");
  153. break;
  154. } else {
  155. const int ms = waits[i];
  156. OS::get_singleton()->delay_usec(ms * 1000);
  157. print_verbose("Remote Debugger: Connection failed with status: '" + String::num(tcp_client->get_status()) + "', retrying in " + String::num(ms) + " msec.");
  158. }
  159. }
  160. if (tcp_client->get_status() != StreamPeerTCP::STATUS_CONNECTED) {
  161. ERR_PRINT("Remote Debugger: Unable to connect. Status: " + String::num(tcp_client->get_status()) + ".");
  162. return FAILED;
  163. }
  164. connected = true;
  165. running = true;
  166. thread.start(_thread_func, this);
  167. return OK;
  168. }
  169. void RemoteDebuggerPeerTCP::_thread_func(void *p_ud) {
  170. // Update in time for 144hz monitors
  171. const uint64_t min_tick = 6900;
  172. RemoteDebuggerPeerTCP *peer = static_cast<RemoteDebuggerPeerTCP *>(p_ud);
  173. while (peer->running && peer->is_peer_connected()) {
  174. uint64_t ticks_usec = OS::get_singleton()->get_ticks_usec();
  175. peer->_poll();
  176. if (!peer->is_peer_connected()) {
  177. break;
  178. }
  179. ticks_usec = OS::get_singleton()->get_ticks_usec() - ticks_usec;
  180. if (ticks_usec < min_tick) {
  181. OS::get_singleton()->delay_usec(min_tick - ticks_usec);
  182. }
  183. }
  184. }
  185. void RemoteDebuggerPeerTCP::poll() {
  186. // Nothing to do, polling is done in thread.
  187. }
  188. void RemoteDebuggerPeerTCP::_poll() {
  189. tcp_client->poll();
  190. if (connected) {
  191. _write_out();
  192. _read_in();
  193. connected = tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED;
  194. }
  195. }
  196. RemoteDebuggerPeer *RemoteDebuggerPeerTCP::create(const String &p_uri) {
  197. ERR_FAIL_COND_V(!p_uri.begins_with("tcp://"), nullptr);
  198. String debug_host = p_uri.replace("tcp://", "");
  199. uint16_t debug_port = 6007;
  200. if (debug_host.contains(":")) {
  201. int sep_pos = debug_host.rfind(":");
  202. debug_port = debug_host.substr(sep_pos + 1).to_int();
  203. debug_host = debug_host.substr(0, sep_pos);
  204. }
  205. RemoteDebuggerPeerTCP *peer = memnew(RemoteDebuggerPeerTCP);
  206. Error err = peer->connect_to_host(debug_host, debug_port);
  207. if (err != OK) {
  208. memdelete(peer);
  209. return nullptr;
  210. }
  211. return peer;
  212. }
  213. RemoteDebuggerPeer::RemoteDebuggerPeer() {
  214. max_queued_messages = (int)GLOBAL_GET("network/limits/debugger/max_queued_messages");
  215. }