-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMain.cs
More file actions
146 lines (124 loc) · 3.42 KB
/
Main.cs
File metadata and controls
146 lines (124 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System.Threading.Channels;
using Bridge;
using Bridge.TCP;
using Microsoft.Extensions.Logging;
using RLBotCS.ManagerTools;
using RLBotCS.Server;
using RLBotCS.Server.BridgeMessage;
using RLBotCS.Server.ServerMessage;
if (args.Length > 0 && args[0] == "--version")
{
Console.WriteLine(
"RLBotServer v5.0.0-rc.2\n"
+ $"Bridge {BridgeVersion.Version}\n"
+ "@ https://www.rlbot.org & https://github.com/RLBot/core"
);
Environment.Exit(0);
}
#if WINDOWS
WinTermColor.EnableVirtualTerminal();
#endif
var logger = Logging.GetLogger("Main");
int rlbotSocketsPort;
int gamePort;
try
{
// Parse RLBot sockets port
rlbotSocketsPort = args.Length > 0 ? int.Parse(args[0]) : LaunchManager.RlbotSocketsPort;
// Validate RLBot sockets port
if (rlbotSocketsPort < 0 || rlbotSocketsPort > 65535)
{
throw new ArgumentOutOfRangeException(
nameof(rlbotSocketsPort),
"Port number must be between 0 and 65535."
);
}
// Parse game port
gamePort =
args.Length > 1
? int.Parse(args[1])
: LaunchManager.FindUsableGamePort(rlbotSocketsPort);
// Validate game port
if (gamePort < 0 || gamePort > 65535)
{
throw new ArgumentOutOfRangeException(
nameof(gamePort),
"Port number must be between 0 and 65535."
);
}
}
catch (ArgumentOutOfRangeException ex)
{
logger.LogError(ex.Message);
return;
}
logger.LogInformation(
"Server will use port "
+ rlbotSocketsPort
+ ", expecting Rocket League on port "
+ gamePort
);
logger.LogInformation("Waiting for connections...");
// Set up the handler to use bridge to talk with the game
var bridgeChannel = Channel.CreateUnbounded<IBridgeMessage>();
var bridgeWriter = bridgeChannel.Writer;
// Set up the TCP server for RLBots
var serverChannel = Channel.CreateUnbounded<IServerMessage>();
var serverWriter = serverChannel.Writer;
Thread rlbotServer = new(() =>
{
FlatBuffersServer flatBuffersServer = new(rlbotSocketsPort, serverChannel, bridgeWriter);
try
{
flatBuffersServer.BlockingRun();
}
finally
{
flatBuffersServer.Cleanup();
}
});
rlbotServer.Start();
Thread bridgeHandler = new(() =>
{
TcpMessenger tcpMessenger = new(gamePort);
MatchStarter matchStarter = new(gamePort, rlbotSocketsPort);
BridgeHandler bridgeHandler = new(
serverWriter,
bridgeChannel.Reader,
tcpMessenger,
matchStarter
);
try
{
bridgeHandler.BlockingRun();
}
finally
{
bridgeHandler.Cleanup();
}
});
bridgeHandler.Start();
// Block until everything properly shuts down
void WaitForShutdown(bool log = true)
{
rlbotServer.Join();
if (log)
logger.LogInformation("TCP handler has shut down successfully.");
bridgeWriter.TryComplete();
bridgeHandler.Join();
if (log)
logger.LogInformation("Bridge handler has shut down successfully.");
}
void Terminate()
{
logger.LogInformation("Shutting down server...");
serverWriter.TryComplete();
WaitForShutdown();
logger.LogInformation("Server shut down successfully.");
}
// Catch sudden termination to clean up the server
AppDomain.CurrentDomain.ProcessExit += (_, _) => Terminate();
// Catch Ctrl+C to clean up the server
Console.CancelKeyPress += (_, _) => Terminate();
// Wait for a normal shutdown
WaitForShutdown(false);