ModernUO/Projects/UOContent/Network/Packets/IncomingTargetingPackets.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

145 lines
4.8 KiB
C#

/*************************************************************************
* ModernUO *
* Copyright 2019-2023 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: IncomingTargetingPackets.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 Server.Diagnostics;
using Server.Targeting;
namespace Server.Network;
public static class IncomingTargetingPackets
{
public static unsafe void Configure()
{
IncomingPackets.Register(0x6C, 19, true, &TargetResponse);
}
public static void TargetResponse(NetState state, SpanReader reader, int packetLength)
{
int type = reader.ReadByte();
var targetID = reader.ReadInt32();
int flags = reader.ReadByte();
var serial = (Serial)reader.ReadUInt32();
int x = reader.ReadInt16();
int y = reader.ReadInt16();
reader.ReadByte();
int z = reader.ReadSByte();
int graphic = reader.ReadUInt16();
if (targetID == unchecked((int)0xDEADBEEF))
{
return;
}
var from = state.Mobile;
var t = from.Target;
if (t == null)
{
return;
}
var prof = TargetProfile.Acquire(t.GetType());
prof?.Start();
try
{
if (x == -1 && y == -1 && !serial.IsValid)
{
// User pressed escape
t.Cancel(from, TargetCancelType.Canceled);
}
else if (t.TargetID == targetID)
{
object toTarget;
if (type == 1)
{
if (graphic == 0)
{
toTarget = new LandTarget(new Point3D(x, y, z), from.Map);
}
else
{
var map = from.Map;
if (map == null || map == Map.Internal)
{
t.Cancel(from, TargetCancelType.Canceled);
return;
}
else
{
var tiles = map.Tiles.GetStaticTiles(x, y, !t.DisallowMultis);
var valid = false;
if (state.HighSeas)
{
var id = TileData.ItemTable[graphic & TileData.MaxItemValue];
if (id.Surface)
{
z -= id.Height;
}
}
int hue = 0;
for (var i = 0; !valid && i < tiles.Length; ++i)
{
var tile = tiles[i];
if (tile.Z == z && tile.ID == graphic)
{
valid = true;
hue = tile.Hue;
}
}
if (!valid)
{
t.Cancel(from, TargetCancelType.Canceled);
return;
}
else
{
toTarget = new StaticTarget(new Point3D(x, y, z), graphic, hue);
}
}
}
}
else if (serial.IsMobile)
{
toTarget = World.FindMobile(serial);
}
else if (serial.IsItem)
{
toTarget = World.FindItem(serial);
}
else
{
t.Cancel(from, TargetCancelType.Canceled);
return;
}
t.Invoke(from, toTarget);
}
}
finally
{
prof?.Finish();
}
}
}