From a8950f98dd9809f5da370185d086d7027e14b76a Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Sat, 24 Sep 2022 22:44:44 +0200 Subject: [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. --- modules/websocket/websocket_multiplayer_peer.h | 73 ++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 11 deletions(-) (limited to 'modules/websocket/websocket_multiplayer_peer.h') diff --git a/modules/websocket/websocket_multiplayer_peer.h b/modules/websocket/websocket_multiplayer_peer.h index 3259e78b3b..8e7b118faa 100644 --- a/modules/websocket/websocket_multiplayer_peer.h +++ b/modules/websocket/websocket_multiplayer_peer.h @@ -32,6 +32,8 @@ #define WEBSOCKET_MULTIPLAYER_PEER_H #include "core/error/error_list.h" +#include "core/io/stream_peer_tls.h" +#include "core/io/tcp_server.h" #include "core/templates/list.h" #include "scene/main/multiplayer_peer.h" #include "websocket_peer.h" @@ -43,6 +45,7 @@ private: Vector _make_pkt(uint8_t p_type, int32_t p_from, int32_t p_to, const uint8_t *p_data, uint32_t p_data_size); void _store_pkt(int32_t p_source, int32_t p_dest, const uint8_t *p_data, uint32_t p_data_size); Error _server_relay(int32_t p_from, int32_t p_to, const uint8_t *p_buffer, uint32_t p_buffer_size); + Ref _create_peer(); protected: enum { @@ -61,19 +64,40 @@ protected: uint32_t size = 0; }; - List _incoming_packets; - HashMap> _peer_map; - Packet _current_packet; + struct PendingPeer { + uint64_t time = 0; + Ref tcp; + Ref connection; + Ref ws; + }; + + uint64_t handshake_timeout = 3000; + Ref peer_config; + HashMap pending_peers; + Ref tcp_server; + bool use_tls = false; + Ref tls_certificate; + Ref tls_key; + + ConnectionStatus connection_status = CONNECTION_DISCONNECTED; - bool _is_multiplayer = false; - int _target_peer = 0; - int _peer_id = 0; + List incoming_packets; + HashMap> peers_map; + Packet current_packet; + + int target_peer = 0; + int unique_id = 0; static void _bind_methods(); - void _send_add(int32_t p_peer_id); + void _send_ack(Ref p_peer, int32_t p_peer_id); void _send_sys(Ref p_peer, uint8_t p_type, int32_t p_peer_id); void _send_del(int32_t p_peer_id); + void _process_multiplayer(Ref p_peer, uint32_t p_peer_id); + + void _poll_client(); + void _poll_server(); + void _clear(); public: /* MultiplayerPeer */ @@ -81,17 +105,44 @@ public: int get_packet_peer() const override; int get_unique_id() const override; + virtual int get_max_packet_size() const override; + virtual bool is_server() const override; + virtual void poll() override; + virtual ConnectionStatus get_connection_status() const override; + /* PacketPeer */ virtual int get_available_packet_count() const override; virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override; /* WebSocketPeer */ - virtual Error set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets) = 0; - virtual Ref get_peer(int p_peer_id) const = 0; + virtual Ref get_peer(int p_peer_id) const; - void _process_multiplayer(Ref p_peer, uint32_t p_peer_id); - void _clear(); + Error create_client(const String &p_url, bool p_verify_tls, Ref p_tls_certificate); + Error create_server(int p_port, IPAddress p_bind_ip, Ref p_tls_key, Ref p_tls_certificate); + + void set_supported_protocols(const Vector &p_protocols); + Vector get_supported_protocols() const; + + void set_handshake_headers(const Vector &p_headers); + Vector get_handshake_headers() const; + + void set_outbound_buffer_size(int p_buffer_size); + int get_outbound_buffer_size() const; + + void set_inbound_buffer_size(int p_buffer_size); + int get_inbound_buffer_size() const; + + float get_handshake_timeout() const; + void set_handshake_timeout(float p_timeout); + + IPAddress get_peer_address(int p_peer_id) const; + int get_peer_port(int p_peer_id) const; + void disconnect_peer(int p_peer_id, int p_code = 1000, String p_reason = ""); + void close(); + + void set_max_queued_packets(int p_max_queued_packets); + int get_max_queued_packets() const; WebSocketMultiplayerPeer(); ~WebSocketMultiplayerPeer(); -- cgit v1.2.3