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
This commit is contained in:
Kamron Batman 2026-02-01 16:02:32 -08:00 committed by GitHub
parent 91a553b0bc
commit 3c0d6cb9d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
70 changed files with 2227 additions and 1468 deletions

View file

@ -42,7 +42,9 @@ jobs:
build-linux:
runs-on: ubuntu-latest
container: ${{ matrix.container }}
container:
image: ${{ matrix.container }}
options: --security-opt seccomp=unconfined
name: Build (${{ matrix.name }})
strategy:
fail-fast: false
@ -68,14 +70,17 @@ jobs:
packageManager: dnf
steps:
- name: Enable EPEL
run: dnf upgrade --refresh -y && dnf install -y epel-release epel-next-release
if: ${{ startsWith(matrix.name, 'CentOS') }}
- name: Enable EPEL and CRB for CentOS
run: |
dnf upgrade --refresh -y
dnf install -y epel-release epel-next-release
dnf config-manager --set-enabled crb
if: ${{ startsWith(matrix.name, 'CentOS') }}
- name: Install Prerequisites using dnf
run: dnf makecache --refresh && dnf install -y findutils libicu libdeflate-devel zstd libargon2-devel
run: dnf makecache --refresh && dnf install -y findutils libicu libdeflate-devel zstd libargon2-devel liburing-devel
if: ${{ matrix.packageManager == 'dnf' }}
- name: Install Prerequisites using apt
run: apt-get update -y && apt-get install -y curl libicu-dev libdeflate-dev zstd libargon2-dev tzdata
run: apt-get update -y && apt-get install -y curl libicu-dev libdeflate-dev zstd libargon2-dev tzdata liburing-dev
if: ${{ matrix.packageManager == 'apt' }}
- uses: actions/checkout@v4
with:

View file

@ -78,6 +78,9 @@ public static class TestServerInitializer
Core.LoopContext = new EventLoopContext();
Core.Expansion = Expansion.EJ;
// Configure networking (initializes RingSocketManager for tests)
Server.Network.NetState.Configure();
// Configure / Initialize
TestMapDefinitions.ConfigureTestMapDefinitions();

View file

@ -1,16 +1,94 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Network;
using Server.Network;
namespace Server.Tests.Network
{
public static class PacketTestUtilities
{
public static Span<byte> Compile(this Packet p) =>
p.Compile(false, out var length).AsSpan(0, length);
namespace Server.Tests.Network;
public static NetState CreateTestNetState() => new(
new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
);
public static class PacketTestUtilities
{
private static nint _testListener;
private static int _testPort;
private static readonly List<Socket> _testSocketClients = [];
public static Span<byte> Compile(this Packet p) => p.Compile(false, out var length).AsSpan(0, length);
/// <summary>
/// Creates a NetState for unit testing.
/// Uses a real Socket and RingSocket with actual buffers.
/// Must be disposed after use (use 'using' statement).
/// </summary>
public static NetState CreateTestNetState()
{
NetState.Slice(); // Process disconnects/disposes
for (var i = _testSocketClients.Count - 1; i >= 0; i--)
{
var sock = _testSocketClients[i];
if (!sock.Connected)
{
sock.Dispose();
_testSocketClients.RemoveAt(i);
}
}
var ring = NetState.Ring;
// Create a test listener if we don't have one (using the ring for RIO-compatible sockets)
if (_testListener == 0)
{
// Disable rate limiter for tests - we don't want connection attempts to be throttled
// NetState.DisableRateLimiter();
_testListener = ring.CreateListener("127.0.0.1", 0, 128);
if (_testListener == -1)
{
throw new InvalidOperationException("Failed to create test listener");
}
_testPort = SocketHelper.GetLocalEndPoint(_testListener)?.Port ?? 0;
if (_testPort == 0)
{
throw new InvalidOperationException("Failed to get test listener port");
}
}
// Queue an accept operation
ring.PrepareAccept(_testListener, 0, 0, IORingUserData.EncodeAccept());
ring.Submit();
Core._now = DateTime.UtcNow;
// Create a client socket and connect to trigger the accept
var testSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
testSocket.Connect(IPAddress.Loopback, _testPort);
_testSocketClients.Add(testSocket);
// Slice until we have a new NetState instance added
// AcceptEx is asynchronous, so we may need to wait/retry
const int maxRetries = 100;
for (var i = 0; i < maxRetries; i++)
{
NetState.Slice();
// Get the latest instance connected.
foreach (var ns in NetState.Instances)
{
if (ns.ConnectedOn == Core._now)
{
return ns;
}
}
// Wait a bit for AcceptEx to complete
if (i < maxRetries - 1)
{
System.Threading.Thread.Sleep(1);
}
}
throw new Exception("Failed to slice for test NetState instance after retries");
}
}

View file

@ -16,6 +16,13 @@
<DataFiles Include="$(SolutionDir)\Distribution\Data\**" />
<ProjectReference Include="..\UOContent\UOContent.csproj" />
</ItemGroup>
<!-- Copy native ioring.dll for tests -->
<ItemGroup>
<Content Include="C:\Repositories\IORingGroup\IORingGroup\runtimes\win-x64\native\ioring.dll" Condition="Exists('C:\Repositories\IORingGroup\IORingGroup\runtimes\win-x64\native\ioring.dll')">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>ioring.dll</Link>
</Content>
</ItemGroup>
<Target Name="CopyData" AfterTargets="AfterBuild">
<Copy SourceFiles="@(DataFiles)" DestinationFolder="$(OutDir)\Data\%(RecursiveDir)" />
</Target>

View file

@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using Server.Accounting;
using Server.Network;
using Server.Tests.Network;
using Xunit;
namespace Server.Tests.Maps;
@ -356,7 +356,7 @@ public class ClientEnumeratorTests
{
var map = Map.Felucca;
var center = new Point3D(900, 900, 0);
var range = 5;
const int range = 5;
var clients = new (NetState, Mobile)[3];
try
@ -456,8 +456,8 @@ public class ClientEnumeratorTests
private static (NetState, Mobile) CreateClientWithMobile(Map map, Point3D location)
{
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
var ns = new NetState(socket);
// Create test NetState with real socket and buffers
var ns = PacketTestUtilities.CreateTestNetState();
// Assign a mock account to avoid null reference issues
ns.Account = new MockAccount();
@ -533,16 +533,16 @@ public class ClientEnumeratorTests
}
}
private static void DeleteAll((NetState, Mobile)[] clients)
private static void DeleteAll((NetState state, Mobile m)[] clients)
{
for (var i = 0; i < clients.Length; i++)
{
if (clients[i].Item1 != null)
if (clients[i].state != null)
{
clients[i].Item1.Mobile = null;
clients[i].Item1.Disconnect("Test cleanup");
clients[i].state.Mobile = null;
clients[i].state.Dispose();
}
clients[i].Item2?.Delete();
clients[i].m?.Delete();
}
}
}

View file

@ -4,7 +4,7 @@ using Server.Json;
using System.Text.Json.Serialization;
using System.Text.Json;
namespace Server.Tests.Tests.Maps
namespace Server.Tests.Maps
{
public class MapSelectionTests
{

View file

@ -92,7 +92,7 @@ public class AccountPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendChangeCharacter(account);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -105,7 +105,7 @@ public class AccountPacketTests
ns.SendClientVersionRequest();
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -117,7 +117,7 @@ public class AccountPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendCharacterDeleteResult(DeleteResultType.BadRequest);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -129,7 +129,7 @@ public class AccountPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendPopupMessage(PMMessage.LoginSyncError);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -156,7 +156,7 @@ public class AccountPacketTests
var expected = new SupportedFeatures(ns).Compile();
ns.SendSupportedFeature();
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -177,7 +177,7 @@ public class AccountPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendLoginConfirmation(m);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -189,7 +189,7 @@ public class AccountPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendLoginComplete();
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -214,7 +214,7 @@ public class AccountPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendCharacterListUpdate(account);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -248,7 +248,7 @@ public class AccountPacketTests
ns.SendCharacterList();
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -281,7 +281,7 @@ public class AccountPacketTests
ns.SendCharacterList();
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -294,7 +294,7 @@ public class AccountPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendAccountLoginRejected(reason);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -313,7 +313,7 @@ public class AccountPacketTests
ns.SendAccountLoginAck();
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -321,7 +321,7 @@ public class AccountPacketTests
public void TestPlayServerAck()
{
var si = new ServerInfo("Test Server", 0, TimeZoneInfo.Local, IPEndPoint.Parse("127.0.0.1"));
var authId = 0x123456;
const int authId = 0x123456;
var expected = new PlayServerAck(si, authId).Compile();
@ -329,7 +329,7 @@ public class AccountPacketTests
ns.SendPlayServerAck(si, authId);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -17,7 +17,7 @@ public class CombatPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendSwing(attacker, defender);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -29,7 +29,7 @@ public class CombatPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendSetWarMode(warmode);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -43,7 +43,7 @@ public class CombatPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendChangeCombatant(serial);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -7,7 +7,6 @@ namespace Server.Tests.Network;
[Collection("Sequential Server Tests")]
public class ContainerPacketTests
{
[Fact]
public void TestContainerDisplay()
{
@ -19,7 +18,7 @@ public class ContainerPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendDisplayContainer(serial, gumpId);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -35,7 +34,7 @@ public class ContainerPacketTests
ns.ProtocolChanges = ns.ProtocolChanges | ProtocolChanges.ContainerGridLines | ProtocolChanges.HighSeas;
ns.SendDisplayContainer(serial, gumpId);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -49,7 +48,7 @@ public class ContainerPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendDisplaySpellbook(serial);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -64,7 +63,7 @@ public class ContainerPacketTests
ns.ProtocolChanges = ns.ProtocolChanges | ProtocolChanges.ContainerGridLines | ProtocolChanges.HighSeas;
ns.SendDisplaySpellbook(serial);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -85,7 +84,7 @@ public class ContainerPacketTests
ns.SendSpellbookContent(serial, graphic, offset, content);
ObjectPropertyList.Enabled = opl;
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -102,7 +101,7 @@ public class ContainerPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendSpellbookContent(serial, graphic, offset, content);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -120,7 +119,7 @@ public class ContainerPacketTests
ns.ProtocolChanges |= ProtocolChanges.ContainerGridLines;
ns.SendSpellbookContent(serial, graphic, offset, content);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -135,7 +134,7 @@ public class ContainerPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendContainerContentUpdate(item);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -151,7 +150,7 @@ public class ContainerPacketTests
ns.ProtocolChanges |= ProtocolChanges.ContainerGridLines;
ns.SendContainerContentUpdate(item);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -172,7 +171,7 @@ public class ContainerPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendContainerContent(m, cont);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -194,7 +193,7 @@ public class ContainerPacketTests
ns.ProtocolChanges |= ProtocolChanges.ContainerGridLines;
ns.SendContainerContent(m, cont);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -16,7 +16,7 @@ public class DamagePacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendDamage(serial, inputAmount);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -32,7 +32,7 @@ public class DamagePacketTests
ns.SendDamage(serial, inputAmount);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -17,7 +17,7 @@ public class EffectPackets
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendSoundEffect(soundID, p);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -99,7 +99,7 @@ public class EffectPackets
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendScreenEffect(screenType);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}

View file

@ -42,7 +42,7 @@ public class EquipmentPacketTests
new List<EquipInfoAttribute>(info.Attributes)
);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -59,7 +59,7 @@ public class EquipmentPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendEquipUpdate(item);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -15,7 +15,7 @@ public class GumpPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendCloseGump(typeId, buttonId);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -32,7 +32,7 @@ public class GumpPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendDisplaySignGump(gumpSerial, gumpId, unknownString, caption);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -46,7 +46,7 @@ public class GumpPacketTests
var expected = gump.Compile(ns).Compile();
ns.SendGump(gump);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -65,7 +65,7 @@ public class GumpPacketTests
var expected = gump.Compile(ns).Compile();
ns.SendGump(gump);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -38,7 +38,7 @@ public class ItemPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendWorldItem(item);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -74,7 +74,7 @@ public class ItemPacketTests
ns.ProtocolChanges = ProtocolChanges.StygianAbyss;
ns.SendWorldItem(item);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -110,7 +110,7 @@ public class ItemPacketTests
ns.ProtocolChanges = ProtocolChanges.StygianAbyss | ProtocolChanges.HighSeas;
ns.SendWorldItem(item);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -15,7 +15,7 @@ public class LightPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendGlobalLightLevel(lightLevel);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -29,7 +29,7 @@ public class LightPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendPersonalLightLevel(serial, lightLevel);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -15,7 +15,7 @@ public class MapPatchesTests
ns.SendMapPatches();
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -27,7 +27,7 @@ public class MapPatchesTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendInvalidMap();
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -42,7 +42,7 @@ public class MapPatchesTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMapChange(map);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -47,7 +47,7 @@ public class MenuPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendDisplayItemListMenu(menu);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -67,7 +67,7 @@ public class MenuPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendDisplayQuestionMenu(menu);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -95,7 +95,7 @@ public class MenuPacketTests
ns.SendDisplayContextMenu(menu);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -42,7 +42,7 @@ public class MessageTests
args
);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -87,7 +87,7 @@ public class MessageTests
args
);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -125,7 +125,7 @@ public class MessageTests
text
);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -165,7 +165,7 @@ public class MessageTests
text
);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -180,7 +180,7 @@ public class MessageTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendFollowMessage(serial, serial2);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -195,7 +195,7 @@ public class MessageTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendHelpResponse(s, text);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -215,9 +215,8 @@ public class MessageTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendPrompt(prompt);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}
}

View file

@ -17,7 +17,7 @@ public class MobilePacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendDeathAnimation(killed, corpse);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -32,7 +32,7 @@ public class MobilePacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendBondedStatus(petSerial, bonded);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -52,7 +52,7 @@ public class MobilePacketTests
ns.SendMobileMoving(m, noto);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -70,7 +70,7 @@ public class MobilePacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileName(m);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -102,7 +102,7 @@ public class MobilePacketTests
delay
);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -128,7 +128,7 @@ public class MobilePacketTests
delay
);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -148,7 +148,7 @@ public class MobilePacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileHealthbar(m, Healthbar.Poison);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -169,7 +169,7 @@ public class MobilePacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileHealthbar(m, Healthbar.Yellow);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -192,7 +192,7 @@ public class MobilePacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileStatusCompact(m, canBeRenamed);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -225,7 +225,7 @@ public class MobilePacketTests
var expected = new MobileStatus(beholder, beheld, ns).Compile();
ns.SendMobileStatus(beholder, beheld);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -263,7 +263,7 @@ public class MobilePacketTests
var expected = new MobileStatusExtended(m, ns).Compile();
ns.SendMobileStatus(m, m);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
Core.Expansion = oldExpansion;
expansionInfo.MobileStatusVersion = oldVersion;
@ -284,7 +284,7 @@ public class MobilePacketTests
var expected = new MobileUpdate(m, ns.StygianAbyss).Compile();
ns.SendMobileUpdate(m);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -339,7 +339,7 @@ public class MobilePacketTests
var expected = new MobileIncoming(ns, beholder, beheld).Compile();
ns.SendMobileIncoming(beholder, beheld);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -356,7 +356,7 @@ public class MobilePacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileHits(m);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -373,7 +373,7 @@ public class MobilePacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileHits(m, true);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -390,7 +390,7 @@ public class MobilePacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileMana(m);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -407,7 +407,7 @@ public class MobilePacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileMana(m, true);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -424,7 +424,7 @@ public class MobilePacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileStam(m);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -441,7 +441,7 @@ public class MobilePacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileStam(m, true);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -462,7 +462,7 @@ public class MobilePacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileAttributes(m);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -483,7 +483,7 @@ public class MobilePacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileAttributes(m, true);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -496,7 +496,7 @@ public class MobilePacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendRemoveEntity(e);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -17,7 +17,7 @@ public class MovementPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendSpeedControl((SpeedControlSetting)speedControl);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -30,7 +30,7 @@ public class MovementPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMovePlayer(d);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -47,7 +47,7 @@ public class MovementPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMovementRej(seq, m);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -64,7 +64,7 @@ public class MovementPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMovementAck(seq, m);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -23,7 +23,7 @@ public class PlayerPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendStatLockInfo(m);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -38,7 +38,7 @@ public class PlayerPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendChangeUpdateRange((byte)range);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -50,7 +50,7 @@ public class PlayerPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendDeathStatus();
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -64,7 +64,7 @@ public class PlayerPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendDisplayProfile((Serial)serial, header, body, footer);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -78,7 +78,7 @@ public class PlayerPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendLiftReject(reason);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -90,7 +90,7 @@ public class PlayerPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendLogoutAck();
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -105,7 +105,7 @@ public class PlayerPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendWeather((byte)type, (byte)density, (byte)temp);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -120,7 +120,7 @@ public class PlayerPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendServerChange(p, map);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -135,7 +135,7 @@ public class PlayerPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendSequence(num);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -151,7 +151,7 @@ public class PlayerPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendLaunchBrowser(uri);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -175,7 +175,7 @@ public class PlayerPacketTests
itemId, hue, amount
);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -189,7 +189,7 @@ public class PlayerPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendSeasonChange((byte)season, playSound);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -204,7 +204,7 @@ public class PlayerPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendDisplayPaperdoll((Serial)m, title, warmode, canLift);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -219,7 +219,7 @@ public class PlayerPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendPlayMusic(music);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -233,7 +233,7 @@ public class PlayerPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendScrollMessage(type, tip ,text);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -247,7 +247,7 @@ public class PlayerPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendCurrentTime(date);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -261,7 +261,7 @@ public class PlayerPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendPathfindMessage(p);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -276,7 +276,7 @@ public class PlayerPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendPingAck(ping);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -291,7 +291,7 @@ public class PlayerPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendDisplayHuePicker(huePicker.Serial, huePicker.ItemID);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -23,7 +23,7 @@ public class SecureTradePacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendDisplaySecureTrade(m, firstCont, secondCont, name);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -37,7 +37,7 @@ public class SecureTradePacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendCloseSecureTrade(cont);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -55,7 +55,7 @@ public class SecureTradePacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendUpdateSecureTrade(cont, first, second);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -70,7 +70,7 @@ public class SecureTradePacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendUpdateSecureTrade(cont, flag, gold, plat);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -88,7 +88,7 @@ public class SecureTradePacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendSecureTradeEquip(itemInCont, m);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -108,7 +108,7 @@ public class SecureTradePacketTests
ns.SendSecureTradeEquip(itemInCont, m);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -44,7 +44,7 @@ public class TargetPacketsTests
ns.ProtocolChanges |= ProtocolChanges.HighSeas;
ns.SendMultiTargetReq(t);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -60,7 +60,7 @@ public class TargetPacketsTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMultiTargetReq(t);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -71,7 +71,7 @@ public class TargetPacketsTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendCancelTarget();
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -84,7 +84,7 @@ public class TargetPacketsTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendTargetReq(t);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -29,7 +29,7 @@ public class VendorBuyPacketTests
ns.SendVendorBuyContent(buyStates);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -48,7 +48,7 @@ public class VendorBuyPacketTests
ns.SendDisplayBuyList(vendor.Serial);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -72,7 +72,7 @@ public class VendorBuyPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendVendorBuyList(vendor, buyStates);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -87,7 +87,7 @@ public class VendorBuyPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendEndVendorBuy(vendor.Serial);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -30,7 +30,7 @@ public class VendorSellPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendVendorSellList(vendor.Serial, sellStates);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -45,7 +45,7 @@ public class VendorSellPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendEndVendorSell(vendor.Serial);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -20,7 +20,7 @@ public class VirtualHairPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendHairEquipUpdatePacket(m, (uint)m.Hair.VirtualSerial, m.Hair.ItemId, m.Hair.Hue, Layer.Hair);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -35,7 +35,7 @@ public class VirtualHairPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendRemoveHairPacket((uint) m.Hair.VirtualSerial);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -1,89 +0,0 @@
using System;
using Server.Network;
using Xunit;
namespace Server.Tests.Network;
public class PipeTests
{
[Fact]
public void TestSizeMatchesPageSize()
{
var pageSize = (uint)Environment.SystemPageSize;
using var pipe = new Pipe(128);
// Available memory should be in increments of system page size minus one.
Assert.Equal(pageSize, pipe.Size);
Assert.Equal(pageSize - 1, (uint)pipe.Writer.AvailableToWrite().Length);
}
[Fact]
public void TestWriteReadsWrap()
{
var pageSize = (uint)Environment.SystemPageSize;
using var pipe = new Pipe(pageSize);
var span = pipe.Writer.AvailableToWrite();
for (var i = 0; i < span.Length; i++)
{
span[i] = (byte)(i % 256);
}
pipe.Writer.Advance((uint)(span.Length - 10));
var readBytes = pipe.Reader.AvailableToRead();
// Make a sequence from what we expect.
Span<byte> seq = new byte[readBytes.Length];
for (var i = 0; i < readBytes.Length; i++)
{
seq[i] = (byte)(i % 256);
}
AssertThat.Equal(readBytes, seq);
// Advance by half. Expected writer length should be half + 10
pipe.Reader.Advance(pageSize / 2);
span = pipe.Writer.AvailableToWrite();
var halfStart = pageSize / 2 + 10;
Assert.Equal(halfStart, (uint)span.Length);
seq = new byte[20];
for (var i = 0; i < 10; i++)
{
seq[i] = (byte)(0xF5 + i);
}
// The last element, the sentinel, is excluded.
// The wrap around values start at 11
for (var i = 11; i < 20; i++)
{
seq[i] = (byte)(i - 11);
}
// Test the uncommitted overwritten memory and shifted offset to make sure the ring is working
AssertThat.Equal(span[..20], seq[..20]);
}
[Fact]
public void TestWriteReadMatches()
{
var pipe = new Pipe(16);
var reader = pipe.Reader;
var writer = pipe.Writer;
for (uint i = 0; i < 16; i++)
{
writer.Advance(i);
Assert.Equal((int)i, reader.AvailableToRead().Length);
reader.Advance(i);
}
}
}

View file

@ -1,37 +0,0 @@
using System;
using System.Runtime.InteropServices;
using System.Threading;
using Server.Network;
using Xunit;
namespace Server.Tests.Network;
public class PollGroupTests
{
[Fact]
public void TestPollGroup()
{
// var group = new KQueuePollGroup();
var nss = new NetState[2048];
var handles = new IntPtr[2048];
for (var i = 0; i < nss.Length; i++)
{
nss[i] = PacketTestUtilities.CreateTestNetState();
handles[i] = (IntPtr)nss[i].Handle;
}
GC.AddMemoryPressure(10000000000);
GC.Collect();
GC.RemoveMemoryPressure(10000000000);
GC.Collect();
Thread.Sleep(1000);
for (var i = 0; i < nss.Length; i++)
{
Assert.Equal(nss[i].Handle, (GCHandle)handles[i]);
}
// group.Dispose();
}
}

View file

@ -1,6 +1,6 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2023 - ModernUO Development Team *
* Copyright 2019-2026 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SocketConnectionEvent.cs *
* *
@ -14,20 +14,20 @@
*************************************************************************/
using System;
using System.Net.Sockets;
using System.Net;
using System.Runtime.CompilerServices;
namespace Server;
public class SocketConnectEventArgs
{
public SocketConnectEventArgs(Socket c)
public SocketConnectEventArgs(IPAddress address)
{
Connection = c;
Address = address;
AllowConnection = true;
}
public Socket Connection { get; }
public IPAddress Address { get; }
public bool AllowConnection { get; set; }
}

View file

@ -339,7 +339,7 @@ public static class Core
World.WaitForWriteCompletion();
World.ExitSerializationThreads();
PingServer.Shutdown();
TcpServer.Shutdown();
NetState.Shutdown();
if (!_crashed)
{
@ -447,7 +447,7 @@ public static class Core
AssemblyHandler.Invoke("Initialize");
TcpServer.Start();
NetState.Start();
PingServer.Start();
EventSink.InvokeServerStarted();
RunEventLoop();

View file

@ -1245,7 +1245,7 @@ public partial class Mobile : IHued, IComparable<Mobile>, ISpawnable, IObjectPro
{
get
{
if (m_NetState?.Connection == null)
if (m_NetState is not { IsConnected: true })
{
m_NetState = null;
}

View file

@ -0,0 +1,45 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2025 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: EncryptionConfig.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
namespace Server.Network;
/// <summary>
/// Specifies which encryption modes the server will accept.
/// </summary>
[Flags]
public enum EncryptionMode
{
/// <summary>
/// Encryption handling is disabled. Current behavior.
/// </summary>
None = 0x0,
/// <summary>
/// Accept unencrypted clients (e.g., ClassicUO with encryption disabled).
/// </summary>
Unencrypted = 0x1,
/// <summary>
/// Accept encrypted clients (original UO client, Enhanced Client).
/// </summary>
Encrypted = 0x2,
/// <summary>
/// Auto-detect and accept both encrypted and unencrypted clients.
/// </summary>
Both = Unencrypted | Encrypted
}

View file

@ -0,0 +1,207 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2025 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: EncryptionManager.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Buffers.Binary;
using Server.Logging;
namespace Server.Network;
/// <summary>
/// Manages encryption detection and configuration for client connections.
/// </summary>
public static class EncryptionManager
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(EncryptionManager));
private static EncryptionMode _mode = EncryptionMode.None;
private static bool _debug;
/// <summary>
/// Gets whether encryption handling is enabled.
/// </summary>
public static bool Enabled => _mode != EncryptionMode.None;
/// <summary>
/// Gets the current encryption mode.
/// </summary>
public static EncryptionMode Mode => _mode;
/// <summary>
/// Gets whether debug logging is enabled for encryption.
/// </summary>
public static bool Debug => _debug;
/// <summary>
/// Configures encryption settings from server configuration.
/// </summary>
public static void Configure()
{
_mode = ServerConfiguration.GetSetting("network.encryptionMode", EncryptionMode.Both);
_debug = ServerConfiguration.GetSetting("network.encryptionDebug", false);
if (_mode != EncryptionMode.None)
{
logger.Information("Encryption support enabled: {Mode}", _mode);
}
}
/// <param name="ns">The network state.</param>
extension(NetState ns)
{
/// <summary>
/// Detects and initializes encryption for a login packet (0x80).
/// </summary>
/// <param name="buffer">The 62-byte login packet buffer.</param>
/// <param name="encryption">The detected encryption, or null if unencrypted.</param>
/// <returns>True if detection succeeded (encrypted or unencrypted), false if rejected.</returns>
public bool DetectLoginEncryption(ReadOnlySpan<byte> buffer, out IClientEncryption encryption)
{
encryption = null;
if (buffer.Length < 62)
{
return false;
}
// Check if unencrypted:
// - Packet ID is 0x80, OR
// - Username and password null terminators are present
var isUnencrypted = buffer[0] == 0x80 || buffer[30] == 0x00 && buffer[60] == 0x00;
if (isUnencrypted)
{
if (!_mode.HasFlag(EncryptionMode.Unencrypted))
{
if (_debug)
{
logger.Debug("Client {Address}: Unencrypted login rejected (mode: {Mode})", ns.Address, _mode);
}
return false;
}
if (_debug)
{
logger.Debug("Client {Address}: Unencrypted login detected", ns.Address);
}
return true;
}
// Try encrypted
if (!_mode.HasFlag(EncryptionMode.Encrypted))
{
if (_debug)
{
logger.Debug("Client {Address}: Encrypted login rejected (mode: {Mode})", ns.Address, _mode);
}
return false;
}
// Attempt decryption with version-derived keys
if (LoginEncryption.TryDecrypt(ns.Version, (uint)ns.Seed, buffer, out var loginEncryption))
{
encryption = loginEncryption;
if (_debug)
{
logger.Debug("Client {Address}: Encrypted login detected (version: {Version})", ns.Address, ns.Version);
}
return true;
}
if (_debug)
{
logger.Debug("Client {Address}: Login encryption detection failed", ns.Address);
}
return false;
}
/// <summary>
/// Detects and initializes encryption for a game server login packet (0x91).
/// </summary>
/// <param name="buffer">The 65-byte game login packet buffer.</param>
/// <param name="encryption">The detected encryption, or null if unencrypted.</param>
/// <returns>True if detection succeeded (encrypted or unencrypted), false if rejected.</returns>
public bool DetectGameEncryption(ReadOnlySpan<byte> buffer, out IClientEncryption encryption)
{
encryption = null;
if (buffer.Length < 65)
{
return false;
}
// Extract auth ID from packet (bytes 1-4, big-endian)
var authId = BinaryPrimitives.ReadUInt32BigEndian(buffer[1..]);
// Check if unencrypted:
// - Packet ID is 0x91, OR
// - Auth ID equals seed (indicates no encryption applied)
var isUnencrypted = buffer[0] == 0x91 || authId == (uint)ns.Seed;
if (isUnencrypted)
{
if (!_mode.HasFlag(EncryptionMode.Unencrypted))
{
if (_debug)
{
logger.Debug("Client {Address}: Unencrypted game login rejected (mode: {Mode})", ns.Address, _mode);
}
return false;
}
if (_debug)
{
logger.Debug("Client {Address}: Unencrypted game login detected", ns.Address);
}
return true;
}
// Try encrypted
if (!_mode.HasFlag(EncryptionMode.Encrypted))
{
if (_debug)
{
logger.Debug("Client {Address}: Encrypted game login rejected (mode: {Mode})", ns.Address, _mode);
}
return false;
}
// Attempt decryption with seed-derived Twofish
if (GameEncryption.TryDecrypt((uint)ns.Seed, buffer, out var gameEncryption))
{
encryption = gameEncryption;
if (_debug)
{
logger.Debug("Client {Address}: Encrypted game login detected (seed: 0x{Seed:X8})", ns.Address, ns.Seed);
}
return true;
}
if (_debug)
{
logger.Debug("Client {Address}: Game encryption detection failed", ns.Address);
}
return false;
}
}
}

View file

@ -0,0 +1,204 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2025 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: GameEncryption.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Security.Cryptography;
using Server.Logging;
namespace Server.Network;
/// <summary>
/// Implements game packet encryption/decryption using Twofish + MD5.
/// Used for all game packets after login (0x91 and onwards).
/// </summary>
public sealed class GameEncryption : IClientEncryption
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(GameEncryption));
private const int CipherTableSize = 256;
private const int BlockSize = 16;
// Static identity table [0..255] for vectorized CopyTo initialization
private static readonly byte[] IdentityTable = CreateIdentityTable();
private static byte[] CreateIdentityTable()
{
var table = new byte[CipherTableSize];
for (var i = 0; i < CipherTableSize; i++)
{
table[i] = (byte)i;
}
return table;
}
private readonly TwofishEngine _twofish;
private readonly byte[] _cipherTable;
private readonly byte[] _xorKey;
private ushort _recvPos;
private byte _sendPos;
public GameEncryption(uint seed)
{
// Create 16-byte key from seed (repeated 4 times)
Span<byte> key = stackalloc byte[16];
key[0] = key[4] = key[8] = key[12] = (byte)((seed >> 24) & 0xFF);
key[1] = key[5] = key[9] = key[13] = (byte)((seed >> 16) & 0xFF);
key[2] = key[6] = key[10] = key[14] = (byte)((seed >> 8) & 0xFF);
key[3] = key[7] = key[11] = key[15] = (byte)(seed & 0xFF);
_twofish = new TwofishEngine(key);
// Initialize cipher table with identity [0..255] using vectorized copy
_cipherTable = GC.AllocateUninitializedArray<byte>(CipherTableSize);
IdentityTable.CopyTo(_cipherTable, 0);
// Encrypt cipher table with Twofish
RefreshCipherTable();
// Compute MD5 hash of cipher table for server->client XOR key
_xorKey = MD5.HashData(_cipherTable);
}
/// <summary>
/// Refreshes the cipher table by encrypting it with Twofish.
/// Called every 256 bytes of received data.
/// </summary>
private void RefreshCipherTable()
{
// Encrypt cipher table in 16-byte blocks
for (var i = 0; i < CipherTableSize; i += BlockSize)
{
_twofish.EncryptBlock(_cipherTable.AsSpan(i, BlockSize));
}
_recvPos = 0;
}
/// <summary>
/// Decrypts incoming data from the client (in-place).
/// XORs with cipher table, refreshing every 256 bytes.
/// </summary>
public void ClientDecrypt(Span<byte> buffer)
{
for (var i = 0; i < buffer.Length; i++)
{
if (_recvPos >= CipherTableSize)
{
RefreshCipherTable();
}
buffer[i] ^= _cipherTable[_recvPos++];
}
}
/// <summary>
/// Encrypts outgoing data to the client (in-place).
/// XORs with MD5 hash of cipher table (16-byte rotating key).
/// Uses SIMD optimization for larger buffers.
/// </summary>
public void ServerEncrypt(Span<byte> buffer)
{
var i = 0;
// SIMD path: process 16 bytes at a time when aligned with key
if (_sendPos == 0 && buffer.Length >= 16 && Vector128.IsHardwareAccelerated)
{
var keyVec = Vector128.Create(_xorKey);
for (; i + 16 <= buffer.Length; i += 16)
{
var chunk = Vector128.LoadUnsafe(ref buffer[i]);
var result = Vector128.Xor(chunk, keyVec);
result.StoreUnsafe(ref buffer[i]);
}
}
// Scalar path for remainder or when not aligned
for (; i < buffer.Length; i++)
{
buffer[i] ^= _xorKey[_sendPos++];
_sendPos &= 0x0F; // Wrap at 16
}
}
/// <summary>
/// Attempts to decrypt a game login packet and validate it.
/// Returns true if the packet appears to be validly encrypted.
/// </summary>
public static bool TryDecrypt(uint seed, ReadOnlySpan<byte> encryptedPacket, out GameEncryption encryption)
{
const int GameLoginPacketSize = 65;
encryption = null;
if (encryptedPacket.Length < GameLoginPacketSize)
{
if (EncryptionManager.Debug)
{
logger.Debug("GameEncryption.TryDecrypt: Invalid buffer length {Length}", encryptedPacket.Length);
}
return false;
}
if (EncryptionManager.Debug)
{
logger.Debug("GameEncryption.TryDecrypt: Seed=0x{Seed:X8}", seed);
logger.Debug("GameEncryption.TryDecrypt: Encrypted[0..16]: {Bytes}", Convert.ToHexString(encryptedPacket[..16]));
}
// Copy and decrypt
Span<byte> decrypted = stackalloc byte[GameLoginPacketSize];
encryptedPacket[..GameLoginPacketSize].CopyTo(decrypted);
var enc = new GameEncryption(seed);
if (EncryptionManager.Debug)
{
logger.Debug("GameEncryption.TryDecrypt: CipherTable[0..16]: {Bytes}",
Convert.ToHexString(enc._cipherTable.AsSpan(0, 16)));
logger.Debug("GameEncryption.TryDecrypt: XorKey: {Bytes}", Convert.ToHexString(enc._xorKey));
}
enc.ClientDecrypt(decrypted);
if (EncryptionManager.Debug)
{
logger.Debug("GameEncryption.TryDecrypt: Decrypted[0..16]: {Bytes}", Convert.ToHexString(decrypted[..16]));
logger.Debug("GameEncryption.TryDecrypt: First byte=0x{Byte:X2} (expected 0x91)", decrypted[0]);
}
// Validate: first byte must be 0x91 (game server login packet ID)
if (decrypted[0] != 0x91)
{
if (EncryptionManager.Debug)
{
logger.Debug("GameEncryption.TryDecrypt: Validation FAILED - first byte is not 0x91");
}
return false;
}
if (EncryptionManager.Debug)
{
logger.Debug("GameEncryption.TryDecrypt: Validation PASSED");
}
// Re-create encryption for actual use
encryption = new GameEncryption(seed);
return true;
}
}

View file

@ -0,0 +1,37 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2025 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: IClientEncryption.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
namespace Server.Network;
/// <summary>
/// Interface for client encryption implementations.
/// Uses Span-based API for zero-allocation in the hot path.
/// </summary>
public interface IClientEncryption
{
/// <summary>
/// Decrypts incoming data from the client (in-place).
/// </summary>
/// <param name="buffer">The buffer containing encrypted data. Modified in-place.</param>
void ClientDecrypt(Span<byte> buffer);
/// <summary>
/// Encrypts outgoing data to the client (in-place).
/// </summary>
/// <param name="buffer">The buffer containing plaintext data. Modified in-place.</param>
void ServerEncrypt(Span<byte> buffer);
}

View file

@ -0,0 +1,123 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2025 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: LoginEncryption.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
namespace Server.Network;
/// <summary>
/// Implements login packet encryption/decryption using XOR with version-derived keys.
/// Used for the initial account login packet (0x80).
/// </summary>
public sealed class LoginEncryption : IClientEncryption
{
private uint _table1;
private uint _table2;
private readonly uint _key1;
private readonly uint _key2;
public LoginEncryption(uint seed, LoginKeys keys)
{
_key1 = keys.Key1;
_key2 = keys.Key2;
// Initialize state tables from seed
_table1 = ((~seed ^ 0x00001357) << 16) | ((seed ^ 0xFFFFAAAA) & 0x0000FFFF);
_table2 = ((seed ^ 0x43210000) >> 16) | ((~seed ^ 0xABCDFFFF) & 0xFFFF0000);
}
/// <summary>
/// Attempts to initialize login encryption and validate the packet.
/// Returns true if the packet appears to be validly encrypted with this scheme.
/// </summary>
public static bool TryDecrypt(
ClientVersion version,
uint seed,
ReadOnlySpan<byte> encryptedPacket,
out LoginEncryption encryption)
{
const int LoginPacketSize = 62;
encryption = null;
var keys = LoginKeys.GetKeys(version);
if (keys is { Key1: 0, Key2: 0 })
{
return false;
}
if (encryptedPacket.Length < LoginPacketSize)
{
return false;
}
// Copy and decrypt
Span<byte> decrypted = stackalloc byte[LoginPacketSize];
encryptedPacket[..LoginPacketSize].CopyTo(decrypted);
var enc = new LoginEncryption(seed, keys);
enc.ClientDecrypt(decrypted);
// Validate decrypted packet structure:
// - Byte 0 must be 0x80 (account login packet ID)
// - Byte 30 must be 0x00 (null terminator for username)
// - Byte 60 must be 0x00 (null terminator for password)
if (decrypted[0] != 0x80 || decrypted[30] != 0x00 || decrypted[60] != 0x00)
{
return false;
}
// Re-initialize encryption state for actual use
encryption = new LoginEncryption(seed, keys);
return true;
}
/// <summary>
/// Decrypts incoming data from the client (in-place).
/// </summary>
public void ClientDecrypt(Span<byte> buffer)
{
for (var i = 0; i < buffer.Length; i++)
{
buffer[i] ^= (byte)(_table1 & 0xFF);
var edx = _table2;
var esi = _table1 << 31;
var eax = _table2 >> 1;
eax |= esi;
eax ^= _key1 - 1;
edx <<= 31;
eax >>= 1;
var ecx = _table1 >> 1;
eax |= esi;
ecx |= edx;
eax ^= _key1;
ecx ^= _key2;
_table1 = ecx;
_table2 = eax;
}
}
/// <summary>
/// Server does not encrypt login responses, so this is a no-op.
/// </summary>
public void ServerEncrypt(Span<byte> buffer)
{
// Login encryption is client-to-server only
}
}

View file

@ -0,0 +1,89 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2025 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: LoginKeys.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Server.Network;
/// <summary>
/// Represents encryption keys derived from a client version.
/// Used for login packet encryption/decryption.
/// </summary>
public readonly struct LoginKeys
{
public static readonly LoginKeys Empty = new(0, 0);
private static readonly Dictionary<ClientVersion, LoginKeys> _cache = [];
public uint Key1 { get; }
public uint Key2 { get; }
private LoginKeys(uint key1, uint key2)
{
Key1 = key1;
Key2 = key2;
}
/// <summary>
/// Gets or computes encryption keys for the specified client version.
/// Results are cached for performance.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static LoginKeys GetKeys(ClientVersion version)
{
if (version == null)
{
return Empty;
}
if (_cache.TryGetValue(version, out var keys))
{
return keys;
}
keys = ComputeKeys(version);
_cache[version] = keys;
return keys;
}
/// <summary>
/// Computes encryption keys from client version using the UO key derivation algorithm.
/// </summary>
private static LoginKeys ComputeKeys(ClientVersion version)
{
uint major = (uint)version.Major;
uint minor = (uint)version.Minor;
uint revision = (uint)version.Revision;
// Key1 derivation
uint key1 = (major << 23) | (minor << 14) | (revision << 4);
key1 ^= (revision * revision) << 9;
key1 ^= minor * minor;
key1 ^= (minor * 11) << 24;
key1 ^= (revision * 7) << 19;
key1 ^= 0x2C13A5FD;
// Key2 derivation
uint key2 = (major << 22) | (revision << 13) | (minor << 3);
key2 ^= (revision * revision * 3) << 10;
key2 ^= minor * minor;
key2 ^= (minor * 13) << 23;
key2 ^= (revision * 7) << 18;
key2 ^= 0xA31D527F;
return new LoginKeys(key1, key2);
}
}

View file

@ -0,0 +1,271 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2025 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: TwofishEngine.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Server.Network;
/// <summary>
/// Twofish block cipher implementation for UO encryption.
/// Implements 128-bit block encryption with 128-bit key in ECB mode.
/// Based on the public domain Twofish algorithm by Bruce Schneier et al.
/// </summary>
public sealed class TwofishEngine
{
private const int BlockSize = 16; // 128 bits
private const int Rounds = 16;
private const int InputWhiten = 0;
private const int OutputWhiten = 4;
private const int RoundSubkeys = 8;
private const int TotalSubkeys = RoundSubkeys + 2 * Rounds;
private const uint SkStep = 0x02020202u;
private const uint SkBump = 0x01010101u;
private const int SkRotl = 9;
private const uint RsGfFdbk = 0x14D;
private const int MdsGfFdbk = 0x169;
// P0 and P1 permutation tables
private static readonly byte[] P0 =
{
0xA9, 0x67, 0xB3, 0xE8, 0x04, 0xFD, 0xA3, 0x76, 0x9A, 0x92, 0x80, 0x78, 0xE4, 0xDD, 0xD1, 0x38,
0x0D, 0xC6, 0x35, 0x98, 0x18, 0xF7, 0xEC, 0x6C, 0x43, 0x75, 0x37, 0x26, 0xFA, 0x13, 0x94, 0x48,
0xF2, 0xD0, 0x8B, 0x30, 0x84, 0x54, 0xDF, 0x23, 0x19, 0x5B, 0x3D, 0x59, 0xF3, 0xAE, 0xA2, 0x82,
0x63, 0x01, 0x83, 0x2E, 0xD9, 0x51, 0x9B, 0x7C, 0xA6, 0xEB, 0xA5, 0xBE, 0x16, 0x0C, 0xE3, 0x61,
0xC0, 0x8C, 0x3A, 0xF5, 0x73, 0x2C, 0x25, 0x0B, 0xBB, 0x4E, 0x89, 0x6B, 0x53, 0x6A, 0xB4, 0xF1,
0xE1, 0xE6, 0xBD, 0x45, 0xE2, 0xF4, 0xB6, 0x66, 0xCC, 0x95, 0x03, 0x56, 0xD4, 0x1C, 0x1E, 0xD7,
0xFB, 0xC3, 0x8E, 0xB5, 0xE9, 0xCF, 0xBF, 0xBA, 0xEA, 0x77, 0x39, 0xAF, 0x33, 0xC9, 0x62, 0x71,
0x81, 0x79, 0x09, 0xAD, 0x24, 0xCD, 0xF9, 0xD8, 0xE5, 0xC5, 0xB9, 0x4D, 0x44, 0x08, 0x86, 0xE7,
0xA1, 0x1D, 0xAA, 0xED, 0x06, 0x70, 0xB2, 0xD2, 0x41, 0x7B, 0xA0, 0x11, 0x31, 0xC2, 0x27, 0x90,
0x20, 0xF6, 0x60, 0xFF, 0x96, 0x5C, 0xB1, 0xAB, 0x9E, 0x9C, 0x52, 0x1B, 0x5F, 0x93, 0x0A, 0xEF,
0x91, 0x85, 0x49, 0xEE, 0x2D, 0x4F, 0x8F, 0x3B, 0x47, 0x87, 0x6D, 0x46, 0xD6, 0x3E, 0x69, 0x64,
0x2A, 0xCE, 0xCB, 0x2F, 0xFC, 0x97, 0x05, 0x7A, 0xAC, 0x7F, 0xD5, 0x1A, 0x4B, 0x0E, 0xA7, 0x5A,
0x28, 0x14, 0x3F, 0x29, 0x88, 0x3C, 0x4C, 0x02, 0xB8, 0xDA, 0xB0, 0x17, 0x55, 0x1F, 0x8A, 0x7D,
0x57, 0xC7, 0x8D, 0x74, 0xB7, 0xC4, 0x9F, 0x72, 0x7E, 0x15, 0x22, 0x12, 0x58, 0x07, 0x99, 0x34,
0x6E, 0x50, 0xDE, 0x68, 0x65, 0xBC, 0xDB, 0xF8, 0xC8, 0xA8, 0x2B, 0x40, 0xDC, 0xFE, 0x32, 0xA4,
0xCA, 0x10, 0x21, 0xF0, 0xD3, 0x5D, 0x0F, 0x00, 0x6F, 0x9D, 0x36, 0x42, 0x4A, 0x5E, 0xC1, 0xE0
};
private static readonly byte[] P1 =
{
0x75, 0xF3, 0xC6, 0xF4, 0xDB, 0x7B, 0xFB, 0xC8, 0x4A, 0xD3, 0xE6, 0x6B, 0x45, 0x7D, 0xE8, 0x4B,
0xD6, 0x32, 0xD8, 0xFD, 0x37, 0x71, 0xF1, 0xE1, 0x30, 0x0F, 0xF8, 0x1B, 0x87, 0xFA, 0x06, 0x3F,
0x5E, 0xBA, 0xAE, 0x5B, 0x8A, 0x00, 0xBC, 0x9D, 0x6D, 0xC1, 0xB1, 0x0E, 0x80, 0x5D, 0xD2, 0xD5,
0xA0, 0x84, 0x07, 0x14, 0xB5, 0x90, 0x2C, 0xA3, 0xB2, 0x73, 0x4C, 0x54, 0x92, 0x74, 0x36, 0x51,
0x38, 0xB0, 0xBD, 0x5A, 0xFC, 0x60, 0x62, 0x96, 0x6C, 0x42, 0xF7, 0x10, 0x7C, 0x28, 0x27, 0x8C,
0x13, 0x95, 0x9C, 0xC7, 0x24, 0x46, 0x3B, 0x70, 0xCA, 0xE3, 0x85, 0xCB, 0x11, 0xD0, 0x93, 0xB8,
0xA6, 0x83, 0x20, 0xFF, 0x9F, 0x77, 0xC3, 0xCC, 0x03, 0x6F, 0x08, 0xBF, 0x40, 0xE7, 0x2B, 0xE2,
0x79, 0x0C, 0xAA, 0x82, 0x41, 0x3A, 0xEA, 0xB9, 0xE4, 0x9A, 0xA4, 0x97, 0x7E, 0xDA, 0x7A, 0x17,
0x66, 0x94, 0xA1, 0x1D, 0x3D, 0xF0, 0xDE, 0xB3, 0x0B, 0x72, 0xA7, 0x1C, 0xEF, 0xD1, 0x53, 0x3E,
0x8F, 0x33, 0x26, 0x5F, 0xEC, 0x76, 0x2A, 0x49, 0x81, 0x88, 0xEE, 0x21, 0xC4, 0x1A, 0xEB, 0xD9,
0xC5, 0x39, 0x99, 0xCD, 0xAD, 0x31, 0x8B, 0x01, 0x18, 0x23, 0xDD, 0x1F, 0x4E, 0x2D, 0xF9, 0x48,
0x4F, 0xF2, 0x65, 0x8E, 0x78, 0x5C, 0x58, 0x19, 0x8D, 0xE5, 0x98, 0x57, 0x67, 0x7F, 0x05, 0x64,
0xAF, 0x63, 0xB6, 0xFE, 0xF5, 0xB7, 0x3C, 0xA5, 0xCE, 0xE9, 0x68, 0x44, 0xE0, 0x4D, 0x43, 0x69,
0x29, 0x2E, 0xAC, 0x15, 0x59, 0xA8, 0x0A, 0x9E, 0x6E, 0x47, 0xDF, 0x34, 0x35, 0x6A, 0xCF, 0xDC,
0x22, 0xC9, 0xC0, 0x9B, 0x89, 0xD4, 0xED, 0xAB, 0x12, 0xA2, 0x0D, 0x52, 0xBB, 0x02, 0x2F, 0xA9,
0xD7, 0x61, 0x1E, 0xB4, 0x50, 0x04, 0xF6, 0xC2, 0x16, 0x25, 0x86, 0x56, 0x55, 0x09, 0xBE, 0x91
};
private readonly uint[] _sboxKeys = new uint[2]; // For 128-bit key
private readonly uint[] _subKeys = new uint[TotalSubkeys];
/// <summary>
/// Creates a new Twofish engine with the specified 128-bit key.
/// </summary>
public TwofishEngine(ReadOnlySpan<byte> key)
{
if (key.Length != 16)
{
throw new ArgumentException("Key must be 16 bytes (128 bits)", nameof(key));
}
GenerateSubkeys(MemoryMarshal.Cast<byte, uint>(key));
}
private void GenerateSubkeys(ReadOnlySpan<uint> keyWords)
{
// Split key into even and odd words
var k0 = keyWords[0];
var k1 = keyWords[1];
var k2 = keyWords[2];
var k3 = keyWords[3];
// Compute S-box keys using RS matrix
_sboxKeys[0] = RsMdsEncode(k2, k3);
_sboxKeys[1] = RsMdsEncode(k0, k1);
// Generate round subkeys
for (var i = 0; i < TotalSubkeys / 2; i++)
{
var a = F32((uint)(i * SkStep), k0, k2);
var b = F32((uint)(i * SkStep + SkBump), k1, k3);
b = BitOperations.RotateLeft(b, 8);
_subKeys[2 * i] = a + b;
_subKeys[2 * i + 1] = BitOperations.RotateLeft(a + 2 * b, SkRotl);
}
}
/// <summary>
/// Encrypts a 16-byte block in place.
/// </summary>
public void EncryptBlock(Span<byte> block)
{
if (block.Length < BlockSize)
{
throw new ArgumentException("Block must be at least 16 bytes", nameof(block));
}
var x = MemoryMarshal.Cast<byte, uint>(block);
// Input whitening
x[0] ^= _subKeys[InputWhiten];
x[1] ^= _subKeys[InputWhiten + 1];
x[2] ^= _subKeys[InputWhiten + 2];
x[3] ^= _subKeys[InputWhiten + 3];
// 16 rounds
for (var r = 0; r < Rounds; r++)
{
var t0 = F32Sbox(x[0]);
var t1 = F32Sbox(BitOperations.RotateLeft(x[1], 8));
x[3] = BitOperations.RotateLeft(x[3], 1);
x[2] ^= t0 + t1 + _subKeys[RoundSubkeys + 2 * r];
x[3] ^= t0 + 2 * t1 + _subKeys[RoundSubkeys + 2 * r + 1];
x[2] = BitOperations.RotateRight(x[2], 1);
if (r < Rounds - 1)
{
// Swap for next round
(x[0], x[2]) = (x[2], x[0]);
(x[1], x[3]) = (x[3], x[1]);
}
}
// Output whitening
x[0] ^= _subKeys[OutputWhiten];
x[1] ^= _subKeys[OutputWhiten + 1];
x[2] ^= _subKeys[OutputWhiten + 2];
x[3] ^= _subKeys[OutputWhiten + 3];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private uint F32Sbox(uint x)
{
// For 128-bit key, use 2 S-box keys
// Permutation sequence from Twofish spec (P_ij constants):
// b0: P0[P0[P0[x]^k1]^k0] then P1 final
// b1: P0[P0[P1[x]^k1]^k0] then P0 final
// b2: P1[P1[P0[x]^k1]^k0] then P1 final
// b3: P1[P1[P1[x]^k1]^k0] then P0 final
var b0 = (byte)x;
var b1 = (byte)(x >> 8);
var b2 = (byte)(x >> 16);
var b3 = (byte)(x >> 24);
var k0 = _sboxKeys[0];
var k1 = _sboxKeys[1];
// First layer: P_02=0(P0), P_12=1(P1), P_22=0(P0), P_32=1(P1)
b0 = (byte)(P0[b0] ^ (byte)k1);
b1 = (byte)(P1[b1] ^ (byte)(k1 >> 8));
b2 = (byte)(P0[b2] ^ (byte)(k1 >> 16));
b3 = (byte)(P1[b3] ^ (byte)(k1 >> 24));
// Second layer: P_01=0(P0), P_11=0(P0), P_21=1(P1), P_31=1(P1)
b0 = (byte)(P0[b0] ^ (byte)k0);
b1 = (byte)(P0[b1] ^ (byte)(k0 >> 8));
b2 = (byte)(P1[b2] ^ (byte)(k0 >> 16));
b3 = (byte)(P1[b3] ^ (byte)(k0 >> 24));
// Final layer: P_00=1(P1), P_10=0(P0), P_20=1(P1), P_30=0(P0)
// MDS matrix multiply
return MdsMultiply(P1[b0], P0[b1], P1[b2], P0[b3]);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint F32(uint x, uint k0, uint k2)
{
var b0 = (byte)x;
var b1 = (byte)(x >> 8);
var b2 = (byte)(x >> 16);
var b3 = (byte)(x >> 24);
// First layer: P_02=0(P0), P_12=1(P1), P_22=0(P0), P_32=1(P1)
b0 = (byte)(P0[b0] ^ (byte)k2);
b1 = (byte)(P1[b1] ^ (byte)(k2 >> 8));
b2 = (byte)(P0[b2] ^ (byte)(k2 >> 16));
b3 = (byte)(P1[b3] ^ (byte)(k2 >> 24));
// Second layer: P_01=0(P0), P_11=0(P0), P_21=1(P1), P_31=1(P1)
b0 = (byte)(P0[b0] ^ (byte)k0);
b1 = (byte)(P0[b1] ^ (byte)(k0 >> 8));
b2 = (byte)(P1[b2] ^ (byte)(k0 >> 16));
b3 = (byte)(P1[b3] ^ (byte)(k0 >> 24));
// Final layer: P_00=1(P1), P_10=0(P0), P_20=1(P1), P_30=0(P0)
return MdsMultiply(P1[b0], P0[b1], P1[b2], P0[b3]);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint MdsMultiply(byte b0, byte b1, byte b2, byte b3)
{
// MDS matrix multiplication (Galois Field 2^8)
var m0 = (uint)(b0 ^ Lfsr2(b1) ^ Lfsr1(b2) ^ Lfsr1(b3));
var m1 = (uint)(Lfsr1(b0) ^ Lfsr2(b1) ^ Lfsr2(b2) ^ b3);
var m2 = (uint)(Lfsr2(b0) ^ Lfsr1(b1) ^ b2 ^ Lfsr2(b3));
var m3 = (uint)(Lfsr2(b0) ^ b1 ^ Lfsr2(b2) ^ Lfsr1(b3));
return m0 | (m1 << 8) | (m2 << 16) | (m3 << 24);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int Lfsr1(int val) => val ^ Lfsr4(val);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int Lfsr2(int val) => val ^ Lfsr3(val) ^ Lfsr4(val);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int Lfsr3(int val) => (val >> 1) ^ ((val & 0x01) == 0x01 ? MdsGfFdbk / 2 : 0);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int Lfsr4(int val) =>
(val >> 2) ^ ((val & 0x02) == 0x02 ? MdsGfFdbk / 2 : 0) ^ ((val & 0x01) == 0x01 ? MdsGfFdbk / 4 : 0);
private static uint RsMdsEncode(uint k0, uint k1)
{
uint r = 0;
for (var i = 0; i < 2; i++)
{
r ^= i > 0 ? k0 : k1;
for (var j = 0; j < 4; j++)
{
var v1 = (byte)(r >> 24);
var v2 = (uint)(((v1 << 1) ^ ((v1 & 0x80) == 0x80 ? RsGfFdbk : 0)) & 0xFF);
var v3 = (uint)(((v1 >> 1) & 0x7F) ^ ((v1 & 1) == 1 ? RsGfFdbk >> 1 : 0) ^ v2);
r = (r << 8) ^ (v3 << 24) ^ (v2 << 16) ^ (v3 << 8) ^ v1;
}
}
return r;
}
}

View file

@ -32,7 +32,7 @@ public static class DumpNetStates
foreach (var ns in NetState.Instances)
{
file.WriteLine($"{ns}, {ns.ConnectedOn}, {ns.NextActivityCheck}, {ns.Connection.Connected}, {ns._protocolState}, {ns._parserState}");
file.WriteLine($"{ns}, {ns.ConnectedOn}, {ns.NextActivityCheck}, {ns.IsConnected}, {ns._protocolState}, {ns._parserState}");
}
}
}

View file

@ -0,0 +1,498 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2025 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: NetState.Network.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Network;
namespace Server.Network;
/// <summary>
/// Network infrastructure for IORingGroup-based socket I/O.
/// </summary>
public partial class NetState
{
// Buffer sizes
private const int RecvBufferSize = 1024 * 64; // 64KB recv buffers
private const int SendBufferSize = 1024 * 256; // 256KB send buffers
private const int MaxConnections = 4096; // Max concurrent connections
// Socket manager handles buffer pools, socket lifecycle, and I/O operations
private static RingSocketManager _socketManager;
// NetState storage indexed by RingSocket.Id
private static readonly NetState[] _netStates = new NetState[MaxConnections];
// Events buffer for ProcessCompletions
private static readonly RingSocketEvent[] _events = new RingSocketEvent[MaxConnections * 2];
// Listener management
private static nint[] _listeners = Array.Empty<nint>();
private static int _pendingAcceptCount;
private const int PendingAcceptsPerListener = 32;
/// <summary>
/// Gets the IORingGroup instance for socket operations.
/// </summary>
public static IIORingGroup Ring => _socketManager?.Ring;
/// <summary>
/// Gets the listening addresses that the server is bound to.
/// </summary>
public static IPEndPoint[] ListeningAddresses { get; private set; }
private static IPRateLimiter _ipRateLimiter;
/// <summary>
/// Configures the IORingGroup and socket manager.
/// </summary>
private static void ConfigureNetwork()
{
// Skip if already configured
if (_socketManager != null)
{
return;
}
// Initialize IP rate limiter
_ipRateLimiter = new IPRateLimiter(10, 10000, 1000, 2.0, 3_600_000, Core.ClosingTokenSource.Token);
// Initialize IORingGroup
var ring = IORingGroup.Create(queueSize: MaxConnections * 2, maxConnections: MaxConnections);
// Create socket manager which handles buffer pools and socket lifecycle
_socketManager = new RingSocketManager(
ring,
maxSockets: MaxConnections,
recvBufferSize: RecvBufferSize,
sendBufferSize: SendBufferSize,
initialBufferSlabs: 8,
maxBufferSlabs: 32
);
}
/// <summary>
/// Starts the network server on configured listening addresses.
/// </summary>
public static void Start()
{
HashSet<IPEndPoint> listeningAddresses = [];
List<nint> listeners = [];
var ring = _socketManager.Ring;
for (var i = 0; i < ServerConfiguration.Listeners.Count; i++)
{
var ipep = ServerConfiguration.Listeners[i];
var listener = ring.CreateListener(ipep.Address.ToString(), (ushort)ipep.Port, 256);
if (listener == -1)
{
logger.Warning("Failed to create listener for {Address}", ipep);
continue;
}
if (ipep.Address.Equals(IPAddress.Any) || ipep.Address.Equals(IPAddress.IPv6Any))
{
listeningAddresses.UnionWith(GetListeningAddresses(ipep));
}
else
{
listeningAddresses.Add(ipep);
}
listeners.Add(listener);
}
foreach (var ipep in listeningAddresses)
{
logger.Information("Listening: {Address}", ipep);
}
ListeningAddresses = listeningAddresses.ToArray();
// Register listeners to start accepting connections
RegisterListeners(listeners.ToArray());
}
/// <summary>
/// Shuts down the network server and closes all listeners.
/// </summary>
public static void Shutdown()
{
CloseListeners();
}
/// <summary>
/// Gets the actual listening addresses for a wildcard endpoint.
/// </summary>
public static IEnumerable<IPEndPoint> GetListeningAddresses(IPEndPoint ipep) =>
NetworkInterface.GetAllNetworkInterfaces().SelectMany(adapter =>
adapter.GetIPProperties().UnicastAddresses
.Where(uip => ipep.AddressFamily == uip.Address.AddressFamily)
.Select(uip => new IPEndPoint(uip.Address, ipep.Port))
);
/// <summary>
/// Registers listeners with the ring and starts accepting connections.
/// </summary>
private static void RegisterListeners(nint[] listeners)
{
_listeners = listeners;
var ring = _socketManager.Ring;
// Queue initial accept operations for each listener
for (var i = 0; i < _listeners.Length; i++)
{
var listener = _listeners[i];
for (var j = 0; j < PendingAcceptsPerListener; j++)
{
ring.PrepareAccept(listener, 0, 0, IORingUserData.EncodeAccept());
_pendingAcceptCount++;
}
}
}
/// <summary>
/// Closes all listeners.
/// </summary>
private static void CloseListeners()
{
var ring = _socketManager?.Ring;
if (ring == null)
{
return;
}
foreach (var listener in _listeners)
{
ring.CloseListener(listener);
}
_listeners = [];
}
private static void HandleAcceptCompletion(int result)
{
_pendingAcceptCount--;
var ring = _socketManager.Ring;
// EAGAIN (-11) means no connection pending - just re-queue
if (result == -11)
{
goto ReplenishAccepts;
}
if (result >= 0)
{
var clientSocket = (nint)result;
var remoteIP = SocketHelper.GetRemoteAddress(clientSocket);
if (remoteIP != null)
{
if (_ipRateLimiter != null && !_ipRateLimiter.Verify(remoteIP, out var totalAttempts))
{
logger.Debug("{Address} Past IP limit threshold ({TotalAttempts})", remoteIP, totalAttempts);
}
else if (Firewall.IsBlocked(remoteIP))
{
logger.Debug("{Address} Firewalled", remoteIP);
}
else
{
// Allow event handlers to reject the connection
var args = new SocketConnectEventArgs(remoteIP);
EventSink.InvokeSocketConnect(args);
if (args.AllowConnection)
{
ring.ConfigureSocket(clientSocket);
CreateFromSocket(clientSocket, remoteIP);
goto ReplenishAccepts;
}
logger.Debug("{Address} Rejected by socket handler", remoteIP);
}
}
ring.CloseSocket(clientSocket);
}
else if (result != -4) // EINTR
{
logger.Debug("Accept error: {Result}", result);
}
ReplenishAccepts:
var targetAccepts = _listeners.Length * PendingAcceptsPerListener;
while (_pendingAcceptCount < targetAccepts && _listeners.Length > 0)
{
var listenerIndex = _pendingAcceptCount % _listeners.Length;
ring.PrepareAccept(_listeners[listenerIndex], 0, 0, IORingUserData.EncodeAccept());
_pendingAcceptCount++;
}
}
/// <summary>
/// Creates a NetState from an accepted socket handle.
/// </summary>
internal static NetState CreateFromSocket(nint socketHandle, IPAddress address)
{
// Use socket manager to create managed socket (handles buffers, registration, recv posting)
var socket = _socketManager.CreateSocket(socketHandle);
if (socket == null)
{
logger.Debug("Failed to create socket (resources exhausted)");
_socketManager.Ring.CloseSocket(socketHandle);
return null;
}
// Create NetState and map by socket ID
var ns = new NetState(socket, address);
return _netStates[socket.Id] = ns;
}
private static void DisconnectUnattachedSockets()
{
var now = Core.Now;
// Process connecting queue with lazy removal - O(1) operations
while (_connectingQueue.TryPeek(out var ns))
{
// Lazy removal: skip already-authenticated or disconnected connections
if (!ns.Running || ns.Account != null)
{
_connectingQueue.Dequeue();
continue;
}
// If the socket has been connected for less than the limit, we can stop
// (queue is ordered by connection time, so remaining entries are newer)
if (now - ns.ConnectedOn < ConnectingSocketIdleLimit)
{
break;
}
_connectingQueue.Dequeue();
// Socket must have finished the entire authentication process or be forcibly disconnected
if (!ns.SentFirstPacket || !ns.Seeded)
{
ns.Disconnect(null);
}
}
}
public static void FlushAll()
{
while (_flushPending.TryDequeue(out var ns))
{
if (ns == null)
{
continue;
}
// Reset flag to allow re-queueing if more data is added later
ns._flushQueued = false;
if (ns.Running)
{
ns._socket?.QueueSend();
}
}
// Submit any pending operations
_socketManager?.Submit();
}
public static void Slice()
{
DisconnectUnattachedSockets();
// Process throttled states
while (_throttled.Count > 0)
{
var ns = _throttled.Dequeue();
if (ns.Running)
{
ns.HandleReceive(true);
}
}
// This is enqueued by HandleReceive if already throttled and still throttled
while (_throttledPending.Count > 0)
{
_throttled.Enqueue(_throttledPending.Dequeue());
}
// Process all completions through the manager FIRST
// This ensures DataReceived events are processed and HandleReceive runs,
// which may call Send() and add to _flushPending
var eventCount = _socketManager.ProcessCompletions(_events);
for (var i = 0; i < eventCount; i++)
{
ref var evt = ref _events[i];
switch (evt.Type)
{
case RingSocketEventType.Accept:
{
// Handle accept - AcceptedSocketHandle contains the result
HandleAcceptCompletion((int)evt.AcceptedSocketHandle);
break;
}
case RingSocketEventType.DataReceived:
{
var nsRecv = _netStates[evt.Socket.Id];
// Verify generation via object identity to avoid stale completion issues
if (nsRecv != null && nsRecv._socket == evt.Socket)
{
HandleDataReceived(nsRecv, evt.BytesTransferred);
}
break;
}
case RingSocketEventType.DataSent:
{
var nsSend = _netStates[evt.Socket.Id];
// Verify generation via object identity
if (nsSend != null && nsSend._socket == evt.Socket)
{
// Update activity check on successful send
nsSend.NextActivityCheck = Core.TickCount + 90000;
}
break;
}
case RingSocketEventType.Disconnected:
{
var nsDisc = _netStates[evt.Socket.Id];
// Verify generation via object identity
if (nsDisc != null && nsDisc._socket == evt.Socket)
{
HandleDisconnected(nsDisc);
}
break;
}
}
}
// Process flush queue AFTER event processing
// This ensures sends triggered by HandleReceive (via packet handlers like SendPlayServerAck)
// are queued in the SAME Slice, not the next one
while (_flushPending.TryDequeue(out var ns))
{
// Reset flag to allow re-queueing if more data is added later
ns._flushQueued = false;
if (ns.Running)
{
ns._socket?.QueueSend();
}
}
// CRITICAL: Process send queue NOW to post pending sends
// This ensures PostSend() runs and sets SendPending=true BEFORE disconnect checks
// Without this, Disconnect() would see SendPending=false even though data is queued
_socketManager.ProcessSendQueue();
// Process pending disconnects AFTER flush queue AND send queue processing
// This ensures the traditional order: Game Logic (Sends/Disconnects) → Receives → Flush → Disconnect
// Any Send() calls made after Disconnect() in the same tick are flushed before disconnect
while (_pendingDisconnects.TryDequeue(out var ns))
{
// Reset flag to allow re-queueing if reconnect happens
ns._disconnectQueued = false;
if (ns.Running && ns._socket != null)
{
// RingSocket.Disconnect() handles graceful disconnect:
// - Waits for pending sends to flush (if SendBuffer.ReadableBytes > 0)
// - Waits for in-flight I/O to complete
// - Ensures buffers aren't released while kernel is still using them
ns._socket.Disconnect();
}
}
// Submit any queued operations
_socketManager.Submit();
// Process disposes
while (_disposed.TryDequeue(out var ns))
{
ns.Dispose();
}
}
private static void HandleDataReceived(NetState ns, int bytesReceived)
{
if (!ns._running)
{
return;
}
// Data is already committed to buffer by RingSocketManager
// Decode if encryption is enabled
ns.DecryptRecvBuffer(bytesReceived);
// Process packets
ns.HandleReceive();
}
private static void HandleDisconnected(NetState ns)
{
var slotId = ns._socket.Id;
// IMPORTANT: Check if the slot still points to this NetState
// During quick reconnect, the slot might have been reused for a new connection
var currentNs = _netStates[slotId];
if (currentNs != ns)
{
// Slot was already reused - don't clear it!
// Just mark this NetState as not running and queue for dispose
ns._running = false;
_disposed.Enqueue(ns);
return;
}
// Clear the NetState slot
_netStates[slotId] = null;
// Mark as not running and queue for dispose
ns._running = false;
_disposed.Enqueue(ns);
}
public static void CheckAllAlive()
{
try
{
var curTicks = Core.TickCount;
foreach (var ns in Instances)
{
ns.CheckAlive(curTicks);
}
}
catch (Exception ex)
{
TraceException(ex);
}
}
}

View file

@ -1,6 +1,6 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2023 - ModernUO Development Team *
* Copyright 2019-2025 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: NetState.cs *
* *
@ -25,55 +25,46 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Network;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Server.Network;
public delegate void DecodePacket(Span<byte> buffer, ref int length);
public delegate int EncodePacket(ReadOnlySpan<byte> inputBuffer, Span<byte> outputBuffer);
public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetState>, IDisposable
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(NetState));
private static readonly TimeSpan ConnectingSocketIdleLimit = TimeSpan.FromMilliseconds(5000); // 5 seconds
private const int RecvPipeSize = 1024 * 64;
private const int SendPipeSize = 1024 * 256;
private const int HuePickerCap = 512;
private const int MenuCap = 512;
private const int PacketPerSecondThreshold = 3000;
private static readonly GCHandle[] _polledStates = new GCHandle[2048];
private static readonly IPollGroup _pollGroup = PollGroup.Create();
private static readonly Queue<NetState> _flushPending = new(2048);
private static readonly Queue<NetState> _flushedPartials = new(256);
private static readonly Queue<NetState> _pendingDisconnects = new(256); // Processed AFTER flush
private static readonly ConcurrentQueue<NetState> _disposed = new();
private static readonly Queue<NetState> _throttled = new(256);
private static readonly Queue<NetState> _throttledPending = new(256);
private static readonly SortedSet<NetState> _connecting = new(NetStateConnectingComparer.Instance);
private static readonly Queue<NetState> _connectingQueue = new(2048);
private static readonly HashSet<NetState> _instances = new(2048);
public static IReadOnlySet<NetState> Instances => _instances;
private readonly string _toString;
private ClientVersion _version;
private bool _running = true;
private volatile DecodePacket _packetDecoder;
private volatile EncodePacket _packetEncoder;
private IClientEncryption _encryption;
private bool _flushQueued;
private bool _disconnectQueued; // Queued for disconnect processing (after flush)
private long[] _packetThrottles;
private long[] _packetCounts;
private string _disconnectReason = string.Empty;
internal ParserState _parserState = ParserState.AwaitingNextPacket;
internal ProtocolState _protocolState = ProtocolState.AwaitingSeed;
internal GCHandle _handle;
private bool _packetLogging;
public GCHandle Handle => _handle;
// Managed socket with buffers (handles lifecycle automatically)
internal RingSocket _socket;
// Speed Hack Prevention
internal long _movementCredit;
@ -108,6 +99,9 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
public static void Configure()
{
_packetLoggingPath = ServerConfiguration.GetSetting("netstate.packetLoggingPath", Path.Combine(Core.BaseDirectory, "Packets"));
// Initialize IORingGroup and buffer pools
ConfigureNetwork();
}
public static void Initialize()
@ -115,45 +109,24 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
Timer.DelayCall(TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1.5), CheckAllAlive);
}
public NetState(Socket connection)
// Internal constructor for accepted sockets
private NetState(RingSocket socket, IPAddress address)
{
Connection = connection;
_socket = socket;
Address = address;
Seeded = false;
HuePickers = [];
Menus = [];
Trades = [];
RecvPipe = new Pipe(RecvPipeSize);
SendPipe = new Pipe(SendPipeSize);
NextActivityCheck = Core.TickCount + 30000;
ConnectedOn = Core.Now;
try
{
Address = Utility.Intern((Connection?.RemoteEndPoint as IPEndPoint)?.Address);
_toString = Address?.ToString() ?? "(error)";
}
catch (Exception ex)
{
TraceException(ex);
Address = IPAddress.None;
_toString = "(error)";
}
_toString = address?.ToString() ?? "(error)";
_instances.Add(this);
_connecting.Add(this);
_handle = GCHandle.Alloc(this);
_connectingQueue.Enqueue(this);
LogInfo($"Connected. [{_instances.Count} Online]");
try
{
_pollGroup.Add(connection, _handle);
}
catch (Exception ex)
{
TraceException(ex);
Disconnect("Unable to add socket to poll group");
}
}
// Sectors
@ -188,16 +161,10 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
public IPAddress Address { get; }
public DecodePacket PacketDecoder
public IClientEncryption Encryption
{
get => _packetDecoder;
set => _packetDecoder = value;
}
public EncodePacket PacketEncoder
{
get => _packetEncoder;
set => _packetEncoder = value;
get => _encryption;
set => _encryption = value;
}
public int CurrentPacket { get; internal set; }
@ -210,13 +177,27 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
public bool Seeded { get; set; }
public Pipe RecvPipe { get; }
public Pipe SendPipe { get; }
public bool Running => _running;
public Socket Connection { get; private set; }
/// <summary>
/// Gets whether the socket is connected.
/// </summary>
public bool IsConnected => _running && _socket != null;
/// <summary>
/// Gets the socket handle.
/// </summary>
public nint SocketHandle => _socket?.Handle ?? 0;
/// <summary>
/// Gets the local endpoint (address/port) the client connected to.
/// </summary>
public IPEndPoint LocalEndPoint => _socket != null ? SocketHelper.GetLocalEndPoint(_socket.Handle) : null;
/// <summary>
/// Gets the send buffer for this connection.
/// </summary>
internal IORingBuffer SendBuffer => _socket?.SendBuffer;
public bool CompressionEnabled { get; set; }
@ -235,15 +216,7 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
public IAccount Account
{
get => _account;
set
{
if (_account != null)
{
_connecting.Remove(this);
}
_account = value;
}
set => _account = value;
}
public string Assistant { get; set; }
@ -464,8 +437,14 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
return false;
}
#endif
buffer = SendPipe.Writer.AvailableToWrite();
return !(SendPipe.Writer.IsClosed || buffer.Length <= 0);
if (!_running || _socket == null)
{
buffer = Span<byte>.Empty;
return false;
}
buffer = _socket.SendBuffer.GetWriteSpan();
return buffer.Length > 0;
}
public void Send(ReadOnlySpan<byte> span)
@ -483,21 +462,25 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
try
{
if (_packetEncoder != null)
// Apply encoding first (e.g., compression from UOContent)
if (CompressionEnabled)
{
length = _packetEncoder(span, buffer);
length = NetworkCompression.Compress(span, buffer);
}
else
{
span.CopyTo(buffer);
}
// Then encrypt (if encryption is enabled)
_encryption?.ServerEncrypt(buffer[..length]);
if (PacketLogging)
{
LogPacket(span, false);
}
SendPipe.Writer.Advance((uint)length);
_socket.SendBuffer.CommitWrite(length);
if (!_flushQueued)
{
@ -554,26 +537,34 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
}
}
public void HandleReceive(bool throttled = false)
private void DecryptRecvBuffer(int bytesReceived)
{
if (!_running)
if (_socket == null || _encryption == null)
{
return;
}
if (!throttled)
// Get the portion of the buffer that was just written (the new data)
var readSpan = _socket.RecvBuffer.GetReadSpan();
var newDataStart = Math.Max(0, readSpan.Length - bytesReceived);
_encryption?.ClientDecrypt(readSpan.Slice(newDataStart, bytesReceived));
}
public void HandleReceive(bool throttled = false)
{
if (!_running || _socket == null)
{
ReceiveData();
return;
}
var reader = RecvPipe.Reader;
// Data already in recv buffer from recv completion - no need to call ReceiveData
try
{
// Process as many packets as we can synchronously
while (_running && _parserState != ParserState.Error && _protocolState != ProtocolState.Error)
{
var buffer = reader.AvailableToRead();
var buffer = _socket.RecvBuffer.GetReadSpan();
var length = buffer.Length;
if (length <= 0)
@ -632,13 +623,63 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
case ProtocolState.LoginServer_AwaitingLogin:
{
if (packetId != 0x80)
// Check for unencrypted login packet
if (packetId == 0x80)
{
// Unencrypted - check if allowed
if (EncryptionManager.Enabled && !EncryptionManager.Mode.HasFlag(EncryptionMode.Unencrypted))
{
LogInfo("Unencrypted client rejected by encryption policy.");
HandleError(packetId, packetLength);
return;
}
_parserState = ParserState.ProcessingPacket;
_parserState = HandlePacket(packetReader, packetId, out packetLength);
if (_parserState == ParserState.AwaitingNextPacket)
{
_protocolState = ProtocolState.LoginServer_AwaitingServerSelect;
}
break;
}
// First byte isn't 0x80 - might be encrypted
if (!EncryptionManager.Enabled)
{
LogInfo("Possible encrypted client detected, disconnecting...");
HandleError(packetId, packetLength);
return;
}
// Need 62 bytes for login packet to attempt decryption
if (length < 62)
{
_parserState = ParserState.AwaitingPartialPacket;
break;
}
// Try to detect and decrypt encrypted login
if (!this.DetectLoginEncryption(buffer[..62], out var loginEncryption))
{
LogInfo("Encrypted client detection failed, disconnecting...");
HandleError(packetId, packetLength);
return;
}
// Decryption succeeded - set up encryption and process
if (loginEncryption != null)
{
_encryption = loginEncryption;
// Decrypt the buffer in place for processing
var mutableBuffer = _socket.RecvBuffer.GetReadSpan();
loginEncryption.ClientDecrypt(mutableBuffer[..62]);
}
// Now process as normal (first byte should now be 0x80)
packetReader = new SpanReader(buffer);
packetId = packetReader.ReadByte();
_parserState = ParserState.ProcessingPacket;
_parserState = HandlePacket(packetReader, packetId, out packetLength);
if (_parserState == ParserState.AwaitingNextPacket)
@ -680,17 +721,68 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
case ProtocolState.GameServer_AwaitingGameServerLogin:
{
// Some clients send 0x80 on game server connection
if (packetId == 0x80)
{
goto case ProtocolState.LoginServer_AwaitingLogin;
}
if (packetId != 0x91)
// Check for unencrypted game login packet
if (packetId == 0x91)
{
// Unencrypted - check if allowed
if (EncryptionManager.Enabled && !EncryptionManager.Mode.HasFlag(EncryptionMode.Unencrypted))
{
LogInfo("Unencrypted game client rejected by encryption policy.");
HandleError(packetId, packetLength);
return;
}
_parserState = ParserState.ProcessingPacket;
_parserState = HandlePacket(packetReader, packetId, out packetLength);
if (_parserState == ParserState.AwaitingNextPacket)
{
_protocolState = ProtocolState.GameServer_LoggedIn;
}
break;
}
// First byte isn't 0x91 - might be encrypted
if (!EncryptionManager.Enabled)
{
HandleError(packetId, packetLength);
return;
}
// Need 65 bytes for game login packet to attempt decryption
if (length < 65)
{
_parserState = ParserState.AwaitingPartialPacket;
break;
}
// Try to detect and decrypt encrypted game login
if (!this.DetectGameEncryption(buffer[..65], out var gameEncryption))
{
LogInfo("Encrypted game client detection failed, disconnecting...");
HandleError(packetId, packetLength);
return;
}
// Decryption succeeded - set up encryption and process
if (gameEncryption != null)
{
_encryption = gameEncryption;
// Decrypt the buffer in place for processing
var mutableBuffer = _socket.RecvBuffer.GetReadSpan();
gameEncryption.ClientDecrypt(mutableBuffer[..65]);
}
// Now process as normal (first byte should now be 0x91)
packetReader = new SpanReader(buffer);
packetId = packetReader.ReadByte();
_parserState = ParserState.ProcessingPacket;
_parserState = HandlePacket(packetReader, packetId, out packetLength);
if (_parserState == ParserState.AwaitingNextPacket)
@ -711,7 +803,7 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
if (_parserState is ParserState.AwaitingNextPacket)
{
reader.Advance((uint)packetLength);
_socket.RecvBuffer.CommitRead(packetLength);
}
else if (_parserState is ParserState.Throttled)
{
@ -844,225 +936,15 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
return ParserState.AwaitingNextPacket;
}
private bool Flush()
{
_flushQueued = false;
// We don't have a running check since we need to send the last bits of data even after a disconnect, but before a dispose.
if (Connection == null)
{
return true;
}
var reader = SendPipe.Reader;
var buffer = reader.AvailableToRead();
if (reader.IsClosed || buffer.Length == 0)
{
return true;
}
var bytesWritten = 0;
try
{
bytesWritten = Connection.Send(buffer, SocketFlags.None);
}
catch (SocketException ex)
{
if (ex.SocketErrorCode != SocketError.WouldBlock)
{
logger.Debug(ex, "Disconnected due to a socket exception");
Disconnect(string.Empty);
return true;
}
}
catch (Exception ex)
{
Disconnect($"Disconnected with error: {ex}");
TraceException(ex);
return true;
}
if (bytesWritten > 0)
{
NextActivityCheck = Core.TickCount + 90000;
reader.Advance((uint)bytesWritten);
}
return bytesWritten == buffer.Length;
}
private void DecodePacket(Span<byte> buffer, ref int length)
{
_packetDecoder?.Invoke(buffer, ref length);
}
private void ReceiveData()
{
var writer = RecvPipe.Writer;
var buffer = writer.AvailableToWrite();
if (writer.IsClosed || buffer.Length == 0)
{
return;
}
var bytesWritten = 0;
try
{
bytesWritten = Connection.Receive(buffer, SocketFlags.None);
}
catch (SocketException ex)
{
if (ex.ErrorCode is not 54 and not 89 and not 995)
{
logger.Debug(ex, "Disconnected due to a socket exception");
}
Disconnect(string.Empty);
}
catch (Exception ex)
{
Disconnect($"Disconnected with error: {ex}");
TraceException(ex);
}
if (bytesWritten <= 0)
{
Disconnect(string.Empty);
return;
}
DecodePacket(buffer, ref bytesWritten);
writer.Advance((uint)bytesWritten);
NextActivityCheck = Core.TickCount + 90000;
}
private static void DisconnectUnattachedSockets()
{
var now = Core.Now;
// Clear out any sockets that have been connecting for too long
while (_connecting.Count > 0)
{
var ns = _connecting.Min;
var socketTime = ns.ConnectedOn;
// If the socket has been connected for less than the limit, we can stop checking
if (now - socketTime < ConnectingSocketIdleLimit)
{
break;
}
// Socket must have finished the entire authentication process or be forcibly disconnected.
if (!ns.Running || !ns.SentFirstPacket || !ns.Seeded || ns.Account == null)
{
// Not sending a message because it will fill up the logs.
ns.Disconnect(null);
}
_connecting.Remove(ns);
}
}
public static void FlushAll()
{
while (_flushPending.Count != 0)
{
_flushPending.Dequeue()?.Flush();
}
}
public static void Slice()
{
DisconnectUnattachedSockets();
while (_throttled.Count > 0)
{
var ns = _throttled.Dequeue();
if (ns.Running)
{
ns.HandleReceive(true);
}
}
// This is enqueued by HandleReceive if already throttled and still throttled
while (_throttledPending.Count > 0)
{
_throttled.Enqueue(_throttledPending.Dequeue());
}
var count = _pollGroup.Poll(_polledStates);
if (count > 0)
{
for (var i = 0; i < count; i++)
{
(_polledStates[i].Target as NetState)?.HandleReceive();
_polledStates[i] = default;
}
}
while (_flushPending.TryDequeue(out var ns))
{
if (!ns.Flush())
{
// Incomplete data, so we need to requeue
_flushedPartials.Enqueue(ns);
}
}
var hasDisposes = false;
while (_disposed.TryDequeue(out var ns))
{
hasDisposes = true;
ns.Dispose();
}
// If they weren't disconnected, requeue them
while (_flushedPartials.TryDequeue(out var ns))
{
if (ns.Running)
{
_flushPending.Enqueue(ns);
}
}
if (hasDisposes)
{
_pollGroup.Poll(_polledStates.Length);
}
}
public void CheckAlive(long curTicks)
{
if (Connection != null && NextActivityCheck - curTicks < 0)
if (_socket != null && NextActivityCheck - curTicks < 0)
{
LogInfo("Disconnecting due to inactivity...");
Disconnect("Disconnecting due to inactivity.");
}
}
public static void CheckAllAlive()
{
try
{
var curTicks = Core.TickCount;
foreach (var ns in Instances)
{
ns.CheckAlive(curTicks);
}
}
catch (Exception ex)
{
TraceException(ex);
}
}
public void Trace(ReadOnlySpan<byte> buffer)
{
// We don't have data, so nothing to trace
@ -1105,17 +987,24 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
Console.WriteLine(ex);
}
/// <summary>
/// Requests a graceful disconnect. The disconnect is queued and processed after the flush
/// queue in Slice(), ensuring Send() calls made in the same tick are processed first.
/// </summary>
public void Disconnect(string reason)
{
if (!_running)
if (!_running || _socket == null)
{
return;
}
_running = false;
_disconnectReason = reason;
_disposed.Enqueue(this);
if (!_disconnectQueued)
{
_disconnectQueued = true;
_pendingDisconnects.Enqueue(this);
}
}
public static void TraceDisconnect(string reason, string ip)
@ -1148,16 +1037,18 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
{
_running = false;
// It's possible we could queue for dispose multiple times
if (Connection == null)
if (_socket == null)
{
return;
}
TraceDisconnect(_disconnectReason, _toString);
// If still running, force immediate disconnect
if (_running)
{
throw new Exception("Disconnected a NetState that is still running.");
_running = false;
_socketManager?.DisconnectImmediate(_socket);
}
var m = Mobile;
@ -1167,21 +1058,17 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
}
_instances.Remove(this);
_connecting.Remove(this);
try
// Clear the NetState slot
var slotId = _socket.Id;
if (slotId >= 0 && slotId < _netStates.Length && _netStates[slotId] == this)
{
_pollGroup.Remove(Connection, _handle);
}
catch (Exception ex)
{
TraceException(ex);
_netStates[slotId] = null;
}
Connection.Close();
_handle.Free();
RecvPipe.Dispose();
SendPipe.Dispose();
// Note: RingSocketManager handles cleanup of ring resources (unregister, close, buffer release)
// when it processes the disconnect event. We just clear our reference.
_socket = null;
Mobile = null;
@ -1192,46 +1079,9 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
Account = null;
ServerInfo = null;
CityInfo = null;
Connection = null;
var count = _instances.Count;
LogInfo(a != null ? $"Disconnected. [{count} Online] [{a}]" : $"Disconnected. [{count} Online]");
}
private class NetStateConnectingComparer : IComparer<NetState>
{
public static readonly IComparer<NetState> Instance = new NetStateConnectingComparer();
public int Compare(NetState x, NetState y)
{
if (x == null && y == null)
{
return 0;
}
if (x == null)
{
return -1;
}
if (y == null)
{
return 1;
}
if (ReferenceEquals(x, y))
{
return 0;
}
var connectedOn = x.ConnectedOn.CompareTo(y.ConnectedOn);
if (connectedOn != 0)
{
return connectedOn;
}
return x.CompareTo(y);
}
}
}

View file

@ -7,7 +7,7 @@ public static class OutgoingPackets
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool CannotSendPackets(this NetState ns) =>
// Do not check for NetState.Running. Packets are sent to a "disconnected" socket as part of the OnDisconnect events
// up until the Connection is nulled. Closing the connection is done synchronously, therefore packets will not be sent
// up until the socket is closed. Closing the connection is done synchronously, therefore packets will not be sent
// once the Mobile.NetState is null.
ns?.Connection == null || ns.BlockAllPackets;
ns == null || ns.SocketHandle == 0 || ns.BlockAllPackets;
}

View file

@ -61,7 +61,7 @@ public static class PingServer
if (ipep.Address.Equals(IPAddress.Any) || ipep.Address.Equals(IPAddress.IPv6Any))
{
listeningAddresses.UnionWith(TcpServer.GetListeningAddresses(ipep));
listeningAddresses.UnionWith(NetState.GetListeningAddresses(ipep));
}
else
{

View file

@ -1,519 +0,0 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2023 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: Pipe.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace Server.Network;
public partial class Pipe : IDisposable
{
public class PipeWriter
{
private readonly Pipe _pipe;
internal PipeWriter(Pipe pipe) => _pipe = pipe;
public unsafe Span<byte> AvailableToWrite()
{
var read = _pipe._readIdx;
var write = _pipe._writeIdx;
uint sz;
if (read <= write)
{
sz = _pipe.Size - write + read - 1;
}
else
{
sz = read - write - 1;
}
return new Span<byte>((void*)(_pipe._buffer + write), (int)sz);
}
public void Advance(uint count)
{
var read = _pipe._readIdx;
var write = _pipe._writeIdx;
if (count == 0)
{
return;
}
if (count > _pipe.Size - 1)
{
throw new EndOfPipeException("Unable to advance beyond the end of the pipe.");
}
if (read <= write)
{
if (count > read + _pipe.Size - write - 1)
{
throw new EndOfPipeException("Unable to advance beyond the end of the pipe.");
}
var sz = Math.Min(count, _pipe.Size - write);
write += sz;
if (write > _pipe.Size - 1)
{
write = 0;
}
count -= sz;
if (count > 0)
{
if (count >= read)
{
throw new EndOfPipeException("Unable to advance beyond the end of the pipe.");
}
write = count;
}
}
else
{
if (count > read - write - 1)
{
throw new EndOfPipeException("Unable to advance beyond the end of the pipe.");
}
write += count;
}
// It's never valid to advance the write pointer to become equal to
// the read pointer. Check that here.
if (write == read)
{
throw new EndOfPipeException("Unable to advance beyond the end of the pipe.");
}
_pipe._writeIdx = write;
}
public void Close() => _pipe._closed = true;
public bool IsClosed => _pipe._closed;
}
public class PipeReader
{
private readonly Pipe _pipe;
internal PipeReader(Pipe pipe) => _pipe = pipe;
public unsafe Span<byte> AvailableToRead()
{
var read = _pipe._readIdx;
var write = _pipe._writeIdx;
uint sz;
if (read <= write)
{
sz = write - read;
}
else
{
sz = _pipe.Size - read + write;
}
return new Span<byte>((void*)(_pipe._buffer + read), (int)sz);
}
public void Advance(uint count)
{
var read = _pipe._readIdx;
var write = _pipe._writeIdx;
if (read <= write)
{
if (count > write - read)
{
throw new EndOfPipeException("Unable to advance beyond the end of the pipe.");
}
read += count;
}
else
{
var sz = Math.Min(count, _pipe.Size - read);
read += sz;
if (read > _pipe.Size - 1)
{
read = 0;
}
count -= sz;
if (count > 0)
{
if (count > write)
{
throw new EndOfPipeException("Unable to advance beyond the end of the pipe.");
}
read = count;
}
}
if (read == write)
{
// If the read pointer catches up to the write pointer, then the pipe is empty.
// As a performance optimization, set both to 0. This should improve the chances cache lines are hit.
_pipe._readIdx = 0;
_pipe._writeIdx = 0;
}
else
{
_pipe._readIdx = read;
}
}
public void Close() => _pipe._closed = true;
public bool IsClosed => _pipe._closed;
}
private IntPtr _handle; // Doubles as the file descriptor for linux/darwin
private IntPtr _buffer;
private readonly uint _bufferSize;
private uint _writeIdx;
private uint _readIdx;
private bool _closed;
public PipeWriter Writer { get; }
public PipeReader Reader { get; }
public uint Size => _bufferSize;
public bool Closed => _closed;
public Pipe(uint size)
{
var pageSize = (uint)Environment.SystemPageSize;
// Virtual allocation requires multiples of system page size
// So let's adjust the requested size rounded to the next available page size
var adjustedSize = (size + pageSize - 1) & ~(pageSize - 1);
if (Core.IsWindows)
{
// Reserve a region of virtual memory. We need twice the size so we can later mirror.
var region = NativeMethods_Windows.VirtualAlloc2(
IntPtr.Zero,
IntPtr.Zero,
adjustedSize * 2,
NativeMethods_Windows.MEM_RESERVE | NativeMethods_Windows.MEM_RESERVE_PLACEHOLDER,
NativeMethods_Windows.PAGE_NOACCESS,
IntPtr.Zero,
0
);
if (region == IntPtr.Zero)
{
throw new InvalidOperationException($"Allocating virtual memory failed. ({Marshal.GetLastPInvokeError()})");
}
// Releases half of the region so we can map the same memory region twice
var freed = NativeMethods_Windows.VirtualFree(
region,
adjustedSize,
NativeMethods_Windows.MEM_RELEASE | NativeMethods_Windows.MEM_PRESERVE_PLACEHOLDER
);
if (!freed)
{
throw new InvalidOperationException($"Creating virtual placeholder failed. ({Marshal.GetLastPInvokeError()})");
}
// Create a file descriptor
_handle = NativeMethods_Windows.CreateFileMappingW(
NativeMethods_Windows.InvalidHandleValue,
IntPtr.Zero,
NativeMethods_Windows.PAGE_READWRITE,
0,
adjustedSize,
null
);
if (_handle == IntPtr.Zero)
{
throw new InvalidOperationException($"Creating file mapping failed. ({Marshal.GetLastPInvokeError()})");
}
// Map the region to the first half of the virtual space
_buffer = NativeMethods_Windows.MapViewOfFile3(
_handle,
IntPtr.Zero,
region,
0,
adjustedSize,
NativeMethods_Windows.MEM_REPLACE_PLACEHOLDER,
NativeMethods_Windows.PAGE_READWRITE,
IntPtr.Zero,
0
);
if (_buffer == IntPtr.Zero)
{
throw new InvalidOperationException($"Mapping file view failed. ({Marshal.GetLastPInvokeError()})");
}
// Map the same region to the second half of the virtual space
var view2 = NativeMethods_Windows.MapViewOfFile3(
_handle,
IntPtr.Zero,
new IntPtr(_buffer + adjustedSize),
0,
adjustedSize,
NativeMethods_Windows.MEM_REPLACE_PLACEHOLDER,
NativeMethods_Windows.PAGE_READWRITE,
IntPtr.Zero,
0
);
if (view2 == IntPtr.Zero)
{
throw new InvalidOperationException($"Mapping file view mirror failed. ({Marshal.GetLastPInvokeError()})");
}
}
else if (Core.IsLinux || Core.IsDarwin)
{
var anon = Core.IsLinux ? NativeMethods_Linux.MAP_ANONYMOUS : NativeMethods_Linux.MAP_ANON;
int fd;
if (Core.IsLinux)
{
// Create a memory-backed file descriptor
fd = NativeMethods_Linux.memfd_create("mirrored_ring_buffer", 0);
}
else
{
var fdName = $"/muo/ring/{GetHashCode()}";
fd = NativeMethods_Linux.shm_open(fdName, NativeMethods_Linux.O_CREAT | NativeMethods_Linux.O_RDWR, 0600);
// Unlink immediately to emulate memfd_create() functionality
NativeMethods_Linux.shm_unlink(fdName);
}
if (fd == NativeMethods_Linux.InvalidPtrValue)
{
throw new InvalidOperationException($"Creating file descriptor failed. ({Marshal.GetLastPInvokeError()})");
}
// Set the size of the file descriptor
if (NativeMethods_Linux.ftruncate(fd, (int)adjustedSize) != 0)
{
throw new InvalidOperationException($"Setting file descriptor size failed. ({Marshal.GetLastPInvokeError()})");
}
// Get virtual address space, must be double the size so we can map twice
_buffer = NativeMethods_Linux.mmap(IntPtr.Zero, adjustedSize * 2,
NativeMethods_Linux.PROT_READ | NativeMethods_Linux.PROT_WRITE,
NativeMethods_Linux.MAP_PRIVATE | anon, NativeMethods_Linux.InvalidFileDescriptor, 0);
if (_buffer == NativeMethods_Linux.InvalidPtrValue)
{
throw new InsufficientMemoryException($"Allocating virtual memory failed. ({Marshal.GetLastPInvokeError()})");
}
// Map the file descriptor to the first half of the virtual space
var view1 = NativeMethods_Linux.mmap(_buffer, adjustedSize,
NativeMethods_Linux.PROT_READ | NativeMethods_Linux.PROT_WRITE,
NativeMethods_Linux.MAP_SHARED | NativeMethods_Linux.MAP_FIXED, fd, 0);
if (view1 == NativeMethods_Linux.InvalidPtrValue)
{
throw new InvalidOperationException($"Mapping memory failed. ({Marshal.GetLastPInvokeError()})");
}
// Map the file descriptor to the second half of the virtual space
var view2 = NativeMethods_Linux.mmap(new IntPtr(_buffer + adjustedSize), adjustedSize,
NativeMethods_Linux.PROT_READ | NativeMethods_Linux.PROT_WRITE,
NativeMethods_Linux.MAP_SHARED | NativeMethods_Linux.MAP_FIXED, fd, 0);
if (view2 == NativeMethods_Linux.InvalidPtrValue)
{
throw new InvalidOperationException($"Mapping mirrored memory failed. ({Marshal.GetLastPInvokeError()})");
}
_handle = fd;
}
_bufferSize = adjustedSize;
_writeIdx = 0;
_readIdx = 0;
_closed = false;
Writer = new PipeWriter(this);
Reader = new PipeReader(this);
}
private static partial class NativeMethods_Windows
{
private const string Kernel32 = "kernel32.dll";
private const string KernelBase = "kernelbase.dll";
public const IntPtr InvalidHandleValue = -1;
[LibraryImport(Kernel32, SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
public static partial IntPtr CreateFileMappingW(
IntPtr hFile, IntPtr lpFileMappingAttributes, uint flProtect, uint dwMaximumSizeHigh, uint dwMaximumSizeLow,
string lpName
);
[LibraryImport(KernelBase, SetLastError = true)]
public static partial IntPtr MapViewOfFile3(
IntPtr hFileMappingObject, IntPtr processHandle, IntPtr pvBaseAddress, ulong ullOffset, ulong ullSize,
uint allocFlags, uint dwDesiredAccess,
IntPtr hExtendedParameter, int parameterCount
);
[LibraryImport(Kernel32, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool UnmapViewOfFile(IntPtr lpBaseAddress);
[LibraryImport(Kernel32, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool CloseHandle(IntPtr hObject);
[LibraryImport(KernelBase, SetLastError = true)]
public static partial IntPtr VirtualAlloc2(
IntPtr process,
IntPtr address,
ulong size,
uint allocationType,
uint protect,
IntPtr extendedParameters,
uint parameterCount
);
[LibraryImport(Kernel32, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool VirtualFree(IntPtr lpAddress, uint dwSize, uint dwFreeType);
public const uint MEM_PRESERVE_PLACEHOLDER = 0x02;
public const uint MEM_RESERVE = 0x2000;
public const uint MEM_REPLACE_PLACEHOLDER = 0x4000;
public const uint MEM_RELEASE = 0x8000;
public const uint MEM_RESERVE_PLACEHOLDER = 0x40000;
public const uint PAGE_NOACCESS = 0x01;
public const uint PAGE_READWRITE = 0x04;
}
private static partial class NativeMethods_Linux
{
private const string LibC = "libc";
public const IntPtr InvalidPtrValue = -1;
public const int InvalidFileDescriptor = -1;
// For MacOS
[LibraryImport(LibC, SetLastError = true, StringMarshalling = StringMarshalling.Utf8)]
public static partial int shm_open(string name, int oflag, int mode);
[LibraryImport(LibC, SetLastError = true, StringMarshalling = StringMarshalling.Utf8)]
public static partial int shm_unlink(string name);
[LibraryImport(LibC, SetLastError = true, StringMarshalling = StringMarshalling.Utf8)]
public static partial int memfd_create(string name, uint flags);
[LibraryImport(LibC, SetLastError = true)]
public static partial int ftruncate(int fd, int length);
[LibraryImport(LibC, SetLastError = true)]
public static partial int close(int fd);
[LibraryImport(LibC, SetLastError = true)]
public static partial IntPtr mmap(IntPtr addr, ulong length, int prot, int flags, int fd, int offset);
[LibraryImport(LibC, SetLastError = true)]
public static partial int munmap(IntPtr addr, ulong length);
public const int PROT_READ = 0x1;
public const int PROT_WRITE = 0x2;
public const int MAP_PRIVATE = 0x02;
public const int MAP_SHARED = 0x01;
public const int MAP_FIXED = 0x10;
public const int MAP_ANONYMOUS = 0x20;
// Darwin
public const int O_RDWR = 0x2;
public const int O_CREAT = 0x200;
public const int MAP_ANON = 0x1000;
}
private void ReleaseUnmanagedResources()
{
if (_buffer == IntPtr.Zero)
{
return;
}
if (Core.IsWindows)
{
if (_handle != IntPtr.Zero)
{
NativeMethods_Windows.CloseHandle(_handle);
_handle = IntPtr.Zero;
}
if (_buffer != IntPtr.Zero)
{
NativeMethods_Windows.UnmapViewOfFile(_buffer);
NativeMethods_Windows.UnmapViewOfFile(new IntPtr(_buffer + _bufferSize));
}
}
else if (Core.IsLinux || Core.IsDarwin)
{
if (_handle != NativeMethods_Linux.InvalidFileDescriptor)
{
#pragma warning disable CA2020
NativeMethods_Linux.close((int)_handle);
#pragma warning restore CA2020
_handle = NativeMethods_Linux.InvalidFileDescriptor;
}
if (_buffer != IntPtr.Zero)
{
NativeMethods_Linux.munmap(_buffer, _bufferSize);
NativeMethods_Linux.munmap(new IntPtr(_buffer + _bufferSize), _bufferSize);
}
}
_buffer = IntPtr.Zero;
}
public void Dispose()
{
ReleaseUnmanagedResources();
GC.SuppressFinalize(this);
}
~Pipe()
{
ReleaseUnmanagedResources();
}
}
public class EndOfPipeException : IOException
{
public EndOfPipeException(string message) : base(message)
{
}
}

View file

@ -0,0 +1,218 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2025 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SocketHelper.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
namespace Server.Network;
/// <summary>
/// Platform-specific socket utilities for working with raw socket handles.
/// </summary>
public static partial class SocketHelper
{
/// <summary>
/// Gets the remote IP address from a socket handle.
/// </summary>
/// <param name="socket">The socket handle.</param>
/// <returns>The remote IP address, or null if unable to retrieve.</returns>
public static IPAddress GetRemoteAddress(nint socket)
{
if (socket is 0 or -1)
{
return null;
}
try
{
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? GetRemoteAddressWindows(socket)
: GetRemoteAddressUnix(socket);
}
catch
{
return null;
}
}
private static unsafe IPAddress GetRemoteAddressWindows(nint socket)
{
Span<byte> buffer = stackalloc byte[128];
var len = buffer.Length;
fixed (byte* ptr = buffer)
{
if (getpeername(socket, ptr, ref len) != 0)
{
return null;
}
}
return ParseSockAddr(buffer[..len]);
}
private static unsafe IPAddress GetRemoteAddressUnix(nint socket)
{
Span<byte> buffer = stackalloc byte[128];
var len = (uint)buffer.Length;
fixed (byte* ptr = buffer)
{
if (getpeername_unix(socket, ptr, ref len) != 0)
{
return null;
}
}
return ParseSockAddr(buffer[..(int)len]);
}
/// <summary>
/// Gets the local endpoint from a socket handle.
/// </summary>
/// <param name="socket">The socket handle.</param>
/// <returns>The local endpoint, or null if unable to retrieve.</returns>
public static IPEndPoint GetLocalEndPoint(nint socket)
{
if (socket is 0 or -1)
{
return null;
}
try
{
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? GetLocalEndPointWindows(socket)
: GetLocalEndPointUnix(socket);
}
catch
{
return null;
}
}
private static unsafe IPEndPoint GetLocalEndPointWindows(nint socket)
{
Span<byte> buffer = stackalloc byte[128];
var len = buffer.Length;
fixed (byte* ptr = buffer)
{
if (getsockname(socket, ptr, ref len) != 0)
{
return null;
}
}
return ParseSockAddrEndPoint(buffer[..len]);
}
private static unsafe IPEndPoint GetLocalEndPointUnix(nint socket)
{
Span<byte> buffer = stackalloc byte[128];
var len = (uint)buffer.Length;
fixed (byte* ptr = buffer)
{
if (getsockname_unix(socket, ptr, ref len) != 0)
{
return null;
}
}
return ParseSockAddrEndPoint(buffer[..(int)len]);
}
private static IPAddress ParseSockAddr(ReadOnlySpan<byte> buffer)
{
if (buffer.Length < 2)
{
return null;
}
// macOS/BSD: sockaddr has sin_len (1 byte) + sin_family (1 byte)
// Linux: sockaddr has sa_family (2 bytes)
var isBsd = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ||
RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD);
var family = isBsd
? (AddressFamily)buffer[1] // BSD: family is second byte
: (AddressFamily)BitConverter.ToInt16(buffer); // Linux: family is first 2 bytes
if (family == AddressFamily.InterNetwork && buffer.Length >= 8)
{
// IPv4: family (2) + port (2) + addr (4)
return new IPAddress(buffer.Slice(4, 4));
}
if (family == AddressFamily.InterNetworkV6 && buffer.Length >= 28)
{
// IPv6: family (2) + port (2) + flowinfo (4) + addr (16) + scope (4)
return new IPAddress(buffer.Slice(8, 16));
}
return null;
}
private static IPEndPoint ParseSockAddrEndPoint(ReadOnlySpan<byte> buffer)
{
if (buffer.Length < 4)
{
return null;
}
// macOS/BSD: sockaddr has sin_len (1 byte) + sin_family (1 byte)
// Linux: sockaddr has sa_family (2 bytes)
var isBsd = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ||
RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD);
var family = isBsd
? (AddressFamily)buffer[1] // BSD: family is second byte
: (AddressFamily)BitConverter.ToInt16(buffer); // Linux: family is first 2 bytes
// Port is in network byte order (big-endian), at offset 2 on both platforms
var port = (buffer[2] << 8) | buffer[3];
if (family == AddressFamily.InterNetwork && buffer.Length >= 8)
{
// IPv4: family (2) + port (2) + addr (4)
return new IPEndPoint(new IPAddress(buffer.Slice(4, 4)), port);
}
if (family == AddressFamily.InterNetworkV6 && buffer.Length >= 28)
{
// IPv6: family (2) + port (2) + flowinfo (4) + addr (16) + scope (4)
return new IPEndPoint(new IPAddress(buffer.Slice(8, 16)), port);
}
return null;
}
// Windows getpeername
[LibraryImport("ws2_32.dll", SetLastError = true)]
private static unsafe partial int getpeername(nint s, byte* name, ref int namelen);
// Unix/Linux getpeername
[LibraryImport("libc", EntryPoint = "getpeername", SetLastError = true)]
private static unsafe partial int getpeername_unix(nint sockfd, byte* addr, ref uint addrlen);
// Windows getsockname
[LibraryImport("ws2_32.dll", SetLastError = true)]
private static unsafe partial int getsockname(nint s, byte* name, ref int namelen);
// Unix/Linux getsockname
[LibraryImport("libc", EntryPoint = "getsockname", SetLastError = true)]
private static unsafe partial int getsockname_unix(nint sockfd, byte* addr, ref uint addrlen);
}

View file

@ -1,254 +0,0 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2024 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: TcpServer.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Server.Logging;
namespace Server.Network;
public static class TcpServer
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(TcpServer));
// AccountLoginReject BadComm
private static readonly byte[] _socketRejected = [0x82, 0xFF];
public static IPEndPoint[] ListeningAddresses { get; private set; }
public static Socket[] Listeners { get; private set; }
private static IPRateLimiter _ipRateLimiter;
public static void Start()
{
_ipRateLimiter = new IPRateLimiter(10, 10000, 1000, 2.0, 3_600_000, Core.ClosingTokenSource.Token);
HashSet<IPEndPoint> listeningAddresses = [];
List<Socket> listeners = [];
foreach (var ipep in ServerConfiguration.Listeners)
{
var listener = CreateListener(ipep);
if (listener == null)
{
continue;
}
if (ipep.Address.Equals(IPAddress.Any) || ipep.Address.Equals(IPAddress.IPv6Any))
{
listeningAddresses.UnionWith(GetListeningAddresses(ipep));
}
else
{
listeningAddresses.Add(ipep);
}
listeners.Add(listener);
BeginAcceptingSockets(listener);
}
foreach (var ipep in listeningAddresses)
{
logger.Information("Listening: {Address}", ipep);
}
ListeningAddresses = listeningAddresses.ToArray();
Listeners = listeners.ToArray();
}
public static void Shutdown()
{
foreach (var listener in Listeners)
{
listener.Close();
}
}
public static IEnumerable<IPEndPoint> GetListeningAddresses(IPEndPoint ipep) =>
NetworkInterface.GetAllNetworkInterfaces().SelectMany(adapter =>
adapter.GetIPProperties().UnicastAddresses
.Where(uip => ipep.AddressFamily == uip.Address.AddressFamily)
.Select(uip => new IPEndPoint(uip.Address, ipep.Port))
);
public static Socket CreateListener(IPEndPoint ipep)
{
var listener = new Socket(ipep.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
{
LingerState = new LingerOption(false, 0),
ExclusiveAddressUse = true,
NoDelay = true,
Blocking = false,
SendBufferSize = 64 * 1024,
ReceiveBufferSize = 64 * 1024
};
try
{
listener.Bind(ipep);
listener.Listen(256);
return listener;
}
catch (SocketException se)
{
// WSAEADDRINUSE
if (se.ErrorCode == 10048)
{
logger.Warning("Listener: {Address} Exception: {Reason}", ipep, "Currently in use");
}
// WSAEADDRNOTAVAIL
else if (se.ErrorCode == 10049)
{
logger.Warning("Listener {Address} Exception: {Reason}", ipep, "Unavailable");
}
else
{
logger.Warning(se, "Listener {Address} Exception: {Reason}", ipep, se.Message);
}
}
return null;
}
private static async ValueTask BeginAcceptingSockets(Socket listener)
{
while (!Core.Closing)
{
try
{
var socket = await listener.AcceptAsync(Core.ClosingTokenSource.Token);
var remoteIP = ((IPEndPoint)socket.RemoteEndPoint)!.Address;
if (!_ipRateLimiter.Verify(remoteIP, out var totalAttempts))
{
logger.Debug("{Address} Past IP limit threshold ({TotalAttempts})", remoteIP, totalAttempts);
}
else if (Firewall.IsBlocked(remoteIP))
{
logger.Debug("{Address} Firewalled", remoteIP);
}
else
{
_ = Task.Run(() => ProcessSocketConnection(socket), Core.ClosingTokenSource.Token);
}
}
catch
{
// ignored
}
}
}
[ThreadStatic]
private static byte[] _firstBytes;
private static async ValueTask ProcessSocketConnection(Socket socket)
{
_firstBytes ??= GC.AllocateUninitializedArray<byte>(128);
using var cts = CancellationTokenSource.CreateLinkedTokenSource(Core.ClosingTokenSource.Token);
cts.CancelAfter(TimeSpan.FromMilliseconds(500));
try
{
var bytesRead = await socket.ReceiveAsync(_firstBytes, SocketFlags.Peek, cts.Token);
var isValid =
// Sometimes when newer clients are connecting to the game server the first 4 bytes are sent separately
bytesRead == 4 ||
// Support Freeshard Protocol (UOGateway)
bytesRead == 8 &&
BinaryPrimitives.ReadUInt32BigEndian(_firstBytes.AsSpan(4)) is 0xF10004FF or 0xF10004FE ||
// Older clients only send the 4 byte seed first then 0x80
(UOClient.MinRequired == null || UOClient.MinRequired < ClientVersion.Version6050) &&
bytesRead >= 66 && _firstBytes[4] == 0x80 ||
// Newer clients
(UOClient.MaxRequired == null || UOClient.MaxRequired >= ClientVersion.Version6050) && (
// Account Login - 0xEF + 0x80 (83 bytes)
bytesRead >= 83 && _firstBytes[0] == 0xEF && _firstBytes[21] == 0x80 ||
bytesRead == 21 && _firstBytes[0] == 0xEF ||
// Game Login - 4 bytes + 0x91 (69 bytes)
bytesRead >= 69 && _firstBytes[4] == 0x91
);
// TODO: Validate client version is v4 -> v7 for 0xEF packet
// TODO: Validate Account Login seed matches Game Login seed
// TODO: Validate AuthId for 0x91 packet
// TODO: Validate username is ascii and not empty
// TODO: Validate password is ascii and not empty
if (isValid)
{
var args = new SocketConnectEventArgs(socket);
EventSink.InvokeSocketConnect(args);
if (args.AllowConnection)
{
Core.LoopContext.Post(() => _ = new NetState(socket), EventLoopContext.Priority.High);
return;
}
logger.Debug("{Address} Rejected by socket handler", ((IPEndPoint)socket.RemoteEndPoint)!.Address);
cts.TryReset();
cts.CancelAfter(TimeSpan.FromMilliseconds(500));
await socket.SendAsync(_socketRejected, SocketFlags.None, cts.Token);
CloseSocket(socket);
}
else
{
ForceCloseSocket(socket);
}
}
catch
{
ForceCloseSocket(socket);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void CloseSocket(Socket socket)
{
try
{
socket.Shutdown(SocketShutdown.Both);
}
finally
{
socket.Close();
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void ForceCloseSocket(Socket socket)
{
try
{
socket.Disconnect(false);
}
finally
{
socket.Close(0);
}
}
}

View file

@ -21,12 +21,12 @@
<Delete Files="..\..\Distribution\$(AssemblyName).runtimeconfig.json" ContinueOnError="true" />
<Delete Files="..\..\Distribution\ModernUO.Serialization.Annotations.dll" ContinueOnError="true" />
<Delete Files="..\..\Distribution\CommunityToolkit.HighPerformance.dll" ContinueOnError="true" />
<Delete Files="..\..\Distribution\PollGroup.dll" ContinueOnError="true" />
<Delete Files="..\..\Distribution\IORingGroup.dll" ContinueOnError="true" />
<Delete Files="..\..\Distribution\ioring.dll" ContinueOnError="true" />
<Delete Files="..\..\Distribution\ref\$(AssemblyName).dll" ContinueOnError="true" />
<Delete Files="..\..\Distribution\Serilog.dll" ContinueOnError="true" />
<Delete Files="..\..\Distribution\Serilog.Sinks.Async.dll" ContinueOnError="true" />
<Delete Files="..\..\Distribution\Serilog.Sinks.Console.dll" ContinueOnError="true" />
<Delete Files="..\..\Distribution\wepoll.dll" ContinueOnError="true" />
<Delete Files="..\..\Distribution\LibDeflate.Bindings.dll" ContinueOnError="true" />
<Delete Files="..\..\Distribution\libdeflate.dll" ContinueOnError="true" />
<Delete Files="..\..\Distribution\libdeflate.dylib" ContinueOnError="true" />
@ -34,9 +34,9 @@
</Target>
<ItemGroup>
<ProjectReference Include="..\Logger\Logger.csproj" />
<PackageReference Include="IORingGroup" Version="1.0.0" />
<PackageReference Include="CommunityToolkit.HighPerformance" Version="8.4.0" />
<PackageReference Include="LibDeflate.Bindings" Version="1.0.2.120" />
<PackageReference Include="PollGroup" Version="1.7.1" />
<PackageReference Include="System.IO.Hashing" Version="10.0.2" />
<PackageReference Include="ModernUO.Serialization.Annotations" Version="2.14.0" />

View file

@ -66,10 +66,22 @@ public struct BitMask256
switch (segment)
{
case 0: Bits0 |= mask; break;
case 1: Bits1 |= mask; break;
case 2: Bits2 |= mask; break;
case 3: Bits3 |= mask; break;
case 0:
{
Bits0 |= mask; break;
}
case 1:
{
Bits1 |= mask; break;
}
case 2:
{
Bits2 |= mask; break;
}
case 3:
{
Bits3 |= mask; break;
}
}
}
@ -90,10 +102,22 @@ public struct BitMask256
switch (segment)
{
case 0: Bits0 &= ~mask; break;
case 1: Bits1 &= ~mask; break;
case 2: Bits2 &= ~mask; break;
case 3: Bits3 &= ~mask; break;
case 0:
{
Bits0 &= ~mask; break;
}
case 1:
{
Bits1 &= ~mask; break;
}
case 2:
{
Bits2 &= ~mask; break;
}
case 3:
{
Bits3 &= ~mask; break;
}
}
}

View file

@ -26,6 +26,9 @@ public class UOContentFixture : ICollectionFixture<UOContentFixture>, IDisposabl
// Load Skills
SkillsInfo.Configure();
// Configure networking (initializes RingSocketManager for tests)
Server.Network.NetState.Configure();
// Configure / Initialize
TestMapDefinitions.ConfigureTestMapDefinitions();

View file

@ -5,7 +5,7 @@ using Xunit;
namespace UOContent.Tests;
[Collection("Sequential Server Tests")]
[Collection("Sequential UOContent Tests")]
public class ChatPacketTests
{
[Theory]
@ -18,7 +18,7 @@ public class ChatPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendChatMessage(lang, number, param1, param2);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -19,7 +19,7 @@ public class TestHelpTopicPacket
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendDisplayHelpTopic(topic, display);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -73,7 +73,7 @@ public class MLQuestPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendRaceChanger(female, race);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -85,7 +85,7 @@ public class MLQuestPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendCloseRaceChanger();
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -19,7 +19,7 @@ public class PartyPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendPartyRemoveMember(m);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -40,7 +40,7 @@ public class PartyPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendPartyRemoveMember(member.Serial, p);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -61,7 +61,7 @@ public class PartyPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendPartyMemberList(p);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -78,7 +78,7 @@ public class PartyPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendPartyTextMessage(serial, text, toAll);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -92,7 +92,7 @@ public class PartyPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendPartyInvitation(m);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -7,7 +7,7 @@ using Xunit;
namespace UOContent.Tests;
[Collection("Sequential Server Tests")]
[Collection("Sequential UOContent Tests")]
public class CharacterStatuePacketTests
{
[Theory]
@ -19,7 +19,7 @@ public class CharacterStatuePacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendStatueAnimation((Serial)s, status, anim, frame);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -50,7 +50,7 @@ public class BookPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendBookCover(m, book);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -87,7 +87,7 @@ public class BookPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendBookContent(book);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -23,7 +23,7 @@ public class BulletinBoardPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendBBDisplayBoard(bb);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -50,7 +50,7 @@ public class BulletinBoardPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendBBMessage(bb, msg, content);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -19,7 +19,7 @@ public class MahjongPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMahjongJoinGame(game);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -39,7 +39,7 @@ public class MahjongPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMahjongPlayersInfo(game, m);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -57,7 +57,7 @@ public class MahjongPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMahjongGeneralInfo(game);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -77,7 +77,7 @@ public class MahjongPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMahjongTilesInfo(game, m);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -97,7 +97,7 @@ public class MahjongPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMahjongTileInfo(game.Tiles[0], m);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -111,7 +111,7 @@ public class MahjongPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMahjongRelieve(game);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -24,7 +24,7 @@ public class TestMapItemPackets
(Packet)new MapDetailsNew(mapItem) : new MapDetails(mapItem)).Compile();
ns.SendMapDetails(mapItem);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -42,7 +42,7 @@ public class TestMapItemPackets
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMapCommand(mapItem, command, x, y, number > 0);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -26,7 +26,7 @@ public class CorpsePacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendCorpseEquip(m, c);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -50,7 +50,7 @@ public class CorpsePacketTests
ns.SendCorpseContent(m, c);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -20,7 +20,7 @@ public class WeaponAbilityPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendToggleSpecialAbility(abilityId, active);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -32,7 +32,7 @@ public class WeaponAbilityPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendClearWeaponAbility();
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -59,7 +59,7 @@ public class BoatPacketTests
ns.SendMoveBoatHS(boat, list, d, speed, xOffset, yOffset);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -103,7 +103,7 @@ public class BoatPacketTests
ns.SendDisplayBoatHS(beholder, boat);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}

View file

@ -20,7 +20,7 @@ public class HousePacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendBeginHouseCustomization((Serial)serial);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -33,7 +33,7 @@ public class HousePacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendEndHouseCustomization((Serial)serial);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -47,7 +47,7 @@ public class HousePacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendDesignStateGeneral((Serial)serial, revision);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}

View file

@ -14,7 +14,7 @@ public class ArrowPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendCancelArrow(0, 0, Serial.Zero);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -29,7 +29,7 @@ public class ArrowPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendSetArrow(x, y, Serial.Zero);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -47,7 +47,7 @@ public class ArrowPacketTests
ns.ProtocolChanges = ProtocolChanges.HighSeas;
ns.SendCancelArrow(x, y, serial);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -65,7 +65,7 @@ public class ArrowPacketTests
ns.ProtocolChanges = ProtocolChanges.HighSeas;
ns.SendSetArrow(x, y, serial);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -24,7 +24,7 @@ public class BuffIconPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendAddBuffPacket((Serial)mob, iconID, titleCliloc, secondaryCliloc, args, (int)timeSpan.TotalMilliseconds);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -38,7 +38,7 @@ public class BuffIconPacketTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendRemoveBuffPacket(m, buffIcon);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -27,7 +27,7 @@ public class SkillPacketsTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendSkillChange(skill);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
@ -45,7 +45,7 @@ public class SkillPacketsTests
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendSkillsUpdate(skills);
var result = ns.SendPipe.Reader.AvailableToRead();
var result = ns.SendBuffer.GetReadSpan();
AssertThat.Equal(result, expected);
}
}

View file

@ -79,14 +79,14 @@ namespace Server.Misc
{
var ns = e.State;
var ipep = (IPEndPoint)ns.Connection?.LocalEndPoint;
if (ipep == null)
var localEndPoint = ns.LocalEndPoint;
if (localEndPoint == null)
{
return;
}
var localAddress = ipep.Address;
var localPort = ipep.Port;
var localAddress = localEndPoint.Address;
var localPort = localEndPoint.Port;
if (_useServerListingAddressConfig)
{
@ -94,9 +94,8 @@ namespace Server.Misc
}
else if (localAddress.IsPrivateNetwork())
{
ipep = (IPEndPoint)ns.Connection.RemoteEndPoint;
if (ipep == null || !ipep.Address.IsPrivateNetwork() && _publicAddress != null)
// Check if client is from a public network
if (!ns.Address.IsPrivateNetwork() && _publicAddress != null)
{
localAddress = _publicAddress;
}

View file

@ -384,7 +384,6 @@ public static class IncomingAccountPackets
// Comment out these lines to turn off huffman compression
state.CompressionEnabled = true;
state.PacketEncoder ??= NetworkCompression.Compress;
state.SendSupportedFeature();
state.SendCharacterList();

View file

@ -69,7 +69,7 @@ namespace Server.Misc
}
}
private static bool HasDisconnected(Mobile m) => m.NetState?.Connection == null;
private static bool HasDisconnected(Mobile m) => m.NetState is not { IsConnected: true };
private static LocationInfo GetRandomDestination() => m_Destinations.RandomElement();

View file

@ -71,13 +71,13 @@ ModernUO [![Discord](https://img.shields.io/discord/751317910504603701?logo=disc
dnf upgrade --refresh -y
# CentOS does not come with EPEL enabled
dnf install -y epel-release epel-next-release
dnf install -y findutils libicu libdeflate-devel zstd libargon2-devel
dnf install -y findutils libicu libdeflate-devel zstd libargon2-devel liburing-devel
```
### Ubuntu, Debian, etc
```shell
apt-get update -y
apt-get install -y libicu-dev libdeflate-dev zstd libargon2-dev
apt-get install -y libicu-dev libdeflate-dev zstd libargon2-dev liburing-dev
```
## OSX Requirements

View file

@ -1,4 +1,4 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "0.15.4"
"version": "0.15.5"
}