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