Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/debugger/editor_debugger_server.cpp
20874 views
1
/**************************************************************************/
2
/* editor_debugger_server.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
31
#include "editor_debugger_server.h"
32
33
#include "core/io/tcp_server.h"
34
#include "core/io/uds_server.h"
35
#include "core/os/thread.h"
36
#include "editor/editor_log.h"
37
#include "editor/editor_node.h"
38
#include "editor/settings/editor_settings.h"
39
40
template <typename T>
41
class EditorDebuggerServerSocket : public EditorDebuggerServer {
42
GDSOFTCLASS(EditorDebuggerServerSocket, EditorDebuggerServer);
43
44
protected:
45
Ref<T> server;
46
String endpoint;
47
48
public:
49
virtual void poll() override {}
50
virtual String get_uri() const override;
51
virtual void stop() override;
52
virtual bool is_active() const override;
53
virtual bool is_connection_available() const override;
54
virtual Ref<RemoteDebuggerPeer> take_connection() override;
55
56
EditorDebuggerServerSocket();
57
};
58
59
class EditorDebuggerServerTCP : public EditorDebuggerServerSocket<TCPServer> {
60
public:
61
static EditorDebuggerServer *create(const String &p_protocol);
62
63
virtual Error start(const String &p_uri) override;
64
};
65
66
EditorDebuggerServer *EditorDebuggerServerTCP::create(const String &p_protocol) {
67
ERR_FAIL_COND_V(p_protocol != "tcp://", nullptr);
68
return memnew(EditorDebuggerServerTCP);
69
}
70
71
template <typename T>
72
EditorDebuggerServerSocket<T>::EditorDebuggerServerSocket() {
73
server.instantiate();
74
}
75
76
template <typename T>
77
String EditorDebuggerServerSocket<T>::get_uri() const {
78
return endpoint;
79
}
80
81
Error EditorDebuggerServerTCP::start(const String &p_uri) {
82
// Default host and port
83
String bind_host = (String)EDITOR_GET("network/debug/remote_host");
84
int bind_port = (int)EDITOR_GET("network/debug/remote_port");
85
86
// Optionally override
87
if (!p_uri.is_empty() && p_uri != "tcp://") {
88
String scheme, path, fragment;
89
Error err = p_uri.parse_url(scheme, bind_host, bind_port, path, fragment);
90
ERR_FAIL_COND_V(err != OK, ERR_INVALID_PARAMETER);
91
ERR_FAIL_COND_V(!bind_host.is_valid_ip_address() && bind_host != "*", ERR_INVALID_PARAMETER);
92
}
93
94
// Try listening on ports
95
const int max_attempts = 5;
96
for (int attempt = 1;; ++attempt) {
97
const Error err = server->listen(bind_port, bind_host);
98
if (err == OK) {
99
break;
100
}
101
if (attempt >= max_attempts) {
102
EditorNode::get_log()->add_message(vformat("Cannot listen on port %d, remote debugging unavailable.", bind_port), EditorLog::MSG_TYPE_ERROR);
103
return err;
104
}
105
int last_port = bind_port++;
106
EditorNode::get_log()->add_message(vformat("Cannot listen on port %d, trying %d instead.", last_port, bind_port), EditorLog::MSG_TYPE_WARNING);
107
}
108
109
// Endpoint that the client should connect to
110
endpoint = vformat("tcp://%s:%d", bind_host, bind_port);
111
112
return OK;
113
}
114
115
template <typename T>
116
void EditorDebuggerServerSocket<T>::stop() {
117
server->stop();
118
}
119
120
template <typename T>
121
bool EditorDebuggerServerSocket<T>::is_active() const {
122
return server->is_listening();
123
}
124
125
template <typename T>
126
bool EditorDebuggerServerSocket<T>::is_connection_available() const {
127
return server->is_listening() && server->is_connection_available();
128
}
129
130
template <typename T>
131
Ref<RemoteDebuggerPeer> EditorDebuggerServerSocket<T>::take_connection() {
132
const Ref<RemoteDebuggerPeer> out;
133
ERR_FAIL_COND_V(!is_connection_available(), out);
134
Ref<StreamPeerSocket> stream = server->take_socket_connection();
135
ERR_FAIL_COND_V(stream.is_null(), out);
136
return memnew(RemoteDebuggerPeerTCP(stream));
137
}
138
139
class EditorDebuggerServerUDS : public EditorDebuggerServerSocket<UDSServer> {
140
public:
141
static EditorDebuggerServer *create(const String &p_protocol);
142
143
virtual Error start(const String &p_uri) override;
144
};
145
146
EditorDebuggerServer *EditorDebuggerServerUDS::create(const String &p_protocol) {
147
ERR_FAIL_COND_V(p_protocol != "unix://", nullptr);
148
return memnew(EditorDebuggerServerUDS);
149
}
150
151
Error EditorDebuggerServerUDS::start(const String &p_uri) {
152
String bind_path = p_uri.is_empty() ? String("/tmp/godot_debugger.sock") : p_uri.replace("unix://", "");
153
154
const Error err = server->listen(bind_path);
155
if (err != OK) {
156
EditorNode::get_log()->add_message(vformat("Cannot listen at path %s, remote debugging unavailable.", bind_path), EditorLog::MSG_TYPE_ERROR);
157
return err;
158
}
159
endpoint = "unix://" + bind_path;
160
return OK;
161
}
162
163
/// EditorDebuggerServer
164
HashMap<StringName, EditorDebuggerServer::CreateServerFunc> EditorDebuggerServer::protocols;
165
166
EditorDebuggerServer *EditorDebuggerServer::create(const String &p_protocol) {
167
CreateServerFunc *create_fn = protocols.getptr(p_protocol);
168
ERR_FAIL_NULL_V(create_fn, nullptr);
169
return (*create_fn)(p_protocol);
170
}
171
172
void EditorDebuggerServer::register_protocol_handler(const String &p_protocol, CreateServerFunc p_func) {
173
ERR_FAIL_COND(protocols.has(p_protocol));
174
protocols[p_protocol] = p_func;
175
}
176
177
void EditorDebuggerServer::initialize() {
178
register_protocol_handler("tcp://", EditorDebuggerServerTCP::create);
179
#if defined(UNIX_ENABLED)
180
register_protocol_handler("unix://", EditorDebuggerServerUDS::create);
181
#endif
182
}
183
184
void EditorDebuggerServer::deinitialize() {
185
protocols.clear();
186
}
187
188