feat: Adds a memory mirrored ring buffer for networking. (#1533)

## Breaking Changes

Incoming packet registration signature has changed to:
```cs
delegate* void OnReceiveCallback(NetState state, SpanReader reader, int packetLength);

IncomingPackets.Register(int packetID, int length, bool ingame, OnReceiveCallback onReceive);
```

For example, an incoming packet handler signature would now look like this:
```cs
public static void SomeIncomingPacket(NetState state, SpanReader reader, int packetLength)
{
    // Parse the data
}
```

## Summary

Updates the network Pipe class to use a mirrored memory technique. This technique involves mapping the same physical memory to two contiguous virtual memory spaces so the byte buffer appears duplicated. This allows writing to a double-sized array to wrap around without the need for the `CircularBuffer` classes.

In practice this allows us to use `Span<byte>` as if the buffer was a regular array.


### Bug Fixes

- [X] Fixes bad fixed length string parsing
This commit is contained in:
Kamron Batman 2023-10-09 00:57:53 -07:00 • committed by GitHub
parent 629a5008c3
commit 10a69bf754
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
83 changed files with 958 additions and 2432 deletions

View file

@ -31,6 +31,8 @@ public ref struct SpanReader
public int Position { get; private set; }
public int Remaining => Length - Position;
public ReadOnlySpan<byte> Buffer => _buffer;
public SpanReader(ReadOnlySpan<byte> span)
{
_buffer = span;
@ -184,13 +186,13 @@ public ref struct SpanReader
{
// In case the remaining is not evenly divisible
size = remaining - (remaining & (byteLength - 1));
int index = _buffer.Slice(Position, size).IndexOfTerminator(byteLength);
size = index < 0 ? size : index;
}
var span = _buffer.Slice(Position, size);
Position += size;
return TextEncoding.GetString(span, encoding, safeString);
var index = span.IndexOfTerminator(byteLength);
Position += isFixedLength || index < 0 ? size : index;
return TextEncoding.GetString(span[..index], encoding, safeString);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]