-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Server.cs
455 lines (386 loc) · 15.6 KB
/
Server.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#region
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using HarmonyLib;
using NebulaAPI;
using NebulaAPI.DataStructures;
using NebulaAPI.GameState;
using NebulaAPI.Networking;
using NebulaModel;
using NebulaModel.DataStructures;
using NebulaModel.Logger;
using NebulaModel.Networking;
using NebulaModel.Networking.Serialization;
using NebulaModel.Packets.GameHistory;
using NebulaModel.Packets.Players;
using NebulaModel.Packets.Session;
using NebulaModel.Utils;
using NebulaNetwork.Messaging;
using NebulaNetwork.Ngrok;
using NebulaWorld;
using NebulaWorld.SocialIntegration;
using Open.Nat;
using UnityEngine;
using WebSocketSharp;
using WebSocketSharp.Server;
using AuthenticationSchemes = WebSocketSharp.Net.AuthenticationSchemes;
using NetworkCredential = WebSocketSharp.Net.NetworkCredential;
#endregion
namespace NebulaNetwork;
public class Server : IServer
{
public INetPacketProcessor PacketProcessor { get; set; } = new NebulaNetPacketProcessor();
private const float GAME_RESEARCH_UPDATE_INTERVAL = 2;
private const float LAUNCH_UPDATE_INTERVAL = 4;
private const float DYSONSPHERE_UPDATE_INTERVAL = 2;
private const float WARNING_UPDATE_INTERVAL = 1;
private readonly bool loadSaveFile;
private float dysonLaunchUpateTimer = 1;
private float dysonSphereUpdateTimer;
private float gameResearchHashUpdateTimer;
private NgrokManager ngrokManager;
private WebSocketServer socket;
private float warningUpdateTimer;
public ConcurrentPlayerCollection Players { get; } = new();
private ConcurrentQueue<ushort> PlayerIdPool = new();
private int highestPlayerID;
public Server(ushort port, bool loadSaveFile = false)
{
Port = port;
this.loadSaveFile = loadSaveFile;
}
public ushort Port { get; set; }
public string NgrokAddress => ngrokManager.NgrokAddress;
public bool NgrokActive => ngrokManager.IsNgrokActive();
public bool NgrokEnabled => ngrokManager.NgrokEnabled;
public string NgrokLastErrorCode => ngrokManager.NgrokLastErrorCode;
public string NgrokLastErrorCodeDesc => ngrokManager.NgrokLastErrorCodeDesc;
public event EventHandler<INebulaConnection> Connected;
public event EventHandler<INebulaConnection> Disconnected;
// Placeholder until we implement Connected and Disconnected event on the socket level.
internal void OnSocketConnection(INebulaConnection conn)
{
// Generate new data for the player
var playerId = GetNextPlayerId();
// this is truncated to ushort.MaxValue
var birthPlanet = GameMain.galaxy.PlanetById(GameMain.galaxy.birthPlanetId);
var playerData = new PlayerData(playerId, -1,
position: new Double3(birthPlanet.uPosition.x, birthPlanet.uPosition.y, birthPlanet.uPosition.z));
conn.ConnectionStatus = EConnectionStatus.Pending;
INebulaPlayer newPlayer = new NebulaPlayer(conn, playerData);
if (!Players.TryAdd(conn, newPlayer))
throw new InvalidOperationException($"Connection {conn.Id} already exists!");
// return newPlayer;
Connected?.Invoke(this, conn);
}
private ushort GetNextPlayerId()
{
ushort nextId;
if (!PlayerIdPool.TryDequeue(out nextId))
nextId = (ushort)Interlocked.Increment(ref highestPlayerID);
return nextId;
}
// Placeholder until we implement Connected and Disconnected event on the socket level.
internal void OnSocketDisconnection(INebulaConnection conn)
{
Multiplayer.Session.NumPlayers -= 1;
DiscordManager.UpdateRichPresence();
Players.TryRemove(conn, out var player);
// @TODO: Why can this happen in the first place?
// Figure out why it was possible before the move and fix that issue at the root.
if (player is null)
{
Log.Warn("Player is null - Disconnect logic NOT CALLED!");
if (!Config.Options.SyncSoil)
{
return;
}
// now we need to recalculate the current sand amount :C
GameMain.mainPlayer.sandCount = Multiplayer.Session.LocalPlayer.Data.Mecha.SandCount;
// using (GetConnectedPlayers(out var connectedPlayers))
{
var connectedPlayers = Players.Connected;
foreach (var entry in connectedPlayers)
{
GameMain.mainPlayer.sandCount += entry.Value.Data.Mecha.SandCount;
}
}
UIRoot.instance.uiGame.OnSandCountChanged(GameMain.mainPlayer.sandCount,
GameMain.mainPlayer.sandCount - Multiplayer.Session.LocalPlayer.Data.Mecha.SandCount);
SendPacket(new PlayerSandCount(GameMain.mainPlayer.sandCount));
return;
}
// player is valid
SendPacketExclude(new PlayerDisconnected(player.Id, Multiplayer.Session.NumPlayers), conn);
// For sync completed player who triggered OnPlayerJoinedGame() before
if (conn.ConnectionStatus == EConnectionStatus.Connected)
{
SimulatedWorld.OnPlayerLeftGame(player);
}
PlayerIdPool.Enqueue(player.Id);
Multiplayer.Session.PowerTowers.ResetAndBroadcast();
Multiplayer.Session.Statistics.UnRegisterPlayer(player.Id);
Multiplayer.Session.DysonSpheres.UnRegisterPlayer(conn);
// Note: using Keys or Values directly creates a readonly snapshot at the moment of call, as opposed to enumerating the dict.
var syncCount = Players.Syncing.Count;
if (conn.ConnectionStatus is not EConnectionStatus.Syncing || syncCount != 0)
{
return;
}
SendPacket(new SyncComplete());
Multiplayer.Session.World.OnAllPlayersSyncCompleted();
Disconnected?.Invoke(this, conn);
}
public void Start()
{
SaveManager.LoadServerData(loadSaveFile);
foreach (var assembly in AssembliesUtils.GetNebulaAssemblies())
{
PacketUtils.RegisterAllPacketNestedTypesInAssembly(assembly, PacketProcessor as NebulaNetPacketProcessor);
}
PacketUtils.RegisterAllPacketProcessorsInCallingAssembly(PacketProcessor as NebulaNetPacketProcessor, true);
foreach (var assembly in NebulaModAPI.TargetAssemblies)
{
PacketUtils.RegisterAllPacketNestedTypesInAssembly(assembly, PacketProcessor as NebulaNetPacketProcessor);
PacketUtils.RegisterAllPacketProcessorsInAssembly(assembly, PacketProcessor as NebulaNetPacketProcessor, true);
}
#if DEBUG
PacketProcessor.SimulateLatency = true;
#endif
if (Config.Options.EnableUPnpOrPmpSupport)
{
Task.Run(async () =>
{
var discoverer = new NatDiscoverer();
try
{
var device = await discoverer.DiscoverDeviceAsync();
await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, Port, Port, "DSP nebula"));
Log.Info($"Successfully created UPnp or Pmp port mapping for {Port}");
}
catch (NatDeviceNotFoundException)
{
Log.WarnInform("No UPnp or Pmp compatible/enabled NAT device found".Translate());
}
catch (MappingException)
{
Log.WarnInform("Could not create UPnp or Pmp port mapping".Translate());
}
});
}
ngrokManager = new NgrokManager(Port);
socket = new WebSocketServer(IPAddress.IPv6Any, Port)
{
Log = { Level = LogLevel.Debug, Output = Log.SocketOutput },
AllowForwardedRequest = true // This is required to make the websocket play nice with tunneling services like ngrok
};
if (!string.IsNullOrWhiteSpace(Config.Options.ServerPassword))
{
socket.AuthenticationSchemes = AuthenticationSchemes.Basic;
socket.UserCredentialsFinder = id =>
{
var name = id.Name;
// Return user name, password, and roles.
return name == "nebula-player"
? new NetworkCredential(name, Config.Options.ServerPassword)
: null; // If the user credentials are not found.
};
}
DisableNagleAlgorithm(socket);
WebSocketService.PacketProcessor = PacketProcessor as NebulaNetPacketProcessor;
WebSocketService.Server = this;
socket.AddWebSocketService<WebSocketService>("/socket", wse => new WebSocketService());
try
{
// Set wait time higher for high latency network
socket.WaitTime = TimeSpan.FromSeconds(20);
socket.KeepClean = Config.Options.CleanupInactiveSessions;
socket.Start();
}
catch (InvalidOperationException e)
{
Log.Warn(e.ToString());
InGamePopup.ShowError("Error", "An error occurred while hosting the game: ".Translate() + e.Message,
"Close".Translate());
Stop();
Multiplayer.LeaveGame();
return;
}
((LocalPlayer)Multiplayer.Session.LocalPlayer).IsHost = true;
((LocalPlayer)Multiplayer.Session.LocalPlayer).SetPlayerData(new PlayerData(
GetNextPlayerId(),
GameMain.localPlanet?.id ?? -1,
!string.IsNullOrWhiteSpace(Config.Options.Nickname) ? Config.Options.Nickname : GameMain.data.account.userName),
loadSaveFile);
Task.Run(async () =>
{
if (ngrokManager.NgrokEnabled)
{
try
{
// Wait up to 15s for ngrok to start, then get the address
var ip = await ngrokManager.GetNgrokAddressAsync();
DiscordManager.UpdateRichPresence(ip, updateTimestamp: true);
if (Multiplayer.IsDedicated)
{
Log.Info($">> Ngrok address: {ip}");
}
return;
}
catch (Exception e)
{
Log.Warn(e);
}
}
else
{
DiscordManager.UpdateRichPresence(
$"{(Config.Options.IPConfiguration != IPUtils.IPConfiguration.IPv6 ? await IPUtils.GetWANv4Address() : string.Empty)};" +
$"{(Config.Options.IPConfiguration != IPUtils.IPConfiguration.IPv4 ? await IPUtils.GetWANv6Address() : string.Empty)};" +
$"{Port}",
updateTimestamp: true);
}
});
try
{
NebulaModAPI.OnMultiplayerSessionChange(true);
NebulaModAPI.OnMultiplayerGameStarted?.Invoke();
}
catch (Exception e)
{
Log.Error("NebulaModAPI.OnMultiplayerGameStarted error:\n" + e);
}
}
public void Stop()
{
socket?.Stop();
ngrokManager?.StopNgrok();
try
{
NebulaModAPI.OnMultiplayerSessionChange(false);
NebulaModAPI.OnMultiplayerGameEnded?.Invoke();
}
catch (Exception e)
{
Log.Error("NebulaModAPI.OnMultiplayerGameEnded error:\n" + e);
}
}
public void Disconnect(INebulaConnection conn, DisconnectionReason reason, string reasonMessage = "")
{
Players.TryRemove(conn, out var player);
if (Encoding.UTF8.GetBytes(reasonMessage).Length <= 123)
{
((NebulaConnection)conn).peerSocket.Close((ushort)reason, reasonMessage);
}
else
{
throw new ArgumentException("Reason string cannot take up more than 123 bytes");
}
}
public void Dispose()
{
Stop();
GC.SuppressFinalize(this);
}
// Just to make a single entry point for all sends.
public void SendToPlayers<T>(IEnumerable<KeyValuePair<INebulaConnection, INebulaPlayer>> players, T packet) where T : class, new()
{
foreach (var kvp in players)
{
kvp.Key.SendPacket(packet);
}
}
public void SendPacket<T>(T packet) where T : class, new()
{
SendToPlayers(Players.Connected, packet);
}
/// <summary>
/// Send a packet to all players that match a predicate
/// </summary>
/// <param name="packet"></param>
/// <param name="condition"></param>
/// <typeparam name="T"></typeparam>
public void SendToMatching<T>(T packet, Predicate<INebulaPlayer> condition)
where T : class, new()
{
var players = Players.Connected
.Where(kvp => condition(kvp.Value));
SendToPlayers(players, packet);
}
public void SendPacketToStar<T>(T packet, int starId) where T : class, new()
{
SendToMatching(packet, p => p.Data.LocalStarId == starId);
}
public void SendPacketToLocalStar<T>(T packet) where T : class, new()
{
var starId = GameMain.data.localStar?.id ?? -1;
SendPacketToStar(packet, starId);
}
public void SendPacketToPlanet<T>(T packet, int planetId) where T : class, new()
{
SendToMatching(packet, p => p.Data.LocalPlanetId == planetId);
}
public void SendPacketToLocalPlanet<T>(T packet) where T : class, new()
{
var planetId = GameMain.data.localPlanet?.id ?? -1;
SendPacketToPlanet(packet, planetId);
}
public void SendPacketExclude<T>(T packet, INebulaConnection exclude) where T : class, new()
{
SendToMatching(packet, p => !p.Connection.Equals(exclude));
}
public void SendPacketToStarExclude<T>(T packet, int starId, INebulaConnection exclude) where T : class, new()
{
SendToMatching(packet, p => p.Data.LocalStarId == starId && !p.Connection.Equals(exclude));
}
public void Update()
{
PacketProcessor.ProcessPacketQueue();
if (!Multiplayer.Session.IsGameLoaded)
{
return;
}
gameResearchHashUpdateTimer += Time.deltaTime;
dysonLaunchUpateTimer += Time.deltaTime;
dysonSphereUpdateTimer += Time.deltaTime;
warningUpdateTimer += Time.deltaTime;
if (gameResearchHashUpdateTimer > GAME_RESEARCH_UPDATE_INTERVAL)
{
gameResearchHashUpdateTimer = 0;
if (GameMain.data.history.currentTech != 0)
{
var state = GameMain.data.history.techStates[GameMain.data.history.currentTech];
SendPacket(new GameHistoryResearchUpdatePacket(GameMain.data.history.currentTech, state.hashUploaded,
state.hashNeeded, GameMain.statistics.techHashedFor10Frames, GameMain.data.history.techQueueLength));
}
}
if (dysonLaunchUpateTimer > LAUNCH_UPDATE_INTERVAL)
{
dysonLaunchUpateTimer = 0;
Multiplayer.Session.Launch.SendBroadcastIfNeeded();
}
if (dysonSphereUpdateTimer > DYSONSPHERE_UPDATE_INTERVAL)
{
dysonSphereUpdateTimer = 0;
Multiplayer.Session.DysonSpheres.UpdateSphereStatusIfNeeded();
}
if (!(warningUpdateTimer > WARNING_UPDATE_INTERVAL))
{
return;
}
warningUpdateTimer = 0;
Multiplayer.Session.Warning.SendBroadcastIfNeeded();
}
private static void DisableNagleAlgorithm(WebSocketServer socketServer)
{
var listener = AccessTools.FieldRefAccess<WebSocketServer, TcpListener>("_listener")(socketServer);
listener.Server.NoDelay = true;
}
}