ModernUO/Projects/UOContent.Tests/Tests/Items/Books/BookPacketTests.cs
Kamron Batman 10a69bf754
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
2023-10-09 00:57:53 -07:00

94 lines
2.8 KiB
C#

using System;
using Server;
using Server.Items;
using Server.Network;
using Server.Tests;
using Server.Tests.Network;
using Xunit;
namespace UOContent.Tests
{
public class TestBook : BaseBook
{
public TestBook(int itemID, int pageCount = 20, bool writable = true) : base(itemID, pageCount, writable)
{
}
public TestBook(int itemID, string title, string author, int pageCount, bool writable) : base(itemID, title, author, pageCount, writable)
{
}
public TestBook(int itemID, bool writable) : base(itemID, writable)
{
}
public TestBook(Serial serial) : base(serial)
{
Pages = new BookPageInfo[20];
for (var i = 0; i < Pages.Length; ++i)
{
Pages[i] = new BookPageInfo();
}
}
}
public class BookPacketTests : IClassFixture<ServerFixture>
{
[Theory]
[InlineData("🅵🅰🅽🅲🆈 🆃🅴🆇🆃 Author", "🅵🅰🅽🅲🆈 🆃🅴🆇🆃 Title")]
public void TestBookCover(string author, string title)
{
var m = new Mobile((Serial)0x1);
m.DefaultMobileInit();
Serial serial = (Serial)0x1001;
var book = new TestBook(serial) { Author = author, Title = title };
var expected = new BookHeader(m, book).Compile();
var ns = PacketTestUtilities.CreateTestNetState();
ns.SendBookCover(m, book);
var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
}
[Fact]
public void TestBookContent()
{
var m = new Mobile((Serial)0x1);
m.DefaultMobileInit();
Serial serial = (Serial)0x1001;
var book = new TestBook(serial) { Author = "Some Author", Title = "Some Title" };
book.Pages[0].Lines = new[]
{
"Some books start with actual content",
"This book does not have any actual content",
"Instead it has several pages of useless text"
};
book.Pages[1].Lines = new[]
{
"Another page exists but this page:",
"Has lots of: 🅵🅰🅽🅲🆈 🆃🅴🆇🆃",
"And just more: 🅵🅰🅽🅲🆈 🆃🅴🆇🆃",
"So everyone can read: 🅵🅰🅽🅲🆈 🆃🅴🆇🆃"
};
book.Pages[2].Lines = new[]
{
"The end"
};
var expected = new BookPageDetails(book).Compile();
var ns = PacketTestUtilities.CreateTestNetState();
ns.SendBookContent(book);
var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
}
}
}