fix(core): Cleans up uninitialized packets (#397)

- [X] Encapsulates/abstracts buffer cache checking
- [X] Changes mobile moving cache to use Span2D
This commit is contained in:
Kamron Batman 2021-01-09 14:10:29 -08:00 committed by GitHub
parent 0b9fb9c071
commit 8763be8a21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 417 additions and 529 deletions

View file

@ -17,6 +17,7 @@ using System;
using System.Buffers;
using System.IO;
using System.Runtime.CompilerServices;
using Microsoft.Toolkit.HighPerformance.Memory;
namespace Server.Network
{
@ -25,7 +26,8 @@ namespace Server.Network
public const int BondedStatusPacketLength = 11;
public const int DeathAnimationPacketLength = 13;
public const int MobileMovingPacketLength = 17;
public const int MobileMovingPacketCacheLength = MobileMovingPacketLength * 8 * 2; // 8 notoriety, 2 client versions
public const int MobileMovingPacketCacheHeight = 16; // 8 notoriety, 2 client versions
public const int MobileMovingPacketCacheByteLength = MobileMovingPacketLength * MobileMovingPacketCacheHeight;
public const int AttributeMaximum = 100;
public const int MobileAttributePacketLength = 9;
public const int MobileAttributesPacketLength = 17;
@ -87,6 +89,11 @@ namespace Server.Network
public static void CreateMobileMoving(Span<byte> buffer, Mobile m, int noto, bool stygianAbyss)
{
if (buffer[0] != 0)
{
return;
}
var loc = m.Location;
var hue = m.SolidHueOverride >= 0 ? m.SolidHueOverride : m.Hue;
@ -114,18 +121,18 @@ namespace Server.Network
return;
}
Span<byte> span = stackalloc byte[MobileMovingPacketLength];
CreateMobileMoving(span, target, noto, ns.StygianAbyss);
ns.Send(span);
Span<byte> buffer = stackalloc byte[MobileMovingPacketLength].InitializePacket();
CreateMobileMoving(buffer, target, noto, ns.StygianAbyss);
ns.Send(buffer);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SendMobileMovingUsingCache(this NetState ns, Span<byte> cache, Mobile source, Mobile target) =>
public static void SendMobileMovingUsingCache(this NetState ns, Span2D<byte> cache, Mobile source, Mobile target) =>
ns.SendMobileMovingUsingCache(cache, target, Notoriety.Compute(source, target));
// Requires a buffer of 16 packets, 17bytes per packet (272 bytes).
// Requires cache to have the first byte of each packet zeroed.
public static void SendMobileMovingUsingCache(this NetState ns, Span<byte> cache, Mobile target, int noto)
public static void SendMobileMovingUsingCache(this NetState ns, Span2D<byte> cache, Mobile target, int noto)
{
if (ns == null)
{
@ -133,14 +140,9 @@ namespace Server.Network
}
var stygianAbyss = ns.StygianAbyss;
var startIndex = (noto * 2 + (stygianAbyss ? 1 : 0)) * MobileMovingPacketLength;
var buffer = cache.Slice(startIndex, MobileMovingPacketLength);
// Packet not created yet
if (buffer[0] == 0)
{
CreateMobileMoving(buffer, target, noto, stygianAbyss);
}
var row = noto * 2 + (stygianAbyss ? 1 : 0);
var buffer = cache.GetRowSpan(row);
CreateMobileMoving(buffer, target, noto, stygianAbyss);
ns.Send(buffer);
}
@ -283,6 +285,11 @@ namespace Server.Network
Serial mobile, int action, int frameCount, int repeatCount, bool forward, bool repeat, int delay
)
{
if (buffer[0] != 0)
{
return;
}
var writer = new SpanWriter(buffer);
writer.Write((byte)0x6E); // Packet ID
writer.Write(mobile);