浏览代码

Merge from relay autodisconnect branch

/main/staging
nathaniel.buck@unity3d.com 3 年前
当前提交
bd3f3544
共有 5 个文件被更改,包括 52 次插入10 次删除
  1. 5
      Assets/Scripts/Lobby/LobbyAsyncRequests.cs
  2. 13
      Assets/Scripts/Lobby/LobbyContentHeartbeat.cs
  3. 23
      Assets/Scripts/Relay/RelayUtpClient.cs
  4. 19
      Assets/Scripts/Relay/RelayUtpHost.cs
  5. 2
      Assets/Scripts/Relay/RelayUtpSetup.cs

5
Assets/Scripts/Lobby/LobbyAsyncRequests.cs


}
/// <summary>
/// Will Attempt to join the first Lobby among the available lobbies that match the filter.(limitToColor)
/// Attempt to join the first lobby among the available lobbies that match the filtered limitToColor.
/// </summary>
public void QuickJoinLobbyAsync(LobbyUser localUser, LobbyColor limitToColor = LobbyColor.None, Action<Lobby> onSuccess = null, Action onFailure = null)
{

onComplete?.Invoke(null);
return;
}
onComplete?.Invoke(response);
onComplete?.Invoke(response); // FUTURE: Consider passing in the exception code here (and elsewhere) to, e.g., specifically handle a 404 indicating a Relay auto-disconnect.
}
}

13
Assets/Scripts/Lobby/LobbyContentHeartbeat.cs


var prevState = m_localLobby.State;
lobby.ToLocalLobby.Convert(lobbyRemote, m_localLobby);
m_shouldPushData = prevShouldPush;
// If the host suddenly leaves, the Lobby service will automatically handle disconnects after about 30s, but we can try to do a disconnect sooner if we detect it.
if (!m_localUser.IsHost)
{
foreach (var lobbyUser in m_localLobby.LobbyUsers)
{
if (lobbyUser.Value.IsHost)
return;
}
Locator.Get.Messenger.OnReceiveMessage(MessageType.DisplayErrorPopup, "Host left the lobby! Disconnecting...");
Locator.Get.Messenger.OnReceiveMessage(MessageType.EndGame, null);
Locator.Get.Messenger.OnReceiveMessage(MessageType.ChangeGameState, GameState.JoinMenu);
}
}
}

23
Assets/Scripts/Relay/RelayUtpClient.cs


protected LocalLobby m_localLobby;
protected NetworkDriver m_networkDriver;
protected List<NetworkConnection> m_connections; // For clients, this has just one member, but for hosts it will have more.
protected bool m_IsRelayConnected { get { return m_localLobby.RelayServer != null; } }
protected bool m_hasSentInitialMessage = false;
private const float k_heartbeatPeriod = 5;

m_localUser.onChanged -= OnLocalChange;
Leave();
Locator.Get.UpdateSlow.Unsubscribe(UpdateSlow);
m_connections.Clear();
m_networkDriver.Dispose();
}
public void OnDestroy()

OnUpdate();
}
/// <summary>
/// Clients need to send any data over UTP periodically, or else Relay will remove them from the allocation.
/// </summary>
// Clients need to send any data over UTP periodically, or else the connection will timeout.
if (!m_IsRelayConnected) // If disconnected from Relay for some reason, we *want* this client to timeout.
return;
foreach (NetworkConnection connection in m_connections)
WriteByte(m_networkDriver, connection, "0", MsgType.Ping, 0); // The ID doesn't matter here, so send a minimal number of bytes.
}

protected virtual void ProcessDisconnectEvent(NetworkConnection conn, DataStreamReader strm)
{
// The host disconnected, and Relay does not support host migration. So, all clients should disconnect.
string msg = "Host disconnected! Leaving the lobby.";
string msg;
if (m_IsRelayConnected)
msg = "Host disconnected! Leaving the lobby.";
else
msg = "Connection to host was lost. Leaving the lobby.";
Debug.LogError(msg);
Locator.Get.Messenger.OnReceiveMessage(MessageType.DisplayErrorPopup, msg);
Leave();

}
}
public void Leave()
/// <summary>
/// Disconnect from Relay, usually while leaving the lobby. (You can also call this elsewhere to see how Lobby will detect a Relay disconnect automatically.)
/// </summary>
public virtual void Leave()
connection.Disconnect(m_networkDriver);
// If the client calls Disconnect, the host might not become aware right away (depending on when the PubSub messages get pumped), so send a message over UTP instead.
WriteByte(m_networkDriver, connection, m_localUser.ID, MsgType.PlayerDisconnect, 0);
m_localLobby.RelayServer = null;
}
}

19
Assets/Scripts/Relay/RelayUtpHost.cs


protected override void OnUpdate()
{
if (!m_IsRelayConnected) // If Relay was disconnected somehow, stop taking actions that will keep the allocation alive.
return;
DoHeartbeat();
UpdateConnections();
}
/// <summary>

}
else if (msgType == MsgType.NewPlayer) // This ensures clients in builds are sent player state once they establish that they can send (and receive) events.
OnNewConnection(conn);
else if (msgType == MsgType.PlayerDisconnect) // Clients message the host when they intend to disconnect, or else the host ends up keeping the connection open.
{
conn.Disconnect(m_networkDriver);
UnityEngine.Debug.LogWarning("Disconnecting a client due to a disconnect message.");
return;
}
// If a client has changed state, check if this changes whether all players have readied.
if (msgType == MsgType.ReadyState)

/// <summary>
/// Clean out destroyed connections, and accept all new ones.
/// </summary>
private void DoHeartbeat()
private void UpdateConnections()
{
for (int c = m_connections.Count - 1; c >= 0; c--)
{

m_connections.Add(conn);
OnNewConnection(conn); // This ensures that clients in editors are sent player state once they establish a connection. The timing differs slightly from builds.
}
}
public override void Leave()
{
foreach (NetworkConnection connection in m_connections)
connection.Disconnect(m_networkDriver); // Note that Lobby won't receive the disconnect immediately, so its auto-disconnect takes 30-40s, if needed.
m_localLobby.RelayServer = null;
}
}
}

2
Assets/Scripts/Relay/RelayUtpSetup.cs


protected LobbyUser m_localUser;
protected Action<bool, RelayUtpClient> m_onJoinComplete;
public enum MsgType { Ping = 0, NewPlayer, ReadyState, PlayerName, Emote, StartCountdown, CancelCountdown, ConfirmInGame, EndInGame }
public enum MsgType { Ping = 0, NewPlayer, ReadyState, PlayerName, Emote, StartCountdown, CancelCountdown, ConfirmInGame, EndInGame, PlayerDisconnect }
public void BeginRelayJoin(LocalLobby localLobby, LobbyUser localUser, Action<bool, RelayUtpClient> onJoinComplete)
{

正在加载...
取消
保存