remote_debugger_peer.cpp 8.1 KB

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