parent
11227391d3
commit
d9f417ee19
17 changed files with 1084 additions and 381 deletions
84
Projects/Server.Tests/Network/Packets/PacketTestUtilities.cs
Normal file
84
Projects/Server.Tests/Network/Packets/PacketTestUtilities.cs
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
using System;
|
||||
using System.Buffers.Binary;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using Server.Network;
|
||||
|
||||
namespace Server.Tests.Network.Packets
|
||||
{
|
||||
public static class PacketTestUtilities
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Span<byte> Compile(this Packet p) =>
|
||||
p.Compile(false, out int length).AsSpan(0, length);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyTo(this ushort number, Span<byte> bytes) => BinaryPrimitives.WriteUInt16BigEndian(bytes, number);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyTo(this short number, Span<byte> bytes) => BinaryPrimitives.WriteInt16BigEndian(bytes, number);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyTo(this Serial s, Span<byte> bytes) => BinaryPrimitives.WriteUInt32BigEndian(bytes, s);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyTo(this uint number, Span<byte> bytes) => BinaryPrimitives.WriteUInt32BigEndian(bytes, number);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyTo(this int number, Span<byte> bytes) => BinaryPrimitives.WriteInt32BigEndian(bytes, number);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyASCIITo(this string str, Span<byte> bytes) => Encoding.ASCII.GetBytes(str.AsSpan(), bytes);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyASCIITo(this string str, int max, Span<byte> bytes) => Encoding.ASCII.GetBytes(str.AsSpan(0, Math.Min(str.Length, max)), bytes);
|
||||
|
||||
// With Position Reference
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyTo(this ushort number, ref int pos, Span<byte> bytes)
|
||||
{
|
||||
BinaryPrimitives.WriteUInt16BigEndian(bytes.Slice(pos, 2), number);
|
||||
pos += 2;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyTo(this short number, ref int pos, Span<byte> bytes)
|
||||
{
|
||||
BinaryPrimitives.WriteInt16BigEndian(bytes.Slice(pos, 2), number);
|
||||
pos += 2;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyTo(this Serial s, ref int pos, Span<byte> bytes)
|
||||
{
|
||||
BinaryPrimitives.WriteUInt32BigEndian(bytes.Slice(pos, 4), s);
|
||||
pos += 4;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyTo(this uint number, ref int pos, Span<byte> bytes)
|
||||
{
|
||||
BinaryPrimitives.WriteUInt32BigEndian(bytes.Slice(pos, 4), number);
|
||||
pos += 4;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyTo(this int number, ref int pos, Span<byte> bytes)
|
||||
{
|
||||
BinaryPrimitives.WriteInt32BigEndian(bytes.Slice(pos, 4), number);
|
||||
pos += 4;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyASCIITo(this string str, ref int pos, Span<byte> bytes)
|
||||
{
|
||||
pos += Encoding.ASCII.GetBytes(str.AsSpan(), bytes.Slice(pos));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyASCIITo(this string str, ref int pos, int max, Span<byte> bytes)
|
||||
{
|
||||
pos += Encoding.ASCII.GetBytes(str.AsSpan(0, Math.Min(str.Length, max)), bytes.Slice(pos));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue