fix: Replaces throttlers and packet callbacks with function pointers (#1063)

Replaces multi-cast delegates with function pointers to gain 25% in performance and lower allocations.
This commit is contained in:
Stefano Merotta 2022-06-15 21:48:43 +02:00 • committed by GitHub
parent b779d7737f
commit f24b98f8e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
31 changed files with 189 additions and 172 deletions

View file

@ -15,40 +15,48 @@
namespace Server.Network
{
public static class ProtocolExtensions
public interface IProtocolExtensionsInfo
{
public static PacketHandler[] Register(byte packetId)
public int PacketId { get; }
}
public static class ProtocolExtensions<T> where T : struct, IProtocolExtensionsInfo
{
private static readonly PacketHandler[] packetHandlers = new PacketHandler[0x100];
private static int packetId;
public static unsafe PacketHandler[] Register(T info)
{
var packetHandlers = new PacketHandler[0x100];
packetId = info.PacketId;
IncomingPackets.Register(packetId, 0, false, &DecodeBundledPacket);
void DecodeBundledPacket(NetState state, CircularBufferReader reader, int packetLength)
return packetHandlers;
}
private static unsafe void DecodeBundledPacket(NetState state, CircularBufferReader reader, int packetLength)
{
int cmd = reader.ReadByte();
PacketHandler ph = packetHandlers[cmd];
if (ph == null)
{
int cmd = reader.ReadByte();
PacketHandler ph = packetHandlers[cmd];
if (ph == null)
{
return;
}
if (ph.Ingame && state.Mobile == null)
{
state.LogInfo($"Sent in-game packet (0x{packetId:X2}x{cmd:X2}) before having been attached to a mobile");
state.Disconnect("Sent in-game packet before being attached to a mobile.");
}
else if (ph.Ingame && state.Mobile.Deleted)
{
state.Disconnect(string.Empty);
}
else
{
ph.OnReceive(state, reader, packetLength);
}
return;
}
IncomingPackets.Register(packetId, 0, false, DecodeBundledPacket);
return packetHandlers;
if (ph.Ingame && state.Mobile == null)
{
state.LogInfo($"Sent in-game packet (0x{packetId:X2}x{cmd:X2}) before having been attached to a mobile");
state.Disconnect("Sent in-game packet before being attached to a mobile.");
}
else if (ph.Ingame && state.Mobile.Deleted)
{
state.Disconnect(string.Empty);
}
else
{
ph.OnReceive(state, reader, packetLength);
}
}
}
}