Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/debugger/editor_debugger_server.cpp
9896 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/os/thread.h"
35
#include "editor/editor_log.h"
36
#include "editor/editor_node.h"
37
#include "editor/settings/editor_settings.h"
38
39
class EditorDebuggerServerTCP : public EditorDebuggerServer {
40
private:
41
Ref<TCPServer> server;
42
String endpoint;
43
44
public:
45
static EditorDebuggerServer *create(const String &p_protocol);
46
47
virtual void poll() override {}
48
virtual String get_uri() const override;
49
virtual Error start(const String &p_uri) override;
50
virtual void stop() override;
51
virtual bool is_active() const override;
52
virtual bool is_connection_available() const override;
53
virtual Ref<RemoteDebuggerPeer> take_connection() override;
54
55
EditorDebuggerServerTCP();
56
};
57
58
EditorDebuggerServer *EditorDebuggerServerTCP::create(const String &p_protocol) {
59
ERR_FAIL_COND_V(p_protocol != "tcp://", nullptr);
60
return memnew(EditorDebuggerServerTCP);
61
}
62
63
EditorDebuggerServerTCP::EditorDebuggerServerTCP() {
64
server.instantiate();
65
}
66
67
String EditorDebuggerServerTCP::get_uri() const {
68
return endpoint;
69
}
70
71
Error EditorDebuggerServerTCP::start(const String &p_uri) {
72
// Default host and port
73
String bind_host = (String)EDITOR_GET("network/debug/remote_host");
74
int bind_port = (int)EDITOR_GET("network/debug/remote_port");
75
76
// Optionally override
77
if (!p_uri.is_empty() && p_uri != "tcp://") {
78
String scheme, path, fragment;
79
Error err = p_uri.parse_url(scheme, bind_host, bind_port, path, fragment);
80
ERR_FAIL_COND_V(err != OK, ERR_INVALID_PARAMETER);
81
ERR_FAIL_COND_V(!bind_host.is_valid_ip_address() && bind_host != "*", ERR_INVALID_PARAMETER);
82
}
83
84
// Try listening on ports
85
const int max_attempts = 5;
86
for (int attempt = 1;; ++attempt) {
87
const Error err = server->listen(bind_port, bind_host);
88
if (err == OK) {
89
break;
90
}
91
if (attempt >= max_attempts) {
92
EditorNode::get_log()->add_message(vformat("Cannot listen on port %d, remote debugging unavailable.", bind_port), EditorLog::MSG_TYPE_ERROR);
93
return err;
94
}
95
int last_port = bind_port++;
96
EditorNode::get_log()->add_message(vformat("Cannot listen on port %d, trying %d instead.", last_port, bind_port), EditorLog::MSG_TYPE_WARNING);
97
}
98
99
// Endpoint that the client should connect to
100
endpoint = vformat("tcp://%s:%d", bind_host, bind_port);
101
102
return OK;
103
}
104
105
void EditorDebuggerServerTCP::stop() {
106
server->stop();
107
}
108
109
bool EditorDebuggerServerTCP::is_active() const {
110
return server->is_listening();
111
}
112
113
bool EditorDebuggerServerTCP::is_connection_available() const {
114
return server->is_listening() && server->is_connection_available();
115
}
116
117
Ref<RemoteDebuggerPeer> EditorDebuggerServerTCP::take_connection() {
118
ERR_FAIL_COND_V(!is_connection_available(), Ref<RemoteDebuggerPeer>());
119
return memnew(RemoteDebuggerPeerTCP(server->take_connection()));
120
}
121
122
/// EditorDebuggerServer
123
HashMap<StringName, EditorDebuggerServer::CreateServerFunc> EditorDebuggerServer::protocols;
124
125
EditorDebuggerServer *EditorDebuggerServer::create(const String &p_protocol) {
126
ERR_FAIL_COND_V(!protocols.has(p_protocol), nullptr);
127
return protocols[p_protocol](p_protocol);
128
}
129
130
void EditorDebuggerServer::register_protocol_handler(const String &p_protocol, CreateServerFunc p_func) {
131
ERR_FAIL_COND(protocols.has(p_protocol));
132
protocols[p_protocol] = p_func;
133
}
134
135
void EditorDebuggerServer::initialize() {
136
register_protocol_handler("tcp://", EditorDebuggerServerTCP::create);
137
}
138
139
void EditorDebuggerServer::deinitialize() {
140
protocols.clear();
141
}
142
143