editor_debugger_server.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*************************************************************************/
  2. /* editor_debugger_server.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "editor_debugger_server.h"
  31. #include "core/io/packet_peer.h"
  32. #include "core/io/tcp_server.h"
  33. #include "core/os/mutex.h"
  34. #include "core/os/thread.h"
  35. #include "editor/editor_log.h"
  36. #include "editor/editor_node.h"
  37. #include "editor/editor_settings.h"
  38. class EditorDebuggerPeerTCP : public EditorDebuggerPeer {
  39. private:
  40. enum {
  41. QUEUE_MAX = 2048,
  42. POLL_USEC_MAX = 100,
  43. };
  44. Ref<StreamPeerTCP> tcp;
  45. Ref<PacketPeerStream> packet_peer;
  46. List<Array> out_queue;
  47. List<Array> in_queue;
  48. Mutex mutex;
  49. bool connected = false;
  50. public:
  51. Error poll() {
  52. MutexLock lock(mutex);
  53. connected = tcp->get_status() == StreamPeerTCP::STATUS_CONNECTED;
  54. Error err = OK;
  55. uint64_t ticks = OS::get_singleton()->get_ticks_usec();
  56. while (connected && packet_peer->get_available_packet_count() > 0 && in_queue.size() < QUEUE_MAX && OS::get_singleton()->get_ticks_usec() - ticks < POLL_USEC_MAX) {
  57. Variant var;
  58. err = packet_peer->get_var(var);
  59. connected = tcp->get_status() == StreamPeerTCP::STATUS_CONNECTED;
  60. if (err != OK) {
  61. ERR_PRINT("Error reading variant from peer");
  62. break;
  63. }
  64. ERR_CONTINUE_MSG(var.get_type() != Variant::ARRAY, "Malformed packet received, not an Array.");
  65. in_queue.push_back(var);
  66. }
  67. ticks = OS::get_singleton()->get_ticks_usec();
  68. while (connected && out_queue.size() > 0 && OS::get_singleton()->get_ticks_usec() - ticks < POLL_USEC_MAX) {
  69. Array arr = out_queue[0];
  70. out_queue.pop_front();
  71. packet_peer->put_var(arr);
  72. connected = tcp->get_status() == StreamPeerTCP::STATUS_CONNECTED;
  73. }
  74. return err;
  75. }
  76. bool has_message() {
  77. return in_queue.size() > 0;
  78. }
  79. Array get_message() {
  80. MutexLock lock(mutex);
  81. ERR_FAIL_COND_V(!has_message(), Array());
  82. Array out = in_queue[0];
  83. in_queue.pop_front();
  84. return out;
  85. }
  86. Error put_message(const Array p_arr) {
  87. MutexLock lock(mutex);
  88. if (out_queue.size() > QUEUE_MAX) {
  89. return ERR_OUT_OF_MEMORY;
  90. }
  91. out_queue.push_back(p_arr);
  92. return OK;
  93. }
  94. int get_max_message_size() const {
  95. return 8 << 20; // 8 MiB
  96. }
  97. bool is_peer_connected() {
  98. return connected;
  99. }
  100. void close() {
  101. MutexLock lock(mutex);
  102. connected = false;
  103. tcp->disconnect_from_host();
  104. }
  105. EditorDebuggerPeerTCP(Ref<StreamPeerTCP> p_stream) {
  106. packet_peer.instance();
  107. tcp = p_stream;
  108. if (tcp.is_null()) {
  109. tcp.instance(); // Bug?
  110. }
  111. packet_peer->set_stream_peer(tcp);
  112. }
  113. ~EditorDebuggerPeerTCP() {
  114. close();
  115. packet_peer->set_stream_peer(Ref<StreamPeer>());
  116. }
  117. };
  118. class EditorDebuggerServerTCP : public EditorDebuggerServer {
  119. private:
  120. Ref<TCP_Server> server;
  121. List<Ref<EditorDebuggerPeer> > peers;
  122. Thread *thread = NULL;
  123. Mutex mutex;
  124. bool running = false;
  125. static void _poll_func(void *p_ud);
  126. public:
  127. virtual Error start();
  128. virtual void stop();
  129. virtual bool is_active() const;
  130. virtual bool is_connection_available() const;
  131. virtual Ref<EditorDebuggerPeer> take_connection();
  132. EditorDebuggerServerTCP();
  133. };
  134. EditorDebuggerServerTCP::EditorDebuggerServerTCP() {
  135. server.instance();
  136. }
  137. Error EditorDebuggerServerTCP::start() {
  138. int remote_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port");
  139. const Error err = server->listen(remote_port);
  140. if (err != OK) {
  141. EditorNode::get_log()->add_message(String("Error listening on port ") + itos(remote_port), EditorLog::MSG_TYPE_ERROR);
  142. return err;
  143. }
  144. running = true;
  145. thread = Thread::create(_poll_func, this);
  146. return err;
  147. }
  148. void EditorDebuggerServerTCP::stop() {
  149. server->stop();
  150. if (thread != NULL) {
  151. running = false;
  152. Thread::wait_to_finish(thread);
  153. memdelete(thread);
  154. thread = NULL;
  155. }
  156. }
  157. bool EditorDebuggerServerTCP::is_active() const {
  158. return server->is_listening();
  159. }
  160. bool EditorDebuggerServerTCP::is_connection_available() const {
  161. return server->is_listening() && server->is_connection_available();
  162. }
  163. Ref<EditorDebuggerPeer> EditorDebuggerServerTCP::take_connection() {
  164. ERR_FAIL_COND_V(!is_connection_available(), Ref<EditorDebuggerPeer>());
  165. MutexLock lock(mutex);
  166. Ref<EditorDebuggerPeerTCP> peer = memnew(EditorDebuggerPeerTCP(server->take_connection()));
  167. peers.push_back(peer);
  168. return peer;
  169. }
  170. void EditorDebuggerServerTCP::_poll_func(void *p_ud) {
  171. EditorDebuggerServerTCP *me = (EditorDebuggerServerTCP *)p_ud;
  172. while (me->running) {
  173. me->mutex.lock();
  174. List<Ref<EditorDebuggerPeer> > remove;
  175. for (int i = 0; i < me->peers.size(); i++) {
  176. Ref<EditorDebuggerPeer> peer = me->peers[i];
  177. Error err = ((EditorDebuggerPeerTCP *)peer.ptr())->poll();
  178. if (err != OK || !peer->is_peer_connected())
  179. remove.push_back(peer);
  180. }
  181. for (List<Ref<EditorDebuggerPeer> >::Element *E = remove.front(); E; E = E->next()) {
  182. me->peers.erase(E->get());
  183. }
  184. me->mutex.unlock();
  185. OS::get_singleton()->delay_usec(50);
  186. }
  187. }
  188. EditorDebuggerServer *EditorDebuggerServer::create_default() {
  189. return memnew(EditorDebuggerServerTCP);
  190. }