Latency update and threading fixes.

This commit is contained in:
Sardelka 2022-06-01 19:05:45 +08:00
parent 87b9a67d0b
commit 42d057e2c5
4 changed files with 31 additions and 29 deletions

View File

@ -7,7 +7,7 @@ using System.Diagnostics;
namespace RageCoop.Core.Logging
{
public class Logger
public class Logger :IDisposable
{
/// <summary>
@ -20,13 +20,14 @@ namespace RageCoop.Core.Logging
private string Buffer="";
private Thread LoggerThread;
private bool Stopping=false;
public Logger(bool overwrite=true)
{
if (File.Exists(LogPath)&&overwrite) { File.Delete(LogPath); }
LoggerThread=new Thread(() =>
{
while (true)
while (!Stopping)
{
Flush();
Thread.Sleep(1000);
@ -132,5 +133,10 @@ namespace RageCoop.Core.Logging
}
}
public void Dispose()
{
Stopping=true;
LoggerThread?.Join();
}
}
}

View File

@ -6,10 +6,10 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\net6.0\publish\linux-arm\</PublishDir>
<PublishDir>bin\Release\net6.0\publish\linux-x64\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<TargetFramework>net6.0</TargetFramework>
<RuntimeIdentifier>linux-arm</RuntimeIdentifier>
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
<SelfContained>true</SelfContained>
<PublishSingleFile>True</PublishSingleFile>
<PublishTrimmed>True</PublishTrimmed>

View File

@ -4,6 +4,6 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<History>True|2022-05-27T07:20:25.7264350Z;True|2022-05-27T15:20:04.2362276+08:00;True|2022-05-27T15:19:21.4852644+08:00;True|2022-05-27T15:18:36.0857345+08:00;True|2022-05-25T10:30:00.0927959+08:00;True|2022-05-25T10:26:50.6739643+08:00;True|2022-05-25T10:20:36.6658425+08:00;True|2022-05-25T10:19:47.8333108+08:00;True|2022-05-24T11:00:13.3617113+08:00;True|2022-05-22T16:56:31.0481188+08:00;True|2022-05-18T13:35:57.1402751+08:00;True|2022-05-18T13:10:28.4995253+08:00;True|2022-05-01T18:35:01.9624101+08:00;True|2022-05-01T12:32:20.8671319+08:00;False|2022-05-01T12:30:25.4596227+08:00;</History>
<History>True|2022-06-01T10:47:39.6707493Z;True|2022-06-01T18:04:32.2932367+08:00;True|2022-06-01T18:03:17.8871227+08:00;True|2022-05-27T15:20:25.7264350+08:00;True|2022-05-27T15:20:04.2362276+08:00;True|2022-05-27T15:19:21.4852644+08:00;True|2022-05-27T15:18:36.0857345+08:00;True|2022-05-25T10:30:00.0927959+08:00;True|2022-05-25T10:26:50.6739643+08:00;True|2022-05-25T10:20:36.6658425+08:00;True|2022-05-25T10:19:47.8333108+08:00;True|2022-05-24T11:00:13.3617113+08:00;True|2022-05-22T16:56:31.0481188+08:00;True|2022-05-18T13:35:57.1402751+08:00;True|2022-05-18T13:10:28.4995253+08:00;True|2022-05-01T18:35:01.9624101+08:00;True|2022-05-01T12:32:20.8671319+08:00;False|2022-05-01T12:30:25.4596227+08:00;</History>
</PropertyGroup>
</Project>

View File

@ -11,6 +11,7 @@ using RageCoop.Core;
using Newtonsoft.Json;
using System.Threading.Tasks;
using Lidgren.Network;
using System.Timers;
namespace RageCoop.Server
{
@ -32,10 +33,9 @@ namespace RageCoop.Server
public static Resource RunningResource = null;
public static readonly Dictionary<Command, Action<CommandContext>> Commands = new();
public static readonly Dictionary<TriggerEvent, Action<EventContext>> TriggerEvents = new();
private static Thread BackgroundThread;
public static readonly Dictionary<long,Client> Clients = new();
private static System.Timers.Timer SendLatencyTimer = new System.Timers.Timer(5000);
public Server()
{
Program.Logger.Info("================");
@ -57,7 +57,9 @@ namespace RageCoop.Server
MainNetServer = new NetServer(config);
MainNetServer.Start();
SendLatencyTimer.Elapsed+=((s,e) => { SendLatency(); });
SendLatencyTimer.AutoReset=true;
SendLatencyTimer.Enabled=true;
Program.Logger.Info(string.Format("Server listening on {0}:{1}", config.LocalAddress.ToString(), config.Port));
if (MainSettings.UPnP)
@ -205,10 +207,8 @@ namespace RageCoop.Server
Program.Logger.Info("Searching for client-side files...");
DownloadManager.CheckForDirectoryAndFiles();
Listen();
BackgroundThread=new Thread(() => Background());
BackgroundThread.Start();
Listen();
}
private void Listen()
@ -587,29 +587,25 @@ namespace RageCoop.Server
// Sleep for 1 second
Thread.Sleep(1000);
}
Program.Logger.Dispose();
}
private void Background()
private void SendLatency()
{
while (true)
foreach (Client c in Clients.Values)
{
foreach(Client c in Clients.Values)
MainNetServer.Connections.FindAll(x => x.RemoteUniqueIdentifier != c.NetID).ForEach(x =>
{
MainNetServer.Connections.FindAll(x => x.RemoteUniqueIdentifier != c.NetID).ForEach(x =>
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
new Packets.PlayerInfoUpdate()
{
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
new Packets.PlayerInfoUpdate()
{
PedID=c.Player.PedID,
Username=c.Player.Username,
Latency=c.Player.Latency=c.Latency,
}.Pack(outgoingMessage);
MainNetServer.SendMessage(outgoingMessage, x, NetDeliveryMethod.ReliableSequenced, (byte)ConnectionChannel.Default);
});
}
// Update Latency every 20 seconds.
Thread.Sleep(1000*20);
PedID=c.Player.PedID,
Username=c.Player.Username,
Latency=c.Player.Latency=c.Latency,
}.Pack(outgoingMessage);
MainNetServer.SendMessage(outgoingMessage, x, NetDeliveryMethod.ReliableSequenced, (byte)ConnectionChannel.Default);
});
}
}
private void DisconnectAndLog(NetConnection senderConnection,PacketTypes type, Exception e)
@ -677,7 +673,7 @@ namespace RageCoop.Server
}
);;
}
Program.Logger.Info($"HandShake sucess, Player:{packet.Username} PedID:{packet.PedID}");
Program.Logger.Info($"Handshake sucess, Player:{packet.Username} PedID:{packet.PedID}");
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
// Create a new handshake packet