From 33dda2e68a40888a7fd41e9a8d421873d9dc7fde Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Mon, 31 Oct 2022 14:18:22 +0100 Subject: [MP] Remove connection state signals from MultiplayerPeer. Now handled directly by the MultiplayerAPI implementation. --- modules/enet/enet_multiplayer_peer.cpp | 9 +------- modules/multiplayer/scene_multiplayer.cpp | 27 +++++++--------------- modules/multiplayer/scene_multiplayer.h | 3 --- .../webrtc/doc_classes/WebRTCMultiplayerPeer.xml | 2 +- modules/webrtc/webrtc_multiplayer_peer.cpp | 5 ---- modules/websocket/websocket_multiplayer_peer.cpp | 5 +--- 6 files changed, 11 insertions(+), 40 deletions(-) (limited to 'modules') diff --git a/modules/enet/enet_multiplayer_peer.cpp b/modules/enet/enet_multiplayer_peer.cpp index 31ae643b59..43c512ae16 100644 --- a/modules/enet/enet_multiplayer_peer.cpp +++ b/modules/enet/enet_multiplayer_peer.cpp @@ -159,10 +159,7 @@ void ENetMultiplayerPeer::_disconnect_inactive_peers() { if (hosts.has(P)) { hosts.erase(P); } - if (active_mode == MODE_CLIENT) { - ERR_CONTINUE(P != TARGET_PEER_SERVER); - emit_signal(SNAME("server_disconnected")); - } + ERR_CONTINUE(active_mode == MODE_CLIENT && P != TARGET_PEER_SERVER); emit_signal(SNAME("peer_disconnected"), P); } } @@ -186,14 +183,10 @@ void ENetMultiplayerPeer::poll() { if (ret == ENetConnection::EVENT_CONNECT) { connection_status = CONNECTION_CONNECTED; emit_signal(SNAME("peer_connected"), 1); - emit_signal(SNAME("connection_succeeded")); } else if (ret == ENetConnection::EVENT_DISCONNECT) { if (connection_status == CONNECTION_CONNECTED) { // Client just disconnected from server. - emit_signal(SNAME("server_disconnected")); emit_signal(SNAME("peer_disconnected"), 1); - } else { - emit_signal(SNAME("connection_failed")); } close(); } else if (ret == ENetConnection::EVENT_RECEIVE) { diff --git a/modules/multiplayer/scene_multiplayer.cpp b/modules/multiplayer/scene_multiplayer.cpp index 73c4346d93..db7c5037cd 100644 --- a/modules/multiplayer/scene_multiplayer.cpp +++ b/modules/multiplayer/scene_multiplayer.cpp @@ -55,6 +55,11 @@ void SceneMultiplayer::_update_status() { MultiplayerPeer::ConnectionStatus status = multiplayer_peer.is_valid() ? multiplayer_peer->get_connection_status() : MultiplayerPeer::CONNECTION_DISCONNECTED; if (last_connection_status != status) { if (status == MultiplayerPeer::CONNECTION_DISCONNECTED) { + if (last_connection_status == MultiplayerPeer::CONNECTION_CONNECTING) { + emit_signal(SNAME("connection_failed")); + } else { + emit_signal(SNAME("server_disconnected")); + } clear(); } last_connection_status = status; @@ -195,9 +200,6 @@ void SceneMultiplayer::set_multiplayer_peer(const Ref &p_peer) if (multiplayer_peer.is_valid()) { multiplayer_peer->disconnect("peer_connected", callable_mp(this, &SceneMultiplayer::_add_peer)); multiplayer_peer->disconnect("peer_disconnected", callable_mp(this, &SceneMultiplayer::_del_peer)); - multiplayer_peer->disconnect("connection_succeeded", callable_mp(this, &SceneMultiplayer::_connected_to_server)); - multiplayer_peer->disconnect("connection_failed", callable_mp(this, &SceneMultiplayer::_connection_failed)); - multiplayer_peer->disconnect("server_disconnected", callable_mp(this, &SceneMultiplayer::_server_disconnected)); clear(); } @@ -206,9 +208,6 @@ void SceneMultiplayer::set_multiplayer_peer(const Ref &p_peer) if (multiplayer_peer.is_valid()) { multiplayer_peer->connect("peer_connected", callable_mp(this, &SceneMultiplayer::_add_peer)); multiplayer_peer->connect("peer_disconnected", callable_mp(this, &SceneMultiplayer::_del_peer)); - multiplayer_peer->connect("connection_succeeded", callable_mp(this, &SceneMultiplayer::_connected_to_server)); - multiplayer_peer->connect("connection_failed", callable_mp(this, &SceneMultiplayer::_connection_failed)); - multiplayer_peer->connect("server_disconnected", callable_mp(this, &SceneMultiplayer::_server_disconnected)); } _update_status(); } @@ -385,6 +384,9 @@ void SceneMultiplayer::_admit_peer(int p_id) { connected_peers.insert(p_id); cache->on_peer_change(p_id, true); replicator->on_peer_change(p_id, true); + if (p_id == 1) { + emit_signal(SNAME("connected_to_server")); + } emit_signal(SNAME("peer_connected"), p_id); } @@ -430,19 +432,6 @@ void SceneMultiplayer::disconnect_peer(int p_id) { multiplayer_peer->disconnect_peer(p_id); } -void SceneMultiplayer::_connected_to_server() { - emit_signal(SNAME("connected_to_server")); -} - -void SceneMultiplayer::_connection_failed() { - emit_signal(SNAME("connection_failed")); -} - -void SceneMultiplayer::_server_disconnected() { - replicator->on_reset(); - emit_signal(SNAME("server_disconnected")); -} - Error SceneMultiplayer::send_bytes(Vector p_data, int p_to, MultiplayerPeer::TransferMode p_mode, int p_channel) { ERR_FAIL_COND_V_MSG(p_data.size() < 1, ERR_INVALID_DATA, "Trying to send an empty raw packet."); ERR_FAIL_COND_V_MSG(!multiplayer_peer.is_valid(), ERR_UNCONFIGURED, "Trying to send a raw packet while no multiplayer peer is active."); diff --git a/modules/multiplayer/scene_multiplayer.h b/modules/multiplayer/scene_multiplayer.h index a8e182db92..b0ecc48f8c 100644 --- a/modules/multiplayer/scene_multiplayer.h +++ b/modules/multiplayer/scene_multiplayer.h @@ -113,9 +113,6 @@ protected: void _add_peer(int p_id); void _admit_peer(int p_id); void _del_peer(int p_id); - void _connected_to_server(); - void _connection_failed(); - void _server_disconnected(); void _update_status(); public: diff --git a/modules/webrtc/doc_classes/WebRTCMultiplayerPeer.xml b/modules/webrtc/doc_classes/WebRTCMultiplayerPeer.xml index 91ee65e9bd..5266a36637 100644 --- a/modules/webrtc/doc_classes/WebRTCMultiplayerPeer.xml +++ b/modules/webrtc/doc_classes/WebRTCMultiplayerPeer.xml @@ -6,7 +6,7 @@ This class constructs a full mesh of [WebRTCPeerConnection] (one connection for each peer) that can be used as a [member MultiplayerAPI.multiplayer_peer]. You can add each [WebRTCPeerConnection] via [method add_peer] or remove them via [method remove_peer]. Peers must be added in [constant WebRTCPeerConnection.STATE_NEW] state to allow it to create the appropriate channels. This class will not create offers nor set descriptions, it will only poll them, and notify connections and disconnections. - [signal MultiplayerPeer.connection_succeeded] and [signal MultiplayerPeer.server_disconnected] will not be emitted unless the peer is created using [method create_client]. Beside that data transfer works like in a [MultiplayerPeer]. + When creating the peer via [method create_client] or [method create_server] the [method MultiplayerPeer.is_server_relay_supported] method will return [code]true[/code] enabling peer exchange and packet relaying when supported by the [MultiplayerAPI] implementation. [b]Note:[/b] When exporting to Android, make sure to enable the [code]INTERNET[/code] permission in the Android export preset before exporting the project or using one-click deploy. Otherwise, network communication of any kind will be blocked by Android. diff --git a/modules/webrtc/webrtc_multiplayer_peer.cpp b/modules/webrtc/webrtc_multiplayer_peer.cpp index 163b17fa6f..38c33a2dbc 100644 --- a/modules/webrtc/webrtc_multiplayer_peer.cpp +++ b/modules/webrtc/webrtc_multiplayer_peer.cpp @@ -341,11 +341,6 @@ void WebRTCMultiplayerPeer::remove_peer(int p_peer_id) { peer->connected = false; emit_signal(SNAME("peer_disconnected"), p_peer_id); if (network_mode == MODE_CLIENT && p_peer_id == TARGET_PEER_SERVER) { - if (connection_status == CONNECTION_CONNECTING) { - emit_signal(SNAME("connection_failed")); - } else { - emit_signal(SNAME("server_disconnected")); - } connection_status = CONNECTION_DISCONNECTED; } } diff --git a/modules/websocket/websocket_multiplayer_peer.cpp b/modules/websocket/websocket_multiplayer_peer.cpp index 827c618e4e..14f9c0ba4d 100644 --- a/modules/websocket/websocket_multiplayer_peer.cpp +++ b/modules/websocket/websocket_multiplayer_peer.cpp @@ -264,9 +264,7 @@ void WebSocketMultiplayerPeer::_poll_client() { } } else if (peer->get_ready_state() == WebSocketPeer::STATE_CLOSED) { if (connection_status == CONNECTION_CONNECTED) { - emit_signal(SNAME("server_disconnected")); - } else { - emit_signal(SNAME("connection_failed")); + emit_signal(SNAME("peer_disconnected"), 1); } _clear(); return; @@ -276,7 +274,6 @@ void WebSocketMultiplayerPeer::_poll_client() { 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)); - emit_signal(SNAME("connection_failed")); _clear(); return; } -- cgit v1.2.3