From 39f3d9d59e80d5a9f62925abca596d87ce1e4d0f Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Sat, 22 Oct 2022 17:49:40 +0200 Subject: [MP] Add MultiplayerPeer disconnect_peer, close. Update ENet, WebRTC, and WebSocket to support peer disconnection and unify the close function. --- .../webrtc/doc_classes/WebRTCMultiplayerPeer.xml | 6 ----- modules/webrtc/webrtc_multiplayer_peer.cpp | 13 +++++++++- modules/webrtc/webrtc_multiplayer_peer.h | 29 +++++++++++----------- 3 files changed, 27 insertions(+), 21 deletions(-) (limited to 'modules/webrtc') diff --git a/modules/webrtc/doc_classes/WebRTCMultiplayerPeer.xml b/modules/webrtc/doc_classes/WebRTCMultiplayerPeer.xml index 0b42c6ed35..91ee65e9bd 100644 --- a/modules/webrtc/doc_classes/WebRTCMultiplayerPeer.xml +++ b/modules/webrtc/doc_classes/WebRTCMultiplayerPeer.xml @@ -22,12 +22,6 @@ Three channels will be created for reliable, unreliable, and ordered transport. The value of [code]unreliable_lifetime[/code] will be passed to the [code]maxPacketLifetime[/code] option when creating unreliable and ordered channels (see [method WebRTCPeerConnection.create_data_channel]). - - - - Close all the add peer connections and channels, freeing all resources. - - diff --git a/modules/webrtc/webrtc_multiplayer_peer.cpp b/modules/webrtc/webrtc_multiplayer_peer.cpp index 5ea81d5a1b..163b17fa6f 100644 --- a/modules/webrtc/webrtc_multiplayer_peer.cpp +++ b/modules/webrtc/webrtc_multiplayer_peer.cpp @@ -42,7 +42,6 @@ void WebRTCMultiplayerPeer::_bind_methods() { ClassDB::bind_method(D_METHOD("has_peer", "peer_id"), &WebRTCMultiplayerPeer::has_peer); ClassDB::bind_method(D_METHOD("get_peer", "peer_id"), &WebRTCMultiplayerPeer::get_peer); ClassDB::bind_method(D_METHOD("get_peers"), &WebRTCMultiplayerPeer::get_peers); - ClassDB::bind_method(D_METHOD("close"), &WebRTCMultiplayerPeer::close); } void WebRTCMultiplayerPeer::set_target_peer(int p_peer_id) { @@ -352,6 +351,18 @@ void WebRTCMultiplayerPeer::remove_peer(int p_peer_id) { } } +void WebRTCMultiplayerPeer::disconnect_peer(int p_peer_id, bool p_force) { + ERR_FAIL_COND(!peer_map.has(p_peer_id)); + if (p_force) { + peer_map.erase(p_peer_id); + if (network_mode == MODE_CLIENT && p_peer_id == TARGET_PEER_SERVER) { + connection_status = CONNECTION_DISCONNECTED; + } + } else { + peer_map[p_peer_id]->connection->close(); // Will be removed during next poll. + } +} + Error WebRTCMultiplayerPeer::get_packet(const uint8_t **r_buffer, int &r_buffer_size) { // Peer not available if (next_packet_peer == 0 || !peer_map.has(next_packet_peer)) { diff --git a/modules/webrtc/webrtc_multiplayer_peer.h b/modules/webrtc/webrtc_multiplayer_peer.h index 3f608200fd..0556ef029c 100644 --- a/modules/webrtc/webrtc_multiplayer_peer.h +++ b/modules/webrtc/webrtc_multiplayer_peer.h @@ -98,28 +98,29 @@ public: bool has_peer(int p_peer_id); Dictionary get_peer(int p_peer_id); Dictionary get_peers(); - void close(); // PacketPeer - Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; ///< buffer is GONE after next get_packet - Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override; - int get_available_packet_count() const override; - int get_max_packet_size() const override; + virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; ///< buffer is GONE after next get_packet + virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override; + virtual int get_available_packet_count() const override; + virtual int get_max_packet_size() const override; // MultiplayerPeer - void set_target_peer(int p_peer_id) override; + virtual void set_target_peer(int p_peer_id) override; - int get_unique_id() const override; - int get_packet_peer() const override; - int get_packet_channel() const override; - TransferMode get_packet_mode() const override; + virtual int get_unique_id() const override; + virtual int get_packet_peer() const override; + virtual int get_packet_channel() const override; + virtual TransferMode get_packet_mode() const override; - bool is_server() const override; - bool is_server_relay_supported() const override; + virtual bool is_server() const override; + virtual bool is_server_relay_supported() const override; - void poll() override; + virtual void poll() override; + virtual void close() override; + virtual void disconnect_peer(int p_peer_id, bool p_force = false) override; - ConnectionStatus get_connection_status() const override; + virtual ConnectionStatus get_connection_status() const override; }; #endif // WEBRTC_MULTIPLAYER_PEER_H -- cgit v1.2.3