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
This commit is contained in:
parent
629a5008c3
commit
10a69bf754
83 changed files with 958 additions and 2432 deletions
|
|
@ -13,6 +13,7 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
*************************************************************************/
|
||||
|
||||
using System.Buffers;
|
||||
using Server.ContextMenus;
|
||||
|
||||
namespace Server.Network;
|
||||
|
|
@ -59,16 +60,16 @@ public static class IncomingExtendedCommandPackets
|
|||
RegisterExtended(0x32, true, &ToggleFlying);
|
||||
}
|
||||
|
||||
private static void UnhandledBF(NetState state, CircularBufferReader reader, int packetLength)
|
||||
private static void UnhandledBF(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
}
|
||||
|
||||
public static void Empty(NetState state, CircularBufferReader reader, int packetLength)
|
||||
public static void Empty(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
}
|
||||
|
||||
public static unsafe void RegisterExtended(int packetID, bool ingame,
|
||||
delegate*<NetState, CircularBufferReader, int, void> onReceive)
|
||||
delegate*<NetState, SpanReader, int, void> onReceive)
|
||||
{
|
||||
if (packetID is >= 0 and < 0x100)
|
||||
{
|
||||
|
|
@ -87,7 +88,7 @@ public static class IncomingExtendedCommandPackets
|
|||
}
|
||||
}
|
||||
|
||||
public static unsafe void ExtendedCommand(NetState state, CircularBufferReader reader, int packetLength)
|
||||
public static unsafe void ExtendedCommand(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
int packetId = reader.ReadUInt16();
|
||||
|
||||
|
|
@ -95,7 +96,7 @@ public static class IncomingExtendedCommandPackets
|
|||
|
||||
if (ph == null)
|
||||
{
|
||||
reader.Trace(state);
|
||||
state.Trace(reader.Buffer);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -116,14 +117,13 @@ public static class IncomingExtendedCommandPackets
|
|||
}
|
||||
}
|
||||
|
||||
public static void ScreenSize(NetState state, CircularBufferReader reader, int packetLength)
|
||||
public static void ScreenSize(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
var width = reader.ReadInt32();
|
||||
var unk = reader.ReadInt32();
|
||||
}
|
||||
|
||||
// TODO: Move out of the core
|
||||
public static void PartyMessage(NetState state, CircularBufferReader reader, int packetLength)
|
||||
public static void PartyMessage(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
if (state.Mobile == null)
|
||||
{
|
||||
|
|
@ -154,22 +154,22 @@ public static class IncomingExtendedCommandPackets
|
|||
PartyMessage_Decline(state, reader, packetLength);
|
||||
break;
|
||||
default:
|
||||
reader.Trace(state);
|
||||
state.Trace(reader.Buffer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static void PartyMessage_AddMember(NetState state, CircularBufferReader reader, int packetLength)
|
||||
public static void PartyMessage_AddMember(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
PartyCommands.Handler?.OnAdd(state.Mobile);
|
||||
}
|
||||
|
||||
public static void PartyMessage_RemoveMember(NetState state, CircularBufferReader reader, int packetLength)
|
||||
public static void PartyMessage_RemoveMember(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
PartyCommands.Handler?.OnRemove(state.Mobile, World.FindMobile((Serial)reader.ReadUInt32()));
|
||||
}
|
||||
|
||||
public static void PartyMessage_PrivateMessage(NetState state, CircularBufferReader reader, int packetLength)
|
||||
public static void PartyMessage_PrivateMessage(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
PartyCommands.Handler?.OnPrivateMessage(
|
||||
state.Mobile,
|
||||
|
|
@ -178,27 +178,27 @@ public static class IncomingExtendedCommandPackets
|
|||
);
|
||||
}
|
||||
|
||||
public static void PartyMessage_PublicMessage(NetState state, CircularBufferReader reader, int packetLength)
|
||||
public static void PartyMessage_PublicMessage(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
PartyCommands.Handler?.OnPublicMessage(state.Mobile, reader.ReadBigUniSafe());
|
||||
}
|
||||
|
||||
public static void PartyMessage_SetCanLoot(NetState state, CircularBufferReader reader, int packetLength)
|
||||
public static void PartyMessage_SetCanLoot(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
PartyCommands.Handler?.OnSetCanLoot(state.Mobile, reader.ReadBoolean());
|
||||
}
|
||||
|
||||
public static void PartyMessage_Accept(NetState state, CircularBufferReader reader, int packetLength)
|
||||
public static void PartyMessage_Accept(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
PartyCommands.Handler?.OnAccept(state.Mobile, World.FindMobile((Serial)reader.ReadUInt32()));
|
||||
}
|
||||
|
||||
public static void PartyMessage_Decline(NetState state, CircularBufferReader reader, int packetLength)
|
||||
public static void PartyMessage_Decline(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
PartyCommands.Handler?.OnDecline(state.Mobile, World.FindMobile((Serial)reader.ReadUInt32()));
|
||||
}
|
||||
|
||||
public static void Animate(NetState state, CircularBufferReader reader, int packetLength)
|
||||
public static void Animate(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
var from = state.Mobile;
|
||||
|
||||
|
|
@ -222,7 +222,7 @@ public static class IncomingExtendedCommandPackets
|
|||
}
|
||||
}
|
||||
|
||||
public static void CastSpell(NetState state, CircularBufferReader reader, int packetLength)
|
||||
public static void CastSpell(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
var from = state.Mobile;
|
||||
|
||||
|
|
@ -237,12 +237,12 @@ public static class IncomingExtendedCommandPackets
|
|||
EventSink.InvokeCastSpellRequest(from, spellID, spellbook);
|
||||
}
|
||||
|
||||
public static void ToggleFlying(NetState state, CircularBufferReader reader, int packetLength)
|
||||
public static void ToggleFlying(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
state.Mobile?.ToggleFlying();
|
||||
}
|
||||
|
||||
public static void StunRequest(NetState state, CircularBufferReader reader, int packetLength)
|
||||
public static void StunRequest(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
var from = state.Mobile;
|
||||
|
||||
|
|
@ -254,7 +254,7 @@ public static class IncomingExtendedCommandPackets
|
|||
EventSink.InvokeStunRequest(from);
|
||||
}
|
||||
|
||||
public static void DisarmRequest(NetState state, CircularBufferReader reader, int packetLength)
|
||||
public static void DisarmRequest(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
var from = state.Mobile;
|
||||
|
||||
|
|
@ -266,7 +266,7 @@ public static class IncomingExtendedCommandPackets
|
|||
EventSink.InvokeDisarmRequest(from);
|
||||
}
|
||||
|
||||
public static void StatLockChange(NetState state, CircularBufferReader reader, int packetLength)
|
||||
public static void StatLockChange(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
var from = state.Mobile;
|
||||
|
||||
|
|
@ -297,12 +297,12 @@ public static class IncomingExtendedCommandPackets
|
|||
}
|
||||
}
|
||||
|
||||
public static void CloseStatus(NetState state, CircularBufferReader reader, int packetLength)
|
||||
public static void CloseStatus(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
var serial = (Serial)reader.ReadUInt32();
|
||||
}
|
||||
|
||||
public static void Language(NetState state, CircularBufferReader reader, int packetLength)
|
||||
public static void Language(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
var from = state.Mobile;
|
||||
|
||||
|
|
@ -314,7 +314,7 @@ public static class IncomingExtendedCommandPackets
|
|||
from.Language = reader.ReadAscii(4);
|
||||
}
|
||||
|
||||
public static void QueryProperties(NetState state, CircularBufferReader reader, int packetLength)
|
||||
public static void QueryProperties(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
if (!ObjectPropertyList.Enabled)
|
||||
{
|
||||
|
|
@ -346,7 +346,7 @@ public static class IncomingExtendedCommandPackets
|
|||
}
|
||||
}
|
||||
|
||||
public static void ContextMenuResponse(NetState state, CircularBufferReader reader, int packetLength)
|
||||
public static void ContextMenuResponse(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
var from = state.Mobile;
|
||||
|
||||
|
|
@ -402,7 +402,7 @@ public static class IncomingExtendedCommandPackets
|
|||
}
|
||||
}
|
||||
|
||||
public static void ContextMenuRequest(NetState state, CircularBufferReader reader, int packetLength)
|
||||
public static void ContextMenuRequest(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
var from = state.Mobile;
|
||||
var target = World.FindEntity((Serial)reader.ReadUInt32());
|
||||
|
|
@ -437,7 +437,7 @@ public static class IncomingExtendedCommandPackets
|
|||
}
|
||||
}
|
||||
|
||||
public static void BandageTarget(NetState state, CircularBufferReader reader, int packetLength)
|
||||
public static void BandageTarget(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
var from = state.Mobile;
|
||||
|
||||
|
|
@ -472,21 +472,21 @@ public static class IncomingExtendedCommandPackets
|
|||
}
|
||||
}
|
||||
|
||||
public static void TargetedSpell(NetState state, CircularBufferReader reader, int packetLength)
|
||||
public static void TargetedSpell(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
var spellId = (short)(reader.ReadInt16() - 1); // zero based;
|
||||
|
||||
EventSink.InvokeTargetedSpell(state.Mobile, World.FindEntity((Serial)reader.ReadUInt32()), spellId);
|
||||
}
|
||||
|
||||
public static void TargetedSkillUse(NetState state, CircularBufferReader reader, int packetLength)
|
||||
public static void TargetedSkillUse(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
var skillId = reader.ReadInt16();
|
||||
|
||||
EventSink.InvokeTargetedSkillUse(state.Mobile, World.FindEntity((Serial)reader.ReadUInt32()), skillId);
|
||||
}
|
||||
|
||||
public static void TargetByResourceMacro(NetState state, CircularBufferReader reader, int packetLength)
|
||||
public static void TargetByResourceMacro(NetState state, SpanReader reader, int packetLength)
|
||||
{
|
||||
var serial = (Serial)reader.ReadUInt32();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue