fix: Fixes packet length checks (#953)

Fixes an issue with DropReq where an old client was sending in 14 bytes, but the server was expecting 15 bytes.

To fix this we introduced a new packet handler, `ContainerGridPacketHandler` and changed the code to determine the length of the packet dynamically using `GetLength(NetState)`.

Also fixed throttling so dropped packets are properly skipped.
This commit is contained in:
Kamron Batman 2022-03-04 12:32:25 -08:00 committed by GitHub
parent 1f4162288e
commit 58b907d39e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 1077 additions and 1126 deletions

View file

@ -1,8 +1,8 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2020 - ModernUO Development Team *
* Copyright 2019-2022 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: ISocket.cs *
* File: ContainerGridPacketHandler.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 *
@ -13,32 +13,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace Server.Network;
namespace Server.Network
public class ContainerGridPacketHandler : PacketHandler
{
public interface ISocket
public ContainerGridPacketHandler(int packetID, int length, bool ingame, OnPacketReceive onReceive)
: base(packetID, length, ingame, onReceive)
{
public IntPtr Handle { get; }
public EndPoint LocalEndPoint { get; }
public EndPoint RemoteEndPoint { get; }
public Task<int> SendAsync(IList<ArraySegment<byte>> buffer, SocketFlags flags);
public int Send(IList<ArraySegment<byte>> buffer, SocketFlags flags);
public Task<int> ReceiveAsync(IList<ArraySegment<byte>> buffer, SocketFlags flags);
public int Receive(IList<ArraySegment<byte>> buffers, SocketFlags flags);
public void Shutdown(SocketShutdown how);
public void Close();
}
public override int GetLength(NetState ns) => base.GetLength(ns) + (ns.ContainerGridLines ? 1 : 0);
}

File diff suppressed because it is too large Load diff

View file

@ -1,27 +1,43 @@
namespace Server.Network
/*************************************************************************
* ModernUO *
* Copyright 2019-2022 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: PacketHandler.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/>. *
*************************************************************************/
namespace Server.Network;
public delegate void OnPacketReceive(NetState state, CircularBufferReader reader, int packetLength);
public delegate bool ThrottlePacketCallback(int packetId, NetState state, out bool drop);
public class PacketHandler
{
public delegate void OnPacketReceive(NetState state, CircularBufferReader reader, ref int packetLength);
private int _length;
public delegate bool ThrottlePacketCallback(int packetId, NetState state, out bool drop);
public class PacketHandler
public PacketHandler(int packetID, int length, bool ingame, OnPacketReceive onReceive)
{
public PacketHandler(int packetID, int length, bool ingame, OnPacketReceive onReceive)
{
PacketID = packetID;
Length = length;
Ingame = ingame;
OnReceive = onReceive;
}
public int PacketID { get; }
public int Length { get; }
public OnPacketReceive OnReceive { get; }
public ThrottlePacketCallback ThrottleCallback { get; set; }
public bool Ingame { get; }
_length = length;
PacketID = packetID;
Ingame = ingame;
OnReceive = onReceive;
}
public int PacketID { get; }
public virtual int GetLength(NetState ns) => _length;
public OnPacketReceive OnReceive { get; }
public ThrottlePacketCallback ThrottleCallback { get; set; }
public bool Ingame { get; }
}

View file

@ -55,7 +55,7 @@ public static class IncomingAccountPackets
IncomingPackets.Register(0xF8, 106, false, CreateCharacter);
}
public static void CreateCharacter(NetState state, CircularBufferReader reader, ref int packetLength)
public static void CreateCharacter(NetState state, CircularBufferReader reader, int packetLength)
{
reader.Seek(9, SeekOrigin.Current);
/*
@ -185,7 +185,7 @@ public static class IncomingAccountPackets
}
}
public static void DeleteCharacter(NetState state, CircularBufferReader reader, ref int packetLength)
public static void DeleteCharacter(NetState state, CircularBufferReader reader, int packetLength)
{
reader.Seek(30, SeekOrigin.Current);
var index = reader.ReadInt32();
@ -193,24 +193,24 @@ public static class IncomingAccountPackets
EventSink.InvokeDeleteRequest(state, index);
}
public static void AccountID(NetState state, CircularBufferReader reader, ref int packetLength)
public static void AccountID(NetState state, CircularBufferReader reader, int packetLength)
{
}
public static void AssistVersion(NetState state, CircularBufferReader reader, ref int packetLength)
public static void AssistVersion(NetState state, CircularBufferReader reader, int packetLength)
{
var unk = reader.ReadInt32();
var av = reader.ReadAscii();
}
public static void ClientVersion(NetState state, CircularBufferReader reader, ref int packetLength)
public static void ClientVersion(NetState state, CircularBufferReader reader, int packetLength)
{
var version = state.Version = new CV(reader.ReadAscii());
EventSink.InvokeClientVersionReceived(state, version);
}
public static void ClientType(NetState state, CircularBufferReader reader, ref int packetLength)
public static void ClientType(NetState state, CircularBufferReader reader, int packetLength)
{
reader.ReadUInt16();
@ -220,7 +220,7 @@ public static class IncomingAccountPackets
EventSink.InvokeClientVersionReceived(state, version);
}
public static void PlayCharacter(NetState state, CircularBufferReader reader, ref int packetLength)
public static void PlayCharacter(NetState state, CircularBufferReader reader, int packetLength)
{
reader.Seek(4, SeekOrigin.Current); // 0xEDEDEDED
@ -359,7 +359,7 @@ public static class IncomingAccountPackets
return authID;
}
public static void GameLogin(NetState state, CircularBufferReader reader, ref int packetLength)
public static void GameLogin(NetState state, CircularBufferReader reader, int packetLength)
{
// TODO: Connection throttling
@ -413,7 +413,7 @@ public static class IncomingAccountPackets
}
}
public static void PlayServer(NetState state, CircularBufferReader reader, ref int packetLength)
public static void PlayServer(NetState state, CircularBufferReader reader, int packetLength)
{
int index = reader.ReadInt16();
var info = state.ServerInfo;
@ -434,7 +434,7 @@ public static class IncomingAccountPackets
}
}
public static void LoginServerSeed(NetState state, CircularBufferReader reader, ref int packetLength)
public static void LoginServerSeed(NetState state, CircularBufferReader reader, int packetLength)
{
state._seed = reader.ReadInt32();
state.Seeded = true;
@ -454,7 +454,7 @@ public static class IncomingAccountPackets
state.Version = new ClientVersion(clientMaj, clientMin, clientRev, clientPat);
}
public static void AccountLogin(NetState state, CircularBufferReader reader, ref int packetLength)
public static void AccountLogin(NetState state, CircularBufferReader reader, int packetLength)
{
// TODO: Throttle Connection

View file

@ -27,7 +27,7 @@ public static class IncomingEntityPackets
IncomingPackets.Register(0xD6, 0, true, BatchQueryProperties);
}
public static void ObjectHelpRequest(NetState state, CircularBufferReader reader, ref int packetLength)
public static void ObjectHelpRequest(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;
@ -56,7 +56,7 @@ public static class IncomingEntityPackets
}
}
public static void UseReq(NetState state, CircularBufferReader reader, ref int packetLength)
public static void UseReq(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;
@ -100,7 +100,7 @@ public static class IncomingEntityPackets
}
}
public static void LookReq(NetState state, CircularBufferReader reader, ref int packetLength)
public static void LookReq(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;
@ -149,7 +149,7 @@ public static class IncomingEntityPackets
}
}
public static void BatchQueryProperties(NetState state, CircularBufferReader reader, ref int packetLength)
public static void BatchQueryProperties(NetState state, CircularBufferReader reader, int packetLength)
{
if (!ObjectPropertyList.Enabled)
{

View file

@ -1,6 +1,6 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2020 - ModernUO Development Team *
* Copyright 2019-2022 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: IncomingExtendedCommandPackets.cs *
* *
@ -13,15 +13,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System.Collections.Generic;
using Server.ContextMenus;
namespace Server.Network;
public static class IncomingExtendedCommandPackets
{
private static readonly PacketHandler[] m_ExtendedHandlersLow = new PacketHandler[0x100];
private static readonly Dictionary<int, PacketHandler> m_ExtendedHandlersHigh = new();
private static readonly PacketHandler[] _extendedHandlers = new PacketHandler[0x100];
// TODO: Change to outside configuration
public static int[] ValidAnimations { get; set; } =
@ -61,50 +59,34 @@ public static class IncomingExtendedCommandPackets
RegisterExtended(0x32, true, ToggleFlying);
}
private static void UnhandledBF(NetState state, CircularBufferReader reader, ref int packetLength)
private static void UnhandledBF(NetState state, CircularBufferReader reader, int packetLength)
{
}
public static void Empty(NetState state, CircularBufferReader reader, ref int packetLength)
public static void Empty(NetState state, CircularBufferReader reader, int packetLength)
{
}
public static void RegisterExtended(int packetID, bool ingame, OnPacketReceive onReceive)
{
if (packetID >= 0 && packetID < 0x100)
if (packetID is >= 0 and < 0x100)
{
m_ExtendedHandlersLow[packetID] = new PacketHandler(packetID, 0, ingame, onReceive);
}
else
{
m_ExtendedHandlersHigh[packetID] = new PacketHandler(packetID, 0, ingame, onReceive);
_extendedHandlers[packetID] = new PacketHandler(packetID, 0, ingame, onReceive);
}
}
public static PacketHandler GetExtendedHandler(int packetID)
{
if (packetID >= 0 && packetID < 0x100)
{
return m_ExtendedHandlersLow[packetID];
}
m_ExtendedHandlersHigh.TryGetValue(packetID, out var handler);
return handler;
}
public static PacketHandler GetExtendedHandler(int packetID) =>
packetID is >= 0 and < 0x100 ? _extendedHandlers[packetID] : null;
public static void RemoveExtendedHandler(int packetID)
{
if (packetID >= 0 && packetID < 0x100)
if (packetID is >= 0 and < 0x100)
{
m_ExtendedHandlersLow[packetID] = null;
}
else
{
m_ExtendedHandlersHigh.Remove(packetID);
_extendedHandlers[packetID] = null;
}
}
public static void ExtendedCommand(NetState state, CircularBufferReader reader, ref int packetLength)
public static void ExtendedCommand(NetState state, CircularBufferReader reader, int packetLength)
{
int packetId = reader.ReadUInt16();
@ -130,18 +112,18 @@ public static class IncomingExtendedCommandPackets
}
else
{
ph.OnReceive(state, reader, ref packetLength);
ph.OnReceive(state, reader, packetLength);
}
}
public static void ScreenSize(NetState state, CircularBufferReader reader, ref int packetLength)
public static void ScreenSize(NetState state, CircularBufferReader 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, ref int packetLength)
public static void PartyMessage(NetState state, CircularBufferReader reader, int packetLength)
{
if (state.Mobile == null)
{
@ -151,25 +133,25 @@ public static class IncomingExtendedCommandPackets
switch (reader.ReadByte())
{
case 0x01:
PartyMessage_AddMember(state, reader, ref packetLength);
PartyMessage_AddMember(state, reader, packetLength);
break;
case 0x02:
PartyMessage_RemoveMember(state, reader, ref packetLength);
PartyMessage_RemoveMember(state, reader, packetLength);
break;
case 0x03:
PartyMessage_PrivateMessage(state, reader, ref packetLength);
PartyMessage_PrivateMessage(state, reader, packetLength);
break;
case 0x04:
PartyMessage_PublicMessage(state, reader, ref packetLength);
PartyMessage_PublicMessage(state, reader, packetLength);
break;
case 0x06:
PartyMessage_SetCanLoot(state, reader, ref packetLength);
PartyMessage_SetCanLoot(state, reader, packetLength);
break;
case 0x08:
PartyMessage_Accept(state, reader, ref packetLength);
PartyMessage_Accept(state, reader, packetLength);
break;
case 0x09:
PartyMessage_Decline(state, reader, ref packetLength);
PartyMessage_Decline(state, reader, packetLength);
break;
default:
reader.Trace(state);
@ -177,17 +159,17 @@ public static class IncomingExtendedCommandPackets
}
}
public static void PartyMessage_AddMember(NetState state, CircularBufferReader reader, ref int packetLength)
public static void PartyMessage_AddMember(NetState state, CircularBufferReader reader, int packetLength)
{
PartyCommands.Handler?.OnAdd(state.Mobile);
}
public static void PartyMessage_RemoveMember(NetState state, CircularBufferReader reader, ref int packetLength)
public static void PartyMessage_RemoveMember(NetState state, CircularBufferReader reader, int packetLength)
{
PartyCommands.Handler?.OnRemove(state.Mobile, World.FindMobile((Serial)reader.ReadUInt32()));
}
public static void PartyMessage_PrivateMessage(NetState state, CircularBufferReader reader, ref int packetLength)
public static void PartyMessage_PrivateMessage(NetState state, CircularBufferReader reader, int packetLength)
{
PartyCommands.Handler?.OnPrivateMessage(
state.Mobile,
@ -196,27 +178,27 @@ public static class IncomingExtendedCommandPackets
);
}
public static void PartyMessage_PublicMessage(NetState state, CircularBufferReader reader, ref int packetLength)
public static void PartyMessage_PublicMessage(NetState state, CircularBufferReader reader, int packetLength)
{
PartyCommands.Handler?.OnPublicMessage(state.Mobile, reader.ReadBigUniSafe());
}
public static void PartyMessage_SetCanLoot(NetState state, CircularBufferReader reader, ref int packetLength)
public static void PartyMessage_SetCanLoot(NetState state, CircularBufferReader reader, int packetLength)
{
PartyCommands.Handler?.OnSetCanLoot(state.Mobile, reader.ReadBoolean());
}
public static void PartyMessage_Accept(NetState state, CircularBufferReader reader, ref int packetLength)
public static void PartyMessage_Accept(NetState state, CircularBufferReader reader, int packetLength)
{
PartyCommands.Handler?.OnAccept(state.Mobile, World.FindMobile((Serial)reader.ReadUInt32()));
}
public static void PartyMessage_Decline(NetState state, CircularBufferReader reader, ref int packetLength)
public static void PartyMessage_Decline(NetState state, CircularBufferReader reader, int packetLength)
{
PartyCommands.Handler?.OnDecline(state.Mobile, World.FindMobile((Serial)reader.ReadUInt32()));
}
public static void Animate(NetState state, CircularBufferReader reader, ref int packetLength)
public static void Animate(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;
@ -240,7 +222,7 @@ public static class IncomingExtendedCommandPackets
}
}
public static void CastSpell(NetState state, CircularBufferReader reader, ref int packetLength)
public static void CastSpell(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;
@ -255,12 +237,12 @@ public static class IncomingExtendedCommandPackets
EventSink.InvokeCastSpellRequest(from, spellID, spellbook);
}
public static void ToggleFlying(NetState state, CircularBufferReader reader, ref int packetLength)
public static void ToggleFlying(NetState state, CircularBufferReader reader, int packetLength)
{
state.Mobile?.ToggleFlying();
}
public static void StunRequest(NetState state, CircularBufferReader reader, ref int packetLength)
public static void StunRequest(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;
@ -272,7 +254,7 @@ public static class IncomingExtendedCommandPackets
EventSink.InvokeStunRequest(from);
}
public static void DisarmRequest(NetState state, CircularBufferReader reader, ref int packetLength)
public static void DisarmRequest(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;
@ -284,7 +266,7 @@ public static class IncomingExtendedCommandPackets
EventSink.InvokeDisarmRequest(from);
}
public static void StatLockChange(NetState state, CircularBufferReader reader, ref int packetLength)
public static void StatLockChange(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;
@ -315,12 +297,12 @@ public static class IncomingExtendedCommandPackets
}
}
public static void CloseStatus(NetState state, CircularBufferReader reader, ref int packetLength)
public static void CloseStatus(NetState state, CircularBufferReader reader, int packetLength)
{
var serial = (Serial)reader.ReadUInt32();
}
public static void Language(NetState state, CircularBufferReader reader, ref int packetLength)
public static void Language(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;
@ -332,7 +314,7 @@ public static class IncomingExtendedCommandPackets
from.Language = reader.ReadAscii(4);
}
public static void QueryProperties(NetState state, CircularBufferReader reader, ref int packetLength)
public static void QueryProperties(NetState state, CircularBufferReader reader, int packetLength)
{
if (!ObjectPropertyList.Enabled)
{
@ -364,7 +346,7 @@ public static class IncomingExtendedCommandPackets
}
}
public static void ContextMenuResponse(NetState state, CircularBufferReader reader, ref int packetLength)
public static void ContextMenuResponse(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;
@ -420,7 +402,7 @@ public static class IncomingExtendedCommandPackets
}
}
public static void ContextMenuRequest(NetState state, CircularBufferReader reader, ref int packetLength)
public static void ContextMenuRequest(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;
var target = World.FindEntity((Serial)reader.ReadUInt32());
@ -455,7 +437,7 @@ public static class IncomingExtendedCommandPackets
}
}
public static void BandageTarget(NetState state, CircularBufferReader reader, ref int packetLength)
public static void BandageTarget(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;
@ -490,21 +472,21 @@ public static class IncomingExtendedCommandPackets
}
}
public static void TargetedSpell(NetState state, CircularBufferReader reader, ref int packetLength)
public static void TargetedSpell(NetState state, CircularBufferReader 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, ref int packetLength)
public static void TargetedSkillUse(NetState state, CircularBufferReader 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, ref int packetLength)
public static void TargetByResourceMacro(NetState state, CircularBufferReader reader, int packetLength)
{
var serial = (Serial)reader.ReadUInt32();

View file

@ -22,7 +22,7 @@ public static class IncomingHousePackets
IncomingPackets.Register(0xFB, 2, false, ShowPublicHouseContent);
}
public static void ShowPublicHouseContent(NetState state, CircularBufferReader reader, ref int packetLength)
public static void ShowPublicHouseContent(NetState state, CircularBufferReader reader, int packetLength)
{
var showPublicHouseContent = reader.ReadBoolean();
}

View file

@ -24,13 +24,13 @@ public static class IncomingItemPackets
public static void Configure()
{
IncomingPackets.Register(0x07, 7, true, LiftReq);
IncomingPackets.Register(0x08, 15, true, DropReq);
IncomingPackets.Register(new ContainerGridPacketHandler(0x08, 14, true, DropReq));
IncomingPackets.Register(0x13, 10, true, EquipReq);
IncomingPackets.Register(0xEC, 0, false, EquipMacro);
IncomingPackets.Register(0xED, 0, false, UnequipMacro);
}
public static void LiftReq(NetState state, CircularBufferReader reader, ref int packetLength)
public static void LiftReq(NetState state, CircularBufferReader reader, int packetLength)
{
var serial = (Serial)reader.ReadUInt32();
int amount = reader.ReadUInt16();
@ -39,7 +39,7 @@ public static class IncomingItemPackets
state.Mobile.Lift(item, amount, out _, out _);
}
public static void EquipReq(NetState state, CircularBufferReader reader, ref int packetLength)
public static void EquipReq(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;
var item = from.Holding;
@ -64,20 +64,17 @@ public static class IncomingItemPackets
item.ClearBounce();
}
public static void DropReq(NetState state, CircularBufferReader reader, ref int packetLength)
public static void DropReq(NetState state, CircularBufferReader reader, int packetLength)
{
reader.ReadInt32(); // serial, ignored
int x = reader.ReadInt16();
int y = reader.ReadInt16();
int z = reader.ReadSByte();
if (state.ContainerGridLines)
{
reader.ReadByte(); // Grid Location?
}
else
{
packetLength -= 1;
}
Serial dest = (Serial)reader.ReadUInt32();
@ -110,7 +107,7 @@ public static class IncomingItemPackets
}
}
public static void DropReq6017(NetState state, CircularBufferReader reader, ref int packetLength)
public static void DropReq6017(NetState state, CircularBufferReader reader, int packetLength)
{
reader.ReadInt32(); // serial, ignored
int x = reader.ReadInt16();
@ -148,7 +145,7 @@ public static class IncomingItemPackets
}
}
public static void EquipMacro(NetState state, CircularBufferReader reader, ref int packetLength)
public static void EquipMacro(NetState state, CircularBufferReader reader, int packetLength)
{
int count = reader.ReadByte();
var serialList = new List<Serial>(count);
@ -160,7 +157,7 @@ public static class IncomingItemPackets
EventSink.InvokeEquipMacro(state.Mobile, serialList);
}
public static void UnequipMacro(NetState state, CircularBufferReader reader, ref int packetLength)
public static void UnequipMacro(NetState state, CircularBufferReader reader, int packetLength)
{
int count = reader.ReadByte();
var layers = new List<Layer>(count);

View file

@ -46,7 +46,7 @@ public static class IncomingMessagePackets
IncomingPackets.Register(0xAD, 0, true, UnicodeSpeech);
}
public static void AsciiSpeech(NetState state, CircularBufferReader reader, ref int packetLength)
public static void AsciiSpeech(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;
@ -73,7 +73,7 @@ public static class IncomingMessagePackets
from.DoSpeech(text, Array.Empty<int>(), type, Utility.ClipDyedHue(hue));
}
public static void UnicodeSpeech(NetState state, CircularBufferReader reader, ref int packetLength)
public static void UnicodeSpeech(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;

View file

@ -27,7 +27,7 @@ public static class IncomingMobilePackets
IncomingPackets.Register(0x6F, 0, true, SecureTrade);
}
public static void RenameRequest(NetState state, CircularBufferReader reader, ref int packetLength)
public static void RenameRequest(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;
var targ = World.FindMobile((Serial)reader.ReadUInt32());
@ -38,7 +38,7 @@ public static class IncomingMobilePackets
}
}
public static void MobileNameRequest(NetState state, CircularBufferReader reader, ref int packetLength)
public static void MobileNameRequest(NetState state, CircularBufferReader reader, int packetLength)
{
var m = World.FindMobile((Serial)reader.ReadUInt32());
@ -48,7 +48,7 @@ public static class IncomingMobilePackets
}
}
public static void ProfileReq(NetState state, CircularBufferReader reader, ref int packetLength)
public static void ProfileReq(NetState state, CircularBufferReader reader, int packetLength)
{
int type = reader.ReadByte();
var serial = (Serial)reader.ReadUInt32();
@ -88,7 +88,7 @@ public static class IncomingMobilePackets
}
}
public static void SecureTrade(NetState state, CircularBufferReader reader, ref int packetLength)
public static void SecureTrade(NetState state, CircularBufferReader reader, int packetLength)
{
switch (reader.ReadByte())
{

View file

@ -78,7 +78,7 @@ public static class IncomingMovementPackets
ns.SendTimeSyncResponse();
}
public static void MovementReq(NetState state, CircularBufferReader reader, ref int packetLength)
public static void MovementReq(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;

View file

@ -1,6 +1,6 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2021 - ModernUO Development Team *
* Copyright 2019-2022 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: IncomingPackets.cs *
* *
@ -13,62 +13,43 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Server.Network;
public static class IncomingPackets
{
private static readonly PacketHandler[] m_6017Handlers = new PacketHandler[0x100];
private static readonly EncodedPacketHandler[] m_EncodedHandlersLow = new EncodedPacketHandler[0x100];
private static readonly Dictionary<int, EncodedPacketHandler> m_EncodedHandlersHigh =
new();
private static readonly EncodedPacketHandler[] _encodedHandlers = new EncodedPacketHandler[0x100];
public static PacketHandler[] Handlers { get; } = new PacketHandler[0x100];
public static void Register(int packetID, int length, bool ingame, OnPacketReceive onReceive)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Register(int packetID, int length, bool ingame, OnPacketReceive onReceive) =>
Register(new PacketHandler(packetID, length, ingame, onReceive));
public static void Register(PacketHandler packetHandler)
{
Handlers[packetID] = new PacketHandler(packetID, length, ingame, onReceive);
m_6017Handlers[packetID] ??= new PacketHandler(packetID, length, ingame, onReceive);
Handlers[packetHandler.PacketID] = packetHandler;
}
public static PacketHandler GetHandler(int packetID) => Handlers[packetID];
public static void RegisterEncoded(int packetID, bool ingame, OnEncodedPacketReceive onReceive)
{
if (packetID >= 0 && packetID < 0x100)
if (packetID is >= 0 and < 0x100)
{
m_EncodedHandlersLow[packetID] = new EncodedPacketHandler(packetID, ingame, onReceive);
}
else
{
m_EncodedHandlersHigh[packetID] = new EncodedPacketHandler(packetID, ingame, onReceive);
_encodedHandlers[packetID] = new EncodedPacketHandler(packetID, ingame, onReceive);
}
}
public static EncodedPacketHandler GetEncodedHandler(int packetID)
{
if (packetID >= 0 && packetID < 0x100)
{
return m_EncodedHandlersLow[packetID];
}
m_EncodedHandlersHigh.TryGetValue(packetID, out var handler);
return handler;
}
public static EncodedPacketHandler GetEncodedHandler(int packetID) =>
packetID is >= 0 and < 0x100 ? _encodedHandlers[packetID] : null;
public static void RemoveEncodedHandler(int packetID)
{
if (packetID >= 0 && packetID < 0x100)
if (packetID is >= 0 and < 0x100)
{
m_EncodedHandlersLow[packetID] = null;
}
else
{
m_EncodedHandlersHigh.Remove(packetID);
_encodedHandlers[packetID] = null;
}
}

View file

@ -52,18 +52,18 @@ public static class IncomingPlayerPackets
IncomingPackets.RegisterEncoded(0x32, true, QuestGumpRequest);
}
public static void DeathStatusResponse(NetState state, CircularBufferReader reader, ref int packetLength)
public static void DeathStatusResponse(NetState state, CircularBufferReader reader, int packetLength)
{
// Ignored
}
public static void RequestScrollWindow(NetState state, CircularBufferReader reader, ref int packetLength)
public static void RequestScrollWindow(NetState state, CircularBufferReader reader, int packetLength)
{
int lastTip = reader.ReadInt16();
int type = reader.ReadByte();
}
public static void AttackReq(NetState state, CircularBufferReader reader, ref int packetLength)
public static void AttackReq(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;
@ -80,7 +80,7 @@ public static class IncomingPlayerPackets
}
}
public static void HuePickerResponse(NetState state, CircularBufferReader reader, ref int packetLength)
public static void HuePickerResponse(NetState state, CircularBufferReader reader, int packetLength)
{
var serial = reader.ReadUInt32();
_ = reader.ReadInt16(); // Item ID
@ -97,7 +97,7 @@ public static class IncomingPlayerPackets
}
}
public static void SystemInfo(NetState state, CircularBufferReader reader, ref int packetLength)
public static void SystemInfo(NetState state, CircularBufferReader reader, int packetLength)
{
int v1 = reader.ReadByte();
int v2 = reader.ReadUInt16();
@ -113,7 +113,7 @@ public static class IncomingPlayerPackets
var v8 = reader.ReadInt32();
}
public static void TextCommand(NetState state, CircularBufferReader reader, ref int packetLength)
public static void TextCommand(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;
@ -208,7 +208,7 @@ public static class IncomingPlayerPackets
}
}
public static void AsciiPromptResponse(NetState state, CircularBufferReader reader, ref int packetLength)
public static void AsciiPromptResponse(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;
@ -244,7 +244,7 @@ public static class IncomingPlayerPackets
}
}
public static void UnicodePromptResponse(NetState state, CircularBufferReader reader, ref int packetLength)
public static void UnicodePromptResponse(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;
@ -281,7 +281,7 @@ public static class IncomingPlayerPackets
}
}
public static void MenuResponse(NetState state, CircularBufferReader reader, ref int packetLength)
public static void MenuResponse(NetState state, CircularBufferReader reader, int packetLength)
{
var serial = reader.ReadUInt32();
int menuID = reader.ReadInt16(); // unused in our implementation
@ -311,33 +311,33 @@ public static class IncomingPlayerPackets
}
}
public static void Disconnect(NetState state, CircularBufferReader reader, ref int packetLength)
public static void Disconnect(NetState state, CircularBufferReader reader, int packetLength)
{
var minusOne = reader.ReadInt32();
}
public static void ConfigurationFile(NetState state, CircularBufferReader reader, ref int packetLength)
public static void ConfigurationFile(NetState state, CircularBufferReader reader, int packetLength)
{
}
public static void LogoutReq(NetState state, CircularBufferReader reader, ref int packetLength)
public static void LogoutReq(NetState state, CircularBufferReader reader, int packetLength)
{
state.SendLogoutAck();
}
public static void ChangeSkillLock(NetState state, CircularBufferReader reader, ref int packetLength)
public static void ChangeSkillLock(NetState state, CircularBufferReader reader, int packetLength)
{
var s = state.Mobile.Skills[reader.ReadInt16()];
s?.SetLockNoRelay((SkillLock)reader.ReadByte());
}
public static void HelpRequest(NetState state, CircularBufferReader reader, ref int packetLength)
public static void HelpRequest(NetState state, CircularBufferReader reader, int packetLength)
{
EventSink.InvokeHelpRequest(state.Mobile);
}
public static void DisplayGumpResponse(NetState state, CircularBufferReader reader, ref int packetLength)
public static void DisplayGumpResponse(NetState state, CircularBufferReader reader, int packetLength)
{
var serial = (Serial)reader.ReadUInt32();
var typeID = reader.ReadInt32();
@ -479,13 +479,13 @@ public static class IncomingPlayerPackets
}
}
public static void SetWarMode(NetState state, CircularBufferReader reader, ref int packetLength)
public static void SetWarMode(NetState state, CircularBufferReader reader, int packetLength)
{
state.Mobile?.DelayChangeWarmode(reader.ReadBoolean());
}
// TODO: Throttle/make this more safe
public static void Resynchronize(NetState state, CircularBufferReader reader, ref int packetLength)
public static void Resynchronize(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;
@ -502,17 +502,17 @@ public static class IncomingPlayerPackets
state.Sequence = 0;
}
public static void PingReq(NetState state, CircularBufferReader reader, ref int packetLength)
public static void PingReq(NetState state, CircularBufferReader reader, int packetLength)
{
state.SendPingAck(reader.ReadByte());
}
public static void SetUpdateRange(NetState state, CircularBufferReader reader, ref int packetLength)
public static void SetUpdateRange(NetState state, CircularBufferReader reader, int packetLength)
{
state.SendChangeUpdateRange(18);
}
public static void MobileQuery(NetState state, CircularBufferReader reader, ref int packetLength)
public static void MobileQuery(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;
if (from == null)
@ -549,7 +549,7 @@ public static class IncomingPlayerPackets
}
}
public static void CrashReport(NetState state, CircularBufferReader reader, ref int packetLength)
public static void CrashReport(NetState state, CircularBufferReader reader, int packetLength)
{
var clientMaj = reader.ReadByte();
var clientMin = reader.ReadByte();
@ -593,11 +593,19 @@ public static class IncomingPlayerPackets
EventSink.InvokeQuestGumpRequest(state.Mobile);
}
public static void EncodedCommand(NetState state, CircularBufferReader reader, ref int packetLength)
public static void EncodedCommand(NetState state, CircularBufferReader reader, int packetLength)
{
var e = World.FindEntity((Serial)reader.ReadUInt32());
int packetId = reader.ReadUInt16();
// We will add support if this is ever a real thing.
if (packetId > 0xFF)
{
var reason = $"Sent unsupported encoded packet (0xD7x{packetId:X4}";
state.LogInfo(reason);
state.Disconnect(reason);
}
var ph = IncomingPackets.GetEncodedHandler(packetId);
if (ph == null)
@ -608,15 +616,13 @@ public static class IncomingPlayerPackets
if (ph.Ingame && state.Mobile == null)
{
state.LogInfo(
"Sent in-game packet (0xD7x{0:X2}) before being attached to a mobile",
packetId
);
state.Disconnect($"Sent in-game packet (0xD7x{packetId:X2}) before being attached to a mobile.");
var reason = $"Sent in-game packet (0xD7x{packetId:X4}) before being attached to a mobile.";
state.LogInfo(reason);
state.Disconnect(reason);
}
else if (ph.Ingame && state.Mobile.Deleted)
{
state.Disconnect($"Sent in-game packet(0xD7x{packetId:X2}) but mobile is deleted.");
state.Disconnect($"Sent in-game packet(0xD7x{packetId:X4}) but mobile is deleted.");
}
else
{

View file

@ -25,7 +25,7 @@ public static class IncomingTargetingPackets
IncomingPackets.Register(0x6C, 19, true, TargetResponse);
}
public static void TargetResponse(NetState state, CircularBufferReader reader, ref int packetLength)
public static void TargetResponse(NetState state, CircularBufferReader reader, int packetLength)
{
int type = reader.ReadByte();
var targetID = reader.ReadInt32();

View file

@ -14,6 +14,7 @@
*************************************************************************/
using System.Collections.Generic;
using System.IO;
namespace Server.Network;
@ -25,7 +26,7 @@ public static class IncomingVendorPackets
IncomingPackets.Register(0x9F, 0, true, VendorSellReply);
}
public static void VendorBuyReply(NetState state, CircularBufferReader reader, ref int packetLength)
public static void VendorBuyReply(NetState state, CircularBufferReader reader, int packetLength)
{
var vendor = World.FindMobile((Serial)reader.ReadUInt32());
@ -65,7 +66,7 @@ public static class IncomingVendorPackets
state.SendEndVendorBuy(vendor.Serial);
}
public static void VendorSellReply(NetState state, CircularBufferReader reader, ref int packetLength)
public static void VendorSellReply(NetState state, CircularBufferReader reader, int packetLength)
{
var serial = (Serial)reader.ReadUInt32();
var vendor = World.FindMobile(serial);

View file

@ -27,7 +27,7 @@ namespace Server.Engines.Chat
IncomingPackets.Register(0xB3, 0, true, ChatAction);
}
public static void OpenChatWindowRequest(NetState state, CircularBufferReader reader, ref int packetLength)
public static void OpenChatWindowRequest(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;
@ -48,7 +48,7 @@ namespace Server.Engines.Chat
ChatUser.AddChatUser(from, chatName);
}
public static void ChatAction(NetState state, CircularBufferReader reader, ref int packetLength)
public static void ChatAction(NetState state, CircularBufferReader reader, int packetLength)
{
if (!ChatSystem.Enabled)
{

View file

@ -193,7 +193,7 @@ namespace Server.Engines.MLQuests.Gumps
return false;
}
private static void RaceChangeReply(NetState state, CircularBufferReader reader, ref int packetLength)
private static void RaceChangeReply(NetState state, CircularBufferReader reader, int packetLength)
{
if (!m_Pending.TryGetValue(state, out var raceChangeState))
{

View file

@ -9,7 +9,7 @@ namespace Server.Engines.UltimaStore
IncomingPackets.Register(0xFA, 1, true, UltimaStoreOpenRequest);
}
public static void UltimaStoreOpenRequest(NetState state, CircularBufferReader reader, ref int packetLength)
public static void UltimaStoreOpenRequest(NetState state, CircularBufferReader reader, int packetLength)
{
state.Mobile.SendMessage("Ultima Store is not currently available.");
}

View file

@ -29,7 +29,7 @@ namespace Server.Items
IncomingPackets.Register(0x93, 99, true, OldHeaderChange);
}
public static void OldHeaderChange(NetState state, CircularBufferReader reader, ref int packetLength)
public static void OldHeaderChange(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;
@ -48,7 +48,7 @@ namespace Server.Items
book.Author = Utility.FixHtml(author);
}
public static void HeaderChange(NetState state, CircularBufferReader reader, ref int packetLength)
public static void HeaderChange(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;
@ -84,7 +84,7 @@ namespace Server.Items
book.Author = Utility.FixHtml(author);
}
public static void ContentChange(NetState state, CircularBufferReader reader, ref int packetLength)
public static void ContentChange(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;

View file

@ -48,7 +48,7 @@ namespace Server.Network
return $"{seconds} second{(seconds == 1 ? "" : "s")}";
}
public static void BBClientRequest(NetState state, CircularBufferReader reader, ref int packetLength)
public static void BBClientRequest(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;

View file

@ -61,7 +61,7 @@ namespace Server.Engines.Mahjong
RegisterSubCommand(0x18, MoveDealerIndicator);
}
public static void OnPacket(NetState state, CircularBufferReader reader, ref int packetLength)
public static void OnPacket(NetState state, CircularBufferReader reader, int packetLength)
{
var game = World.FindItem((Serial)reader.ReadUInt32()) as MahjongGame;

View file

@ -25,7 +25,7 @@ namespace Server.Network
IncomingPackets.Register(0x56, 11, true, OnMapCommand);
}
private static void OnMapCommand(NetState state, CircularBufferReader reader, ref int packetLength)
private static void OnMapCommand(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;

View file

@ -141,7 +141,7 @@ namespace Server
}
}
public static void OnReceive(NetState state, CircularBufferReader reader, ref int packetLength)
public static void OnReceive(NetState state, CircularBufferReader reader, int packetLength)
{
reader.ReadByte(); // 1: <4.0.1a, 2>=4.0.1a

View file

@ -38,8 +38,8 @@ namespace Server.Network
}
else
{
Delays[0x03] = 5; // Speech
Delays[0xAD] = 5; // Speech
Delays[0x03] = 25; // Speech
Delays[0xAD] = 25; // Speech
Delays[0x75] = 500; // Rename request
}
@ -141,7 +141,7 @@ namespace Server.Network
return true;
}
if (Core.TickCount < ns.GetPacketDelay(packetID) + Delays[packetID])
if (Core.TickCount < ns.GetPacketTime(packetID) + Delays[packetID])
{
drop = true;
return false;

View file

@ -1766,7 +1766,7 @@ namespace Server.Multis
context.Foundation.SendInfoTo(state);
}
public static void QueryDesignDetails(NetState state, CircularBufferReader reader, ref int packetLength)
public static void QueryDesignDetails(NetState state, CircularBufferReader reader, int packetLength)
{
var from = state.Mobile;

View file

@ -70,7 +70,7 @@ namespace Server.Network
}
}
public static void PollInfo(NetState ns, CircularBufferReader reader, ref int packetLength)
public static void PollInfo(NetState state, CircularBufferReader reader, int packetLength)
{
var version = reader.ReadByte();
@ -83,21 +83,21 @@ namespace Server.Network
if (!span.SequenceEqual(_token))
{
ns.Disconnect("Invalid token sent for ConnectUO");
state.Disconnect("Invalid token sent for ConnectUO");
return;
}
}
}
ns.LogInfo($"ConnectUO (v{version}) is requesting stats.");
state.LogInfo($"ConnectUO (v{version}) is requesting stats.");
if (version > ConnectUOProtocolVersion)
{
Utility.PushColor(ConsoleColor.Yellow);
ns.LogInfo("Warning! ConnectUO (v{version}) is newer than what is supported.");
state.LogInfo("Warning! ConnectUO (v{version}) is newer than what is supported.");
Utility.PopColor();
}
ns.SendServerPollInfo();
state.SendServerPollInfo();
}
public static void SendServerPollInfo(this NetState ns)

View file

@ -36,14 +36,14 @@ namespace Server.Network
public static void Register(int cmd, bool ingame, OnPacketReceive onReceive) =>
_handlers[cmd] = new PacketHandler(cmd, 0, ingame, onReceive);
public static void QueryGuildMemberLocations(NetState state, CircularBufferReader reader, ref int packetLength)
public static void QueryGuildMemberLocations(NetState state, CircularBufferReader reader, int packetLength)
{
Mobile from = state.Mobile;
state.SendGuildMemberLocations(from, from.Guild as Guild, reader.ReadBoolean());
}
public static void QueryPartyMemberLocations(NetState state, CircularBufferReader reader, ref int packetLength)
public static void QueryPartyMemberLocations(NetState state, CircularBufferReader reader, int packetLength)
{
Mobile from = state.Mobile;
var party = Party.Get(from);

View file

@ -21,11 +21,11 @@ namespace Server.Network
{
var packetHandlers = new PacketHandler[0x100];
void DecodeBundledPacket(NetState state, CircularBufferReader reader, ref int packetLength)
void DecodeBundledPacket(NetState state, CircularBufferReader reader, int packetLength)
{
int cmd = reader.ReadByte();
PacketHandler ph = cmd >= 0 && cmd < packetHandlers.Length ? packetHandlers[cmd] : null;
PacketHandler ph = packetHandlers[cmd];
if (ph == null)
{
@ -43,7 +43,7 @@ namespace Server.Network
}
else
{
ph.OnReceive(state, reader, ref packetLength);
ph.OnReceive(state, reader, packetLength);
}
}

View file

@ -33,9 +33,9 @@ namespace Server.Network
}
}
public static void QueryCompactShardStats(NetState ns, CircularBufferReader reader, ref int packetLength)
public static void QueryCompactShardStats(NetState state, CircularBufferReader reader, int packetLength)
{
ns.SendCompactShardStats(
state.SendCompactShardStats(
(uint)(Core.TickCount / 1000),
TcpServer.Instances.Count - 1, // Shame if you modify this!
World.Items.Count,
@ -44,10 +44,10 @@ namespace Server.Network
);
}
public static void QueryExtendedShardStats(NetState ns, CircularBufferReader reader, ref int packetLength)
public static void QueryExtendedShardStats(NetState state, CircularBufferReader reader, int packetLength)
{
const long ticksInHour = 1000 * 60 * 60;
ns.SendExtendedShardStats(
state.SendExtendedShardStats(
ServerList.ServerName,
(int)(Core.TickCount / ticksInHour),
TcpServer.Instances.Count - 1, // Shame if you modify this!

View file

@ -19,7 +19,7 @@ namespace Server.SkillHandlers
SkillInfo.Table[(int)SkillName.Tracking].Callback = OnUse;
}
public static void QuestArrow(NetState state, CircularBufferReader reader, ref int packetLength)
public static void QuestArrow(NetState state, CircularBufferReader reader, int packetLength)
{
if (state.Mobile is PlayerMobile from)
{