use SortedSet with sorter

This commit is contained in:
vis2k 2022-04-28 20:10:56 +08:00
parent aa5a84f38b
commit 4d891fc5a1

View File

@ -86,12 +86,17 @@ public static class NetworkServer
//
// IMPORTANT: always clear .observing before modifying the sorter
// origin! otherwise the SortedSet may wind up in corrupted state.
static readonly NetworkIdentitySorter sorter = new NetworkIdentitySorter();
static readonly NetworkConnectionDistanceSorter sorter =
new NetworkConnectionDistanceSorter();
// allocate newObservers helper HashSet only once
// internal for tests
internal static readonly SortedSet<NetworkConnectionToClient> newObservers =
new SortedSet<NetworkConnectionToClient>();
internal static readonly SortedSet<NetworkConnectionToClient> newObservers;
static NetworkServer()
{
newObservers = new SortedSet<NetworkConnectionToClient>(sorter);
}
// initialization / shutdown ///////////////////////////////////////////
static void Initialize()
@ -99,6 +104,7 @@ static void Initialize()
if (initialized)
return;
// Debug.Log($"NetworkServer Created version {Version.Current}");
//Make sure connections are cleared in case any old connections references exist from previous sessions
@ -1473,8 +1479,12 @@ static void RebuildObserversDefault(NetworkIdentity identity, bool initialize)
// rebuild observers via interest management system
static void RebuildObserversCustom(NetworkIdentity identity, bool initialize)
{
// clear newObservers hashset before using it
// assign a new sorter origin AND clear the .observing SortedSet.
// otherwise the SortedSet would wind up in corrupted state if we
// change the sorter while it still has entries.
// TODO is this safe though? what about already allocated items in the HashSet?
newObservers.Clear();
sorter.Reset(identity.transform.position, identity.netId);
// not force hidden?
if (identity.visible != Visibility.ForceHidden)
@ -1494,7 +1504,7 @@ static void RebuildObserversCustom(NetworkIdentity identity, bool initialize)
newObservers.Add(identity.connectionToClient);
}
// TODO sort & cap newObserver
// TODO cap
bool changed = false;
@ -1607,8 +1617,6 @@ public static void RebuildObservers(NetworkIdentity identity, bool initialize)
{
RebuildObserversCustom(identity, initialize);
}
// TODO cap observers here maybe? or where/when
}
// broadcasting ////////////////////////////////////////////////////////