-
Notifications
You must be signed in to change notification settings - Fork 12
/
Client.cs
409 lines (350 loc) · 18.8 KB
/
Client.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
using System;
using System.IO;
using System.Linq;
using System.Net;
using LiteNetLib;
using LiteNetLib.Utils;
using Swihoni.Collections;
using Swihoni.Components;
using Swihoni.Components.Networking;
using Swihoni.Sessions.Components;
using Swihoni.Sessions.Config;
using Swihoni.Sessions.Player.Components;
using Swihoni.Sessions.Player.Visualization;
using Swihoni.Util;
using Unity.Profiling;
using UnityEngine;
using UnityEngine.Profiling;
namespace Swihoni.Sessions
{
public sealed partial class Client : NetworkedSessionBase, IReceiver
{
private const int DemoWriteSize = 1 << 18, DemoTickRate = 20;
private ComponentClientSocket m_Socket;
private readonly NetDataWriter m_DemoWriter = new(true, DemoWriteSize);
private static readonly ProfilerMarker ClientPredictMarker = new("Client Prediction");
public override ComponentSocketBase Socket => m_Socket;
public bool WriteDemo { get; set; }
[RuntimeInitializeOnLoadMethod]
private static void Initialize()
{
ConsoleCommandExecutor.SetCommand("start_demo", args => SessionEnumerable.OfType<Client>().First().WriteDemo = true);
ConsoleCommandExecutor.SetCommand("end_demo", args => SessionEnumerable.OfType<Client>().First().WriteDemo = false);
}
public Client(SessionElements elements, IPEndPoint ipEndPoint, SessionInjectorBase injector)
: base(elements, ipEndPoint, injector)
{
/* Prediction */
m_CommandHistory = new CyclicArray<ClientCommandsContainer>(HistoryCount, () => m_EmptyClientCommands.Clone());
SetFirstCommand(m_CommandHistory.Peek());
m_PlayerPredictionHistory = new CyclicArray<Container>(HistoryCount, () => new Container(elements.playerElements.Append(typeof(ClientStampComponent))));
foreach (ServerSessionContainer session in m_SessionHistory)
{
session.RegisterAppend(typeof(LocalizedClientStampComponent));
foreach (Container player in session.Require<PlayerArray>())
player.RegisterAppend(typeof(LocalizedClientStampComponent));
}
}
public override void Start()
{
base.Start();
m_Socket = new ComponentClientSocket(IpEndPoint, m_Injector.GetConnectWriter());
m_Socket.Listener.PeerDisconnectedEvent += OnDisconnect;
m_Socket.Receiver = this;
RegisterMessages(m_Socket);
}
private void OnDisconnect(NetPeer peer, DisconnectInfo disconnect)
{
if (disconnect.AdditionalData.TryGetString(out string reason))
Debug.LogWarning($"Disconnected for reason: {reason}");
Dispose();
}
private void UpdateInputs(Container player, int localPlayerId)
{
ClientCommandsContainer commands = m_CommandHistory.Peek();
GetPlayerModifier(player, localPlayerId).ModifyCommands(this, commands, localPlayerId);
_indexer = localPlayerId; // Prevent closure allocation
_session = this;
_container = commands;
ForEachSessionInterface(@interface => @interface.ModifyLocalTrusted(_indexer, _session, _container));
}
public override Container GetLocalCommands() => m_CommandHistory.Peek();
public override Container GetLocalPlayer() => m_PlayerPredictionHistory.Peek();
protected override void Input(uint timeUs, uint durationUs)
{
Container verifiedLatestSession = GetLatestSession();
if (!GetLocalPlayerId(verifiedLatestSession, out int localPlayerId))
return;
Container verifiedPlayer = GetModifyingPlayerFromId(localPlayerId, verifiedLatestSession);
UpdateInputs(verifiedPlayer, localPlayerId);
var context = new SessionContext(this, commands: m_CommandHistory.Peek(), playerId: localPlayerId, player: m_CommandHistory.Peek(),
timeUs: timeUs, durationUs: durationUs);
GetPlayerModifier(verifiedPlayer, localPlayerId).ModifyTrusted(context, verifiedPlayer);
}
// private static CyclicArray<Container> _predictionHistory;
private static CyclicArray<Container> _predictionHistory;
private static void MergeCommandInto(ElementBase element, ElementBase command) => ElementExtensions.NavigateZipped(element, command, (_element, _command) =>
{
if (_command.WithoutAttribute<ClientTrustedAttribute>()) return Navigation.Continue;
_element.SetTo(_command);
return Navigation.SkipDescendents;
});
protected override void Tick(uint tick, uint timeUs, uint durationUs)
{
PhysicsScene.Simulate(durationUs * TimeConversions.MicrosecondToSecond);
Container latestSession = GetLatestSession();
using (ClientPredictMarker.Auto())
{
if (GetLocalPlayerId(latestSession, out int localPlayerId))
{
m_Injector.OnPreTick(latestSession);
Predict(tick, timeUs, localPlayerId); // Advances commands
}
}
Profiler.BeginSample("Client Send");
SendCommand();
Profiler.EndSample();
Profiler.BeginSample("Client Receive");
Receive(timeUs);
Profiler.EndSample();
base.Tick(tick, timeUs, durationUs);
m_Injector.OnPostTick(latestSession);
Profiler.BeginSample("Client End");
ClearSingleTicks(m_CommandHistory.Peek());
Profiler.EndSample();
}
private void Receive(uint timeUs)
{
_timeUs = timeUs;
m_Socket.PollEvents();
}
private static uint _timeUs;
// private static readonly DurationAverage DurationAverage = new DurationAverage(60, "Navigate");
void IReceiver.OnReceive(NetPeer fromPeer, NetDataReader reader, byte code)
{
switch (code)
{
case ServerSessionCode:
{
Profiler.BeginSample("Client Receive Setup");
ServerSessionContainer previousServerSession = m_SessionHistory.Peek();
m_Injector.DeserializeReceived(m_EmptyServerSession, reader);
ServerSessionContainer receivedServerSession = m_EmptyServerSession;
uint serverTick = receivedServerSession.Require<ServerStampComponent>().tick;
bool everySecond = serverTick % 60 == 0;
UIntProperty previousServerTick = previousServerSession.Require<ServerStampComponent>().tick;
ServerSessionContainer serverSession;
var isMostRecent = true;
if (previousServerTick.WithValue)
{
var delta = checked((int) (serverTick - (long) previousServerTick));
if (delta > 0)
{
m_CommandHistory.Peek().Require<AcknowledgedServerTickProperty>().Value = serverTick;
for (var i = 0; i < delta - 1; i++) // We skipped tick(s). Reserve spaces to fill later
{
ServerSessionContainer reserved = m_SessionHistory.ClaimNext();
L.Warning($"[{GetType().Name}] Inserting out of order server update");
reserved.SetTo(previousServerSession);
}
serverSession = m_SessionHistory.ClaimNext();
}
else
{
// We received an old tick. Fill in history
serverSession = m_SessionHistory.Get(delta);
L.Warning($"[{GetType().Name}] Received out of order server update");
isMostRecent = false;
}
}
else serverSession = m_SessionHistory.ClaimNext();
UpdateCurrentSessionFromReceived(previousServerSession, serverSession, receivedServerSession);
Profiler.EndSample();
m_Injector.OnClientReceive(serverSession);
RenderVerified(new SessionContext(this, serverSession));
if (!isMostRecent)
{
L.Warning("Is not most recent!");
break;
}
/* Most Recent */
{
// TODO:refactor make class
var serverStamp = serverSession.Require<ServerStampComponent>();
var previousServerStamp = serverSession.Require<LocalizedClientStampComponent>();
UIntProperty serverTimeUs = serverStamp.timeUs, localizedServerTimeUs = previousServerStamp.timeUs;
if (localizedServerTimeUs.WithValue)
{
uint previousTimeUs = previousServerSession.Require<ServerStampComponent>().timeUs;
if (serverTimeUs >= previousTimeUs) localizedServerTimeUs.Value += checked(serverTimeUs - previousTimeUs);
else Debug.LogError($"Updated server session time was less. Current: {serverStamp}; Previous: {previousServerStamp} μs");
}
else localizedServerTimeUs.Value = _timeUs;
long delta = localizedServerTimeUs.Value - (long) _timeUs;
if (Math.Abs(delta) > serverSession.Require<TickRateProperty>().TickIntervalUs * 3u)
{
ResetErrors++;
localizedServerTimeUs.Value = _timeUs;
}
AnalysisLogger.AddDataPoint(string.Empty, localizedServerTimeUs.Value, _timeUs, serverTimeUs.Value);
}
Profiler.BeginSample("Client Update Players");
var serverPlayers = serverSession.Require<PlayerArray>();
bool isLocalPlayerOnServer = GetLocalPlayerId(serverSession, out int localPlayerId);
for (var playerId = 0; playerId < serverPlayers.Length; playerId++)
{
Container serverPlayer = serverPlayers[playerId];
HealthProperty healthProperty = serverPlayer.Health();
UIntProperty localizedServerTimeUs = serverPlayer.Require<LocalizedClientStampComponent>().timeUs;
if (healthProperty.WithoutValue)
localizedServerTimeUs.Clear(); // Is something a client only has so we have to clear it
if (healthProperty.IsInactiveOrDead)
continue;
/* Valid player */
UIntProperty serverTimeUs = serverPlayer.Require<ServerStampComponent>().timeUs;
if (localizedServerTimeUs.WithValue)
{
uint previousTimeUs = previousServerSession.GetPlayer(playerId).Require<ServerStampComponent>().timeUs;
if (serverTimeUs >= previousTimeUs) localizedServerTimeUs.Value += checked(serverTimeUs - previousTimeUs);
else L.Warning($"Updated server player time was greater. Current: {serverTimeUs.Value} μs; Previous: {previousTimeUs} μs");
}
else localizedServerTimeUs.Value = _timeUs;
if (playerId != localPlayerId) GetPlayerModifier(serverPlayer, playerId).Synchronize(new SessionContext(player: serverPlayer));
long delta = localizedServerTimeUs.Value - (long) _timeUs;
if (Math.Abs(delta) > serverSession.Require<TickRateProperty>().TickIntervalUs * 3u)
{
ResetErrors++;
localizedServerTimeUs.Value = _timeUs;
}
}
Profiler.EndSample();
// Debug.Log($"{receivedServerSession.Require<ServerStampComponent>().time} {trackedTime.Value}");
Profiler.BeginSample("Client Check Prediction");
if (isLocalPlayerOnServer)
{
Container serverPlayer = serverSession.GetPlayer(localPlayerId);
CheckPrediction(serverPlayer, localPlayerId);
}
Profiler.EndSample();
Profiler.BeginSample("Write Demo");
try
{
if (WriteDemo) OnDemoCandidate(serverSession);
else if (m_DemoWriter.Length > 0) FlushDemo();
}
catch (Exception exception)
{
L.Exception(exception, "Error putting demo session");
}
Profiler.EndSample();
Profiler.BeginSample("Client Overrides");
ElementExtensions.NavigateZipped(serverSession.GetPlayer(localPlayerId), m_CommandHistory.Peek(), (_server, _command) =>
{
if (_server is PropertyBase {IsOverride: true} serverProperty && _command is PropertyBase commandProperty)
{
commandProperty.SetTo(serverProperty);
Debug.Log($"Overriding with server: {serverProperty}");
}
return Navigation.Continue;
});
Profiler.EndSample();
break;
}
}
}
public static string GetDemoFile()
{
string parentFolder = Directory.GetParent(Application.dataPath).FullName;
if (Application.platform == RuntimePlatform.OSXPlayer) parentFolder = Directory.GetParent(parentFolder).FullName;
return Path.ChangeExtension(Path.Combine(parentFolder, "Demo"), "vfd");
}
private void OnDemoCandidate(Container session)
{
string demoFile = GetDemoFile();
if (!File.Exists(demoFile))
{
m_DemoWriter.Reset();
m_DemoWriter.Put(Application.version);
using (new FileStream(demoFile, FileMode.Create))
{
}
}
if (session.Require<ServerStampComponent>().tick % (session.Require<TickRateProperty>().Value / DemoTickRate) != 0) return;
session.Serialize(m_DemoWriter);
if (m_DemoWriter.Length > DemoWriteSize)
FlushDemo();
}
private void FlushDemo()
{
using (var stream = new FileStream(GetDemoFile(), FileMode.Append))
{
stream.Write(m_DemoWriter.Data, 0, m_DemoWriter.Length);
#if UNITY_EDITOR
Debug.Log("Wrote demo section");
#endif
}
m_DemoWriter.Reset();
}
public static void ClearSingleTicks(ElementBase commands) => commands.Navigate(_element =>
{
if (!_element.TryAttribute(out SingleTickAttribute singleTickAttribute)) return Navigation.Continue;
if (singleTickAttribute.Zero) _element.Zero();
else _element.Clear();
return Navigation.SkipDescendents;
});
private void SendCommand() => m_Socket.SendToServer(m_CommandHistory.Peek(), DeliveryMethod.ReliableUnordered);
private static bool _predictionIsAccurate; // Prevents heap allocation in closure
private static readonly Func<ElementBase, ElementBase, ElementBase, Navigation> VisitPredictedFunction = VisitPredicted;
private void UpdateCurrentSessionFromReceived(Container previousServerSession, Container serverSession, Container receivedServerSession)
{
m_Injector.UpdateCurrentSessionFromReceived(previousServerSession, serverSession, receivedServerSession);
// TODO:refactor need some sort of zip longest
serverSession.Require<LocalizedClientStampComponent>().SetTo(previousServerSession.Require<LocalizedClientStampComponent>());
var previousArray = previousServerSession.Require<PlayerArray>();
var array = serverSession.Require<PlayerArray>();
for (var playerIndex = 0; playerIndex < array.Length; playerIndex++)
array[playerIndex].Require<LocalizedClientStampComponent>().SetTo(previousArray[playerIndex].Require<LocalizedClientStampComponent>());
}
private static bool GetLocalPlayerId(Container session, out int localPlayerId)
{
if (session.With(out LocalPlayerId localPlayerProperty) && localPlayerProperty.WithValue)
{
localPlayerId = localPlayerProperty;
return true;
}
localPlayerId = default;
return false;
}
public override Ray GetRayForPlayerId(int playerId) => m_PlayerPredictionHistory.Peek().GetRayForPlayer();
protected override void RollbackHitboxes(in SessionContext context)
{
if (!DebugBehavior.Singleton.SendDebug) return;
for (var i = 0; i < MaxPlayers; i++)
{
// int copiedPlayerId = i;
// Container GetInHistory(int historyIndex) => m_SessionHistory.Get(-historyIndex).GetPlayer(copiedPlayerId);
//
// Container render = m_RenderSession.GetPlayer(i).Clone();
//
// float rollback = DebugBehavior.Singleton.RollbackOverride.OrElse(GetSettings().TickInterval) * 3;
// RenderInterpolatedPlayer<LocalizedClientStampComponent>(Time.realtimeSinceStartup - rollback, render, m_SessionHistory.Size, GetInHistory);
//
// PlayerModifierDispatcherBehavior modifier = m_Modifier[i];
// modifier.EvaluateHitboxes(i, render);
Container recentPlayer = ((PlayerVisualsDispatcherBehavior) PlayerManager.UnsafeVisuals[i]).GetRecentPlayer();
if (i == 0 && recentPlayer != null) SendDebug(recentPlayer);
}
}
private void SendDebug(Container player)
{
DebugClientView debug = m_EmptyDebugClientView.Clone();
debug.SetTo(player);
m_Socket.SendToServer(debug, DeliveryMethod.ReliableOrdered);
}
public override void Dispose()
{
base.Dispose();
m_Socket?.Dispose();
}
}
}