2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* remote_debugger_peer_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 17:02:14 +01:00
|
|
|
|
|
|
|
#include "remote_debugger_peer_websocket.h"
|
|
|
|
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/config/project_settings.h"
|
2020-03-16 17:02:14 +01:00
|
|
|
|
|
|
|
Error RemoteDebuggerPeerWebSocket::connect_to_host(const String &p_uri) {
|
2022-10-13 16:58:00 +02:00
|
|
|
ws_peer = Ref<WebSocketPeer>(WebSocketPeer::create());
|
|
|
|
ERR_FAIL_COND_V(ws_peer.is_null(), ERR_BUG);
|
|
|
|
|
2020-03-16 17:02:14 +01:00
|
|
|
Vector<String> protocols;
|
|
|
|
protocols.push_back("binary"); // Compatibility for emscripten TCP-to-WebSocket.
|
|
|
|
|
[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 22:44:44 +02:00
|
|
|
ws_peer->set_supported_protocols(protocols);
|
|
|
|
ws_peer->set_max_queued_packets(max_queued_messages);
|
|
|
|
ws_peer->set_inbound_buffer_size((1 << 23) - 1);
|
|
|
|
ws_peer->set_outbound_buffer_size((1 << 23) - 1);
|
|
|
|
|
|
|
|
Error err = ws_peer->connect_to_url(p_uri);
|
|
|
|
ERR_FAIL_COND_V(err != OK, err);
|
|
|
|
|
|
|
|
ws_peer->poll();
|
|
|
|
WebSocketPeer::State ready_state = ws_peer->get_ready_state();
|
|
|
|
if (ready_state != WebSocketPeer::STATE_CONNECTING && ready_state != WebSocketPeer::STATE_OPEN) {
|
|
|
|
ERR_PRINT(vformat("Remote Debugger: Unable to connect. State: %s.", ws_peer->get_ready_state()));
|
2020-03-16 17:02:14 +01:00
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RemoteDebuggerPeerWebSocket::is_peer_connected() {
|
[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 22:44:44 +02:00
|
|
|
return ws_peer.is_valid() && (ws_peer->get_ready_state() == WebSocketPeer::STATE_OPEN || ws_peer->get_ready_state() == WebSocketPeer::STATE_CONNECTING);
|
2020-03-16 17:02:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteDebuggerPeerWebSocket::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 22:44:44 +02:00
|
|
|
ERR_FAIL_COND(ws_peer.is_null());
|
|
|
|
ws_peer->poll();
|
2020-03-16 17:02:14 +01:00
|
|
|
|
[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 22:44:44 +02:00
|
|
|
while (ws_peer->get_ready_state() == WebSocketPeer::STATE_OPEN && ws_peer->get_available_packet_count() > 0 && in_queue.size() < max_queued_messages) {
|
2020-03-16 17:02:14 +01:00
|
|
|
Variant var;
|
|
|
|
Error err = ws_peer->get_var(var);
|
|
|
|
ERR_CONTINUE(err != OK);
|
|
|
|
ERR_CONTINUE(var.get_type() != Variant::ARRAY);
|
|
|
|
in_queue.push_back(var);
|
|
|
|
}
|
|
|
|
|
[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 22:44:44 +02:00
|
|
|
while (ws_peer->get_ready_state() == WebSocketPeer::STATE_OPEN && out_queue.size() > 0) {
|
2024-04-15 15:18:34 +02:00
|
|
|
Array var = out_queue.front()->get();
|
2020-03-16 17:02:14 +01:00
|
|
|
Error err = ws_peer->put_var(var);
|
|
|
|
ERR_BREAK(err != OK); // Peer buffer full?
|
|
|
|
out_queue.pop_front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int RemoteDebuggerPeerWebSocket::get_max_message_size() const {
|
2022-10-13 16:58:00 +02:00
|
|
|
ERR_FAIL_COND_V(ws_peer.is_null(), 0);
|
[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 22:44:44 +02:00
|
|
|
return ws_peer->get_max_packet_size();
|
2020-03-16 17:02:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RemoteDebuggerPeerWebSocket::has_message() {
|
|
|
|
return in_queue.size() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Array RemoteDebuggerPeerWebSocket::get_message() {
|
2024-01-19 13:21:39 +01:00
|
|
|
ERR_FAIL_COND_V(in_queue.is_empty(), Array());
|
2024-04-15 15:18:34 +02:00
|
|
|
Array msg = in_queue.front()->get();
|
2020-03-16 17:02:14 +01:00
|
|
|
in_queue.pop_front();
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error RemoteDebuggerPeerWebSocket::put_message(const Array &p_arr) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (out_queue.size() >= max_queued_messages) {
|
2020-03-16 17:02:14 +01:00
|
|
|
return ERR_OUT_OF_MEMORY;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2020-03-16 17:02:14 +01:00
|
|
|
out_queue.push_back(p_arr);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteDebuggerPeerWebSocket::close() {
|
|
|
|
if (ws_peer.is_valid()) {
|
|
|
|
ws_peer.unref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RemoteDebuggerPeerWebSocket::can_block() const {
|
2022-08-28 20:27:45 +02:00
|
|
|
#ifdef WEB_ENABLED
|
2020-03-16 17:02:14 +01:00
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoteDebuggerPeerWebSocket::RemoteDebuggerPeerWebSocket(Ref<WebSocketPeer> p_peer) {
|
|
|
|
max_queued_messages = (int)GLOBAL_GET("network/limits/debugger/max_queued_messages");
|
|
|
|
ws_peer = p_peer;
|
[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 22:44:44 +02:00
|
|
|
if (ws_peer.is_valid()) {
|
|
|
|
ws_peer->set_max_queued_packets(max_queued_messages);
|
|
|
|
ws_peer->set_inbound_buffer_size((1 << 23) - 1);
|
|
|
|
ws_peer->set_outbound_buffer_size((1 << 23) - 1);
|
|
|
|
}
|
2020-03-16 17:02:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
RemoteDebuggerPeer *RemoteDebuggerPeerWebSocket::create(const String &p_uri) {
|
2020-05-14 10:15:48 +02:00
|
|
|
ERR_FAIL_COND_V(!p_uri.begins_with("ws://") && !p_uri.begins_with("wss://"), nullptr);
|
2020-03-16 17:02:14 +01:00
|
|
|
RemoteDebuggerPeerWebSocket *peer = memnew(RemoteDebuggerPeerWebSocket);
|
|
|
|
Error err = peer->connect_to_host(p_uri);
|
|
|
|
if (err != OK) {
|
|
|
|
memdelete(peer);
|
2020-05-14 10:15:48 +02:00
|
|
|
return nullptr;
|
2020-03-16 17:02:14 +01:00
|
|
|
}
|
|
|
|
return peer;
|
|
|
|
}
|