diff --git a/RageCoop.Client/Networking/Networking.cs b/RageCoop.Client/Networking/Networking.cs index ae2a41c..485b733 100644 --- a/RageCoop.Client/Networking/Networking.cs +++ b/RageCoop.Client/Networking/Networking.cs @@ -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; diff --git a/RageCoop.Client/Networking/Receive.cs b/RageCoop.Client/Networking/Receive.cs index e99dd4b..3898088 100644 --- a/RageCoop.Client/Networking/Receive.cs +++ b/RageCoop.Client/Networking/Receive.cs @@ -10,6 +10,7 @@ namespace RageCoop.Client { internal static partial class Networking { + private static PacketPool PacketPool=new PacketPool(); private static readonly Func _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()); + packet = data.GetPacket(PacketPool); + VehicleSync((Packets.VehicleSync)packet); + PacketPool.Recycle((Packets.VehicleSync)packet); break; case PacketType.PedSync: - PedSync(data.GetPacket()); + packet = data.GetPacket(PacketPool); + PedSync((Packets.PedSync)packet); + PacketPool.Recycle((Packets.PedSync)packet); break; case PacketType.ProjectileSync: ProjectileSync(data.GetPacket()); diff --git a/RageCoop.Client/RageCoop.Client.csproj b/RageCoop.Client/RageCoop.Client.csproj index 54342ea..74daed0 100644 --- a/RageCoop.Client/RageCoop.Client.csproj +++ b/RageCoop.Client/RageCoop.Client.csproj @@ -89,6 +89,9 @@ ..\libs\Lidgren.Network.dll + + ..\packages\Microsoft.Extensions.ObjectPool.6.0.8\lib\net461\Microsoft.Extensions.ObjectPool.dll + ..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll True diff --git a/RageCoop.Client/packages.config b/RageCoop.Client/packages.config index 0a23e39..fae344e 100644 --- a/RageCoop.Client/packages.config +++ b/RageCoop.Client/packages.config @@ -2,6 +2,7 @@ + diff --git a/RageCoop.Core/CoreUtils.cs b/RageCoop.Core/CoreUtils.cs index 3c2369a..2d5c56e 100644 --- a/RageCoop.Core/CoreUtils.cs +++ b/RageCoop.Core/CoreUtils.cs @@ -261,14 +261,14 @@ namespace RageCoop.Core } - public static T GetPacket(this NetIncomingMessage msg) where T : Packet, new() + public static T GetPacket(this NetIncomingMessage msg,PacketPool pool=null) where T : Packet, new() { msg.ReadByte(); return GetPacket(msg.ReadBytes(msg.ReadInt32())); } - public static T GetPacket(this byte[] data) where T : Packet, new() + public static T GetPacket(this byte[] data, PacketPool pool = null) where T : Packet, new() { - T p = new T(); + T p = pool?.Get() ?? new T(); p.Deserialize(data); return p; } diff --git a/RageCoop.Core/Networking/PacketPool.cs b/RageCoop.Core/Networking/PacketPool.cs new file mode 100644 index 0000000..a38b727 --- /dev/null +++ b/RageCoop.Core/Networking/PacketPool.cs @@ -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 VehicleSyncPool=ObjectPool.Create(); + public ObjectPool PedSyncPool = ObjectPool.Create(); + 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() 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; + } + } +} diff --git a/RageCoop.Core/RageCoop.Core.csproj b/RageCoop.Core/RageCoop.Core.csproj index 3b332e3..9eaa1a1 100644 --- a/RageCoop.Core/RageCoop.Core.csproj +++ b/RageCoop.Core/RageCoop.Core.csproj @@ -39,6 +39,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/RageCoop.Server/RageCoop.Server.csproj b/RageCoop.Server/RageCoop.Server.csproj index 3b6143a..fa2a73e 100644 --- a/RageCoop.Server/RageCoop.Server.csproj +++ b/RageCoop.Server/RageCoop.Server.csproj @@ -47,6 +47,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive +