浏览代码

Adding in a call to CheckIfAllUsersReady in the RelayUtpHost within the lobby rate limits, so that if a player disconnects and all remaining players are readied, the countdown will begin. (If and when Wire is integrated, that might obsolesce this.)

/main/staging/ready_when_player_leaves
nathaniel.buck@unity3d.com 2 年前
当前提交
b3bdc3d6
共有 3 个文件被更改,包括 14 次插入11 次删除
  1. 10
      Assets/Scripts/Lobby/LobbyAsyncRequests.cs
  2. 2
      Assets/Scripts/Relay/RelayUtpClient.cs
  3. 13
      Assets/Scripts/Relay/RelayUtpHost.cs

10
Assets/Scripts/Lobby/LobbyAsyncRequests.cs


private float m_timeSinceLastCall = float.MaxValue;
private readonly float m_cooldownTime;
private Queue<Action> m_pendingOperations = new Queue<Action>();
private bool m_isHandlingPending = false; // Just in case a pending operation tries to enqueue itself again.
if (!m_isHandlingPending)
m_pendingOperations.Enqueue(action);
m_pendingOperations.Enqueue(action);
}
private bool m_isInCooldown = false;

private void OnUpdate(float dt)
{
m_timeSinceLastCall += dt;
m_isHandlingPending = false; // (Backup in case a pending operation hit an exception.)
if (m_timeSinceLastCall >= m_cooldownTime)
{
IsInCooldown = false;

m_isHandlingPending = true;
while (m_pendingOperations.Count > 0)
int numPending = m_pendingOperations.Count; // It's possible a pending operation will re-enqueue itself or new operations, which should wait until the next loop.
for (; numPending > 0; numPending--)
m_isHandlingPending = false;
}
}
}

2
Assets/Scripts/Relay/RelayUtpClient.cs


// The host disconnected, and Relay does not support host migration. So, all clients should disconnect.
string msg;
if (m_IsRelayConnected)
msg = "Host disconnected! Leaving the lobby.";
msg = "The host disconnected! Leaving the lobby.";
else
msg = "Connection to host was lost. Leaving the lobby.";

13
Assets/Scripts/Relay/RelayUtpHost.cs


OnNewConnection(conn, id);
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);
conn.Disconnect(m_networkDriver);
m_connections.Remove(conn);
LobbyAsyncRequests.Instance.GetRateLimit(LobbyAsyncRequests.RequestType.Query).EnqueuePendingOperation(WaitToCheckForUsers);
// The user ready status lives in the lobby data, which won't update immediately, but we need to use it to identify if all remaining players have readied.
// So, we'll wait two lobby update loops before we check remaining players to ensure the lobby has received the disconnect message.
void WaitToCheckForUsers()
{ LobbyAsyncRequests.Instance.GetRateLimit(LobbyAsyncRequests.RequestType.Query).EnqueuePendingOperation(CheckIfAllUsersReady);
}
}
// If a client has changed state, check if this changes whether all players have readied.

protected override void ProcessDisconnectEvent(NetworkConnection conn, DataStreamReader strm)
{
// When a disconnect from the host occurs, no additional action is required. This override just prevents the base behavior from occurring.
// TODO: If a client disconnects, see if remaining players are all already ready.
UnityEngine.Debug.LogError("Client disconnected!");
// We rely on the PlayerDisconnect message instead of this disconnect message since this message might not arrive for a long time after the disconnect actually occurs.
}
public void OnReceiveMessage(MessageType type, object msg)

正在加载...
取消
保存