-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNetworkSystem.cs
More file actions
225 lines (182 loc) · 6.73 KB
/
NetworkSystem.cs
File metadata and controls
225 lines (182 loc) · 6.73 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using AtomicFramework;
using NuclearOption.Networking;
using NuclearOption.Chat;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace NuclearVOIP
{
internal class NetworkSystem: MonoBehaviour
{
private const float INTERVAL = 1f;
private readonly List<ulong> connections = [];
private float elapsed = 0;
public event Action<ulong>? NewConnection;
public event Action<ulong, byte[]>? OnPacket;
public event Action<ulong>? ConnectionLost;
public event Action<NetworkStatus, NetworkStatus>? OnNetworkMeasurement;
private NetworkChannel chan;
~NetworkSystem()
{
Plugin.Instance.Networking?.CloseChannel(0);
}
private void Awake()
{
Plugin.Logger.LogDebug("Initializing NetworkSystem");
if (Plugin.Instance.Networking == null)
{
Destroy(this);
return;
}
chan = Plugin.Instance.Networking.OpenChannel(0);
chan.OnConnection += OnSession;
chan.OnDisconnect += OnDisconnect;
chan.OnConnected += player =>
{
connections.Add(player);
NewConnection?.Invoke(player);
};
Discovery discovery = NetworkingManager.instance!.discovery;
List<ulong> awaiting = [.. discovery.Players];
foreach (ulong player in discovery.Players)
{
if (discovery.GetMods(player).Contains(MyPluginInfo.PLUGIN_GUID))
{
chan.Connect(player);
awaiting.Remove(player);
}
}
void OnMods(ulong player)
{
if (discovery.GetMods(player).Contains(MyPluginInfo.PLUGIN_GUID))
{
chan.Connect(player);
awaiting.Remove(player);
}
}
discovery.ModsAvailable += OnMods;
chan.OnMessage += OnMessage;
}
private void FixedUpdate()
{
elapsed += Time.fixedDeltaTime;
if (elapsed >= INTERVAL)
{
elapsed -= INTERVAL;
List<float> losses = new(connections.Count);
List<int> pings = new(connections.Count);
List<int> bandwidths = new(connections.Count);
List<float> teamLosses = [];
List<int> teamPings = [];
List<int> teamBandwidths = [];
for (int i = 0; i < connections.Count; i++)
{
ulong identity = connections[i];
Player? peer = NetworkingManager.GetPlayer(identity);
if (peer != null && ChatManager.IsMuted(peer))
{
chan.Disconnect(identity);
ConnectionLost?.Invoke(identity);
}
else
{
NetworkStatistics state = chan.GetStatistics(identity);
GameManager.GetLocalHQ(out FactionHQ localHq);
if (peer?.HQ == localHq)
{
teamLosses.Add(state.packetLoss);
teamPings.Add(state.ping);
teamBandwidths.Add(state.bandwidth);
}
losses.Add(state.packetLoss);
pings.Add(state.ping);
bandwidths.Add(state.bandwidth);
}
if (teamLosses.Count == 0)
{
teamLosses.Add(1);
teamPings.Add(0);
teamBandwidths.Add(0);
}
if (losses.Count == 0)
{
losses.Add(1);
pings.Add(0);
bandwidths.Add(0);
}
NetworkStatus teamStatus = new()
{
avgLoss = teamLosses.Average(),
maxLoss = teamLosses.Max(),
avgPing = (int)teamPings.Average(),
maxPing = teamPings.Max(),
avgBandwidth = (int)teamBandwidths.Average(),
minBandwidth = teamBandwidths.Min()
};
NetworkStatus allStatus = new()
{
avgLoss = losses.Average(),
maxLoss = losses.Max(),
avgPing = (int)pings.Average(),
maxPing = pings.Max(),
avgBandwidth = (int)bandwidths.Average(),
minBandwidth = bandwidths.Min()
};
OnNetworkMeasurement?.Invoke(allStatus, teamStatus);
}
}
}
public void Disconnect(ulong player)
{
connections.Remove(player);
OnDisconnect(player);
}
public void SendToTeam(byte[] data)
{
GameManager.GetLocalHQ(out FactionHQ? ourHQ);
if (ourHQ == null)
{
SendToAll(data);
return;
}
Player[] team = [..ourHQ.GetPlayers(false)];
foreach (ulong identity in connections)
if (team.Any(a => a.SteamID == identity))
chan.Send(identity, data, true);
}
public void SendToAll(byte[] data)
{
foreach (ulong identity in connections)
chan.Send(identity, data, true);
}
public void SendTo(ulong target, byte[] data)
{
chan.Send(target, data, true);
}
public void SendToSlow(ulong target, byte[] data)
{
chan.Send(target, data);
}
private bool OnSession(ulong player)
{
Player? playerObj = NetworkingManager.GetPlayer(player);
if (playerObj != null && !ChatManager.IsMuted(playerObj)) // TODO: When a player is unmuted (might need a patch) retry connection
return true;
else
{
Plugin.Logger.LogWarning("Received P2P request from random user");
return false;
}
}
private void OnDisconnect(ulong player)
{
if (connections.Remove(player))
ConnectionLost?.Invoke(player);
}
private void OnMessage(NetworkMessage message)
{
OnPacket?.Invoke(message.player, message.data);
}
}
}