2017-12-21 02:13:23 +00:00
|
|
|
/**************************************************************************/
|
2019-02-12 21:28:47 +00:00
|
|
|
/* websocket_multiplayer_peer.cpp */
|
2017-12-21 02:13:23 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
2017-12-21 02:47:16 +00:00
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
2017-12-21 02:13:23 +00:00
|
|
|
/**************************************************************************/
|
2018-03-21 09:07:51 +00:00
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
2017-12-21 02:13:23 +00:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
2019-01-01 11:46:36 +00:00
|
|
|
|
2019-02-12 21:28:47 +00:00
|
|
|
#include "websocket_multiplayer_peer.h"
|
|
|
|
|
2017-12-21 02:13:23 +00:00
|
|
|
#include "core/os/os.h"
|
|
|
|
|
|
|
|
WebSocketMultiplayerPeer::WebSocketMultiplayerPeer() {
|
[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
|
|
|
peer_config = Ref<WebSocketPeer>(WebSocketPeer::create());
|
2017-12-21 02:13:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WebSocketMultiplayerPeer::~WebSocketMultiplayerPeer() {
|
|
|
|
_clear();
|
|
|
|
}
|
|
|
|
|
[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
|
|
|
Ref<WebSocketPeer> WebSocketMultiplayerPeer::_create_peer() {
|
|
|
|
Ref<WebSocketPeer> peer = Ref<WebSocketPeer>(WebSocketPeer::create());
|
|
|
|
peer->set_supported_protocols(get_supported_protocols());
|
|
|
|
peer->set_handshake_headers(get_handshake_headers());
|
|
|
|
peer->set_inbound_buffer_size(get_inbound_buffer_size());
|
|
|
|
peer->set_outbound_buffer_size(get_outbound_buffer_size());
|
|
|
|
peer->set_max_queued_packets(get_max_queued_packets());
|
|
|
|
return peer;
|
|
|
|
}
|
|
|
|
|
2017-12-21 02:13:23 +00:00
|
|
|
void WebSocketMultiplayerPeer::_clear() {
|
[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
|
|
|
connection_status = CONNECTION_DISCONNECTED;
|
|
|
|
unique_id = 0;
|
|
|
|
peers_map.clear();
|
|
|
|
tcp_server.unref();
|
|
|
|
pending_peers.clear();
|
2023-01-20 00:51:35 +00:00
|
|
|
tls_server_options.unref();
|
[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 (current_packet.data != nullptr) {
|
|
|
|
memfree(current_packet.data);
|
2022-10-13 16:04:12 +00:00
|
|
|
current_packet.data = nullptr;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-12-21 02:13:23 +00: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 20:44:44 +00:00
|
|
|
for (Packet &E : incoming_packets) {
|
2021-07-16 03:45:57 +00:00
|
|
|
memfree(E.data);
|
|
|
|
E.data = nullptr;
|
2017-12-21 02:13:23 +00: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 20:44:44 +00:00
|
|
|
incoming_packets.clear();
|
2017-12-21 02:13:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketMultiplayerPeer::_bind_methods() {
|
2023-01-20 00:51:35 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("create_client", "url", "tls_client_options"), &WebSocketMultiplayerPeer::create_client, DEFVAL(Ref<TLSOptions>()));
|
|
|
|
ClassDB::bind_method(D_METHOD("create_server", "port", "bind_address", "tls_server_options"), &WebSocketMultiplayerPeer::create_server, DEFVAL("*"), DEFVAL(Ref<TLSOptions>()));
|
[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
|
|
|
|
2017-12-21 02:13:23 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_peer", "peer_id"), &WebSocketMultiplayerPeer::get_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 20:44:44 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_peer_address", "id"), &WebSocketMultiplayerPeer::get_peer_address);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_peer_port", "id"), &WebSocketMultiplayerPeer::get_peer_port);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_supported_protocols"), &WebSocketMultiplayerPeer::get_supported_protocols);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_supported_protocols", "protocols"), &WebSocketMultiplayerPeer::set_supported_protocols);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_handshake_headers"), &WebSocketMultiplayerPeer::get_handshake_headers);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_handshake_headers", "protocols"), &WebSocketMultiplayerPeer::set_handshake_headers);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_inbound_buffer_size"), &WebSocketMultiplayerPeer::get_inbound_buffer_size);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_inbound_buffer_size", "buffer_size"), &WebSocketMultiplayerPeer::set_inbound_buffer_size);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_outbound_buffer_size"), &WebSocketMultiplayerPeer::get_outbound_buffer_size);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_outbound_buffer_size", "buffer_size"), &WebSocketMultiplayerPeer::set_outbound_buffer_size);
|
2017-12-21 02:13:23 +00: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 20:44:44 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_handshake_timeout"), &WebSocketMultiplayerPeer::get_handshake_timeout);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_handshake_timeout", "timeout"), &WebSocketMultiplayerPeer::set_handshake_timeout);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_max_queued_packets", "max_queued_packets"), &WebSocketMultiplayerPeer::set_max_queued_packets);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_max_queued_packets"), &WebSocketMultiplayerPeer::get_max_queued_packets);
|
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "supported_protocols"), "set_supported_protocols", "get_supported_protocols");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "handshake_headers"), "set_handshake_headers", "get_handshake_headers");
|
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "inbound_buffer_size"), "set_inbound_buffer_size", "get_inbound_buffer_size");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "outbound_buffer_size"), "set_outbound_buffer_size", "get_outbound_buffer_size");
|
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "handshake_timeout"), "set_handshake_timeout", "get_handshake_timeout");
|
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "max_queued_packets"), "set_max_queued_packets", "get_max_queued_packets");
|
2017-12-21 02:13:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// PacketPeer
|
|
|
|
//
|
|
|
|
int WebSocketMultiplayerPeer::get_available_packet_count() 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 incoming_packets.size();
|
2017-12-21 02:13:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Error WebSocketMultiplayerPeer::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
|
[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
|
|
|
ERR_FAIL_COND_V(get_connection_status() != CONNECTION_CONNECTED, ERR_UNCONFIGURED);
|
2017-12-21 02:13:23 +00:00
|
|
|
|
2019-01-27 13:59:25 +00:00
|
|
|
r_buffer_size = 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 20:44:44 +00:00
|
|
|
if (current_packet.data != nullptr) {
|
|
|
|
memfree(current_packet.data);
|
|
|
|
current_packet.data = nullptr;
|
2017-12-21 02:13:23 +00: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 20:44:44 +00:00
|
|
|
ERR_FAIL_COND_V(incoming_packets.size() == 0, ERR_UNAVAILABLE);
|
2021-04-30 13:36:14 +00: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 20:44:44 +00:00
|
|
|
current_packet = incoming_packets.front()->get();
|
|
|
|
incoming_packets.pop_front();
|
2017-12-21 02:13:23 +00: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 20:44:44 +00:00
|
|
|
*r_buffer = current_packet.data;
|
|
|
|
r_buffer_size = current_packet.size;
|
2017-12-21 02:13:23 +00:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error WebSocketMultiplayerPeer::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
|
[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
|
|
|
ERR_FAIL_COND_V(get_connection_status() != CONNECTION_CONNECTED, ERR_UNCONFIGURED);
|
2017-12-21 02:13:23 +00:00
|
|
|
|
|
|
|
if (is_server()) {
|
2022-10-08 18:50:19 +00:00
|
|
|
if (target_peer > 0) {
|
|
|
|
ERR_FAIL_COND_V_MSG(!peers_map.has(target_peer), ERR_INVALID_PARAMETER, "Peer not found: " + itos(target_peer));
|
|
|
|
get_peer(target_peer)->put_packet(p_buffer, p_buffer_size);
|
|
|
|
} else {
|
|
|
|
for (KeyValue<int, Ref<WebSocketPeer>> &E : peers_map) {
|
|
|
|
if (target_peer && -target_peer == E.key) {
|
|
|
|
continue; // Excluded.
|
|
|
|
}
|
|
|
|
E.value->put_packet(p_buffer, p_buffer_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return OK;
|
2017-12-21 02:13:23 +00:00
|
|
|
} else {
|
2022-10-08 18:50:19 +00:00
|
|
|
return get_peer(1)->put_packet(p_buffer, p_buffer_size);
|
2017-12-21 02:13:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2021-07-12 14:11:05 +00:00
|
|
|
// MultiplayerPeer
|
2017-12-21 02:13:23 +00:00
|
|
|
//
|
|
|
|
void WebSocketMultiplayerPeer::set_target_peer(int p_target_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 20:44:44 +00:00
|
|
|
target_peer = p_target_peer;
|
2017-12-21 02:13:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int WebSocketMultiplayerPeer::get_packet_peer() 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
|
|
|
ERR_FAIL_COND_V(incoming_packets.size() == 0, 1);
|
2017-12-21 02:13:23 +00: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 20:44:44 +00:00
|
|
|
return incoming_packets.front()->get().source;
|
2017-12-21 02:13:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int WebSocketMultiplayerPeer::get_unique_id() 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 unique_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WebSocketMultiplayerPeer::get_max_packet_size() const {
|
|
|
|
return get_outbound_buffer_size() - PROTO_SIZE;
|
|
|
|
}
|
|
|
|
|
2023-01-20 00:51:35 +00:00
|
|
|
Error WebSocketMultiplayerPeer::create_server(int p_port, IPAddress p_bind_ip, Ref<TLSOptions> p_options) {
|
[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
|
|
|
ERR_FAIL_COND_V(get_connection_status() != CONNECTION_DISCONNECTED, ERR_ALREADY_IN_USE);
|
2023-01-20 00:51:35 +00:00
|
|
|
ERR_FAIL_COND_V(p_options.is_valid() && !p_options->is_server(), ERR_INVALID_PARAMETER);
|
[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
|
|
|
_clear();
|
|
|
|
tcp_server.instantiate();
|
|
|
|
Error err = tcp_server->listen(p_port, p_bind_ip);
|
|
|
|
if (err != OK) {
|
|
|
|
tcp_server.unref();
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
unique_id = 1;
|
|
|
|
connection_status = CONNECTION_CONNECTED;
|
2023-01-20 00:51:35 +00:00
|
|
|
tls_server_options = p_options;
|
[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 OK;
|
|
|
|
}
|
|
|
|
|
2023-01-20 00:51:35 +00:00
|
|
|
Error WebSocketMultiplayerPeer::create_client(const String &p_url, Ref<TLSOptions> p_options) {
|
[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
|
|
|
ERR_FAIL_COND_V(get_connection_status() != CONNECTION_DISCONNECTED, ERR_ALREADY_IN_USE);
|
2023-01-20 00:51:35 +00:00
|
|
|
ERR_FAIL_COND_V(p_options.is_valid() && p_options->is_server(), ERR_INVALID_PARAMETER);
|
[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
|
|
|
_clear();
|
|
|
|
Ref<WebSocketPeer> peer = _create_peer();
|
2023-01-20 00:51:35 +00:00
|
|
|
Error err = peer->connect_to_url(p_url, p_options);
|
[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 (err != OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
PendingPeer pending;
|
|
|
|
pending.time = OS::get_singleton()->get_ticks_msec();
|
|
|
|
pending_peers[1] = pending;
|
|
|
|
peers_map[1] = peer;
|
|
|
|
connection_status = CONNECTION_CONNECTING;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebSocketMultiplayerPeer::is_server() const {
|
|
|
|
return tcp_server.is_valid();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketMultiplayerPeer::_poll_client() {
|
|
|
|
ERR_FAIL_COND(connection_status == CONNECTION_DISCONNECTED); // Bug.
|
|
|
|
ERR_FAIL_COND(!peers_map.has(1) || peers_map[1].is_null()); // Bug.
|
|
|
|
Ref<WebSocketPeer> peer = peers_map[1];
|
|
|
|
peer->poll(); // Update state and fetch packets.
|
|
|
|
WebSocketPeer::State ready_state = peer->get_ready_state();
|
|
|
|
if (ready_state == WebSocketPeer::STATE_OPEN) {
|
2022-10-08 18:50:19 +00:00
|
|
|
if (connection_status == CONNECTION_CONNECTING) {
|
|
|
|
if (peer->get_available_packet_count() > 0) {
|
|
|
|
const uint8_t *in_buffer;
|
|
|
|
int size = 0;
|
|
|
|
Error err = peer->get_packet(&in_buffer, size);
|
|
|
|
if (err != OK || size != 4) {
|
|
|
|
peer->close(); // Will cause connection error on next poll.
|
|
|
|
ERR_FAIL_MSG("Invalid ID received from server");
|
|
|
|
}
|
|
|
|
unique_id = *((int32_t *)in_buffer);
|
|
|
|
if (unique_id < 2) {
|
|
|
|
peer->close(); // Will cause connection error on next poll.
|
|
|
|
ERR_FAIL_MSG("Invalid ID received from server");
|
|
|
|
}
|
|
|
|
connection_status = CONNECTION_CONNECTED;
|
|
|
|
emit_signal("peer_connected", 1);
|
|
|
|
} else {
|
|
|
|
return; // Still waiting for an ID.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int pkts = peer->get_available_packet_count();
|
|
|
|
while (pkts > 0 && peer->get_ready_state() == WebSocketPeer::STATE_OPEN) {
|
|
|
|
const uint8_t *in_buffer;
|
|
|
|
int size = 0;
|
|
|
|
Error err = peer->get_packet(&in_buffer, size);
|
|
|
|
ERR_FAIL_COND(err != OK);
|
|
|
|
ERR_FAIL_COND(size <= 0);
|
|
|
|
Packet packet;
|
|
|
|
packet.data = (uint8_t *)memalloc(size);
|
|
|
|
memcpy(packet.data, in_buffer, size);
|
|
|
|
packet.size = size;
|
|
|
|
packet.source = 1;
|
|
|
|
incoming_packets.push_back(packet);
|
|
|
|
pkts--;
|
[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
|
|
|
}
|
|
|
|
} else if (peer->get_ready_state() == WebSocketPeer::STATE_CLOSED) {
|
|
|
|
if (connection_status == CONNECTION_CONNECTED) {
|
2022-10-31 13:18:22 +00:00
|
|
|
emit_signal(SNAME("peer_disconnected"), 1);
|
[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
|
|
|
}
|
|
|
|
_clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (connection_status == CONNECTION_CONNECTING) {
|
|
|
|
// Still connecting
|
|
|
|
ERR_FAIL_COND(!pending_peers.has(1)); // Bug.
|
|
|
|
if (OS::get_singleton()->get_ticks_msec() - pending_peers[1].time > handshake_timeout) {
|
|
|
|
print_verbose(vformat("WebSocket handshake timed out after %.3f seconds.", handshake_timeout * 0.001));
|
|
|
|
_clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketMultiplayerPeer::_poll_server() {
|
|
|
|
ERR_FAIL_COND(connection_status != CONNECTION_CONNECTED); // Bug.
|
|
|
|
ERR_FAIL_COND(tcp_server.is_null() || !tcp_server->is_listening()); // Bug.
|
|
|
|
|
|
|
|
// Accept new connections.
|
|
|
|
if (!is_refusing_new_connections() && tcp_server->is_connection_available()) {
|
|
|
|
PendingPeer peer;
|
|
|
|
peer.time = OS::get_singleton()->get_ticks_msec();
|
|
|
|
peer.tcp = tcp_server->take_connection();
|
|
|
|
peer.connection = peer.tcp;
|
|
|
|
pending_peers[generate_unique_id()] = peer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process pending peers.
|
|
|
|
HashSet<int> to_remove;
|
|
|
|
for (KeyValue<int, PendingPeer> &E : pending_peers) {
|
|
|
|
PendingPeer &peer = E.value;
|
|
|
|
int id = E.key;
|
|
|
|
|
|
|
|
if (OS::get_singleton()->get_ticks_msec() - peer.time > handshake_timeout) {
|
|
|
|
print_verbose(vformat("WebSocket handshake timed out after %.3f seconds.", handshake_timeout * 0.001));
|
|
|
|
to_remove.insert(id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (peer.ws.is_valid()) {
|
|
|
|
peer.ws->poll();
|
|
|
|
WebSocketPeer::State state = peer.ws->get_ready_state();
|
|
|
|
if (state == WebSocketPeer::STATE_OPEN) {
|
|
|
|
// Connected.
|
|
|
|
to_remove.insert(id);
|
|
|
|
if (is_refusing_new_connections()) {
|
|
|
|
// The user does not want new connections, dropping it.
|
|
|
|
continue;
|
|
|
|
}
|
2022-10-08 18:50:19 +00:00
|
|
|
int32_t peer_id = id;
|
|
|
|
Error err = peer.ws->put_packet((const uint8_t *)&peer_id, sizeof(peer_id));
|
|
|
|
if (err == OK) {
|
|
|
|
peers_map[id] = peer.ws;
|
|
|
|
emit_signal("peer_connected", id);
|
|
|
|
} else {
|
|
|
|
ERR_PRINT("Failed to send ID to newly connected 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 20:44:44 +00:00
|
|
|
continue;
|
|
|
|
} else if (state == WebSocketPeer::STATE_CONNECTING) {
|
|
|
|
continue; // Still connecting.
|
|
|
|
}
|
|
|
|
to_remove.insert(id); // Error.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (peer.tcp->get_status() != StreamPeerTCP::STATUS_CONNECTED) {
|
|
|
|
to_remove.insert(id); // Error.
|
|
|
|
continue;
|
|
|
|
}
|
2023-01-20 00:51:35 +00:00
|
|
|
if (tls_server_options.is_null()) {
|
[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
|
|
|
peer.ws = _create_peer();
|
|
|
|
peer.ws->accept_stream(peer.tcp);
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
if (peer.connection == peer.tcp) {
|
|
|
|
Ref<StreamPeerTLS> tls = Ref<StreamPeerTLS>(StreamPeerTLS::create());
|
2023-01-20 00:51:35 +00:00
|
|
|
Error err = tls->accept_stream(peer.tcp, tls_server_options);
|
[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 (err != OK) {
|
|
|
|
to_remove.insert(id);
|
|
|
|
continue;
|
|
|
|
}
|
2023-02-23 09:59:55 +00:00
|
|
|
peer.connection = tls;
|
[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
|
|
|
}
|
|
|
|
Ref<StreamPeerTLS> tls = static_cast<Ref<StreamPeerTLS>>(peer.connection);
|
|
|
|
tls->poll();
|
|
|
|
if (tls->get_status() == StreamPeerTLS::STATUS_CONNECTED) {
|
|
|
|
peer.ws = _create_peer();
|
|
|
|
peer.ws->accept_stream(peer.connection);
|
|
|
|
continue;
|
|
|
|
} else if (tls->get_status() == StreamPeerTLS::STATUS_HANDSHAKING) {
|
|
|
|
// Still connecting.
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
// Error
|
|
|
|
to_remove.insert(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove disconnected pending peers.
|
|
|
|
for (const int &pid : to_remove) {
|
|
|
|
pending_peers.erase(pid);
|
|
|
|
}
|
|
|
|
to_remove.clear();
|
|
|
|
|
|
|
|
// Process connected peers.
|
|
|
|
for (KeyValue<int, Ref<WebSocketPeer>> &E : peers_map) {
|
|
|
|
Ref<WebSocketPeer> ws = E.value;
|
|
|
|
int id = E.key;
|
|
|
|
ws->poll();
|
|
|
|
if (ws->get_ready_state() != WebSocketPeer::STATE_OPEN) {
|
|
|
|
to_remove.insert(id); // Disconnected.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Fetch packets
|
|
|
|
int pkts = ws->get_available_packet_count();
|
2022-10-08 18:50:19 +00:00
|
|
|
while (pkts > 0 && ws->get_ready_state() == WebSocketPeer::STATE_OPEN) {
|
|
|
|
const uint8_t *in_buffer;
|
|
|
|
int size = 0;
|
|
|
|
Error err = ws->get_packet(&in_buffer, size);
|
|
|
|
if (err != OK || size <= 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Packet packet;
|
|
|
|
packet.data = (uint8_t *)memalloc(size);
|
|
|
|
memcpy(packet.data, in_buffer, size);
|
|
|
|
packet.size = size;
|
|
|
|
packet.source = E.key;
|
|
|
|
incoming_packets.push_back(packet);
|
[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
|
|
|
pkts--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove disconnected peers.
|
|
|
|
for (const int &pid : to_remove) {
|
|
|
|
emit_signal(SNAME("peer_disconnected"), pid);
|
|
|
|
peers_map.erase(pid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketMultiplayerPeer::poll() {
|
|
|
|
if (connection_status == CONNECTION_DISCONNECTED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (is_server()) {
|
|
|
|
_poll_server();
|
|
|
|
} else {
|
|
|
|
_poll_client();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MultiplayerPeer::ConnectionStatus WebSocketMultiplayerPeer::get_connection_status() const {
|
|
|
|
return connection_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<WebSocketPeer> WebSocketMultiplayerPeer::get_peer(int p_id) const {
|
|
|
|
ERR_FAIL_COND_V(!peers_map.has(p_id), Ref<WebSocketPeer>());
|
|
|
|
return peers_map[p_id];
|
2017-12-21 02:13:23 +00: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 20:44:44 +00:00
|
|
|
void WebSocketMultiplayerPeer::set_supported_protocols(const Vector<String> &p_protocols) {
|
|
|
|
peer_config->set_supported_protocols(p_protocols);
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<String> WebSocketMultiplayerPeer::get_supported_protocols() const {
|
|
|
|
return peer_config->get_supported_protocols();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketMultiplayerPeer::set_handshake_headers(const Vector<String> &p_headers) {
|
|
|
|
peer_config->set_handshake_headers(p_headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<String> WebSocketMultiplayerPeer::get_handshake_headers() const {
|
|
|
|
return peer_config->get_handshake_headers();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketMultiplayerPeer::set_outbound_buffer_size(int p_buffer_size) {
|
|
|
|
peer_config->set_outbound_buffer_size(p_buffer_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
int WebSocketMultiplayerPeer::get_outbound_buffer_size() const {
|
|
|
|
return peer_config->get_outbound_buffer_size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketMultiplayerPeer::set_inbound_buffer_size(int p_buffer_size) {
|
|
|
|
peer_config->set_inbound_buffer_size(p_buffer_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
int WebSocketMultiplayerPeer::get_inbound_buffer_size() const {
|
|
|
|
return peer_config->get_inbound_buffer_size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketMultiplayerPeer::set_max_queued_packets(int p_max_queued_packets) {
|
|
|
|
peer_config->set_max_queued_packets(p_max_queued_packets);
|
|
|
|
}
|
|
|
|
|
|
|
|
int WebSocketMultiplayerPeer::get_max_queued_packets() const {
|
|
|
|
return peer_config->get_max_queued_packets();
|
|
|
|
}
|
|
|
|
|
|
|
|
float WebSocketMultiplayerPeer::get_handshake_timeout() const {
|
|
|
|
return handshake_timeout / 1000.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketMultiplayerPeer::set_handshake_timeout(float p_timeout) {
|
|
|
|
ERR_FAIL_COND(p_timeout <= 0.0);
|
|
|
|
handshake_timeout = p_timeout * 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
IPAddress WebSocketMultiplayerPeer::get_peer_address(int p_peer_id) const {
|
|
|
|
ERR_FAIL_COND_V(!peers_map.has(p_peer_id), IPAddress());
|
|
|
|
return peers_map[p_peer_id]->get_connected_host();
|
|
|
|
}
|
|
|
|
|
|
|
|
int WebSocketMultiplayerPeer::get_peer_port(int p_peer_id) const {
|
|
|
|
ERR_FAIL_COND_V(!peers_map.has(p_peer_id), 0);
|
|
|
|
return peers_map[p_peer_id]->get_connected_port();
|
|
|
|
}
|
|
|
|
|
2022-10-22 15:49:40 +00:00
|
|
|
void WebSocketMultiplayerPeer::disconnect_peer(int p_peer_id, bool p_force) {
|
[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
|
|
|
ERR_FAIL_COND(!peers_map.has(p_peer_id));
|
2022-10-22 15:49:40 +00:00
|
|
|
peers_map[p_peer_id]->close();
|
|
|
|
if (p_force) {
|
|
|
|
peers_map.erase(p_peer_id);
|
|
|
|
if (!is_server()) {
|
|
|
|
_clear();
|
|
|
|
}
|
|
|
|
}
|
[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
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketMultiplayerPeer::close() {
|
|
|
|
_clear();
|
|
|
|
}
|