NetworkLobbyManager: removed static message caching to simplify code and reduce state

This commit is contained in:
vis2k 2018-06-10 10:09:21 +02:00
parent 8e7095262f
commit 734db6edfe

View File

@ -31,11 +31,6 @@ struct PendingPlayer
List<PendingPlayer> m_PendingPlayers = new List<PendingPlayer>(); List<PendingPlayer> m_PendingPlayers = new List<PendingPlayer>();
public NetworkLobbyPlayer[] lobbySlots; public NetworkLobbyPlayer[] lobbySlots;
// static message objects to avoid runtime-allocations
static LobbyReadyToBeginMessage s_ReadyToBeginMessage = new LobbyReadyToBeginMessage();
static IntegerMessage s_SceneLoadedMessage = new IntegerMessage();
static LobbyReadyToBeginMessage s_LobbyReadyToBeginMessage = new LobbyReadyToBeginMessage();
// properties // properties
public bool showLobbyGUI { get { return m_ShowLobbyGUI; } set { m_ShowLobbyGUI = value; } } public bool showLobbyGUI { get { return m_ShowLobbyGUI; } set { m_ShowLobbyGUI = value; } }
public int maxPlayers { get { return m_MaxPlayers; } set { m_MaxPlayers = value; } } public int maxPlayers { get { return m_MaxPlayers; } set { m_MaxPlayers = value; } }
@ -375,9 +370,10 @@ public override void OnServerRemovePlayer(NetworkConnection conn, PlayerControll
{ {
lobbyPlayer.GetComponent<NetworkLobbyPlayer>().readyToBegin = false; lobbyPlayer.GetComponent<NetworkLobbyPlayer>().readyToBegin = false;
s_LobbyReadyToBeginMessage.slotId = lobbyPlayer.slot; LobbyReadyToBeginMessage msg = new LobbyReadyToBeginMessage();
s_LobbyReadyToBeginMessage.readyState = false; msg.slotId = lobbyPlayer.slot;
NetworkServer.SendToReady(null, MsgType.LobbyReadyToBegin, s_LobbyReadyToBeginMessage); msg.readyState = false;
NetworkServer.SendToReady(null, MsgType.LobbyReadyToBegin, msg);
} }
} }
@ -433,23 +429,24 @@ public override void OnServerSceneChanged(string sceneName)
void OnServerReadyToBeginMessage(NetworkMessage netMsg) void OnServerReadyToBeginMessage(NetworkMessage netMsg)
{ {
if (LogFilter.logDebug) { Debug.Log("NetworkLobbyManager OnServerReadyToBeginMessage"); } if (LogFilter.logDebug) { Debug.Log("NetworkLobbyManager OnServerReadyToBeginMessage"); }
netMsg.ReadMessage(s_ReadyToBeginMessage); LobbyReadyToBeginMessage msg = new LobbyReadyToBeginMessage();
netMsg.ReadMessage(msg);
PlayerController lobbyController; PlayerController lobbyController;
if (!netMsg.conn.GetPlayerController((short)s_ReadyToBeginMessage.slotId, out lobbyController)) if (!netMsg.conn.GetPlayerController((short)msg.slotId, out lobbyController))
{ {
if (LogFilter.logError) { Debug.LogError("NetworkLobbyManager OnServerReadyToBeginMessage invalid playerControllerId " + s_ReadyToBeginMessage.slotId); } if (LogFilter.logError) { Debug.LogError("NetworkLobbyManager OnServerReadyToBeginMessage invalid playerControllerId " + msg.slotId); }
return; return;
} }
// set this player ready // set this player ready
var lobbyPlayer = lobbyController.gameObject.GetComponent<NetworkLobbyPlayer>(); var lobbyPlayer = lobbyController.gameObject.GetComponent<NetworkLobbyPlayer>();
lobbyPlayer.readyToBegin = s_ReadyToBeginMessage.readyState; lobbyPlayer.readyToBegin = msg.readyState;
// tell every player that this player is ready // tell every player that this player is ready
var outMsg = new LobbyReadyToBeginMessage(); var outMsg = new LobbyReadyToBeginMessage();
outMsg.slotId = lobbyPlayer.slot; outMsg.slotId = lobbyPlayer.slot;
outMsg.readyState = s_ReadyToBeginMessage.readyState; outMsg.readyState = msg.readyState;
NetworkServer.SendToReady(null, MsgType.LobbyReadyToBegin, outMsg); NetworkServer.SendToReady(null, MsgType.LobbyReadyToBegin, outMsg);
// maybe start the game // maybe start the game
@ -459,13 +456,13 @@ void OnServerReadyToBeginMessage(NetworkMessage netMsg)
void OnServerSceneLoadedMessage(NetworkMessage netMsg) void OnServerSceneLoadedMessage(NetworkMessage netMsg)
{ {
if (LogFilter.logDebug) { Debug.Log("NetworkLobbyManager OnSceneLoadedMessage"); } if (LogFilter.logDebug) { Debug.Log("NetworkLobbyManager OnSceneLoadedMessage"); }
IntegerMessage msg = new IntegerMessage();
netMsg.ReadMessage(s_SceneLoadedMessage); netMsg.ReadMessage(msg);
PlayerController lobbyController; PlayerController lobbyController;
if (!netMsg.conn.GetPlayerController((short)s_SceneLoadedMessage.value, out lobbyController)) if (!netMsg.conn.GetPlayerController((short)msg.value, out lobbyController))
{ {
if (LogFilter.logError) { Debug.LogError("NetworkLobbyManager OnServerSceneLoadedMessage invalid playerControllerId " + s_SceneLoadedMessage.value); } if (LogFilter.logError) { Debug.LogError("NetworkLobbyManager OnServerSceneLoadedMessage invalid playerControllerId " + msg.value); }
return; return;
} }
@ -588,23 +585,24 @@ public override void OnClientSceneChanged(NetworkConnection conn)
void OnClientReadyToBegin(NetworkMessage netMsg) void OnClientReadyToBegin(NetworkMessage netMsg)
{ {
netMsg.ReadMessage(s_LobbyReadyToBeginMessage); LobbyReadyToBeginMessage msg = new LobbyReadyToBeginMessage();
netMsg.ReadMessage(msg);
if (s_LobbyReadyToBeginMessage.slotId >= lobbySlots.Count()) if (msg.slotId >= lobbySlots.Count())
{ {
if (LogFilter.logError) { Debug.LogError("NetworkLobbyManager OnClientReadyToBegin invalid lobby slot " + s_LobbyReadyToBeginMessage.slotId); } if (LogFilter.logError) { Debug.LogError("NetworkLobbyManager OnClientReadyToBegin invalid lobby slot " + msg.slotId); }
return; return;
} }
var lobbyPlayer = lobbySlots[s_LobbyReadyToBeginMessage.slotId]; var lobbyPlayer = lobbySlots[msg.slotId];
if (lobbyPlayer == null || lobbyPlayer.gameObject == null) if (lobbyPlayer == null || lobbyPlayer.gameObject == null)
{ {
if (LogFilter.logError) { Debug.LogError("NetworkLobbyManager OnClientReadyToBegin no player at lobby slot " + s_LobbyReadyToBeginMessage.slotId); } if (LogFilter.logError) { Debug.LogError("NetworkLobbyManager OnClientReadyToBegin no player at lobby slot " + msg.slotId); }
return; return;
} }
lobbyPlayer.readyToBegin = s_LobbyReadyToBeginMessage.readyState; lobbyPlayer.readyToBegin = msg.readyState;
lobbyPlayer.OnClientReady(s_LobbyReadyToBeginMessage.readyState); lobbyPlayer.OnClientReady(msg.readyState);
} }
void OnClientAddPlayerFailedMessage(NetworkMessage netMsg) void OnClientAddPlayerFailedMessage(NetworkMessage netMsg)