Adds PacketDecoder & Makes NetState testable (#286)

This commit is contained in:
Kamron Batman 2020-10-24 21:49:28 -07:00 committed by GitHub
parent e0da7209f6
commit 3337cfa4e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 177 additions and 35 deletions

View file

@ -1,4 +1,5 @@
using System;
using System.Buffers;
namespace Server.Network
{
@ -60,6 +61,66 @@ namespace Server.Network
0x4, 0x00D
};
public static void Compress(ReadOnlySpan<byte> input, CircularBufferWriter output)
{
int inputCapacity = input.Length;
if (inputCapacity > DefiniteOverflow)
{
return;
}
int bitCount = 0;
int bitValue = 0;
int inputIdx = 0;
while (inputIdx < inputCapacity)
{
int i = input[inputIdx++] << 1;
bitCount += _huffmanTable[i];
bitValue <<= _huffmanTable[i];
bitValue |= _huffmanTable[i + 1];
while (bitCount >= 8)
{
bitCount -= 8;
if (output.Length < output.Position + 1)
{
return;
}
output.Write((byte)(bitValue >> bitCount));
}
}
// terminal code
bitCount += _huffmanTable[0x200];
bitValue <<= _huffmanTable[0x200];
bitValue |= _huffmanTable[0x201];
// align on byte boundary
if ((bitCount & 7) != 0)
{
bitValue <<= (8 - (bitCount & 7));
bitCount += (8 - (bitCount & 7));
}
while (bitCount >= 8)
{
bitCount -= 8;
if (output.Length < output.Position + 1)
{
return;
}
output.Write((byte)(bitValue >> bitCount));
}
}
public static unsafe void Compress(
ReadOnlySpan<byte> input, int offset, int count, Span<byte> output, out int length
)