Skip to content

Commit 4795fde

Browse files
committed
OnClientException
1 parent 9a109c5 commit 4795fde

File tree

9 files changed

+59
-48
lines changed

9 files changed

+59
-48
lines changed

Zolian.GameServer/Zolian.GameServer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
</PropertyGroup>
4040

4141
<ItemGroup>
42-
<PackageReference Include="Chaos-Networking" Version="2.3.3" />
42+
<PackageReference Include="Chaos-Networking" Version="2.4.0" />
4343
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.0.0-preview3.24332.3" />
4444
<PackageReference Include="Microsoft.DependencyValidation.Analyzers" Version="0.11.0" />
4545
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0">

Zolian.Server.Base/Network/Client/Abstractions/IWorldClient.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,11 @@
99

1010
using System.Diagnostics;
1111
using Chaos.Networking.Entities.Server;
12-
using Darkages.Network.Server;
1312

1413
namespace Darkages.Network.Client.Abstractions;
1514

1615
public interface IWorldClient : IConnectedClient
1716
{
18-
ServerPacketLogger ServerPacketLogger { get; }
19-
ClientPacketLogger ClientPacketLogger { get; }
2017
bool MapUpdating { get; set; }
2118
Aisling Aisling { get; set; }
2219
DialogSession DlgSession { get; set; }
Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
1-
namespace Darkages.Network.Client;
1+
using System.Collections.Concurrent;
2+
using System.Net;
3+
4+
namespace Darkages.Network.Client;
25

36
public class ClientPacketLogger
47
{
58
private const int MaxLogSize = 15;
6-
private readonly Queue<string> _packetLog = [];
9+
private ConcurrentDictionary<IPAddress, Queue<string>> _packetLog = [];
710
private readonly Lock _logLock = new();
811

9-
public void LogPacket(string packetDetails)
12+
public void LogPacket(IPAddress ip, string packetDetails)
1013
{
1114
lock (_logLock)
1215
{
13-
if (_packetLog.Count >= MaxLogSize)
14-
_packetLog.Dequeue(); // Remove the oldest log
16+
_packetLog.TryGetValue(ip, out var packetLog);
17+
if (packetLog is null) return;
18+
if (packetLog.Count >= MaxLogSize)
19+
packetLog.Dequeue(); // Remove the oldest log
1520

16-
_packetLog.Enqueue(packetDetails);
21+
packetLog.Enqueue(packetDetails);
1722
}
1823
}
1924

20-
public IEnumerable<string> GetRecentLogs()
25+
public IEnumerable<string> GetRecentLogs(IPAddress ip)
2126
{
2227
lock (_logLock)
2328
{
24-
return _packetLog;
29+
_packetLog.TryGetValue(ip, out var packetLog);
30+
return packetLog;
2531
}
2632
}
2733
}

Zolian.Server.Base/Network/Client/WorldClient.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,13 @@
4949
using Nation = Chaos.DarkAges.Definitions.Nation;
5050
using RestPosition = Chaos.DarkAges.Definitions.RestPosition;
5151
using Darkages.Sprites.Entity;
52-
using static ServiceStack.Diagnostics.Events;
5352

5453
namespace Darkages.Network.Client;
5554

5655
[UsedImplicitly]
5756
public class WorldClient : WorldClientBase, IWorldClient
5857
{
5958
private readonly IWorldServer<WorldClient> _server;
60-
public ServerPacketLogger ServerPacketLogger { get; } = new();
61-
public ClientPacketLogger ClientPacketLogger { get; } = new();
6259
public readonly WorldServerTimer SkillSpellTimer = new(TimeSpan.FromMilliseconds(1000));
6360
public readonly Stopwatch CooldownControl = new();
6461
public readonly Stopwatch SpellControl = new();
@@ -1345,8 +1342,9 @@ private void ServerPacketDisplayBuilder()
13451342
var stackTrace = new StackTrace();
13461343
var currentMethod = stackTrace.GetFrame(1)?.GetMethod()?.Name ?? "Unknown";
13471344
var callingMethod = stackTrace.GetFrame(2)?.GetMethod()?.Name ?? "Unknown";
1348-
1349-
ServerPacketLogger.LogPacket($"{Aisling?.Username ?? RemoteIp.ToString()} with {currentMethod}, called from {callingMethod}");
1345+
var callingMethodTwo = stackTrace.GetFrame(3)?.GetMethod()?.Name ?? "Unknown";
1346+
var callingMethodThree = stackTrace.GetFrame(4)?.GetMethod()?.Name ?? "Unknown";
1347+
ServerSetup.Instance.Game.ServerPacketLogger.LogPacket(RemoteIp, $"{Aisling?.Username ?? RemoteIp.ToString()} with: {currentMethod}, from: {callingMethod}, from: {callingMethodTwo}, from: {callingMethodThree}");
13501348
}
13511349

13521350
/// <summary>
@@ -1360,7 +1358,6 @@ public void SendLoginMessage(LoginMessageType loginMessageType, string message =
13601358
Message = message
13611359
};
13621360

1363-
ServerPacketDisplayBuilder();
13641361
Send(args);
13651362
}
13661363

@@ -1475,7 +1472,8 @@ public void SendAnimation(ushort targetEffect, Position position = null, uint ta
14751472
var callingMethodOne = stackTrace.GetFrame(2)?.GetMethod()?.Name ?? "Unknown";
14761473
var callingMethodTwo = stackTrace.GetFrame(3)?.GetMethod()?.Name ?? "Unknown";
14771474
var callingMethodThree = stackTrace.GetFrame(4)?.GetMethod()?.Name ?? "Unknown";
1478-
ServerPacketLogger.LogPacket($"{Aisling?.Username ?? RemoteIp.ToString()} Animation from: {callingMethodOne}, from: {callingMethodTwo}, from: {callingMethodThree}");
1475+
var callingMethodFour = stackTrace.GetFrame(5)?.GetMethod()?.Name ?? "Unknown";
1476+
ServerSetup.Instance.Game.ServerPacketLogger.LogPacket(RemoteIp, $"{Aisling?.Username ?? RemoteIp.ToString()} Animation from: {callingMethodOne}, from: {callingMethodTwo}, from: {callingMethodThree}, from: {callingMethodFour}");
14791477
Send(args);
14801478
}
14811479
catch
@@ -1782,7 +1780,6 @@ public void SendConfirmExit()
17821780
ExitConfirmed = ExitConfirmed
17831781
};
17841782

1785-
ServerPacketDisplayBuilder();
17861783
Send(args);
17871784
}
17881785

@@ -1808,7 +1805,6 @@ public void SendCooldown(bool skill, byte slot, int cooldownSeconds)
18081805
CooldownSecs = (uint)cooldownSeconds
18091806
};
18101807

1811-
ServerPacketDisplayBuilder();
18121808
Send(args);
18131809
}
18141810

@@ -1956,7 +1952,6 @@ public void SendEffect(EffectColor effectColor, byte effectIcon)
19561952
EffectIcon = effectIcon
19571953
};
19581954

1959-
ServerPacketDisplayBuilder();
19601955
Send(args);
19611956
}
19621957

@@ -2160,7 +2155,6 @@ public void SendLightLevel(LightLevel lightLevel)
21602155
LightLevel = lightLevel
21612156
};
21622157

2163-
ServerPacketDisplayBuilder();
21642158
Send(args);
21652159
}
21662160

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
1-
namespace Darkages.Network.Server;
1+
using System.Collections.Concurrent;
2+
using System.Net;
3+
4+
namespace Darkages.Network.Server;
25

36
public class ServerPacketLogger
47
{
58
private const int MaxLogSize = 15;
6-
private readonly Queue<string> _packetLog = [];
9+
private ConcurrentDictionary<IPAddress, Queue<string>> _packetLog = [];
710
private readonly Lock _logLock = new();
811

9-
public void LogPacket(string packetDetails)
12+
public void LogPacket(IPAddress ip, string packetDetails)
1013
{
1114
lock (_logLock)
1215
{
13-
if (_packetLog.Count >= MaxLogSize)
14-
_packetLog.Dequeue(); // Remove the oldest log
16+
_packetLog.TryGetValue(ip, out var packetLog);
17+
if (packetLog is null) return;
18+
if (packetLog.Count >= MaxLogSize)
19+
packetLog.Dequeue(); // Remove the oldest log
1520

16-
_packetLog.Enqueue(packetDetails);
21+
packetLog.Enqueue(packetDetails);
1722
}
1823
}
1924

20-
public IEnumerable<string> GetRecentLogs()
25+
public IEnumerable<string> GetRecentLogs(IPAddress ip)
2126
{
2227
lock (_logLock)
2328
{
24-
return _packetLog;
29+
_packetLog.TryGetValue(ip, out var packetLog);
30+
return packetLog;
2531
}
2632
}
2733
}

Zolian.Server.Base/Network/Server/WorldServer.cs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ namespace Darkages.Network.Server;
4949
public sealed class WorldServer : ServerBase<IWorldClient>, IWorldServer<IWorldClient>
5050
{
5151
private readonly IClientFactory<WorldClient> _clientProvider;
52+
public ServerPacketLogger ServerPacketLogger { get; } = new();
53+
public ClientPacketLogger ClientPacketLogger { get; } = new();
5254
private readonly MServerTable _serverTable;
5355
private const string InternalIP = "192.168.50.1"; // Cannot use ServerConfig due to value needing to be constant
5456
private static readonly string[] GameMastersIPs = ServerSetup.Instance.GameMastersIPs;
@@ -2664,6 +2666,19 @@ static ValueTask InnerOnWorldMapClick(IWorldClient localClient, WorldMapClickArg
26642666
}
26652667
}
26662668

2669+
/// <summary>
2670+
/// 0x42 - Client Exception reported to the Server
2671+
/// </summary>
2672+
/// <returns>Prints details of the packets leading up to the crash</returns>
2673+
public ValueTask OnClientException(IWorldClient client, in Packet packet)
2674+
{
2675+
var args = PacketSerializer.Deserialize<ClientExceptionArgs>(in packet);
2676+
ServerSetup.EventsLogger($"{client.RemoteIp} encountered an exception: {args.ExceptionStr} - See packetLogger for details", LogLevel.Critical);
2677+
DisplayRecentServerPacketLogs(client);
2678+
DisplayRecentClientPacketLogs(client);
2679+
return default;
2680+
}
2681+
26672682
/// <summary>
26682683
/// 0x43 - Client Click (map, player, npc, monster) - F1 Button
26692684
/// </summary>
@@ -3137,7 +3152,7 @@ public override ValueTask HandlePacketAsync(IWorldClient client, in Packet packe
31373152
{
31383153
if (handler is not null)
31393154
{
3140-
client.ClientPacketLogger.LogPacket($"{client.Aisling?.Username ?? client.RemoteIp.ToString()} with Client OpCode: {opCode} ({Enum.GetName(typeof(ClientOpCode), opCode)})");
3155+
ClientPacketLogger.LogPacket(client.RemoteIp, $"{client.Aisling?.Username ?? client.RemoteIp.ToString()} with Client OpCode: {opCode} ({Enum.GetName(typeof(ClientOpCode), opCode)})");
31413156
return handler(client, in packet);
31423157
}
31433158

@@ -3186,6 +3201,7 @@ protected override void IndexHandlers()
31863201
ClientHandlers[(byte)ClientOpCode.BoardInteraction] = OnBoardInteraction; // 0x3B
31873202
ClientHandlers[(byte)ClientOpCode.SkillUse] = OnSkillUse; // 0x3E
31883203
ClientHandlers[(byte)ClientOpCode.WorldMapClick] = OnWorldMapClick; // 0x3F
3204+
ClientHandlers[(byte)ClientOpCode.ClientException] = OnClientException; // 0x42
31893205
ClientHandlers[(byte)ClientOpCode.Click] = OnClick; // 0x43
31903206
ClientHandlers[(byte)ClientOpCode.Unequip] = OnUnequip; // 0x44
31913207
ClientHandlers[(byte)ClientOpCode.HeartBeat] = OnHeartBeatAsync; // 0x45
@@ -3284,9 +3300,6 @@ private async void OnDisconnect(object sender, EventArgs e)
32843300

32853301
if (aisling == null)
32863302
{
3287-
// Capture Unclean Exit
3288-
DisplayRecentServerPacketLogs(client);
3289-
DisplayRecentClientPacketLogs(client);
32903303
ClientRegistry.TryRemove(client.Id, out _);
32913304
return;
32923305
}
@@ -3299,10 +3312,6 @@ private async void OnDisconnect(object sender, EventArgs e)
32993312

33003313
try
33013314
{
3302-
// Capture Unclean Exit
3303-
DisplayRecentServerPacketLogs(client);
3304-
DisplayRecentClientPacketLogs(client);
3305-
33063315
// Close Popups
33073316
client.CloseDialog();
33083317
aisling.CancelExchange();
@@ -3469,20 +3478,20 @@ private static void ReportEndpoint(string remoteIp, string comment)
34693478
}
34703479
}
34713480

3472-
private static void DisplayRecentClientPacketLogs(IWorldClient client)
3481+
private void DisplayRecentClientPacketLogs(IWorldClient client)
34733482
{
3474-
var logs = client.ClientPacketLogger.GetRecentLogs().ToList();
3483+
var logs = ClientPacketLogger.GetRecentLogs(client.RemoteIp).ToList();
34753484

34763485
foreach (var log in logs)
3477-
ServerSetup.PacketLogger(log, LogLevel.Critical);
3486+
ServerSetup.PacketLogger(log);
34783487
}
34793488

3480-
private static void DisplayRecentServerPacketLogs(IWorldClient client)
3489+
private void DisplayRecentServerPacketLogs(IWorldClient client)
34813490
{
3482-
var logs = client.ServerPacketLogger.GetRecentLogs().ToList();
3491+
var logs = ServerPacketLogger.GetRecentLogs(client.RemoteIp).ToList();
34833492

34843493
foreach (var log in logs)
3485-
ServerSetup.PacketLogger(log, LogLevel.Critical);
3494+
ServerSetup.PacketLogger(log);
34863495
}
34873496

34883497
private static bool IsManualAction(ClientOpCode opCode) => opCode switch

Zolian.Server.Base/Object/ObjectService.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Darkages.Types;
33

44
using System.Collections.Concurrent;
5-
using JetBrains.Annotations;
65
using Darkages.Sprites.Entity;
76
using Darkages.Network.Server;
87

Zolian.Server.Base/Zolian.Server.Base.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
</PropertyGroup>
4141

4242
<ItemGroup>
43-
<PackageReference Include="Chaos-Networking" Version="2.3.3" />
43+
<PackageReference Include="Chaos-Networking" Version="2.4.0" />
4444
<PackageReference Include="Dapper.StrongName" Version="2.1.35" />
4545
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.0.0-preview3.24332.3" />
4646
<PackageReference Include="Microsoft.DependencyValidation.Analyzers" Version="0.11.0" />

ZolianTest/ZolianTest.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</PropertyGroup>
1919

2020
<ItemGroup>
21-
<PackageReference Include="Chaos-Networking" Version="2.3.3" />
21+
<PackageReference Include="Chaos-Networking" Version="2.4.0" />
2222
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.0.0-preview3.24332.3" />
2323
<PackageReference Include="Microsoft.DependencyValidation.Analyzers" Version="0.11.0" />
2424
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />

0 commit comments

Comments
 (0)