This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
NetworkStatisticsSystem.cs
66 lines (57 loc) · 2.21 KB
/
NetworkStatisticsSystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System.Diagnostics;
using Unity.Entities;
using UnityEngine;
using Debug = UnityEngine.Debug;
namespace Improbable.Gdk.Core.NetworkStats
{
/*
* This system runs before the receive system such that the order is:
*
* NetworkStatisticsSystem -> SpatialOSReceiveSystem -> ... -> SpatialOSSendSystem
*
* When both SpatialOSReceiveSystem and SpatialOSSendSystem runs they provide
* network data to this system. This will store them until the next time it runs when it
* will push this data in the underlying data storage and reset the temporary storage.
*/
[DisableAutoCreation]
[UpdateInGroup(typeof(SpatialOSReceiveGroup.InternalSpatialOSReceiveGroup))]
[UpdateBefore(typeof(SpatialOSReceiveSystem))]
public class NetworkStatisticsSystem : ComponentSystem
{
private const int DefaultBufferSize = 60;
private readonly NetStats netStats = new NetStats(DefaultBufferSize);
private readonly NetFrameStats lastIncomingData = new NetFrameStats();
private readonly NetFrameStats lastOutgoingData = new NetFrameStats();
private float lastFrameTime;
#if !UNITY_EDITOR
protected override void OnCreate()
{
Enabled = false;
}
#endif
public (DataPoint, float) GetSummary(MessageTypeUnion messageType, int numFrames, Direction direction)
{
return netStats.GetSummary(messageType, numFrames, direction);
}
protected override void OnUpdate()
{
netStats.SetFrameStats(lastIncomingData, Direction.Incoming);
netStats.SetFrameStats(lastOutgoingData, Direction.Outgoing);
netStats.SetFrameTime(lastFrameTime);
netStats.FinishFrame();
lastIncomingData.Clear();
lastOutgoingData.Clear();
lastFrameTime = Time.deltaTime;
}
[Conditional("UNITY_EDITOR")]
internal void ApplyDiff(ViewDiff diff)
{
lastIncomingData.CopyFrom(diff.GetNetStats());
}
[Conditional("UNITY_EDITOR")]
internal void AddOutgoingSample(NetFrameStats frameStats)
{
lastOutgoingData.CopyFrom(frameStats);
}
}
}