Compare commits

..

1 Commits

Author SHA1 Message Date
6d3f9f8afb packet parsing optimisations 2026-02-17 16:35:20 +02:00
4 changed files with 31 additions and 15 deletions

View File

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

View File

@@ -1,14 +1,15 @@
using BoneSync.Data; using System;
using BoneSync.Networking.Transport;
using BoneSync.Sync;
using MelonLoader;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using BoneSync.Data;
using BoneSync.Networking.Transport;
using BoneSync.Sync;
using MelonLoader;
using Oculus.Platform;
#if TEST #if TEST
using Xunit; using Xunit;
#endif #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) public static NetworkMessage ParsePacket(Packet packet)
{ {
//SyncLogger.Msg("Received packet of type " + packet.Info.packetType + " from " + packet.Info.senderId + " Length: " + packet.Data.Length); //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 // find a class that can parse this packet using Reflection
// and return it // 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]; ConstructorInfo constructor = GetConstructor(packet);
// 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);
NetworkMessage networkMessage = (NetworkMessage)constructor.Invoke(new object[] { packet }); NetworkMessage networkMessage = (NetworkMessage)constructor.Invoke(new object[] { packet });
networkMessage.senderId = packet.Info.senderId; networkMessage.senderId = packet.Info.senderId;
return networkMessage; return networkMessage;

View File

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

View File

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