Skip to content

Commit 33756c6

Browse files
committed
chore: add timed work runner
1 parent e7faa06 commit 33756c6

File tree

4 files changed

+86
-51
lines changed

4 files changed

+86
-51
lines changed

src/OTAPI.UnifiedServerProcess.GlobalNetwork/Network/Router.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace OTAPI.UnifiedServerProcess.GlobalNetwork.Network
1010
{
1111
public class Router
1212
{
13+
public int ListenPort { get; private set; }
1314
public static readonly RemoteClient[] globalClients = new RemoteClient[256];
1415
public static readonly MessageBuffer[] globalMsgBuffers = new MessageBuffer[257];
1516
public readonly ServerContext[] clientCurrentlyServers = new ServerContext[256];
@@ -33,6 +34,7 @@ static Router() {
3334
}
3435
}
3536
public Router(int listenPort, ServerContext main, params ServerContext[] allServers) {
37+
ListenPort = listenPort;
3638
Array.Fill(clientCurrentlyServers, main);
3739

3840
On.Terraria.NetMessageSystemContext.mfwh_CheckBytes += ProcessBytes;

src/OTAPI.UnifiedServerProcess.GlobalNetwork/OTAPI.UnifiedServerProcess.GlobalNetwork.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
9+
<LangVersion>preview</LangVersion>
910
</PropertyGroup>
1011

1112
<ItemGroup>

src/OTAPI.UnifiedServerProcess.GlobalNetwork/Program.cs

Lines changed: 36 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
using OTAPI.UnifiedServerProcess.GlobalNetwork.Servers;
44
using ReLogic.OS;
55
using System.Diagnostics;
6+
using System.Diagnostics.Metrics;
67
using System.Reflection;
8+
using System.Threading.Tasks;
79
using UnifiedServerProcess;
810

911
namespace OTAPI.UnifiedServerProcess.GlobalNetwork
@@ -22,66 +24,49 @@ static void Main(string[] args) {
2224
Console.WriteLine(@"---------------------------------------------------------------------------------------------------");
2325
Console.WriteLine(@" Demonstration For Terraria v{0} & OTAPI v{1} ", version.TerrariaVersion, version.OTAPIVersion);
2426
Console.WriteLine(@"---------------------------------------------------------------------------------------------------");
25-
26-
27-
Console.Write("[USP|Info] Global initialization started... ");
28-
var spinner = new ConsoleSpinner(100);
29-
spinner.Start();
30-
31-
Stopwatch stopwatch = new();
32-
stopwatch.Start();
33-
SynchronizedGuard.Load();
34-
NetworkPatcher.Load();
35-
AppDomain.CurrentDomain.AssemblyResolve += ResolveHelpers.ResolveAssembly;
36-
Terraria.Program.SavePath = Platform.Get<IPathService>().GetStoragePath("Terraria");
37-
Terraria.Main.SkipAssemblyLoad = true;
38-
GlobalInitializer.Initialize();
39-
stopwatch.Stop();
40-
spinner.Stop();
41-
Console.WriteLine($"- done. (used {stopwatch.ElapsedMilliseconds:.00}ms)");
42-
43-
Console.Write("[USP|Info] Creating server instances... ");
44-
spinner = new ConsoleSpinner(100);
45-
spinner.Start();
46-
stopwatch.Restart();
47-
48-
int port = 7777;
49-
50-
var server1 = new ServerContext("Server1", TestWorlds.World_1);
51-
var server2 = new ServerContext("Server2", TestWorlds.World_2);
52-
53-
var router = new Router(port, server1, [server1, server2]);
54-
var cmd = new CommandHandler(router);
55-
56-
stopwatch.Stop();
57-
spinner.Stop();
58-
Console.WriteLine($"- done. (used {stopwatch.ElapsedMilliseconds:.00}ms)");
59-
60-
Task.Run(() => {
61-
server1.Program.LaunchGame(args);
62-
});
63-
Task.Run(() => {
64-
server2.Program.LaunchGame(args);
27+
28+
WorkRunner.RunTimedWork("Global initialization started...", () => {
29+
SynchronizedGuard.Load();
30+
NetworkPatcher.Load();
31+
AppDomain.CurrentDomain.AssemblyResolve += ResolveHelpers.ResolveAssembly;
32+
Terraria.Program.SavePath = Platform.Get<IPathService>().GetStoragePath("Terraria");
33+
Terraria.Main.SkipAssemblyLoad = true;
34+
GlobalInitializer.Initialize();
6535
});
6636

37+
var (server1, server2) = WorkRunner.RunTimedWork("Creating server instances...", () => {
38+
var server1 = new ServerContext("Server1", TestWorlds.World_1);
39+
var server2 = new ServerContext("Server2", TestWorlds.World_2);
40+
return (server1, server2);
41+
});
6742

68-
Console.Write("[USP|Info] Starting main servers... ");
69-
spinner = new ConsoleSpinner(100);
70-
spinner.Start();
71-
stopwatch.Restart();
43+
var (router, cmdh) = WorkRunner.RunTimedWork("Creating global network...", () => {
44+
var router = new Router(7777, server1, [server1, server2]);
45+
var cmdh = new CommandHandler(router);
46+
return (router, cmdh);
47+
});
7248

73-
router.Started += () => {
74-
stopwatch.Stop();
75-
spinner.Stop();
76-
Console.WriteLine($"- done. (used {stopwatch.ElapsedMilliseconds:.00}ms)");
49+
WorkRunner.RunTimedWorkAsync("Starting main servers...",
50+
() => {
51+
Task.Run(() => {
52+
server1.Program.LaunchGame(args);
53+
});
54+
Task.Run(() => {
55+
server2.Program.LaunchGame(args);
56+
});
57+
var tcs = new TaskCompletionSource();
58+
router.Started += () => tcs.SetResult();
59+
return tcs;
60+
},
61+
() => {
7762
Console.WriteLine();
7863
Console.WriteLine("[USP] Unified Server Process Launched successfully.");
79-
Console.WriteLine("[USP] Listening on port: {0}.", port);
64+
Console.WriteLine("[USP] Listening on port: {0}.", router.ListenPort);
8065
Console.WriteLine("[USP] Type 'help' for more information.");
8166
Console.WriteLine();
82-
};
67+
});
8368

84-
cmd.KeepReadingInput();
69+
cmdh.KeepReadingInput();
8570
}
8671
}
8772
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using OTAPI.UnifiedServerProcess.GlobalNetwork.IO;
2+
using System.Diagnostics;
3+
4+
namespace OTAPI.UnifiedServerProcess.GlobalNetwork
5+
{
6+
internal static class WorkRunner
7+
{
8+
public static void RunTimedWork(string message, Action work) {
9+
Console.Write($"[USP|Info] {message}");
10+
var spinner = new ConsoleSpinner(100);
11+
spinner.Start();
12+
Stopwatch stopwatch = new();
13+
stopwatch.Start();
14+
work();
15+
stopwatch.Stop();
16+
spinner.Stop();
17+
Console.WriteLine($" - done. (used {stopwatch.ElapsedMilliseconds:.00}ms)");
18+
}
19+
public static TOut RunTimedWork<TOut>(string message, WorkDelegate<TOut> work) {
20+
Console.Write($"[USP|Info] {message}");
21+
var spinner = new ConsoleSpinner(100);
22+
spinner.Start();
23+
Stopwatch stopwatch = new();
24+
stopwatch.Start();
25+
var output = work();
26+
stopwatch.Stop();
27+
spinner.Stop();
28+
Console.WriteLine($" - done. (used {stopwatch.ElapsedMilliseconds:.00}ms)");
29+
return output;
30+
}
31+
public static void RunTimedWorkAsync(string message, Func<TaskCompletionSource> work, Action? endAction = null) {
32+
Console.Write($"[USP|Info] {message}");
33+
var spinner = new ConsoleSpinner(100);
34+
spinner.Start();
35+
Stopwatch stopwatch = new();
36+
stopwatch.Start();
37+
work().Task.Wait();
38+
stopwatch.Stop();
39+
spinner.Stop();
40+
Console.WriteLine($" - done. (used {stopwatch.ElapsedMilliseconds:.00}ms)");
41+
endAction?.Invoke();
42+
}
43+
44+
public delegate TOut WorkDelegate<TIn, TOut>(TIn input);
45+
public delegate TOut WorkDelegate<TOut>();
46+
}
47+
}

0 commit comments

Comments
 (0)