remote_debugger_peer.cpp 8.7 KB

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