ModernUO/Projects/UOContent/Network/Packets/IncomingVendorPackets.cs
Kamron Batman 10a69bf754
feat: Adds a memory mirrored ring buffer for networking. (#1533)
## Breaking Changes

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

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

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

## Summary

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

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


### Bug Fixes

- [X] Fixes bad fixed length string parsing
2023-10-09 00:57:53 -07:00

110 lines
3.6 KiB
C#

/*************************************************************************
* ModernUO *
* Copyright 2019-2023 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: IncomingVendorPackets.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.Buffers;
using System.Collections.Generic;
namespace Server.Network;
public static class IncomingVendorPackets
{
public static unsafe void Configure()
{
IncomingPackets.Register(0x3B, 0, true, &VendorBuyReply);
IncomingPackets.Register(0x9F, 0, true, &VendorSellReply);
}
public static void VendorBuyReply(NetState state, SpanReader reader, int packetLength)
{
var vendor = World.FindMobile((Serial)reader.ReadUInt32());
if (vendor == null)
{
return;
}
var flag = reader.ReadByte();
if (!vendor.Deleted && Utility.InRange(vendor.Location, state.Mobile.Location, 10) && flag == 0x02)
{
var msgSize = packetLength - 8; // Remaining bytes
if (msgSize / 7 > 100)
{
return;
}
var buyList = new List<BuyItemResponse>(msgSize / 7);
while (msgSize > 0)
{
var layer = reader.ReadByte();
var serial = (Serial)reader.ReadUInt32();
int amount = reader.ReadInt16();
buyList.Add(new BuyItemResponse(serial, amount));
msgSize -= 7;
}
if (buyList.Count <= 0 || (vendor as IVendor)?.OnBuyItems(state.Mobile, buyList) != true)
{
return;
}
}
state.SendEndVendorBuy(vendor.Serial);
}
public static void VendorSellReply(NetState state, SpanReader reader, int packetLength)
{
var serial = (Serial)reader.ReadUInt32();
var vendor = World.FindMobile(serial);
if (vendor == null)
{
return;
}
if (vendor.Deleted || !Utility.InRange(vendor.Location, state.Mobile.Location, 10))
{
state.SendEndVendorSell(vendor.Serial);
return;
}
int count = reader.ReadUInt16();
if (count >= 100 || reader.Remaining != count * 6)
{
return;
}
var sellList = new List<SellItemResponse>(count);
for (var i = 0; i < count; i++)
{
var item = World.FindItem((Serial)reader.ReadUInt32());
int amount = reader.ReadInt16();
if (item != null && amount > 0)
{
sellList.Add(new SellItemResponse(item, amount));
}
}
if (sellList.Count > 0 && vendor is IVendor v && v.OnSellItems(state.Mobile, sellList))
{
state.SendEndVendorSell(vendor.Serial);
}
}
}