Skip to content

Commit 8687d3d

Browse files
Decrease Idle cpu usage
1 parent c330b2f commit 8687d3d

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

Cove/Server/Server.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public partial class CoveServer
3434
public string LobbyCode = new string(Enumerable.Range(0, 5).Select(_ => "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"[new Random().Next(36)]).ToArray());
3535
public bool codeOnly = true;
3636
public bool ageRestricted = false;
37-
37+
3838
public string joinMessage = "This is a Cove dedicated server!\nPlease report any issues to the github (xr0.xyz/cove)";
3939
public bool displayJoinMessage = true;
4040

@@ -66,7 +66,10 @@ public partial class CoveServer
6666
public void Init()
6767
{
6868
cbThread = new(runSteamworksUpdate);
69+
cbThread.Name = "Steamworks Callback Thread";
70+
6971
networkThread = new(RunNetwork);
72+
networkThread.Name = "Network Thread";
7073

7174
Console.WriteLine("Loading world!");
7275
string worldFile = $"{AppDomain.CurrentDomain.BaseDirectory}worlds/main_zone.tscn";
@@ -207,7 +210,7 @@ public void Init()
207210
// like 10 minutes of delay within 30 seconds
208211
networkThread.IsBackground = true;
209212
networkThread.Start();
210-
213+
211214
bool LogServices = false;
212215
ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
213216
{
@@ -367,13 +370,17 @@ private bool getBoolFromString(string str)
367370
void runSteamworksUpdate()
368371
{
369372
while (true)
373+
{
374+
Thread.Sleep(25);
370375
SteamAPI.RunCallbacks();
376+
}
371377
}
372378

373379
void RunNetwork()
374380
{
375381
while (true)
376382
{
383+
bool didWork = false;
377384
try
378385
{
379386
for (int i = 0; i < 6; i++)
@@ -382,6 +389,7 @@ void RunNetwork()
382389
// we are going to check if there are any incoming net packets!
383390
if (SteamNetworking.IsP2PPacketAvailable(out packetSize, nChannel: i))
384391
{
392+
didWork = true;
385393
byte[] packet = new byte[packetSize];
386394
uint bytesRead = 0;
387395
CSteamID sender;
@@ -397,10 +405,13 @@ void RunNetwork()
397405
{
398406
if (!showErrorMessages)
399407
return;
400-
408+
401409
Console.WriteLine("-- Error responding to packet! --");
402410
Console.WriteLine(e.ToString());
403411
}
412+
413+
if (!didWork)
414+
Thread.Sleep(10);
404415
}
405416
}
406417

0 commit comments

Comments
 (0)