72 lines
2.0 KiB
C#
72 lines
2.0 KiB
C#
using BCG;
|
|
|
|
using bts;
|
|
|
|
using Bts.PubSubServer;
|
|
|
|
using CommandLine;
|
|
|
|
using mbts.config;
|
|
|
|
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
Parser.Default.ParseArguments<PubSubServerConfig>(args)
|
|
.MapResult(Run, errs => 1);
|
|
|
|
int Run(PubSubServerConfig cfg) {
|
|
try {
|
|
cfg.MergeWithConfigFile();
|
|
Configuration.initialize();
|
|
return cfg.SimpleEchoServer ?? false ?
|
|
RunEchoServer(cfg.Port ?? 12345, cfg.EchoDelay) :
|
|
RunPubSubServer(cfg);
|
|
} catch (Exception ex) {
|
|
// Write the error to console first, in case the logging system is compromised.
|
|
var error_msg = $"PubSubServer threw exception:\n{ex}";
|
|
Console.Error.WriteLine(error_msg);
|
|
Console.Error.Flush();
|
|
Log.Gen.error(error_msg);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
int RunPubSubServer(PubSubServerConfig cfg) {
|
|
var config = PubSub3Config.instance;
|
|
long currentMemory = 0;
|
|
int defaultWaitTime = 100;
|
|
bool gced = false;
|
|
Task.Run(() => {
|
|
while (true) {
|
|
gced = false;
|
|
currentMemory = GC.GetTotalMemory(false);
|
|
if (config.PS3_GCActivationThreshold > 0 && currentMemory > config.PS3_GCActivationThreshold) {
|
|
Console.WriteLine($"GCing memory, mem before GC = {currentMemory}");
|
|
GC.Collect();
|
|
gced = true;
|
|
}
|
|
Thread.Sleep(gced ? config.PS3_WaitPeriodAfterGC : defaultWaitTime);
|
|
}
|
|
});
|
|
new PubSubMessageReactor().start();
|
|
return 0;
|
|
}
|
|
|
|
int RunEchoServer(short port, TimeSpan? echoDelay) {
|
|
Console.WriteLine("Running as TCP Echo server, Hit enter to continue");
|
|
Console.ReadLine();
|
|
Console.WriteLine($"ready to listen to port: {port}");
|
|
var sts = new SimpleTCPServer(port, echoDelay);
|
|
sts.init();
|
|
Console.CancelKeyPress += OnCancelKeyPress;
|
|
while (true) {
|
|
Thread.Sleep(TimeSpan.FromSeconds(3));
|
|
}
|
|
|
|
void OnCancelKeyPress(object? sender, ConsoleCancelEventArgs e) {
|
|
Log.Gen.error("Cancel key press. Disposing echo server");
|
|
sts.Dispose();
|
|
}
|
|
}
|