2023-01-05 12:25:55 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* editor_debugger_server_websocket.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2020-03-16 16:02:14 +00:00
|
|
|
|
|
|
|
#include "editor_debugger_server_websocket.h"
|
|
|
|
|
2022-03-28 12:10:28 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
|
|
|
|
#include "../remote_debugger_peer_websocket.h"
|
2023-06-13 14:56:21 +00:00
|
|
|
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/config/project_settings.h"
|
2021-09-30 02:06:28 +00:00
|
|
|
#include "editor/editor_log.h"
|
|
|
|
#include "editor/editor_node.h"
|
2020-03-16 16:02:14 +00:00
|
|
|
#include "editor/editor_settings.h"
|
|
|
|
|
|
|
|
void EditorDebuggerServerWebSocket::poll() {
|
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-24 20:44:44 +00:00
|
|
|
if (pending_peer.is_null() && tcp_server->is_connection_available()) {
|
2022-10-13 14:58:00 +00:00
|
|
|
Ref<WebSocketPeer> peer = Ref<WebSocketPeer>(WebSocketPeer::create());
|
|
|
|
ERR_FAIL_COND(peer.is_null()); // Bug.
|
|
|
|
|
|
|
|
Vector<String> ws_protocols;
|
|
|
|
ws_protocols.push_back("binary"); // Compatibility for emscripten TCP-to-WebSocket.
|
|
|
|
peer->set_supported_protocols(ws_protocols);
|
|
|
|
|
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-24 20:44:44 +00:00
|
|
|
Error err = peer->accept_stream(tcp_server->take_connection());
|
|
|
|
if (err == OK) {
|
|
|
|
pending_timer = OS::get_singleton()->get_ticks_msec();
|
|
|
|
pending_peer = peer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pending_peer.is_valid() && pending_peer->get_ready_state() != WebSocketPeer::STATE_OPEN) {
|
|
|
|
pending_peer->poll();
|
|
|
|
WebSocketPeer::State ready_state = pending_peer->get_ready_state();
|
|
|
|
if (ready_state != WebSocketPeer::STATE_CONNECTING && ready_state != WebSocketPeer::STATE_OPEN) {
|
|
|
|
pending_peer.unref(); // Failed.
|
|
|
|
}
|
|
|
|
if (ready_state == WebSocketPeer::STATE_CONNECTING && OS::get_singleton()->get_ticks_msec() - pending_timer > 3000) {
|
|
|
|
pending_peer.unref(); // Timeout.
|
|
|
|
}
|
|
|
|
}
|
2020-03-16 16:02:14 +00:00
|
|
|
}
|
|
|
|
|
2021-09-30 02:06:28 +00:00
|
|
|
String EditorDebuggerServerWebSocket::get_uri() const {
|
|
|
|
return endpoint;
|
|
|
|
}
|
|
|
|
|
2021-08-29 00:13:58 +00:00
|
|
|
Error EditorDebuggerServerWebSocket::start(const String &p_uri) {
|
2021-09-30 02:06:28 +00:00
|
|
|
// Default host and port
|
2022-10-18 14:43:37 +00:00
|
|
|
String bind_host = (String)EDITOR_GET("network/debug/remote_host");
|
|
|
|
int bind_port = (int)EDITOR_GET("network/debug/remote_port");
|
2021-09-30 02:06:28 +00:00
|
|
|
|
|
|
|
// Optionally override
|
2021-08-29 00:13:58 +00:00
|
|
|
if (!p_uri.is_empty() && p_uri != "ws://") {
|
|
|
|
String scheme, path;
|
|
|
|
Error err = p_uri.parse_url(scheme, bind_host, bind_port, path);
|
|
|
|
ERR_FAIL_COND_V(err != OK, ERR_INVALID_PARAMETER);
|
|
|
|
ERR_FAIL_COND_V(!bind_host.is_valid_ip_address() && bind_host != "*", ERR_INVALID_PARAMETER);
|
|
|
|
}
|
2021-09-30 02:06:28 +00:00
|
|
|
|
|
|
|
// Try listening on ports
|
|
|
|
const int max_attempts = 5;
|
|
|
|
for (int attempt = 1;; ++attempt) {
|
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-24 20:44:44 +00:00
|
|
|
const Error err = tcp_server->listen(bind_port, bind_host);
|
2021-09-30 02:06:28 +00:00
|
|
|
if (err == OK) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (attempt >= max_attempts) {
|
|
|
|
EditorNode::get_log()->add_message(vformat("Cannot listen on port %d, remote debugging unavailable.", bind_port), EditorLog::MSG_TYPE_ERROR);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
int last_port = bind_port++;
|
|
|
|
EditorNode::get_log()->add_message(vformat("Cannot listen on port %d, trying %d instead.", last_port, bind_port), EditorLog::MSG_TYPE_WARNING);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Endpoint that the client should connect to
|
|
|
|
endpoint = vformat("ws://%s:%d", bind_host, bind_port);
|
|
|
|
|
|
|
|
return OK;
|
2020-03-16 16:02:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorDebuggerServerWebSocket::stop() {
|
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-24 20:44:44 +00:00
|
|
|
pending_peer.unref();
|
|
|
|
tcp_server->stop();
|
2020-03-16 16:02:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool EditorDebuggerServerWebSocket::is_active() const {
|
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-24 20:44:44 +00:00
|
|
|
return tcp_server->is_listening();
|
2020-03-16 16:02:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool EditorDebuggerServerWebSocket::is_connection_available() const {
|
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-24 20:44:44 +00:00
|
|
|
return pending_peer.is_valid() && pending_peer->get_ready_state() == WebSocketPeer::STATE_OPEN;
|
2020-03-16 16:02:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<RemoteDebuggerPeer> EditorDebuggerServerWebSocket::take_connection() {
|
|
|
|
ERR_FAIL_COND_V(!is_connection_available(), Ref<RemoteDebuggerPeer>());
|
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-24 20:44:44 +00:00
|
|
|
RemoteDebuggerPeer *peer = memnew(RemoteDebuggerPeerWebSocket(pending_peer));
|
|
|
|
pending_peer.unref();
|
2020-03-16 16:02:14 +00:00
|
|
|
return peer;
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorDebuggerServerWebSocket::EditorDebuggerServerWebSocket() {
|
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-24 20:44:44 +00:00
|
|
|
tcp_server.instantiate();
|
2020-03-16 16:02:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EditorDebuggerServerWebSocket::~EditorDebuggerServerWebSocket() {
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorDebuggerServer *EditorDebuggerServerWebSocket::create(const String &p_protocol) {
|
2020-05-14 08:15:48 +00:00
|
|
|
ERR_FAIL_COND_V(p_protocol != "ws://", nullptr);
|
2020-03-16 16:02:14 +00:00
|
|
|
return memnew(EditorDebuggerServerWebSocket);
|
|
|
|
}
|
2022-03-28 12:10:28 +00:00
|
|
|
|
|
|
|
#endif // TOOLS_ENABLED
|