Latency fix

This commit is contained in:
Sardelka
2022-07-29 21:15:23 +08:00
parent c8bbdc69d0
commit 93d705e402
10 changed files with 21 additions and 45 deletions

View File

@ -16,23 +16,13 @@ namespace RageCoop.Client
UseMouse = false,
Alignment = Main.Settings.FlipMenu ? GTA.UI.Alignment.Right : GTA.UI.Alignment.Left
};
private static NativeItem d1 = new NativeItem("PositionPrediction");
static DebugMenu()
{
Menu.Banner.Color = Color.FromArgb(225, 0, 0, 0);
Menu.Title.Color = Color.FromArgb(255, 165, 0);
d1.Activated+=(sender, e) =>
{
try { SyncParameters.PositioinPredictionDefault =float.Parse(Game.GetUserInput(WindowTitle.EnterMessage20, SyncParameters.PositioinPredictionDefault.ToString(), 20)); }
catch { }
Update();
};
Menu.Add(d1);
Menu.AddSubMenu(DiagnosticMenu);
Menu.Opening+=(sender, e) => Update();
DiagnosticMenu.Opening+=(sender, e) =>
{
DiagnosticMenu.Clear();
@ -43,14 +33,9 @@ namespace RageCoop.Client
}
};
Update();
}
private static void Update()
{
d1.AltTitle = SyncParameters.PositioinPredictionDefault.ToString();
}
}
}

View File

@ -1,7 +1,7 @@
using Lidgren.Network;
using RageCoop.Core;
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
@ -13,10 +13,17 @@ namespace RageCoop.Client
public static NetClient Client;
public static float Latency = 0;
public static bool ShowNetworkInfo = false;
public static Security Security;
public static Security Security;
private static readonly Dictionary<int, Action<PacketType, byte[]>> PendingResponses = new Dictionary<int, Action<PacketType, byte[]>>();
internal static readonly Dictionary<PacketType, Func<byte[], Packet>> RequestHandlers = new Dictionary<PacketType, Func<byte[], Packet>>();
static Networking()
{
Security=new Security(Main.Logger);
RequestHandlers.Add(PacketType.PingPong, (b) =>
{
return new Packets.PingPong();
});
Task.Run(() =>
{
while (true)

View File

@ -28,8 +28,6 @@ namespace RageCoop.Client
}
};
private static readonly AutoResetEvent _publicKeyReceived = new AutoResetEvent(false);
private static readonly Dictionary<int, Action<PacketType, byte[]>> PendingResponses = new Dictionary<int, Action<PacketType, byte[]>>();
internal static readonly Dictionary<PacketType, Func<byte[], Packet>> RequestHandlers = new Dictionary<PacketType, Func<byte[], Packet>>();
public static void ProcessMessage(NetIncomingMessage message)
{
if (message == null) { return; }
@ -111,7 +109,6 @@ namespace RageCoop.Client
int id = message.ReadInt32();
var realType = (PacketType)message.ReadByte();
int len = message.ReadInt32();
Main.Logger.Debug($"{id},{realType},{len}");
if (RequestHandlers.TryGetValue(realType, out var handler))
{
var response = Client.CreateMessage();
@ -119,6 +116,7 @@ namespace RageCoop.Client
response.Write(id);
handler(message.ReadBytes(len)).Pack(response);
Client.SendMessage(response, NetDeliveryMethod.ReliableOrdered, message.SequenceChannel);
Client.FlushSendQueue();
}
break;
}

View File

@ -75,11 +75,9 @@ namespace RageCoop.Client
}
public static void UpdatePlayer(Packets.PlayerInfoUpdate packet)
{
if (packet.PedID == Main.LocalPlayerID) {Main.Logger.Debug("Latency updated"); Networking.Latency=packet.Latency; }
var p = GetPlayer(packet.PedID);
if (p?.Character != null)
{
p.Latency= packet.Latency;
}
p.Latency= packet.Latency;
}
public static PlayerData GetPlayer(int id)
{

View File

@ -56,7 +56,6 @@
<Compile Include="Sync\Entities\SyncedVehicle.cs" />
<Compile Include="Sync\EntityPool.cs" />
<Compile Include="Sync\SyncEvents.cs" />
<Compile Include="Sync\SyncParameters.cs" />
<Compile Include="Util\Memory.cs" />
<Compile Include="Util\NativeCaller.cs" />
<Compile Include="Util\PedConfigFlags.cs" />
@ -257,4 +256,4 @@
<PropertyGroup>
<PostBuildEvent>cd /d $(ProjectDir) %26%26 copy ..\bin\$(Configuration)\Core\RageCoop.Core.dll $(OutDir)RageCoop.Core.dll /y</PostBuildEvent>
</PropertyGroup>
</Project>
</Project>

View File

@ -1,7 +0,0 @@
namespace RageCoop.Client
{
internal class SyncParameters
{
public static float PositioinPredictionDefault = 0.01f;
}
}

View File

@ -160,9 +160,9 @@ namespace RageCoop.Client
}
}
public static Vector3 PredictPosition(this Entity e, bool applyDefault = true)
public static Vector3 PredictPosition(this Entity e)
{
return e.ReadPosition()+e.Velocity*((applyDefault ? SyncParameters.PositioinPredictionDefault : 0)+Networking.Latency);
return e.ReadPosition()+e.Velocity*(Networking.Latency);
}
public static Vehicle CreateVehicle(Model model, Vector3 position, float heading = 0f)

View File

@ -12,9 +12,9 @@ namespace RageCoop.Core
public override void Pack(NetOutgoingMessage message)
{
#region PacketToNetOutGoingMessage
message.Write((byte)PacketType.ChatMessage);
message.Write(0);
message.Write((byte)PacketType.PingPong);
message.Write(1);
message.Write((byte)0);
#endregion
}

View File

@ -17,12 +17,8 @@ namespace RageCoop.Core
PublicKeyResponse=5,
Request=6,
Response=7,
ChatMessage=10,
// NativeCall=11,
// NativeResponse=12,
// Mod=13,
// CleanUpWorld=14,
PingPong = 8,
ChatMessage =10,
FileTransferChunk=11,
FileTransferRequest=12,

View File

@ -85,7 +85,7 @@ namespace RageCoop.Server
{
while (!_stopping)
{
foreach(var c in Clients.Values)
foreach(var c in Clients.Values.ToArray())
{
c.UpdateLatency();
}