packet parsing optimisations

This commit is contained in:
2026-02-17 16:35:20 +02:00
parent 1fa8516ebe
commit 6d3f9f8afb
4 changed files with 31 additions and 15 deletions

View File

@@ -31,7 +31,7 @@ namespace BoneSync
public static LobbyManager lobby;
public static TransportBase transport;
public static ITransportBase transport;
public override void OnApplicationStart()
{
SteamClient.Init(823500, true);

View File

@@ -1,14 +1,15 @@
using BoneSync.Data;
using BoneSync.Networking.Transport;
using BoneSync.Sync;
using MelonLoader;
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using BoneSync.Data;
using BoneSync.Networking.Transport;
using BoneSync.Sync;
using MelonLoader;
using Oculus.Platform;
#if TEST
using Xunit;
#endif
@@ -137,19 +138,34 @@ namespace BoneSync.Networking
}
}
}
private static readonly Dictionary<PacketType, ConstructorInfo> ConstructorCache = new Dictionary<PacketType, ConstructorInfo>();
public static ConstructorInfo GetConstructor(Packet packet)
{
if (ConstructorCache.ContainsKey(packet.Info.packetType))
{
return ConstructorCache[packet.Info.packetType];
}
if (!PacketTypeMap.ContainsKey(packet.Info.packetType))
{
throw new Exception("No class found for packet type '" + packet.Info.packetType + "'");
}
Type type = PacketTypeMap[packet.Info.packetType];
// get the constructor that takes a Packet
ConstructorInfo constructor = type.GetConstructor(new Type[] { typeof(Packet) }) ?? throw new Exception("No constructor found for type " + type.Name);
ConstructorCache[packet.Info.packetType] = constructor;
return constructor;
}
public static NetworkMessage ParsePacket(Packet packet)
{
//SyncLogger.Msg("Received packet of type " + packet.Info.packetType + " from " + packet.Info.senderId + " Length: " + packet.Data.Length);
// find a class that can parse this packet using Reflection
// and return it
if (!PacketTypeMap.ContainsKey(packet.Info.packetType))
{
throw new Exception("No class found for packet type '" + packet.Info.packetType+"'");
}
Type type = PacketTypeMap[packet.Info.packetType];
// get the constructor that takes a Packet
ConstructorInfo constructor = type.GetConstructor(new Type[] { typeof(Packet) }) ?? throw new Exception("No constructor found for type " + type.Name);
ConstructorInfo constructor = GetConstructor(packet);
NetworkMessage networkMessage = (NetworkMessage)constructor.Invoke(new object[] { packet });
networkMessage.senderId = packet.Info.senderId;
return networkMessage;

View File

@@ -13,7 +13,7 @@ using Oculus.Platform;
namespace BoneSync.Networking.Transport
{
internal class SteamTransport : TransportBase
internal class SteamTransport : ITransportBase
{
public SteamTransport()

View File

@@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace BoneSync.Networking.Transport
{
public interface TransportBase
public interface ITransportBase
{
ulong BROADCAST_ID { get; }
void Send(Packet packet);