fix(core): Converts account packets to spanwriter (#381)

- [X] Converts account packets to spanwriter
This commit is contained in:
Kamron Batman 2021-01-04 18:47:20 -08:00 committed by GitHub
parent 3b413371dc
commit ac76c57c39
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 157 additions and 117 deletions

View file

@ -479,22 +479,6 @@ namespace System.Buffers
Write((byte)0); Write((byte)0);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Clear()
{
if (Position < _first.Length)
{
_first.Slice(Position).Clear();
_second.Clear();
}
else
{
_second.Slice(Position - _first.Length).Clear();
}
Position = Length;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Clear(int amount) public void Clear(int amount)
{ {

View file

@ -31,7 +31,7 @@ namespace System.Buffers
private Span<byte> _buffer; private Span<byte> _buffer;
private int _position; private int _position;
public int Length { get; private set; } public int BytesWritten { get; private set; }
public int Position public int Position
{ {
@ -40,9 +40,9 @@ namespace System.Buffers
{ {
_position = value; _position = value;
if (value > Length) if (value > BytesWritten)
{ {
Length = value; BytesWritten = value;
} }
} }
} }
@ -58,7 +58,7 @@ namespace System.Buffers
_resize = resize; _resize = resize;
_buffer = initialBuffer; _buffer = initialBuffer;
_position = 0; _position = 0;
Length = 0; BytesWritten = 0;
_arrayToReturnToPool = null; _arrayToReturnToPool = null;
} }
@ -68,16 +68,16 @@ namespace System.Buffers
_arrayToReturnToPool = ArrayPool<byte>.Shared.Rent(initialCapacity); _arrayToReturnToPool = ArrayPool<byte>.Shared.Rent(initialCapacity);
_buffer = _arrayToReturnToPool; _buffer = _arrayToReturnToPool;
_position = 0; _position = 0;
Length = 0; BytesWritten = 0;
} }
[MethodImpl(MethodImplOptions.NoInlining)] [MethodImpl(MethodImplOptions.NoInlining)]
private void Grow(int additionalCapacity) private void Grow(int additionalCapacity)
{ {
var newSize = Math.Max(Length + additionalCapacity, _buffer.Length * 2); var newSize = Math.Max(BytesWritten + additionalCapacity, _buffer.Length * 2);
byte[] poolArray = ArrayPool<byte>.Shared.Rent(newSize); byte[] poolArray = ArrayPool<byte>.Shared.Rent(newSize);
_buffer.Slice(0, Length).CopyTo(poolArray); _buffer.Slice(0, BytesWritten).CopyTo(poolArray);
byte[]? toReturn = _arrayToReturnToPool; byte[]? toReturn = _arrayToReturnToPool;
_buffer = _arrayToReturnToPool = poolArray; _buffer = _arrayToReturnToPool = poolArray;
@ -112,7 +112,7 @@ namespace System.Buffers
throw new OutOfMemoryException(); throw new OutOfMemoryException();
} }
Grow(capacity - Length); Grow(capacity - BytesWritten);
} }
} }
@ -161,6 +161,14 @@ namespace System.Buffers
Position += 4; Position += 4;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteLE(int value)
{
GrowIfNeeded(4);
BinaryPrimitives.WriteInt32LittleEndian(_buffer.Slice(_position), value);
Position += 4;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Write(uint value) public void Write(uint value)
{ {
@ -169,6 +177,14 @@ namespace System.Buffers
Position += 4; Position += 4;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteLE(uint value)
{
GrowIfNeeded(4);
BinaryPrimitives.WriteUInt32LittleEndian(_buffer.Slice(_position), value);
Position += 4;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Write(long value) public void Write(long value)
{ {
@ -317,7 +333,7 @@ namespace System.Buffers
var newPosition = Math.Max(0, origin switch var newPosition = Math.Max(0, origin switch
{ {
SeekOrigin.Current => _position + offset, SeekOrigin.Current => _position + offset,
SeekOrigin.End => Length + offset, SeekOrigin.End => BytesWritten + offset,
_ => offset // Begin _ => offset // Begin
}); });

View file

@ -32,7 +32,8 @@ namespace Server.Network
{ {
public delegate void NetStateCreatedCallback(NetState ns); public delegate void NetStateCreatedCallback(NetState ns);
public delegate void EncodePacket(ref CircularBuffer<byte> buffer, ref int length); public delegate void DecodePacket(ref CircularBuffer<byte> buffer, ref int length);
public delegate void EncodePacket(ReadOnlySpan<byte> inputBuffer, ref CircularBuffer<byte> outputBuffer, out int length);
public partial class NetState : IComparable<NetState>, IDisposable public partial class NetState : IComparable<NetState>, IDisposable
{ {
@ -54,7 +55,7 @@ namespace Server.Network
private byte[] _sendBuffer; private byte[] _sendBuffer;
private long m_NextCheckActivity; private long m_NextCheckActivity;
private volatile bool m_Running; private volatile bool m_Running;
private volatile EncodePacket _packetDecoder; private volatile DecodePacket _packetDecoder;
private volatile EncodePacket _packetEncoder; private volatile EncodePacket _packetEncoder;
internal int m_AuthID; internal int m_AuthID;
@ -115,7 +116,7 @@ namespace Server.Network
public IPAddress Address { get; } public IPAddress Address { get; }
public EncodePacket PacketDecoder public DecodePacket PacketDecoder
{ {
get => _packetDecoder; get => _packetDecoder;
set => _packetDecoder = value; set => _packetDecoder = value;
@ -392,8 +393,15 @@ namespace Server.Network
try try
{ {
buffer.CopyFrom(span); if (_packetEncoder != null)
_packetEncoder?.Invoke(ref buffer, ref length); {
_packetEncoder(span, ref buffer, out length);
}
else
{
buffer.CopyFrom(span);
}
SendPipe.Writer.Advance((uint)length); SendPipe.Writer.Advance((uint)length);
} }
catch (Exception ex) catch (Exception ex)
@ -418,7 +426,12 @@ namespace Server.Network
try try
{ {
_packetEncoder?.Invoke(ref buffer, ref length); // _packetEncoder?.Invoke(ref buffer, ref length);
if (CompressionEnabled)
{
NetworkCompression.Compress(ref buffer, ref length); // This will be changed soon
}
SendPipe.Writer.Advance((uint)length); SendPipe.Writer.Advance((uint)length);
} }
catch (Exception ex) catch (Exception ex)

View file

@ -125,6 +125,70 @@ namespace Server.Network
return outputIdx; return outputIdx;
} }
public static void Compress(ReadOnlySpan<byte> input, ref CircularBuffer<byte> output, out int length)
{
length = Compress(input, ref output);
}
public static int Compress(ReadOnlySpan<byte> input, ref 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(ReadOnlySpan<byte> input, Span<byte> output) public static int Compress(ReadOnlySpan<byte> input, Span<byte> output)
{ {
if (input.Length > DefiniteOverflow) if (input.Length > DefiniteOverflow)

View file

@ -417,6 +417,8 @@ namespace Server.Network
if (e.Accepted) if (e.Accepted)
{ {
state.CityInfo = e.CityInfo; state.CityInfo = e.CityInfo;
// Comment out these lines to turn off huffman compression
state.CompressionEnabled = true; state.CompressionEnabled = true;
state.PacketEncoder = NetworkCompression.Compress; state.PacketEncoder = NetworkCompression.Compress;

View file

@ -61,13 +61,13 @@ namespace Server.Network
*/ */
public static void SendChangeCharacter(this NetState ns, IAccount a) public static void SendChangeCharacter(this NetState ns, IAccount a)
{ {
if (ns == null || a == null || !ns.GetSendBuffer(out var buffer)) if (ns == null || a == null)
{ {
return; return;
} }
var length = 5 + a.Length * 60; var length = 5 + a.Length * 60;
var writer = new CircularBufferWriter(buffer); var writer = new SpanWriter(stackalloc byte[length]);
writer.Write((byte)0x81); // Packet ID writer.Write((byte)0x81); // Packet ID
writer.Write((ushort)length); writer.Write((ushort)length);
@ -98,7 +98,7 @@ namespace Server.Network
writer.Write((byte)count); writer.Write((byte)count);
writer.Seek(position, SeekOrigin.Begin); writer.Seek(position, SeekOrigin.Begin);
ns.Send(ref buffer, writer.Position); ns.Send(writer.Span);
} }
/** /**
@ -107,17 +107,7 @@ namespace Server.Network
* *
* Sends a requests for the client version * Sends a requests for the client version
*/ */
public static void SendClientVersionRequest(this NetState ns) public static void SendClientVersionRequest(this NetState ns) => ns?.Send(stackalloc byte[] { 0xBD, 0x00, 0x03 });
{
if (ns != null && ns.GetSendBuffer(out var buffer))
{
buffer[0] = 0xBD; // Packet ID
buffer[1] = 0x00;
buffer[2] = 0x03; // Length
ns.Send(ref buffer, 3);
}
}
/** /**
* Packet: 0x85 * Packet: 0x85
@ -125,16 +115,8 @@ namespace Server.Network
* *
* Sends the result of a deletion request * Sends the result of a deletion request
*/ */
public static void SendCharacterDeleteResult(this NetState ns, DeleteResultType res) public static void SendCharacterDeleteResult(this NetState ns, DeleteResultType res) =>
{ ns?.Send(stackalloc byte[] { 0x85, (byte)res });
if (ns != null && ns.GetSendBuffer(out var buffer))
{
buffer[0] = 0x85; // Packet ID
buffer[1] = (byte)res;
ns.Send(ref buffer, 2);
}
}
/** /**
* Packet: 0x53 * Packet: 0x53
@ -142,16 +124,8 @@ namespace Server.Network
* *
* Sends a PopupMessage with a predetermined message * Sends a PopupMessage with a predetermined message
*/ */
public static void SendPopupMessage(this NetState ns, PMMessage msg) public static void SendPopupMessage(this NetState ns, PMMessage msg) =>
{ ns?.Send(stackalloc byte[] { 0x53, (byte)msg });
if (ns != null && ns.GetSendBuffer(out var buffer))
{
buffer[0] = 0x53; // Packet ID
buffer[1] = (byte)msg;
ns.Send(ref buffer, 2);
}
}
/** /**
* Packet: 0xB9 * Packet: 0xB9
@ -161,7 +135,7 @@ namespace Server.Network
*/ */
public static void SendSupportedFeature(this NetState ns) public static void SendSupportedFeature(this NetState ns)
{ {
if (ns == null || !ns.GetSendBuffer(out var buffer)) if (ns == null)
{ {
return; return;
} }
@ -183,7 +157,8 @@ namespace Server.Network
} }
} }
var writer = new CircularBufferWriter(buffer); var length = ns.ExtendedSupportedFeatures ? 5 : 3;
var writer = new SpanWriter(stackalloc byte[length]);
writer.Write((byte)0xB9); // Packet ID writer.Write((byte)0xB9); // Packet ID
if (ns.ExtendedSupportedFeatures) if (ns.ExtendedSupportedFeatures)
@ -195,7 +170,7 @@ namespace Server.Network
writer.Write((ushort)flags); writer.Write((ushort)flags);
} }
ns.Send(ref buffer, ns.ExtendedSupportedFeatures ? 5 : 3); ns.Send(writer.Span);
} }
/** /**
@ -206,12 +181,12 @@ namespace Server.Network
*/ */
public static void SendLoginConfirmation(this NetState ns, Mobile m) public static void SendLoginConfirmation(this NetState ns, Mobile m)
{ {
if (ns == null || !ns.GetSendBuffer(out var buffer)) if (ns == null)
{ {
return; return;
} }
var writer = new CircularBufferWriter(buffer); var writer = new SpanWriter(stackalloc byte[37]);
writer.Write((byte)0x1B); // PacketID writer.Write((byte)0x1B); // PacketID
writer.Write(m.Serial); writer.Write(m.Serial);
writer.Write(0); writer.Write(0);
@ -234,9 +209,9 @@ namespace Server.Network
writer.Write((short)(map?.Width ?? Map.Felucca.Width)); writer.Write((short)(map?.Width ?? Map.Felucca.Width));
writer.Write((short)(map?.Height ?? Map.Felucca.Height)); writer.Write((short)(map?.Height ?? Map.Felucca.Height));
writer.Clear(); writer.Clear(writer.Capacity - writer.Position); // Remaining is zero
ns.Send(ref buffer, 37); ns.Send(writer.Span);
} }
/** /**
@ -247,12 +222,7 @@ namespace Server.Network
*/ */
public static void SendLoginComplete(this NetState ns) public static void SendLoginComplete(this NetState ns)
{ {
if (ns != null && ns.GetSendBuffer(out var buffer)) ns?.Send(stackalloc byte[] { 0x55 });
{
buffer[0] = 0x55; // Packet ID
ns.Send(ref buffer, 1);
}
} }
/** /**
@ -268,10 +238,6 @@ namespace Server.Network
return; return;
} }
var writer = new CircularBufferWriter(buffer);
writer.Write((byte)0x86); // Packet ID
writer.Seek(2, SeekOrigin.Current); // Length
var highSlot = -1; var highSlot = -1;
for (var i = a.Length - 1; i >= 0; i--) for (var i = a.Length - 1; i >= 0; i--)
@ -284,6 +250,11 @@ namespace Server.Network
} }
var count = Math.Max(Math.Max(highSlot + 1, a.Limit), 5); var count = Math.Max(Math.Max(highSlot + 1, a.Limit), 5);
var length = 4 + count * 60;
var writer = new SpanWriter(stackalloc byte[length]);
writer.Write((byte)0x86); // Packet ID
writer.Write((ushort)length);
writer.Write((byte)count); writer.Write((byte)count);
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
@ -302,8 +273,7 @@ namespace Server.Network
} }
} }
writer.WritePacketLength(); ns.Send(writer.Span);
ns.Send(ref buffer, writer.Position);
} }
/** /**
@ -316,7 +286,7 @@ namespace Server.Network
{ {
var acct = ns?.Account; var acct = ns?.Account;
if (acct == null || !ns.GetSendBuffer(out var buffer)) if (acct == null)
{ {
return; return;
} }
@ -326,10 +296,6 @@ namespace Server.Network
var cityInfo = ns.CityInfo; var cityInfo = ns.CityInfo;
var writer = new CircularBufferWriter(buffer);
writer.Write((byte)0xA9); // Packet ID
writer.Seek(2, SeekOrigin.Current); // Length
var highSlot = -1; var highSlot = -1;
for (var i = acct.Length - 1; i >= 0; i--) for (var i = acct.Length - 1; i >= 0; i--)
@ -342,6 +308,12 @@ namespace Server.Network
} }
var count = Math.Max(Math.Max(highSlot + 1, acct.Limit), 5); var count = Math.Max(Math.Max(highSlot + 1, acct.Limit), 5);
var length = (client70130 ?
11 + (textLength * 2 + 25) * cityInfo.Length :
9 + (textLength * 2 + 1) * cityInfo.Length) + count * 60;
var writer = new SpanWriter(stackalloc byte[length]);
writer.Write((byte)0xA9); // Packet ID
writer.Write((ushort)length);
writer.Write((byte)count); writer.Write((byte)count);
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
@ -403,8 +375,7 @@ namespace Server.Network
writer.Write((short)-1); writer.Write((short)-1);
} }
writer.WritePacketLength(); ns.Send(writer.Span);
ns.Send(ref buffer, writer.Position);
} }
/** /**
@ -413,37 +384,28 @@ namespace Server.Network
* *
* Sends a reason for rejecting the login * Sends a reason for rejecting the login
*/ */
public static void SendAccountLoginRejected(this NetState ns, ALRReason reason) public static void SendAccountLoginRejected(this NetState ns, ALRReason reason) =>
{ ns?.Send(stackalloc byte[] { 0x82, (byte)reason });
if (ns != null && ns.GetSendBuffer(out var buffer))
{
buffer[0] = 0x82; // Packet ID
buffer[1] = (byte)reason;
ns.Send(ref buffer, 2);
}
}
/** /**
* Packet: 0xA8 * Packet: 0xA8
* Length: up to 240 bytes * Length: 6 + 40 bytes per server listing
* *
* Sends login acknowledge with server listing * Sends login acknowledge with server listing
*/ */
public static void SendAccountLoginAck(this NetState ns) public static void SendAccountLoginAck(this NetState ns)
{ {
if (ns == null || !ns.GetSendBuffer(out var buffer)) if (ns == null)
{ {
return; return;
} }
var writer = new CircularBufferWriter(buffer);
writer.Write((byte)0xA8); // Packet ID
writer.Seek(2, SeekOrigin.Current); // Length
writer.Write((byte)0x5D);
var info = ns.ServerInfo; var info = ns.ServerInfo;
var length = 6 + 40 * info.Length;
var writer = new SpanWriter(stackalloc byte[length]);
writer.Write((byte)0xA8); // Packet ID
writer.Write((ushort)length);
writer.Write((byte)0x5D);
writer.Write((ushort)info.Length); writer.Write((ushort)info.Length);
for (var i = 0; i < info.Length; ++i) for (var i = 0; i < info.Length; ++i)
@ -458,8 +420,7 @@ namespace Server.Network
writer.Write(si.RawAddress); writer.Write(si.RawAddress);
} }
writer.WritePacketLength(); ns.Send(writer.Span);
ns.Send(ref buffer, writer.Position);
} }
/** /**
@ -470,19 +431,19 @@ namespace Server.Network
*/ */
public static void SendPlayServerAck(this NetState ns, ServerInfo si, int authId) public static void SendPlayServerAck(this NetState ns, ServerInfo si, int authId)
{ {
if (ns == null || !ns.GetSendBuffer(out var buffer)) if (ns == null)
{ {
return; return;
} }
var writer = new CircularBufferWriter(buffer); var writer = new SpanWriter(stackalloc byte[11]);
writer.Write((byte)0x8C); // Packet ID writer.Write((byte)0x8C); // Packet ID
writer.WriteLE(si.RawAddress); writer.WriteLE(si.RawAddress);
writer.Write((short)si.Address.Port); writer.Write((short)si.Address.Port);
writer.Write(authId); writer.Write(authId);
ns.Send(ref buffer, writer.Position); ns.Send(writer.Span);
} }
} }
} }

View file

@ -28,12 +28,12 @@ namespace Server.Network
public static void SendArrow(this NetState ns, byte command, int x, int y, Serial s) public static void SendArrow(this NetState ns, byte command, int x, int y, Serial s)
{ {
if (ns == null || !ns.GetSendBuffer(out var buffer)) if (ns == null)
{ {
return; return;
} }
var writer = new CircularBufferWriter(buffer); var writer = new SpanWriter(stackalloc byte[10]);
writer.Write((byte)0xBA); // Packet ID writer.Write((byte)0xBA); // Packet ID
writer.Write(command); writer.Write(command);
@ -55,7 +55,7 @@ namespace Server.Network
writer.Write((short)-1); writer.Write((short)-1);
} }
ns.Send(ref buffer, writer.Position); ns.Send(writer.Span);
} }
} }
} }