Sync every 30 ms

This commit is contained in:
Sardelka 2022-08-04 17:38:28 +08:00
parent 5d5bc668eb
commit eda903abde
4 changed files with 20 additions and 9 deletions

View File

@ -10,7 +10,7 @@ namespace RageCoop.Client
internal static partial class Networking
{
public static int SyncInterval = 30;
#region -- SEND --
/// <summary>
/// Pack the packet then send to server.
@ -27,6 +27,10 @@ namespace RageCoop.Client
public static void SendPed(SyncedPed c, bool full)
{
if (c.LastSentStopWatch.ElapsedMilliseconds<SyncInterval)
{
return;
}
Ped p = c.MainPed;
var packet = new Packets.PedSync()
{
@ -54,6 +58,7 @@ namespace RageCoop.Client
{
packet.Position = p.ReadPosition();
}
c.LastSentStopWatch.Restart();
if (full)
{
packet.Flags |= PedDataFlags.IsFullSync;
@ -84,6 +89,10 @@ namespace RageCoop.Client
}
public static void SendVehicle(SyncedVehicle v, bool full)
{
if (v.LastSentStopWatch.ElapsedMilliseconds<SyncInterval)
{
return;
}
Vehicle veh = v.MainVehicle;
var packet = new Packets.VehicleSync()
{

View File

@ -1,5 +1,6 @@
using GTA;
using GTA.Math;
using System.Diagnostics;
namespace RageCoop.Client
{
@ -60,6 +61,9 @@ namespace RageCoop.Client
/// Last time the local entity has been updated,
/// </summary>
public ulong LastUpdated { get; set; } = 0;
internal Stopwatch LastSentStopWatch { get; set; } = Stopwatch.StartNew();
#endregion
public bool SendNextFrame { get; set; } = false;

View File

@ -484,7 +484,6 @@ namespace RageCoop.Client
internal float LastEngineHealth { get; set; }
internal Vector3 LastVelocity { get; set; }
internal Stopwatch LastSentStopWatch { get; set; }=new Stopwatch();
#endregion
}

View File

@ -325,11 +325,11 @@ namespace RageCoop.Server
if (status == NetConnectionStatus.Disconnected)
{
SendPlayerDisconnectPacket(sender);
PlayerDisconnected(sender);
}
else if (status == NetConnectionStatus.Connected)
{
SendPlayerConnectPacket(sender);
PlayerConnected(sender);
_worker.QueueJob(() => API.Events.InvokePlayerConnected(sender));
Resources.SendTo(sender);
}
@ -512,8 +512,6 @@ namespace RageCoop.Server
senderConnection.Disconnect(e.Message);
}
#region -- SYNC --
// Before we approve the connection, we must shake hands
private void GetHandshake(NetConnection connection, Packets.Handshake packet)
{
Logger?.Debug("New handshake from: [Name: " + packet.Username + " | Address: " + connection.RemoteEndPoint.Address.ToString() + "]");
@ -595,7 +593,7 @@ namespace RageCoop.Server
}
// The connection has been approved, now we need to send all other players to the new player and the new player to all players
private void SendPlayerConnectPacket(Client newClient)
private void PlayerConnected(Client newClient)
{
if (newClient==_hostClient)
{
@ -647,7 +645,7 @@ namespace RageCoop.Server
}
// Send all players a message that someone has left the server
private void SendPlayerDisconnectPacket(Client localClient)
private void PlayerDisconnected(Client localClient)
{
var cons = MainNetServer.Connections.Exclude(localClient.Connection);
if (cons.Count!=0)
@ -674,6 +672,7 @@ namespace RageCoop.Server
Security.RemoveConnection(localClient.Connection.RemoteEndPoint);
}
#region -- SYNC --
#region SyncEntities
private void PedSync(Packets.PedSync packet, Client client)