using System.Collections.Generic; using FishNet.Managing.Statistic; using FishNet.Transporting; using GameKit.Dependencies.Utilities; using UnityEngine; namespace FishNet.Editing.NetworkProfiler { internal class NetworkTraffic : IResettable { /// /// PacketGroup for each PacketId processed. /// private Dictionary _packetGroups; /// /// Total bytes for all packets. /// public ulong Bytes; /// /// Adds traffic from a specified packetId. /// public void AddPacketIdData(PacketId packetId, string details, ulong bytes, GameObject gameObject) => LAddPacketId(packetId, details, bytes, gameObject); /// /// Adds traffic from a specified packetId. /// public void AddSocketData(ulong bytes) { LAddPacketId(NetworkTrafficStatistics.UNSPECIFIED_PACKETID, details: string.Empty, bytes, gameObject: null); Bytes += bytes; } /// /// Adds traffic to a PackerGroup. /// private void LAddPacketId(PacketId packetId, string details, ulong bytes, GameObject gameObject) { if (!_packetGroups.TryGetValue(packetId, out PacketGroup packetGroup)) { packetGroup = ResettableObjectCaches.Retrieve(); packetGroup.Initialize(packetId); _packetGroups[packetId] = packetGroup; } packetGroup.AddPacket(details, bytes, gameObject); } /// /// Calculates and sets Percentage value on each PacketGroup. /// /// This should only be called after all PacketGroup entries have been created. public void SetPacketGroupPercentages() { //Field would probably get cached at runtime during iteration but let's be certain. ulong bytes = Bytes; foreach (PacketGroup pg in _packetGroups.Values) pg.SetPercent(bytes); } public void ResetState() { Bytes = 0; ResettableT2CollectionCaches.StoreAndDefault(ref _packetGroups); } public void InitializeState() { _packetGroups = ResettableT2CollectionCaches.RetrieveDictionary(); } } }