Interest Management: NetworkConnection.visList renamed to observing

This commit is contained in:
vis2k 2021-02-11 15:08:00 +08:00
parent bd4fa37cd5
commit 650231025f
3 changed files with 22 additions and 22 deletions

View File

@ -18,8 +18,8 @@ public abstract class NetworkConnection
{ {
public const int LocalConnectionId = 0; public const int LocalConnectionId = 0;
// internal so it can be tested // NetworkIdentities that this connection can see
internal readonly HashSet<NetworkIdentity> visList = new HashSet<NetworkIdentity>(); internal readonly HashSet<NetworkIdentity> observing = new HashSet<NetworkIdentity>();
Dictionary<int, NetworkMessageDelegate> messageHandlers; Dictionary<int, NetworkMessageDelegate> messageHandlers;
@ -156,17 +156,17 @@ public override string ToString()
return $"connection({connectionId})"; return $"connection({connectionId})";
} }
internal void AddToVisList(NetworkIdentity identity) internal void AddToObserving(NetworkIdentity identity)
{ {
visList.Add(identity); observing.Add(identity);
// spawn identity for this conn // spawn identity for this conn
NetworkServer.ShowForConnection(identity, this); NetworkServer.ShowForConnection(identity, this);
} }
internal void RemoveFromVisList(NetworkIdentity identity, bool isDestroyed) internal void RemoveFromObserving(NetworkIdentity identity, bool isDestroyed)
{ {
visList.Remove(identity); observing.Remove(identity);
if (!isDestroyed) if (!isDestroyed)
{ {
@ -177,11 +177,11 @@ internal void RemoveFromVisList(NetworkIdentity identity, bool isDestroyed)
internal void RemoveObservers() internal void RemoveObservers()
{ {
foreach (NetworkIdentity identity in visList) foreach (NetworkIdentity identity in observing)
{ {
identity.RemoveObserverInternal(this); identity.RemoveObserverInternal(this);
} }
visList.Clear(); observing.Clear();
} }
// helper function // helper function

View File

@ -1143,7 +1143,7 @@ internal void ClearObservers()
{ {
foreach (NetworkConnection conn in observers.Values) foreach (NetworkConnection conn in observers.Values)
{ {
conn.RemoveFromVisList(this, true); conn.RemoveFromObserving(this, true);
} }
observers.Clear(); observers.Clear();
} }
@ -1167,7 +1167,7 @@ internal void AddObserver(NetworkConnection conn)
// Debug.Log("Added observer " + conn.address + " added for " + gameObject); // Debug.Log("Added observer " + conn.address + " added for " + gameObject);
observers[conn.connectionId] = conn; observers[conn.connectionId] = conn;
conn.AddToVisList(this); conn.AddToObserving(this);
} }
/// <summary> /// <summary>
@ -1264,7 +1264,7 @@ public void RebuildObservers(bool initialize)
if (initialize || !observers.ContainsKey(conn.connectionId)) if (initialize || !observers.ContainsKey(conn.connectionId))
{ {
// new observer // new observer
conn.AddToVisList(this); conn.AddToObserving(this);
// Debug.Log("New Observer for " + gameObject + " " + conn); // Debug.Log("New Observer for " + gameObject + " " + conn);
changed = true; changed = true;
} }
@ -1277,7 +1277,7 @@ public void RebuildObservers(bool initialize)
if (!newObservers.Contains(conn)) if (!newObservers.Contains(conn))
{ {
// removed observer // removed observer
conn.RemoveFromVisList(this, false); conn.RemoveFromObserving(this, false);
// Debug.Log("Removed Observer for " + gameObject + " " + conn); // Debug.Log("Removed Observer for " + gameObject + " " + conn);
changed = true; changed = true;
} }

View File

@ -1628,9 +1628,9 @@ public void RebuildObserversDoesNotAddServerConnectionsIfImplemented()
} }
// RebuildObservers is complex. let's do one full test where we check // RebuildObservers is complex. let's do one full test where we check
// add, remove and vislist. // add, remove and observing.
[Test] [Test]
public void RebuildObserversAddRemoveAndVisListTest() public void RebuildObserversAddRemoveAndObservingTest()
{ {
// add observer component with ready observer // add observer component with ready observer
RebuildObserversNetworkBehaviour comp = gameObject.AddComponent<RebuildObserversNetworkBehaviour>(); RebuildObserversNetworkBehaviour comp = gameObject.AddComponent<RebuildObserversNetworkBehaviour>();
@ -1645,9 +1645,9 @@ public void RebuildObserversAddRemoveAndVisListTest()
Assert.That(identity.observers.Count, Is.EqualTo(1)); Assert.That(identity.observers.Count, Is.EqualTo(1));
Assert.That(identity.observers.ContainsKey(observerA.connectionId)); Assert.That(identity.observers.ContainsKey(observerA.connectionId));
// identity should have added itself to the observer's visList // identity should have added itself to the observer's observing
Assert.That(observerA.visList.Count, Is.EqualTo(1)); Assert.That(observerA.observing.Count, Is.EqualTo(1));
Assert.That(observerA.visList.Contains(identity), Is.True); Assert.That(observerA.observing.Contains(identity), Is.True);
// let the component find another observer // let the component find another observer
NetworkConnectionToClient observerB = new NetworkConnectionToClient(43, false, 0) { isReady = true }; NetworkConnectionToClient observerB = new NetworkConnectionToClient(43, false, 0) { isReady = true };
@ -1658,11 +1658,11 @@ public void RebuildObserversAddRemoveAndVisListTest()
Assert.That(identity.observers.Count, Is.EqualTo(1)); Assert.That(identity.observers.Count, Is.EqualTo(1));
Assert.That(identity.observers.ContainsKey(observerB.connectionId)); Assert.That(identity.observers.ContainsKey(observerB.connectionId));
// identity should have removed itself from the old observer's visList // identity should have removed itself from the old observer's observing
// and added itself to new observer's vislist // and added itself to new observer's observing
Assert.That(observerA.visList.Count, Is.EqualTo(0)); Assert.That(observerA.observing.Count, Is.EqualTo(0));
Assert.That(observerB.visList.Count, Is.EqualTo(1)); Assert.That(observerB.observing.Count, Is.EqualTo(1));
Assert.That(observerB.visList.Contains(identity), Is.True); Assert.That(observerB.observing.Contains(identity), Is.True);
// clean up // clean up
NetworkServer.Shutdown(); NetworkServer.Shutdown();