ModernUO/Projects/Server.Tests/Tests/Network/Packets/Outgoing/MobilePacketTests.cs
Kamron Batman 3c0d6cb9d6
feat: Upgrades networking to use io_uring. (#2315)
> [!IMPORTANT]
> **Breaking Changes**
> - DecodePacket and EncodePacket delegates replaced with IClientEncryption interface
> - NetState.Connection (Socket) replaced with internal RingSocket management
> - NetState.RecvPipe and NetState.SendPipe removed (buffers managed internally)

## Summary

Upgrades the networking stack from PollGroup-based I/O to io_uring, significantly improving I/O performance on Linux.
This also adds native client encryption support for encrypted UO clients.

## Major Changes

io_uring Networking Architecture
- Replaced PollGroup with IORingGroup for async socket I/O operations
- Removed Pipe.cs (mirrored ring buffer) and TcpServer.cs in favor of RingSocketManager
- Added NetState.Network.cs - centralized network infrastructure handling accept, recv, send, and disconnect
completions
- Added SocketHelper.cs - platform-specific socket utilities for raw socket handle operations (getpeername,
getsockname)
- Buffer management now handled by RingSocketManager with configurable slab allocation

### Client Encryption Support
- Added full encryption stack in Network/Encryption/:
  - EncryptionConfig.cs - configurable encryption modes (None, Unencrypted, Encrypted, Both)
  - EncryptionManager.cs - encryption detection and initialization for login/game packets
  - LoginEncryption.cs - handles login packet encryption with version-derived keys
  - GameEncryption.cs - handles game server encryption using Twofish
  - TwofishEngine.cs - optimized Twofish block cipher implementation
  - LoginKeys.cs - encryption key table for client versions
  - IClientEncryption.cs - interface for client encryption implementations

### NetState Improvements
- Replaced Socket Connection with RingSocket _socket for managed socket lifecycle
- Changed from GCHandle polling to event-based completion processing
- Disconnect handling now properly waits for pending sends to flush
- Simplified connecting socket management using lazy queue removal

### Configuration
- New settings: network.encryptionMode and network.encryptionDebug
- Encryption mode flags: Unencrypted, Encrypted, or Both

### Dependencies
- Replaced PollGroup NuGet package with IORingGroup
- Linux requires liburing-dev / liburing-devel package

### Test plan

- Verify server starts and accepts connections on Linux with io_uring
- Verify server starts and accepts connections on Windows (fallback to IOCP)
- Test unencrypted client connections (ClassicUO with encryption disabled)
- Test encrypted client connections if available
- Verify graceful disconnect flushes pending data
- Confirm CI builds pass on all target platforms
2026-02-01 16:02:32 -08:00

502 lines
14 KiB
C#

using Server.Network;
using Xunit;
namespace Server.Tests.Network;
[Collection("Sequential Server Tests")]
public class MobilePacketTests
{
[Fact]
public void TestDeathAnimation()
{
var killed = (Serial)0x1;
var corpse = (Serial)0x1000;
var expected = new DeathAnimation(killed, corpse).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendDeathAnimation(killed, corpse);
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
[Fact]
public void TestBondStatus()
{
var petSerial = (Serial)0x1;
const bool bonded = true;
var expected = new BondedStatus(petSerial, bonded).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendBondedStatus(petSerial, bonded);
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
[Theory]
[InlineData(ProtocolChanges.StygianAbyss)]
[InlineData(ProtocolChanges.None)]
public void TestMobileMoving(ProtocolChanges protocolChanges)
{
var m = new Mobile((Serial)0x1);
m.DefaultMobileInit();
var noto = 10;
using var ns = PacketTestUtilities.CreateTestNetState();
ns.ProtocolChanges = protocolChanges;
var expected = new MobileMoving(m, noto, ns.StygianAbyss).Compile();
ns.SendMobileMoving(m, noto);
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
[Theory]
[InlineData(null)]
[InlineData("Kamron")]
[InlineData("Some Really Long Mobile Name That Gets Cut off")]
public void TestMobileName(string name)
{
var m = new Mobile((Serial)0x1) { Name = name };
m.DefaultMobileInit();
var expected = new MobileName(m).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileName(m);
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
[Theory]
[InlineData(200, 5, 1, false, false, 5)]
[InlineData(10, 100, 25, true, false, 0)]
public void TestMobileAnimation(int action, int frameCount, int repeatCount, bool reverse, bool repeat, byte delay)
{
var mobile = (Serial)0x1;
var expected = new MobileAnimation(
mobile,
action,
frameCount,
repeatCount,
!reverse,
repeat,
delay
).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileAnimation(
mobile,
action,
frameCount,
repeatCount,
!reverse,
repeat,
delay
);
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
[Theory]
[InlineData(200, 5, 5)]
[InlineData(10, 100, 20)]
public void TestNewMobileAnimation(int action, int frameCount, byte delay)
{
var mobile = (Serial)0x1;
var expected = new NewMobileAnimation(
mobile,
action,
frameCount,
delay
).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendNewMobileAnimation(
mobile,
action,
frameCount,
delay
);
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
[Theory]
[InlineData("None")]
[InlineData("Lesser")]
[InlineData("Lethal")]
public void TestHealthbarPoison(string pName)
{
var p = Poison.GetPoison(pName);
var m = new Mobile((Serial)0x1);
m.DefaultMobileInit();
m.Poison = p;
var expected = new HealthbarPoison(m).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileHealthbar(m, Healthbar.Poison);
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
[Theory]
[InlineData(false, false)]
[InlineData(true, false)]
[InlineData(false, true)]
[InlineData(true, true)]
public void TestYellowBar(bool isBlessed, bool isYellowHealth)
{
var m = new Mobile((Serial)0x1);
m.DefaultMobileInit();
m.Blessed = isBlessed;
m.YellowHealthbar = isYellowHealth;
var expected = new HealthbarYellow(m).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileHealthbar(m, Healthbar.Yellow);
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void TestMobileStatusCompact(bool canBeRenamed)
{
var m = new Mobile((Serial)0x1) { Name = "Random Mobile 1" };
m.DefaultMobileInit();
m.Str = 50;
m.Hits = 100;
m.Int = 75;
m.Mana = 100;
m.Dex = 25;
m.Stam = 100;
var expected = new MobileStatusCompact(canBeRenamed, m).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileStatusCompact(m, canBeRenamed);
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
[Theory]
[InlineData(ProtocolChanges.Version70610)]
[InlineData(ProtocolChanges.Version502b)]
public void TestMobileStatus(ProtocolChanges changes)
{
var beholder = new Mobile((Serial)0x1) { Name = "Random Mobile 1" };
beholder.DefaultMobileInit();
beholder.Str = 50;
beholder.Hits = 100;
beholder.Int = 75;
beholder.Mana = 100;
beholder.Dex = 25;
beholder.Stam = 100;
var beheld = new Mobile((Serial)0x2) { Name = "Random Mobile 2" };
beheld.DefaultMobileInit();
beheld.Str = 50;
beheld.Hits = 100;
beheld.Int = 75;
beheld.Mana = 100;
beheld.Dex = 25;
beheld.Stam = 100;
using var ns = PacketTestUtilities.CreateTestNetState();
ns.ProtocolChanges = changes;
var expected = new MobileStatus(beholder, beheld, ns).Compile();
ns.SendMobileStatus(beholder, beheld);
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
[Theory]
[InlineData(ProtocolChanges.Version70610, "7.0.61.0", ClientFlags.TerMur, Expansion.HS, 6)]
[InlineData(ProtocolChanges.Version502b, "5.0.2b", ClientFlags.Malas, Expansion.ML, 5)]
public void TestMobileStatusExtendedSelf(
ProtocolChanges changes,
string version,
ClientFlags clientFlags,
Expansion expansion,
int mobileStatusVersion
)
{
var expansionInfo = ExpansionInfo.CoreExpansion;
var oldExpansion = Core.Expansion;
var oldVersion = expansionInfo.MobileStatusVersion;
Core.Expansion = expansion;
ExpansionInfo.CoreExpansion.MobileStatusVersion = mobileStatusVersion;
var m = new Mobile((Serial)0x1) { Name = "Random Mobile 1" };
m.DefaultMobileInit();
m.Str = 50;
m.Hits = 100;
m.Int = 75;
m.Mana = 100;
m.Dex = 25;
m.Stam = 100;
using var ns = PacketTestUtilities.CreateTestNetState();
ns.ProtocolChanges = changes;
ns.Version = new ClientVersion(version);
ns.Flags = clientFlags;
var expected = new MobileStatusExtended(m, ns).Compile();
ns.SendMobileStatus(m, m);
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
Core.Expansion = oldExpansion;
expansionInfo.MobileStatusVersion = oldVersion;
}
[Theory]
[InlineData(ProtocolChanges.None, 0)]
[InlineData(ProtocolChanges.StygianAbyss, 100)]
public void TestMobileUpdate(ProtocolChanges changes, int solidHueOverride)
{
var m = new Mobile((Serial)0x1);
m.DefaultMobileInit();
m.SolidHueOverride = solidHueOverride;
using var ns = PacketTestUtilities.CreateTestNetState();
ns.ProtocolChanges = changes;
var expected = new MobileUpdate(m, ns.StygianAbyss).Compile();
ns.SendMobileUpdate(m);
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
[Theory]
[InlineData(ProtocolChanges.Version70331, 0, 0, 0, 0)]
[InlineData(ProtocolChanges.Version70331, 10, 1024, 0, 0)]
[InlineData(ProtocolChanges.Version70331, 10, 1024, 11, 2048)]
[InlineData(ProtocolChanges.Version6000, 0, 0, 0, 0)]
[InlineData(ProtocolChanges.Version6000, 10, 1024, 0, 0)]
[InlineData(ProtocolChanges.Version6000, 10, 1024, 11, 2048)]
[InlineData(ProtocolChanges.Version7000, 0, 0, 0, 0)]
[InlineData(ProtocolChanges.Version7000, 10, 1024, 0, 0)]
[InlineData(ProtocolChanges.Version7000, 10, 1024, 11, 2048)]
public void TestMobileIncoming(
ProtocolChanges changes, int hairItemId, int hairHue, int facialHairItemId, int facialHairHue
)
{
var beholder = new Mobile((Serial)0x1)
{
Name = "Random Mobile 1"
};
beholder.DefaultMobileInit();
var beheld = new Mobile((Serial)0x2)
{
Name = "Random Mobile 2"
};
beheld.DefaultMobileInit();
beheld.AddItem(
new Item((Serial)0x1000)
{
Layer = Layer.OneHanded
}
);
// Test Dupe
beheld.AddItem(
new Item((Serial)0x1001)
{
Layer = Layer.OneHanded
}
);
beheld.HairItemID = hairItemId;
beheld.HairHue = hairHue;
beheld.FacialHairItemID = facialHairItemId;
beheld.FacialHairHue = facialHairHue;
using var ns = PacketTestUtilities.CreateTestNetState();
ns.ProtocolChanges = changes;
var expected = new MobileIncoming(ns, beholder, beheld).Compile();
ns.SendMobileIncoming(beholder, beheld);
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
[Fact]
public void TestMobileHits()
{
var m = new Mobile((Serial)0x1);
m.DefaultMobileInit();
m.Str = 100;
m.Hits = 100;
var expected = new MobileHits(m).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileHits(m);
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
[Fact]
public void TestMobileHitsN()
{
var m = new Mobile((Serial)0x1);
m.DefaultMobileInit();
m.Str = 100;
m.Hits = 100;
var expected = new MobileHitsN(m).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileHits(m, true);
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
[Fact]
public void TestMobileMana()
{
var m = new Mobile((Serial)0x1);
m.DefaultMobileInit();
m.Int = 75;
m.Mana = 100;
var expected = new MobileMana(m).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileMana(m);
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
[Fact]
public void TestMobileManaN()
{
var m = new Mobile((Serial)0x1);
m.DefaultMobileInit();
m.Int = 75;
m.Mana = 100;
var expected = new MobileManaN(m).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileMana(m, true);
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
[Fact]
public void TestMobileStam()
{
var m = new Mobile((Serial)0x1);
m.DefaultMobileInit();
m.Dex = 75;
m.Stam = 100;
var expected = new MobileStam(m).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileStam(m);
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
[Fact]
public void TestMobileStamN()
{
var m = new Mobile((Serial)0x1);
m.DefaultMobileInit();
m.Dex = 75;
m.Stam = 100;
var expected = new MobileStamN(m).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileStam(m, true);
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
[Fact]
public void TestMobileAttributes()
{
var m = new Mobile((Serial)0x1);
m.DefaultMobileInit();
m.Str = 50;
m.Hits = 100;
m.Int = 75;
m.Mana = 100;
m.Dex = 25;
m.Stam = 100;
var expected = new MobileAttributes(m).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileAttributes(m);
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
[Fact]
public void TestMobileAttributesN()
{
var m = new Mobile((Serial)0x1);
m.DefaultMobileInit();
m.Str = 50;
m.Hits = 100;
m.Int = 75;
m.Mana = 100;
m.Dex = 25;
m.Stam = 100;
var expected = new MobileAttributesN(m).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileAttributes(m, true);
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
[Fact]
public void TestRemoveEntity()
{
var e = (Serial)0x1000;
var expected = new RemoveEntity(e).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendRemoveEntity(e);
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}