fix(core): Removes refs for network stack (#461)

This commit is contained in:
Kamron Batman 2021-02-05 01:11:13 -08:00 • committed by GitHub
parent 6295699b3d
commit 97aa092f17
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 7 deletions

View file

@ -32,8 +32,8 @@ namespace Server.Network
{ {
public delegate void NetStateCreatedCallback(NetState ns); public delegate void NetStateCreatedCallback(NetState ns);
public delegate void DecodePacket(ref CircularBuffer<byte> buffer, ref int length); public delegate void DecodePacket(CircularBuffer<byte> buffer, ref int length);
public delegate void EncodePacket(ReadOnlySpan<byte> inputBuffer, ref CircularBuffer<byte> outputBuffer, out int length); public delegate void EncodePacket(ReadOnlySpan<byte> inputBuffer, CircularBuffer<byte> outputBuffer, out int length);
public partial class NetState : IComparable<NetState>, IDisposable public partial class NetState : IComparable<NetState>, IDisposable
{ {
@ -392,7 +392,7 @@ namespace Server.Network
{ {
if (_packetEncoder != null) if (_packetEncoder != null)
{ {
_packetEncoder(span, ref buffer, out length); _packetEncoder(span, buffer, out length);
} }
else else
{ {
@ -533,7 +533,7 @@ namespace Server.Network
private void DecodePacket(ArraySegment<byte>[] buffer, ref int length) private void DecodePacket(ArraySegment<byte>[] buffer, ref int length)
{ {
CircularBuffer<byte> cBuffer = new CircularBuffer<byte>(buffer); CircularBuffer<byte> cBuffer = new CircularBuffer<byte>(buffer);
_packetDecoder?.Invoke(ref cBuffer, ref length); _packetDecoder?.Invoke(cBuffer, ref length);
} }
private async void RecvTask(object state) private async void RecvTask(object state)

View file

@ -61,12 +61,72 @@ namespace Server.Network
0x4, 0x00D 0x4, 0x00D
}; };
public static void Compress(ReadOnlySpan<byte> input, ref CircularBuffer<byte> output, out int length) public static void Compress(ReadOnlySpan<byte> input, CircularBuffer<byte> output, out int length)
{ {
length = Compress(input, ref output); length = Compress(input, output);
} }
public static int Compress(ReadOnlySpan<byte> input, ref CircularBuffer<byte> output) public static int Compress(ReadOnlySpan<byte> input, CircularBuffer<byte> output)
{
if (input.Length > DefiniteOverflow)
{
return 0;
}
int bitCount = 0;
int bitValue = 0;
int inputIdx = 0;
int outputIdx = 0;
while (inputIdx < input.Length)
{
int i = input[inputIdx++] << 1;
bitCount += _huffmanTable[i];
bitValue = (bitValue << _huffmanTable[i]) | _huffmanTable[i + 1];
while (bitCount >= 8)
{
bitCount -= 8;
if (output.Length < outputIdx + 1)
{
return 0;
}
output[outputIdx++] = (byte)(bitValue >> bitCount);
}
}
// terminal code
bitCount += _huffmanTable[0x200];
bitValue = (bitValue << _huffmanTable[0x200]) | _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 < outputIdx + 1)
{
return 0;
}
output[outputIdx++] = (byte)(bitValue >> bitCount);
}
return outputIdx;
}
public static int Compress(CircularBuffer<byte> input, CircularBuffer<byte> output)
{ {
if (input.Length > DefiniteOverflow) if (input.Length > DefiniteOverflow)
{ {