Better packet reuse
This commit is contained in:
@ -10,7 +10,20 @@ namespace RageCoop.Client
|
|||||||
{
|
{
|
||||||
internal static partial class Networking
|
internal static partial class Networking
|
||||||
{
|
{
|
||||||
private static PacketPool PacketPool=new PacketPool();
|
|
||||||
|
/// <summary>
|
||||||
|
/// Reduce GC pressure by reusing frequently used packets
|
||||||
|
/// </summary>
|
||||||
|
static class ReceivedPackets
|
||||||
|
{
|
||||||
|
public static Packets.PedSync PedPacket = new Packets.PedSync();
|
||||||
|
public static Packets.VehicleSync VehicelPacket = new Packets.VehicleSync();
|
||||||
|
public static Packets.ProjectileSync ProjectilePacket = new Packets.ProjectileSync();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Used to reslove entity handle in a <see cref="Packets.CustomEvent"/>
|
||||||
|
/// </summary>
|
||||||
private static readonly Func<byte, BitReader, object> _resolveHandle = (t, reader) =>
|
private static readonly Func<byte, BitReader, object> _resolveHandle = (t, reader) =>
|
||||||
{
|
{
|
||||||
switch (t)
|
switch (t)
|
||||||
@ -201,7 +214,6 @@ namespace RageCoop.Client
|
|||||||
|
|
||||||
Peer.Recycle(message);
|
Peer.Recycle(message);
|
||||||
}
|
}
|
||||||
static Packet packet;
|
|
||||||
private static void HandlePacket(PacketType packetType, byte[] data, NetConnection senderConnection)
|
private static void HandlePacket(PacketType packetType, byte[] data, NetConnection senderConnection)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -224,18 +236,17 @@ namespace RageCoop.Client
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case PacketType.VehicleSync:
|
case PacketType.VehicleSync:
|
||||||
packet = data.GetPacket<Packets.VehicleSync>(PacketPool);
|
ReceivedPackets.VehicelPacket.Deserialize(data);
|
||||||
VehicleSync((Packets.VehicleSync)packet);
|
VehicleSync(ReceivedPackets.VehicelPacket);
|
||||||
PacketPool.Recycle((Packets.VehicleSync)packet);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case PacketType.PedSync:
|
case PacketType.PedSync:
|
||||||
packet = data.GetPacket<Packets.PedSync>(PacketPool);
|
ReceivedPackets.PedPacket.Deserialize(data);
|
||||||
PedSync((Packets.PedSync)packet);
|
PedSync(ReceivedPackets.PedPacket);
|
||||||
PacketPool.Recycle((Packets.PedSync)packet);
|
|
||||||
break;
|
break;
|
||||||
case PacketType.ProjectileSync:
|
case PacketType.ProjectileSync:
|
||||||
ProjectileSync(data.GetPacket<Packets.ProjectileSync>());
|
ReceivedPackets.ProjectilePacket.Deserialize(data);
|
||||||
|
ProjectileSync(ReceivedPackets.ProjectilePacket);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case PacketType.ChatMessage:
|
case PacketType.ChatMessage:
|
||||||
|
@ -10,7 +10,15 @@ namespace RageCoop.Client
|
|||||||
{
|
{
|
||||||
internal static partial class Networking
|
internal static partial class Networking
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Reduce GC pressure by reusing frequently used packets
|
||||||
|
/// </summary>
|
||||||
|
static class SendPackets
|
||||||
|
{
|
||||||
|
public static Packets.PedSync PedPacket = new Packets.PedSync();
|
||||||
|
public static Packets.VehicleSync VehicelPacket = new Packets.VehicleSync();
|
||||||
|
public static Packets.ProjectileSync ProjectilePacket = new Packets.ProjectileSync();
|
||||||
|
}
|
||||||
public static int SyncInterval = 30;
|
public static int SyncInterval = 30;
|
||||||
public static List<NetConnection> Targets = new List<NetConnection>();
|
public static List<NetConnection> Targets = new List<NetConnection>();
|
||||||
public static void SendSync(Packet p, ConnectionChannel channel = ConnectionChannel.Default, NetDeliveryMethod method = NetDeliveryMethod.UnreliableSequenced)
|
public static void SendSync(Packet p, ConnectionChannel channel = ConnectionChannel.Default, NetDeliveryMethod method = NetDeliveryMethod.UnreliableSequenced)
|
||||||
@ -25,17 +33,15 @@ namespace RageCoop.Client
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Ped p = c.MainPed;
|
Ped p = c.MainPed;
|
||||||
var packet = new Packets.PedSync()
|
var packet = SendPackets.PedPacket;
|
||||||
{
|
packet.ID =c.ID;
|
||||||
ID =c.ID,
|
packet.OwnerID=c.OwnerID;
|
||||||
OwnerID=c.OwnerID,
|
packet.Health = p.Health;
|
||||||
Health = p.Health,
|
packet.Rotation = p.ReadRotation();
|
||||||
Rotation = p.ReadRotation(),
|
packet.Velocity = p.ReadVelocity();
|
||||||
Velocity = p.ReadVelocity(),
|
packet.Speed = p.GetPedSpeed();
|
||||||
Speed = p.GetPedSpeed(),
|
packet.Flags = p.GetPedFlags();
|
||||||
Flags = p.GetPedFlags(),
|
packet.Heading=p.Heading;
|
||||||
Heading=p.Heading,
|
|
||||||
};
|
|
||||||
if (packet.Flags.HasPedFlag(PedDataFlags.IsAiming))
|
if (packet.Flags.HasPedFlag(PedDataFlags.IsAiming))
|
||||||
{
|
{
|
||||||
packet.AimCoords = p.GetAimCoord();
|
packet.AimCoords = p.GetAimCoord();
|
||||||
@ -77,6 +83,10 @@ namespace RageCoop.Client
|
|||||||
packet.BlipScale=0.5f;
|
packet.BlipScale=0.5f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
packet.BlipColor=(BlipColor)255;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
SendSync(packet, ConnectionChannel.PedSync);
|
SendSync(packet, ConnectionChannel.PedSync);
|
||||||
}
|
}
|
||||||
@ -87,19 +97,17 @@ namespace RageCoop.Client
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Vehicle veh = v.MainVehicle;
|
Vehicle veh = v.MainVehicle;
|
||||||
var packet = new Packets.VehicleSync()
|
var packet = SendPackets.VehicelPacket;
|
||||||
{
|
packet.ID =v.ID;
|
||||||
ID =v.ID,
|
packet.OwnerID=v.OwnerID;
|
||||||
OwnerID=v.OwnerID,
|
packet.Flags = veh.GetVehicleFlags();
|
||||||
Flags = veh.GetVehicleFlags(),
|
packet.SteeringAngle = veh.SteeringAngle;
|
||||||
SteeringAngle = veh.SteeringAngle,
|
packet.Position = veh.Position;
|
||||||
Position = veh.Position,
|
packet.Velocity=veh.Velocity;
|
||||||
Velocity=veh.Velocity,
|
packet.Quaternion=veh.ReadQuaternion();
|
||||||
Quaternion=veh.ReadQuaternion(),
|
packet.RotationVelocity=veh.RotationVelocity;
|
||||||
RotationVelocity=veh.RotationVelocity,
|
packet.ThrottlePower = veh.ThrottlePower;
|
||||||
ThrottlePower = veh.ThrottlePower,
|
packet.BrakePower = veh.BrakePower;
|
||||||
BrakePower = veh.BrakePower,
|
|
||||||
};
|
|
||||||
if (v.LastVelocity==default) {v.LastVelocity=packet.Velocity; }
|
if (v.LastVelocity==default) {v.LastVelocity=packet.Velocity; }
|
||||||
packet.Acceleration = (packet.Velocity-v.LastVelocity)*1000/v.LastSentStopWatch.ElapsedMilliseconds;
|
packet.Acceleration = (packet.Velocity-v.LastVelocity)*1000/v.LastSentStopWatch.ElapsedMilliseconds;
|
||||||
v.LastSentStopWatch.Restart();
|
v.LastSentStopWatch.Restart();
|
||||||
@ -140,16 +148,14 @@ namespace RageCoop.Client
|
|||||||
public static void SendProjectile(SyncedProjectile sp)
|
public static void SendProjectile(SyncedProjectile sp)
|
||||||
{
|
{
|
||||||
var p = sp.MainProjectile;
|
var p = sp.MainProjectile;
|
||||||
var packet = new Packets.ProjectileSync()
|
var packet = SendPackets.ProjectilePacket;
|
||||||
{
|
packet.ID =sp.ID;
|
||||||
ID =sp.ID,
|
packet.ShooterID=sp.ShooterID;
|
||||||
ShooterID=sp.ShooterID,
|
packet.Rotation=p.Rotation;
|
||||||
Rotation=p.Rotation,
|
packet.Position=p.Position;
|
||||||
Position=p.Position,
|
packet.Velocity=p.Velocity;
|
||||||
Velocity=p.Velocity,
|
packet.WeaponHash=(uint)p.WeaponHash;
|
||||||
WeaponHash=(uint)p.WeaponHash,
|
packet.Exploded=p.IsDead;
|
||||||
Exploded=p.IsDead
|
|
||||||
};
|
|
||||||
if (p.IsDead) { EntityPool.RemoveProjectile(sp.ID, "Dead"); }
|
if (p.IsDead) { EntityPool.RemoveProjectile(sp.ID, "Dead"); }
|
||||||
SendSync(packet, ConnectionChannel.ProjectileSync);
|
SendSync(packet, ConnectionChannel.ProjectileSync);
|
||||||
}
|
}
|
||||||
|
@ -258,6 +258,7 @@
|
|||||||
</Target>
|
</Target>
|
||||||
<Import Project="..\packages\Costura.Fody.5.7.0\build\Costura.Fody.targets" Condition="Exists('..\packages\Costura.Fody.5.7.0\build\Costura.Fody.targets')" />
|
<Import Project="..\packages\Costura.Fody.5.7.0\build\Costura.Fody.targets" Condition="Exists('..\packages\Costura.Fody.5.7.0\build\Costura.Fody.targets')" />
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<PostBuildEvent>cd /d $(ProjectDir) %26%26 copy ..\bin\$(Configuration)\Core\RageCoop.Core.dll $(OutDir)RageCoop.Core.dll /y</PostBuildEvent>
|
<PostBuildEvent>
|
||||||
|
</PostBuildEvent>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
</Project>
|
</Project>
|
@ -290,14 +290,14 @@ namespace RageCoop.Core
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static T GetPacket<T>(this NetIncomingMessage msg,PacketPool pool=null) where T : Packet, new()
|
public static T GetPacket<T>(this NetIncomingMessage msg, T existingPacket = null) where T : Packet, new()
|
||||||
{
|
{
|
||||||
msg.ReadByte();
|
msg.ReadByte();
|
||||||
return GetPacket<T>(msg.ReadBytes(msg.ReadInt32()));
|
return GetPacket<T>(msg.ReadBytes(msg.ReadInt32()),existingPacket);
|
||||||
}
|
}
|
||||||
public static T GetPacket<T>(this byte[] data, PacketPool pool = null) where T : Packet, new()
|
public static T GetPacket<T>(this byte[] data, T existingPacket=null) where T : Packet, new()
|
||||||
{
|
{
|
||||||
T p = pool?.Get<T>() ?? new T();
|
var p = existingPacket??new T();
|
||||||
p.Deserialize(data);
|
p.Deserialize(data);
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
@ -1,42 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user