mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 11:00:32 +00:00
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:
parent
e572417fbb
commit
855e724304
@ -826,7 +826,7 @@ public void RebuildObservers(bool initialize)
|
|||||||
foreach (KeyValuePair<int, NetworkConnection> kvp in NetworkServer.connections)
|
foreach (KeyValuePair<int, NetworkConnection> kvp in NetworkServer.connections)
|
||||||
{
|
{
|
||||||
NetworkConnection conn = kvp.Value;
|
NetworkConnection conn = kvp.Value;
|
||||||
if (conn != null && conn.isReady)
|
if (conn.isReady)
|
||||||
AddObserver(conn);
|
AddObserver(conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,6 +148,8 @@ public static bool AddConnection(NetworkConnection conn)
|
|||||||
{
|
{
|
||||||
if (!s_Connections.ContainsKey(conn.connectionId))
|
if (!s_Connections.ContainsKey(conn.connectionId))
|
||||||
{
|
{
|
||||||
|
// connection cannot be null here or conn.connectionId
|
||||||
|
// would throw NRE
|
||||||
s_Connections[conn.connectionId] = conn;
|
s_Connections[conn.connectionId] = conn;
|
||||||
conn.SetHandlers(s_MessageHandlers);
|
conn.SetHandlers(s_MessageHandlers);
|
||||||
return true;
|
return true;
|
||||||
@ -244,8 +246,7 @@ public static bool SendToAll(short msgType, MessageBase msg)
|
|||||||
foreach (KeyValuePair<int, NetworkConnection> kvp in connections)
|
foreach (KeyValuePair<int, NetworkConnection> kvp in connections)
|
||||||
{
|
{
|
||||||
NetworkConnection conn = kvp.Value;
|
NetworkConnection conn = kvp.Value;
|
||||||
if (conn != null)
|
result &= conn.Send(msgType, msg);
|
||||||
result &= conn.Send(msgType, msg);
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -260,7 +261,7 @@ public static bool SendToReady(GameObject contextObj, short msgType, MessageBase
|
|||||||
foreach (KeyValuePair<int, NetworkConnection> kvp in connections)
|
foreach (KeyValuePair<int, NetworkConnection> kvp in connections)
|
||||||
{
|
{
|
||||||
NetworkConnection conn = kvp.Value;
|
NetworkConnection conn = kvp.Value;
|
||||||
if (conn != null && conn.isReady)
|
if (conn.isReady)
|
||||||
{
|
{
|
||||||
conn.Send(msgType, msg);
|
conn.Send(msgType, msg);
|
||||||
}
|
}
|
||||||
@ -295,11 +296,8 @@ public static void DisconnectAllConnections()
|
|||||||
foreach (KeyValuePair<int, NetworkConnection> kvp in connections)
|
foreach (KeyValuePair<int, NetworkConnection> kvp in connections)
|
||||||
{
|
{
|
||||||
NetworkConnection conn = kvp.Value;
|
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
|
// add player info
|
||||||
NetworkConnection conn = (NetworkConnection)Activator.CreateInstance(s_NetworkConnectionClass);
|
NetworkConnection conn = (NetworkConnection)Activator.CreateInstance(s_NetworkConnectionClass);
|
||||||
conn.SetHandlers(s_MessageHandlers);
|
|
||||||
conn.Initialize(address, s_ServerHostId, connectionId);
|
conn.Initialize(address, s_ServerHostId, connectionId);
|
||||||
|
AddConnection(conn);
|
||||||
// add connection
|
|
||||||
s_Connections[connectionId] = conn;
|
|
||||||
|
|
||||||
OnConnected(conn);
|
OnConnected(conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -417,7 +411,7 @@ static void HandleDisconnect(int connectionId, byte error)
|
|||||||
if (s_Connections.TryGetValue(connectionId, out conn))
|
if (s_Connections.TryGetValue(connectionId, out conn))
|
||||||
{
|
{
|
||||||
conn.Disconnect();
|
conn.Disconnect();
|
||||||
s_Connections.Remove(connectionId);
|
RemoveConnection(connectionId);
|
||||||
if (LogFilter.logDebug) { Debug.Log("Server lost client:" + connectionId); }
|
if (LogFilter.logDebug) { Debug.Log("Server lost client:" + connectionId); }
|
||||||
|
|
||||||
OnDisconnected(conn);
|
OnDisconnected(conn);
|
||||||
@ -536,8 +530,7 @@ public static void SendToClientOfPlayer(GameObject player, short msgType, Messag
|
|||||||
foreach (KeyValuePair<int, NetworkConnection> kvp in connections)
|
foreach (KeyValuePair<int, NetworkConnection> kvp in connections)
|
||||||
{
|
{
|
||||||
NetworkConnection conn = kvp.Value;
|
NetworkConnection conn = kvp.Value;
|
||||||
if (conn != null &&
|
if (conn.playerController != null &&
|
||||||
conn.playerController != null &&
|
|
||||||
conn.playerController.gameObject == player)
|
conn.playerController.gameObject == player)
|
||||||
{
|
{
|
||||||
conn.Send(msgType, msg);
|
conn.Send(msgType, msg);
|
||||||
@ -553,11 +546,8 @@ public static void SendToClient(int connectionId, short msgType, MessageBase msg
|
|||||||
NetworkConnection conn;
|
NetworkConnection conn;
|
||||||
if (connections.TryGetValue(connectionId, out 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"); }
|
if (LogFilter.logError) { Debug.LogError("Failed to send message to connection ID '" + connectionId + ", not found in connection list"); }
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user