Remove redundant null checks. (#74)

* Remove redundant null checks.

Enforce that connections are added in AddConnection and removed with RemoveConnection.
Since connection cannot be null anymore,  remove redundant null checks.

* Add connection after we added connection id
This commit is contained in:
Paul Pacheco 2018-10-15 11:21:30 -05:00 committed by vis2k
parent e572417fbb
commit 855e724304
2 changed files with 12 additions and 22 deletions

View File

@ -826,7 +826,7 @@ public void RebuildObservers(bool initialize)
foreach (KeyValuePair<int, NetworkConnection> kvp in NetworkServer.connections)
{
NetworkConnection conn = kvp.Value;
if (conn != null && conn.isReady)
if (conn.isReady)
AddObserver(conn);
}

View File

@ -148,6 +148,8 @@ public static bool AddConnection(NetworkConnection conn)
{
if (!s_Connections.ContainsKey(conn.connectionId))
{
// connection cannot be null here or conn.connectionId
// would throw NRE
s_Connections[conn.connectionId] = conn;
conn.SetHandlers(s_MessageHandlers);
return true;
@ -244,8 +246,7 @@ public static bool SendToAll(short msgType, MessageBase msg)
foreach (KeyValuePair<int, NetworkConnection> kvp in connections)
{
NetworkConnection conn = kvp.Value;
if (conn != null)
result &= conn.Send(msgType, msg);
result &= conn.Send(msgType, msg);
}
return result;
}
@ -260,7 +261,7 @@ public static bool SendToReady(GameObject contextObj, short msgType, MessageBase
foreach (KeyValuePair<int, NetworkConnection> kvp in connections)
{
NetworkConnection conn = kvp.Value;
if (conn != null && conn.isReady)
if (conn.isReady)
{
conn.Send(msgType, msg);
}
@ -295,11 +296,8 @@ public static void DisconnectAllConnections()
foreach (KeyValuePair<int, NetworkConnection> kvp in connections)
{
NetworkConnection conn = kvp.Value;
if (conn != null)
{
conn.Disconnect();
conn.Dispose();
}
conn.Disconnect();
conn.Dispose();
}
}
@ -394,12 +392,8 @@ static void HandleConnect(int connectionId, byte error)
// add player info
NetworkConnection conn = (NetworkConnection)Activator.CreateInstance(s_NetworkConnectionClass);
conn.SetHandlers(s_MessageHandlers);
conn.Initialize(address, s_ServerHostId, connectionId);
// add connection
s_Connections[connectionId] = conn;
AddConnection(conn);
OnConnected(conn);
}
@ -417,7 +411,7 @@ static void HandleDisconnect(int connectionId, byte error)
if (s_Connections.TryGetValue(connectionId, out conn))
{
conn.Disconnect();
s_Connections.Remove(connectionId);
RemoveConnection(connectionId);
if (LogFilter.logDebug) { Debug.Log("Server lost client:" + connectionId); }
OnDisconnected(conn);
@ -536,8 +530,7 @@ public static void SendToClientOfPlayer(GameObject player, short msgType, Messag
foreach (KeyValuePair<int, NetworkConnection> kvp in connections)
{
NetworkConnection conn = kvp.Value;
if (conn != null &&
conn.playerController != null &&
if (conn.playerController != null &&
conn.playerController.gameObject == player)
{
conn.Send(msgType, msg);
@ -553,11 +546,8 @@ public static void SendToClient(int connectionId, short msgType, MessageBase msg
NetworkConnection conn;
if (connections.TryGetValue(connectionId, out conn))
{
if (conn != null)
{
conn.Send(msgType, msg);
return;
}
conn.Send(msgType, msg);
return;
}
if (LogFilter.logError) { Debug.LogError("Failed to send message to connection ID '" + connectionId + ", not found in connection list"); }
}