Packet recycling

This commit is contained in:
Sardelka 2022-08-11 18:25:01 +08:00
parent a26a799a9f
commit 1d3e53f734
8 changed files with 61 additions and 6 deletions

View File

@ -26,7 +26,7 @@ namespace RageCoop.Client
public static void ToggleConnection(string address, string username = null, string password = null)
{
Peer?.Dispose();
Peer?.Shutdown("Bye");
if (IsOnServer)
{
}
@ -37,6 +37,8 @@ namespace RageCoop.Client
}
else
{
Peer?.Dispose();
IsConnecting = true;
password = password ?? Main.Settings.Password;
username=username ?? Main.Settings.Username;

View File

@ -10,6 +10,7 @@ namespace RageCoop.Client
{
internal static partial class Networking
{
private static PacketPool PacketPool=new PacketPool();
private static readonly Func<byte, BitReader, object> _resolveHandle = (t, reader) =>
{
switch (t)
@ -205,6 +206,7 @@ namespace RageCoop.Client
Peer.Recycle(message);
}
static Packet packet;
private static void HandlePacket(PacketType packetType, byte[] data, NetConnection senderConnection)
{
@ -227,11 +229,15 @@ namespace RageCoop.Client
break;
case PacketType.VehicleSync:
VehicleSync(data.GetPacket<Packets.VehicleSync>());
packet = data.GetPacket<Packets.VehicleSync>(PacketPool);
VehicleSync((Packets.VehicleSync)packet);
PacketPool.Recycle((Packets.VehicleSync)packet);
break;
case PacketType.PedSync:
PedSync(data.GetPacket<Packets.PedSync>());
packet = data.GetPacket<Packets.PedSync>(PacketPool);
PedSync((Packets.PedSync)packet);
PacketPool.Recycle((Packets.PedSync)packet);
break;
case PacketType.ProjectileSync:
ProjectileSync(data.GetPacket<Packets.ProjectileSync>());

View File

@ -89,6 +89,9 @@
<HintPath>..\libs\Lidgren.Network.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.Extensions.ObjectPool, Version=6.0.8.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Extensions.ObjectPool.6.0.8\lib\net461\Microsoft.Extensions.ObjectPool.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Win32.Primitives, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll</HintPath>
<Private>True</Private>

View File

@ -2,6 +2,7 @@
<packages>
<package id="Costura.Fody" version="5.7.0" targetFramework="net48" developmentDependency="true" />
<package id="Fody" version="6.6.3" targetFramework="net48" developmentDependency="true" />
<package id="Microsoft.Extensions.ObjectPool" version="6.0.8" targetFramework="net48" />
<package id="Microsoft.NETCore.Platforms" version="1.1.0" targetFramework="net48" />
<package id="Microsoft.Win32.Primitives" version="4.3.0" targetFramework="net48" />
<package id="NETStandard.Library" version="1.6.1" targetFramework="net48" />

View File

@ -261,14 +261,14 @@ namespace RageCoop.Core
}
public static T GetPacket<T>(this NetIncomingMessage msg) where T : Packet, new()
public static T GetPacket<T>(this NetIncomingMessage msg,PacketPool pool=null) where T : Packet, new()
{
msg.ReadByte();
return GetPacket<T>(msg.ReadBytes(msg.ReadInt32()));
}
public static T GetPacket<T>(this byte[] data) where T : Packet, new()
public static T GetPacket<T>(this byte[] data, PacketPool pool = null) where T : Packet, new()
{
T p = new T();
T p = pool?.Get<T>() ?? new T();
p.Deserialize(data);
return p;
}

View File

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.ObjectPool;
namespace RageCoop.Core
{
internal class PacketPool
{
public ObjectPool<Packets.VehicleSync> VehicleSyncPool=ObjectPool.Create<Packets.VehicleSync>();
public ObjectPool<Packets.PedSync> PedSyncPool = ObjectPool.Create<Packets.PedSync>();
public void Recycle(Packets.VehicleSync p)
{
VehicleSyncPool.Return(p);
}
public void Recycle(Packets.PedSync p)
{
PedSyncPool.Return(p);
}
public Packets.PedSync GetPedPacket()
{
return PedSyncPool.Get();
}
public Packets.VehicleSync GetVehiclePacket()
{
return VehicleSyncPool.Get();
}
public T Get<T>() where T : Packet
{
var type=typeof(T);
if (type==typeof(Packets.VehicleSync))
{
return (T)(Packet)VehicleSyncPool.Get();
}
else if (type==typeof(Packets.PedSync))
{
return (T)(Packet)PedSyncPool.Get();
}
return null;
}
}
}

View File

@ -39,6 +39,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="6.0.8" />
<PackageReference Include="SharpZipLib" Version="1.3.3" />
<PackageReference Include="System.Buffers" Version="4.5.1" />
</ItemGroup>

View File

@ -47,6 +47,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="6.0.8" />
<PackageReference Include="SharpZipLib" Version="1.3.3" />
</ItemGroup>