remote_debugger_peer.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**************************************************************************/
  2. /* remote_debugger_peer.h */
  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. #ifndef REMOTE_DEBUGGER_PEER_H
  31. #define REMOTE_DEBUGGER_PEER_H
  32. #include "core/io/stream_peer_tcp.h"
  33. #include "core/object/ref_counted.h"
  34. #include "core/os/mutex.h"
  35. #include "core/os/thread.h"
  36. #include "core/string/ustring.h"
  37. class RemoteDebuggerPeer : public RefCounted {
  38. protected:
  39. int max_queued_messages = 4096;
  40. public:
  41. virtual bool is_peer_connected() = 0;
  42. virtual int get_max_message_size() const = 0;
  43. virtual bool has_message() = 0;
  44. virtual Error put_message(const Array &p_arr) = 0;
  45. virtual Array get_message() = 0;
  46. virtual void close() = 0;
  47. virtual void poll() = 0;
  48. virtual bool can_block() const { return true; } // If blocking io is allowed on main thread (debug).
  49. RemoteDebuggerPeer();
  50. };
  51. class RemoteDebuggerPeerTCP : public RemoteDebuggerPeer {
  52. private:
  53. Ref<StreamPeerTCP> tcp_client;
  54. Mutex mutex;
  55. Thread thread;
  56. List<Array> in_queue;
  57. List<Array> out_queue;
  58. int out_left = 0;
  59. int out_pos = 0;
  60. Vector<uint8_t> out_buf;
  61. int in_left = 0;
  62. int in_pos = 0;
  63. Vector<uint8_t> in_buf;
  64. bool connected = false;
  65. bool running = false;
  66. static void _thread_func(void *p_ud);
  67. void _poll();
  68. void _write_out();
  69. void _read_in();
  70. public:
  71. static RemoteDebuggerPeer *create(const String &p_uri);
  72. Error connect_to_host(const String &p_host, uint16_t p_port);
  73. bool is_peer_connected() override;
  74. int get_max_message_size() const override;
  75. bool has_message() override;
  76. Error put_message(const Array &p_arr) override;
  77. Array get_message() override;
  78. void poll() override;
  79. void close() override;
  80. RemoteDebuggerPeerTCP(Ref<StreamPeerTCP> p_stream = Ref<StreamPeerTCP>());
  81. ~RemoteDebuggerPeerTCP();
  82. };
  83. #endif // REMOTE_DEBUGGER_PEER_H