Skip to content

Commit

Permalink
Merge pull request #30 from ShadowJonathan/decrease-idle-usage
Browse files Browse the repository at this point in the history
Decrease Idle cpu usage
  • Loading branch information
DrMeepso authored Nov 30, 2024
2 parents c330b2f + 8687d3d commit 7354134
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions Cove/Server/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public partial class CoveServer
public string LobbyCode = new string(Enumerable.Range(0, 5).Select(_ => "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"[new Random().Next(36)]).ToArray());
public bool codeOnly = true;
public bool ageRestricted = false;

public string joinMessage = "This is a Cove dedicated server!\nPlease report any issues to the github (xr0.xyz/cove)";
public bool displayJoinMessage = true;

Expand Down Expand Up @@ -66,7 +66,10 @@ public partial class CoveServer
public void Init()
{
cbThread = new(runSteamworksUpdate);
cbThread.Name = "Steamworks Callback Thread";

networkThread = new(RunNetwork);
networkThread.Name = "Network Thread";

Console.WriteLine("Loading world!");
string worldFile = $"{AppDomain.CurrentDomain.BaseDirectory}worlds/main_zone.tscn";
Expand Down Expand Up @@ -207,7 +210,7 @@ public void Init()
// like 10 minutes of delay within 30 seconds
networkThread.IsBackground = true;
networkThread.Start();

bool LogServices = false;
ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
{
Expand Down Expand Up @@ -367,13 +370,17 @@ private bool getBoolFromString(string str)
void runSteamworksUpdate()
{
while (true)
{
Thread.Sleep(25);
SteamAPI.RunCallbacks();
}
}

void RunNetwork()
{
while (true)
{
bool didWork = false;
try
{
for (int i = 0; i < 6; i++)
Expand All @@ -382,6 +389,7 @@ void RunNetwork()
// we are going to check if there are any incoming net packets!
if (SteamNetworking.IsP2PPacketAvailable(out packetSize, nChannel: i))
{
didWork = true;
byte[] packet = new byte[packetSize];
uint bytesRead = 0;
CSteamID sender;
Expand All @@ -397,10 +405,13 @@ void RunNetwork()
{
if (!showErrorMessages)
return;

Console.WriteLine("-- Error responding to packet! --");
Console.WriteLine(e.ToString());
}

if (!didWork)
Thread.Sleep(10);
}
}

Expand Down

0 comments on commit 7354134

Please sign in to comment.