Skip to content

Commit

Permalink
Remove memory watcher, make code thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
thatbakamono committed Feb 9, 2020
1 parent f268c2b commit 1f7d25f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 82 deletions.
59 changes: 1 addition & 58 deletions LocalAdmin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Linq;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using LocalAdmin.V2.Commands;

Expand All @@ -28,20 +27,12 @@ internal class LocalAdmin
public string LocalAdminExecutable { get; private set; }

private CommandService commandService = new CommandService();

private Process gameProcess;

private TcpServer server;

private Task memoryWatcherTask;
private Task readerTask;

private string scpslExecutable = "";

private int tooLowMemory;
private ushort gamePort;
private ushort consolePort;

private bool _exit;

public void Start(string[] args)
Expand Down Expand Up @@ -84,7 +75,6 @@ public void Start(string[] args)

SetupPlatform();
RegisterCommands();
SetupMemoryWatcher();
SetupReader();

Menu();
Expand All @@ -98,9 +88,8 @@ public void Start(string[] args)
RunSCPSL(gamePort);

readerTask.Start();
memoryWatcherTask.Start();

Task.WaitAll(readerTask, memoryWatcherTask);
Task.WaitAll(readerTask);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -209,51 +198,6 @@ private void SetupReader()
});
}

private void SetupMemoryWatcher()
{
memoryWatcherTask = new Task(async () =>
{
while (!_exit)
{
await Task.Delay(2000);

if (gameProcess == null) continue;
if (!gameProcess.HasExited)
{
gameProcess.Refresh();
var UsedRAM_MB = (int)(gameProcess.WorkingSet64 / 1048576);

if (UsedRAM_MB < 400 && gameProcess.StartTime.AddMinutes(3) < DateTime.Now)
tooLowMemory++;
else
tooLowMemory = 0;

if (tooLowMemory > 5 || gameProcess.MainWindowTitle != "")
{
ConsoleUtil.WriteLine("Session crashed. Trying to restart dedicated server...", ConsoleColor.Red);

gameProcess?.Kill();

Restart();
}

continue;
}

ConsoleUtil.WriteLine("Session crashed. Trying to restart dedicated server...", ConsoleColor.Red);

Restart();
}

if (gameProcess != null)
while (!gameProcess.HasExited)
Thread.Sleep(100);

ConsoleUtil.WriteLine("Game process successfully exited.", ConsoleColor.DarkGreen);
ConsoleUtil.WriteLine("Exiting LocalAdmin...", ConsoleColor.DarkGreen);
});
}

private void RunSCPSL(ushort port)
{
if (File.Exists(scpslExecutable))
Expand Down Expand Up @@ -304,7 +248,6 @@ private string ReadInput(Func<string, bool> checkInput, Action validInputAction,
private void Exit(int code = -1)
{
server.Stop();

Console.ReadKey(true);
Environment.Exit(code);
}
Expand Down
67 changes: 43 additions & 24 deletions TcpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace LocalAdmin.V2
{
Expand All @@ -16,7 +16,8 @@ public class TcpServer
private NetworkStream? networkStream;
private StreamReader? streamReader;

private volatile bool exit = false;
private volatile bool exit = true;
private object lck = new object();

public TcpServer(ushort port)
{
Expand All @@ -25,45 +26,63 @@ public TcpServer(ushort port)

public void Start()
{
exit = false;

new Thread(() =>
lock (lck)
{
listener.Start();
client = listener.AcceptTcpClient();
networkStream = client.GetStream();
streamReader = new StreamReader(networkStream);
exit = false;

while (!exit)
listener.Start();
listener.BeginAcceptTcpClient(new AsyncCallback((result) =>
{
if (!streamReader!.EndOfStream)
client = listener.EndAcceptTcpClient(result);
networkStream = client.GetStream();
streamReader = new StreamReader(networkStream);

Task.Run(async () =>
{
Received?.Invoke(this, streamReader!.ReadLine()!);
}
while (true)
{
lock (lck)
{
if (exit)
break;
}

Thread.Sleep(10);
}
}).Start();
if (!streamReader?.EndOfStream == true)
{
Received?.Invoke(this, streamReader!.ReadLine()!);
}

await Task.Delay(10);
}
});
}), listener);
}
}

public void Stop()
{
if (!exit)
lock (lck)
{
exit = true;
if (!exit)
{
exit = true;

listener.Stop();
client!.Close();
streamReader!.Dispose();
listener.Stop();
client!.Close();
streamReader!.Dispose();
}
}
}

public void WriteLine(string input)
{
if (!exit)
lock (lck)
{
var buffer = Encoding.UTF8.GetBytes(input + Environment.NewLine);
networkStream!.Write(buffer, 0, buffer.Length);
if (!exit)
{
var buffer = Encoding.UTF8.GetBytes(input + Environment.NewLine);
networkStream!.Write(buffer, 0, buffer.Length);
}
}
}
}
Expand Down

0 comments on commit 1f7d25f

Please sign in to comment.