Skip to content

Commit

Permalink
feat: Adds a memory mirrored ring buffer for networking. (#1533)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
kamronbatman authored Oct 9, 2023
1 parent 629a500 commit 10a69bf
Show file tree
Hide file tree
Showing 83 changed files with 925 additions and 2,399 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ jobs:
- name: Setup .NET 7
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.400
dotnet-version: 7.0.401
- name: Build
run: ./publish.sh Release
- name: Test
Expand Down
103 changes: 0 additions & 103 deletions Projects/Server.Tests/Tests/Buffers/CircularBufferReaderTests.cs

This file was deleted.

82 changes: 0 additions & 82 deletions Projects/Server.Tests/Tests/Buffers/CircularBufferWriterTests.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public void TestChangeCharacter()
var ns = PacketTestUtilities.CreateTestNetState();
ns.SendChangeCharacter(account);

var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
}

[Fact]
Expand All @@ -97,8 +97,8 @@ public void TestClientVersionReq()

ns.SendClientVersionRequest();

var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
}

[Fact]
Expand All @@ -109,8 +109,8 @@ public void TestDeleteResult()
var ns = PacketTestUtilities.CreateTestNetState();
ns.SendCharacterDeleteResult(DeleteResultType.BadRequest);

var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
}

[Fact]
Expand All @@ -121,8 +121,8 @@ public void TestPopupMessage()
var ns = PacketTestUtilities.CreateTestNetState();
ns.SendPopupMessage(PMMessage.LoginSyncError);

var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
}

[Theory, InlineData(ProtocolChanges.Version70610), InlineData(ProtocolChanges.Version6000)]
Expand All @@ -148,8 +148,8 @@ public void TestSupportedFeatures(ProtocolChanges protocolChanges)
var expected = new SupportedFeatures(ns).Compile();
ns.SendSupportedFeature();

var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
}

[Fact]
Expand All @@ -169,8 +169,8 @@ public void TestLoginConfirm()
var ns = PacketTestUtilities.CreateTestNetState();
ns.SendLoginConfirmation(m);

var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
}

[Fact]
Expand All @@ -181,8 +181,8 @@ public void TestLoginComplete()
var ns = PacketTestUtilities.CreateTestNetState();
ns.SendLoginComplete();

var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
}

[Fact]
Expand All @@ -206,8 +206,8 @@ public void TestCharacterListUpdate()
var ns = PacketTestUtilities.CreateTestNetState();
ns.SendCharacterListUpdate(account);

var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
}

[Fact]
Expand Down Expand Up @@ -240,8 +240,8 @@ public void TestCharacterList70130()

ns.SendCharacterList();

var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
}

[Fact]
Expand Down Expand Up @@ -273,8 +273,8 @@ public void TestCharacterListOld()

ns.SendCharacterList();

var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
}

[Fact]
Expand All @@ -286,8 +286,8 @@ public void TestAccountLoginRej()
var ns = PacketTestUtilities.CreateTestNetState();
ns.SendAccountLoginRejected(reason);

var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
}

[Fact]
Expand All @@ -305,8 +305,8 @@ public void TestAccountLoginAck()

ns.SendAccountLoginAck();

var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
}

[Fact]
Expand All @@ -321,7 +321,7 @@ public void TestPlayServerAck()

ns.SendPlayServerAck(si, authId);

var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public void TestSwing()
var ns = PacketTestUtilities.CreateTestNetState();
ns.SendSwing(attacker, defender);

var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
}

[Theory, InlineData(true), InlineData(false)]
Expand All @@ -29,8 +29,8 @@ public void TestSetWarMode(bool warmode)
var ns = PacketTestUtilities.CreateTestNetState();
ns.SendSetWarMode(warmode);

var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
}

[Fact]
Expand All @@ -43,8 +43,8 @@ public void TestChangeCombatant()
var ns = PacketTestUtilities.CreateTestNetState();
ns.SendChangeCombatant(serial);

var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
}
}
}
Loading

0 comments on commit 10a69bf

Please sign in to comment.