Skip to content

Commit

Permalink
OnClientException
Browse files Browse the repository at this point in the history
  • Loading branch information
FallenDev committed Dec 27, 2024
1 parent 9a109c5 commit 4795fde
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Zolian.GameServer/Zolian.GameServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Chaos-Networking" Version="2.3.3" />
<PackageReference Include="Chaos-Networking" Version="2.4.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.0.0-preview3.24332.3" />
<PackageReference Include="Microsoft.DependencyValidation.Analyzers" Version="0.11.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@

using System.Diagnostics;
using Chaos.Networking.Entities.Server;
using Darkages.Network.Server;

namespace Darkages.Network.Client.Abstractions;

public interface IWorldClient : IConnectedClient
{
ServerPacketLogger ServerPacketLogger { get; }
ClientPacketLogger ClientPacketLogger { get; }
bool MapUpdating { get; set; }
Aisling Aisling { get; set; }
DialogSession DlgSession { get; set; }
Expand Down
22 changes: 14 additions & 8 deletions Zolian.Server.Base/Network/Client/ClientPacketLogger.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
namespace Darkages.Network.Client;
using System.Collections.Concurrent;
using System.Net;

namespace Darkages.Network.Client;

public class ClientPacketLogger
{
private const int MaxLogSize = 15;
private readonly Queue<string> _packetLog = [];
private ConcurrentDictionary<IPAddress, Queue<string>> _packetLog = [];
private readonly Lock _logLock = new();

public void LogPacket(string packetDetails)
public void LogPacket(IPAddress ip, string packetDetails)
{
lock (_logLock)
{
if (_packetLog.Count >= MaxLogSize)
_packetLog.Dequeue(); // Remove the oldest log
_packetLog.TryGetValue(ip, out var packetLog);
if (packetLog is null) return;
if (packetLog.Count >= MaxLogSize)
packetLog.Dequeue(); // Remove the oldest log

_packetLog.Enqueue(packetDetails);
packetLog.Enqueue(packetDetails);
}
}

public IEnumerable<string> GetRecentLogs()
public IEnumerable<string> GetRecentLogs(IPAddress ip)
{
lock (_logLock)
{
return _packetLog;
_packetLog.TryGetValue(ip, out var packetLog);
return packetLog;
}
}
}
16 changes: 5 additions & 11 deletions Zolian.Server.Base/Network/Client/WorldClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,13 @@
using Nation = Chaos.DarkAges.Definitions.Nation;
using RestPosition = Chaos.DarkAges.Definitions.RestPosition;
using Darkages.Sprites.Entity;
using static ServiceStack.Diagnostics.Events;

namespace Darkages.Network.Client;

[UsedImplicitly]
public class WorldClient : WorldClientBase, IWorldClient
{
private readonly IWorldServer<WorldClient> _server;
public ServerPacketLogger ServerPacketLogger { get; } = new();
public ClientPacketLogger ClientPacketLogger { get; } = new();
public readonly WorldServerTimer SkillSpellTimer = new(TimeSpan.FromMilliseconds(1000));
public readonly Stopwatch CooldownControl = new();
public readonly Stopwatch SpellControl = new();
Expand Down Expand Up @@ -1345,8 +1342,9 @@ private void ServerPacketDisplayBuilder()
var stackTrace = new StackTrace();
var currentMethod = stackTrace.GetFrame(1)?.GetMethod()?.Name ?? "Unknown";
var callingMethod = stackTrace.GetFrame(2)?.GetMethod()?.Name ?? "Unknown";

ServerPacketLogger.LogPacket($"{Aisling?.Username ?? RemoteIp.ToString()} with {currentMethod}, called from {callingMethod}");
var callingMethodTwo = stackTrace.GetFrame(3)?.GetMethod()?.Name ?? "Unknown";
var callingMethodThree = stackTrace.GetFrame(4)?.GetMethod()?.Name ?? "Unknown";
ServerSetup.Instance.Game.ServerPacketLogger.LogPacket(RemoteIp, $"{Aisling?.Username ?? RemoteIp.ToString()} with: {currentMethod}, from: {callingMethod}, from: {callingMethodTwo}, from: {callingMethodThree}");
}

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

ServerPacketDisplayBuilder();
Send(args);
}

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

ServerPacketDisplayBuilder();
Send(args);
}

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

ServerPacketDisplayBuilder();
Send(args);
}

Expand Down Expand Up @@ -1956,7 +1952,6 @@ public void SendEffect(EffectColor effectColor, byte effectIcon)
EffectIcon = effectIcon
};

ServerPacketDisplayBuilder();
Send(args);
}

Expand Down Expand Up @@ -2160,7 +2155,6 @@ public void SendLightLevel(LightLevel lightLevel)
LightLevel = lightLevel
};

ServerPacketDisplayBuilder();
Send(args);
}

Expand Down
22 changes: 14 additions & 8 deletions Zolian.Server.Base/Network/Server/ServerPacketLogger.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
namespace Darkages.Network.Server;
using System.Collections.Concurrent;
using System.Net;

namespace Darkages.Network.Server;

public class ServerPacketLogger
{
private const int MaxLogSize = 15;
private readonly Queue<string> _packetLog = [];
private ConcurrentDictionary<IPAddress, Queue<string>> _packetLog = [];
private readonly Lock _logLock = new();

public void LogPacket(string packetDetails)
public void LogPacket(IPAddress ip, string packetDetails)
{
lock (_logLock)
{
if (_packetLog.Count >= MaxLogSize)
_packetLog.Dequeue(); // Remove the oldest log
_packetLog.TryGetValue(ip, out var packetLog);
if (packetLog is null) return;
if (packetLog.Count >= MaxLogSize)
packetLog.Dequeue(); // Remove the oldest log

_packetLog.Enqueue(packetDetails);
packetLog.Enqueue(packetDetails);
}
}

public IEnumerable<string> GetRecentLogs()
public IEnumerable<string> GetRecentLogs(IPAddress ip)
{
lock (_logLock)
{
return _packetLog;
_packetLog.TryGetValue(ip, out var packetLog);
return packetLog;
}
}
}
37 changes: 23 additions & 14 deletions Zolian.Server.Base/Network/Server/WorldServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ namespace Darkages.Network.Server;
public sealed class WorldServer : ServerBase<IWorldClient>, IWorldServer<IWorldClient>
{
private readonly IClientFactory<WorldClient> _clientProvider;
public ServerPacketLogger ServerPacketLogger { get; } = new();
public ClientPacketLogger ClientPacketLogger { get; } = new();
private readonly MServerTable _serverTable;
private const string InternalIP = "192.168.50.1"; // Cannot use ServerConfig due to value needing to be constant
private static readonly string[] GameMastersIPs = ServerSetup.Instance.GameMastersIPs;
Expand Down Expand Up @@ -2664,6 +2666,19 @@ static ValueTask InnerOnWorldMapClick(IWorldClient localClient, WorldMapClickArg
}
}

/// <summary>
/// 0x42 - Client Exception reported to the Server
/// </summary>
/// <returns>Prints details of the packets leading up to the crash</returns>
public ValueTask OnClientException(IWorldClient client, in Packet packet)
{
var args = PacketSerializer.Deserialize<ClientExceptionArgs>(in packet);
ServerSetup.EventsLogger($"{client.RemoteIp} encountered an exception: {args.ExceptionStr} - See packetLogger for details", LogLevel.Critical);
DisplayRecentServerPacketLogs(client);
DisplayRecentClientPacketLogs(client);
return default;
}

/// <summary>
/// 0x43 - Client Click (map, player, npc, monster) - F1 Button
/// </summary>
Expand Down Expand Up @@ -3137,7 +3152,7 @@ public override ValueTask HandlePacketAsync(IWorldClient client, in Packet packe
{
if (handler is not null)
{
client.ClientPacketLogger.LogPacket($"{client.Aisling?.Username ?? client.RemoteIp.ToString()} with Client OpCode: {opCode} ({Enum.GetName(typeof(ClientOpCode), opCode)})");
ClientPacketLogger.LogPacket(client.RemoteIp, $"{client.Aisling?.Username ?? client.RemoteIp.ToString()} with Client OpCode: {opCode} ({Enum.GetName(typeof(ClientOpCode), opCode)})");
return handler(client, in packet);
}

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

if (aisling == null)
{
// Capture Unclean Exit
DisplayRecentServerPacketLogs(client);
DisplayRecentClientPacketLogs(client);
ClientRegistry.TryRemove(client.Id, out _);
return;
}
Expand All @@ -3299,10 +3312,6 @@ private async void OnDisconnect(object sender, EventArgs e)

try
{
// Capture Unclean Exit
DisplayRecentServerPacketLogs(client);
DisplayRecentClientPacketLogs(client);

// Close Popups
client.CloseDialog();
aisling.CancelExchange();
Expand Down Expand Up @@ -3469,20 +3478,20 @@ private static void ReportEndpoint(string remoteIp, string comment)
}
}

private static void DisplayRecentClientPacketLogs(IWorldClient client)
private void DisplayRecentClientPacketLogs(IWorldClient client)
{
var logs = client.ClientPacketLogger.GetRecentLogs().ToList();
var logs = ClientPacketLogger.GetRecentLogs(client.RemoteIp).ToList();

foreach (var log in logs)
ServerSetup.PacketLogger(log, LogLevel.Critical);
ServerSetup.PacketLogger(log);
}

private static void DisplayRecentServerPacketLogs(IWorldClient client)
private void DisplayRecentServerPacketLogs(IWorldClient client)
{
var logs = client.ServerPacketLogger.GetRecentLogs().ToList();
var logs = ServerPacketLogger.GetRecentLogs(client.RemoteIp).ToList();

foreach (var log in logs)
ServerSetup.PacketLogger(log, LogLevel.Critical);
ServerSetup.PacketLogger(log);
}

private static bool IsManualAction(ClientOpCode opCode) => opCode switch
Expand Down
1 change: 0 additions & 1 deletion Zolian.Server.Base/Object/ObjectService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Darkages.Types;

using System.Collections.Concurrent;
using JetBrains.Annotations;
using Darkages.Sprites.Entity;
using Darkages.Network.Server;

Expand Down
2 changes: 1 addition & 1 deletion Zolian.Server.Base/Zolian.Server.Base.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Chaos-Networking" Version="2.3.3" />
<PackageReference Include="Chaos-Networking" Version="2.4.0" />
<PackageReference Include="Dapper.StrongName" Version="2.1.35" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.0.0-preview3.24332.3" />
<PackageReference Include="Microsoft.DependencyValidation.Analyzers" Version="0.11.0" />
Expand Down
2 changes: 1 addition & 1 deletion ZolianTest/ZolianTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Chaos-Networking" Version="2.3.3" />
<PackageReference Include="Chaos-Networking" Version="2.4.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.0.0-preview3.24332.3" />
<PackageReference Include="Microsoft.DependencyValidation.Analyzers" Version="0.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
Expand Down

0 comments on commit 4795fde

Please sign in to comment.