@@ -16,24 +16,14 @@ namespace LightReflectiveMirror
16
16
partial class Program
17
17
{
18
18
public static void Main ( string [ ] args ) => new Program ( ) . MainAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
19
- public List < Room > GetRooms ( ) => _relay . rooms ;
20
19
21
20
public async Task MainAsync ( )
22
21
{
23
22
WriteTitle ( ) ;
24
23
instance = this ;
25
24
_startupTime = DateTime . Now ;
26
- using ( WebClient wc = new WebClient ( ) )
27
- {
28
- try
29
- {
30
- publicIP = wc . DownloadString ( "http://ipv4.icanhazip.com" ) . Replace ( "\\ r" , "" ) . Replace ( "\\ n" , "" ) . Trim ( ) ;
31
- }
32
- catch {
33
- WriteLogMessage ( "Failed to reach public IP endpoint! Using loopback address." , ConsoleColor . Yellow ) ;
34
- publicIP = "127.0.0.1" ;
35
- }
36
- }
25
+
26
+ GetPublicIP ( ) ;
37
27
38
28
if ( ! File . Exists ( CONFIG_PATH ) )
39
29
{
@@ -46,22 +36,7 @@ public async Task MainAsync()
46
36
{
47
37
conf = JsonConvert . DeserializeObject < Config > ( File . ReadAllText ( CONFIG_PATH ) ) ;
48
38
49
- // Docker variables.
50
- if ( ushort . TryParse ( Environment . GetEnvironmentVariable ( "LRM_ENDPOINT_PORT" ) , out ushort endpointPort ) )
51
- conf . EndpointPort = endpointPort ;
52
-
53
- if ( ushort . TryParse ( Environment . GetEnvironmentVariable ( "LRM_TRANSPORT_PORT" ) , out ushort transportPort ) )
54
- conf . TransportPort = transportPort ;
55
-
56
- if ( ushort . TryParse ( Environment . GetEnvironmentVariable ( "LRM_PUNCHER_PORT" ) , out ushort puncherPort ) )
57
- conf . NATPunchtroughPort = puncherPort ;
58
-
59
- string LBAuthKey = Environment . GetEnvironmentVariable ( "LRM_LB_AUTHKEY" ) ;
60
- if ( ! string . IsNullOrWhiteSpace ( LBAuthKey ) )
61
- {
62
- conf . LoadBalancerAuthKey = LBAuthKey ;
63
- WriteLogMessage ( "\n Loaded LB auth key from environment variable\n " , ConsoleColor . Green ) ;
64
- }
39
+ ConfigureDocker ( ) ;
65
40
66
41
WriteLogMessage ( "Loading Assembly... " , ConsoleColor . White , true ) ;
67
42
try
@@ -75,109 +50,13 @@ public async Task MainAsync()
75
50
76
51
if ( transport != null )
77
52
{
78
- var transportClass = asm . GetType ( conf . TransportClass ) ;
79
- WriteLogMessage ( "OK" , ConsoleColor . Green ) ;
80
-
81
- WriteLogMessage ( "\n Loading Transport Methods... " , ConsoleColor . White , true ) ;
82
- CheckMethods ( transportClass ) ;
83
- WriteLogMessage ( "OK" , ConsoleColor . Green ) ;
84
-
85
- WriteLogMessage ( "\n Invoking Transport Methods..." ) ;
86
-
87
- if ( _awakeMethod != null )
88
- _awakeMethod . Invoke ( transport , null ) ;
89
-
90
- if ( _startMethod != null )
91
- _startMethod . Invoke ( transport , null ) ;
92
-
93
- WriteLogMessage ( "\n Starting Transport... " , ConsoleColor . White , true ) ;
94
-
95
- transport . OnServerError = ( clientID , error ) =>
96
- {
97
- WriteLogMessage ( $ "Transport Error, Client: { clientID } , Error: { error } ", ConsoleColor . Red ) ;
98
- } ;
99
-
100
- transport . OnServerConnected = ( clientID ) =>
101
- {
102
- WriteLogMessage ( $ "Transport Connected, Client: { clientID } ", ConsoleColor . Cyan ) ;
103
- _currentConnections . Add ( clientID ) ;
104
- _relay . ClientConnected ( clientID ) ;
105
-
106
- if ( conf . EnableNATPunchtroughServer )
107
- {
108
- string natID = Guid . NewGuid ( ) . ToString ( ) ;
109
- _pendingNATPunches . Add ( clientID , natID ) ;
110
- _NATRequestPosition = 0 ;
111
- _NATRequest . WriteByte ( ref _NATRequestPosition , ( byte ) OpCodes . RequestNATConnection ) ;
112
- _NATRequest . WriteString ( ref _NATRequestPosition , natID ) ;
113
- _NATRequest . WriteInt ( ref _NATRequestPosition , conf . NATPunchtroughPort ) ;
114
- transport . ServerSend ( clientID , 0 , new ArraySegment < byte > ( _NATRequest , 0 , _NATRequestPosition ) ) ;
115
- }
116
- } ;
117
-
118
- _relay = new RelayHandler ( transport . GetMaxPacketSize ( 0 ) ) ;
119
-
120
- transport . OnServerDataReceived = _relay . HandleMessage ;
121
- transport . OnServerDisconnected = ( clientID ) =>
122
- {
123
- _currentConnections . Remove ( clientID ) ;
124
- _relay . HandleDisconnect ( clientID ) ;
125
-
126
- if ( NATConnections . ContainsKey ( clientID ) )
127
- NATConnections . Remove ( clientID ) ;
128
-
129
- if ( _pendingNATPunches . TryGetByFirst ( clientID , out _ ) )
130
- _pendingNATPunches . Remove ( clientID ) ;
131
- } ;
132
-
133
- transport . ServerStart ( conf . TransportPort ) ;
134
-
135
- WriteLogMessage ( "OK" , ConsoleColor . Green ) ;
53
+ ConfigureTransport ( asm ) ;
136
54
137
55
if ( conf . UseEndpoint )
138
- {
139
- WriteLogMessage ( "\n Starting Endpoint Service... " , ConsoleColor . White , true ) ;
140
- var endpointService = new EndpointServer ( ) ;
141
-
142
- if ( endpointService . Start ( conf . EndpointPort ) )
143
- {
144
- WriteLogMessage ( "OK" , ConsoleColor . Green ) ;
145
- Endpoint . RoomsModified ( ) ;
146
- }
147
- else
148
- {
149
- WriteLogMessage ( "FAILED\n Please run as administrator or check if port is in use." , ConsoleColor . DarkRed ) ;
150
- }
151
- }
56
+ ConfigureEndpoint ( ) ;
152
57
153
58
if ( conf . EnableNATPunchtroughServer )
154
- {
155
- WriteLogMessage ( "\n Starting NatPunchthrough Socket... " , ConsoleColor . White , true ) ;
156
-
157
- try
158
- {
159
- _punchServer = new UdpClient ( conf . NATPunchtroughPort ) ;
160
-
161
- WriteLogMessage ( "OK\n " , ConsoleColor . Green , true ) ;
162
-
163
- WriteLogMessage ( "\n Starting NatPunchthrough Thread... " , ConsoleColor . White , true ) ;
164
- var natThread = new Thread ( new ThreadStart ( RunNATPunchLoop ) ) ;
165
-
166
- try
167
- {
168
- natThread . Start ( ) ;
169
- }
170
- catch ( Exception e )
171
- {
172
- WriteLogMessage ( "FAILED\n " + e , ConsoleColor . DarkRed ) ;
173
- }
174
- }
175
- catch ( Exception e )
176
- {
177
- WriteLogMessage ( "FAILED\n Check if port is in use." , ConsoleColor . DarkRed , true ) ;
178
- Console . WriteLine ( e ) ;
179
- }
180
- }
59
+ ConfigurePunchthrough ( ) ;
181
60
}
182
61
else
183
62
{
@@ -197,6 +76,11 @@ public async Task MainAsync()
197
76
await RegisterSelfToLoadBalancer ( ) ;
198
77
}
199
78
79
+ await HeartbeatLoop ( ) ;
80
+ }
81
+
82
+ private async Task HeartbeatLoop ( )
83
+ {
200
84
while ( true )
201
85
{
202
86
try
@@ -209,7 +93,6 @@ public async Task MainAsync()
209
93
WriteLogMessage ( "Error During Transport Update! " + e , ConsoleColor . Red ) ;
210
94
}
211
95
212
-
213
96
_currentHeartbeatTimer ++ ;
214
97
215
98
if ( _currentHeartbeatTimer >= conf . UpdateHeartbeatInterval )
@@ -224,7 +107,9 @@ public async Task MainAsync()
224
107
if ( DateTime . Now > Endpoint . lastPing . AddSeconds ( 60 ) )
225
108
{
226
109
// Dont await that on main thread. It would cause a lag spike for clients.
110
+ #pragma warning disable CS4014
227
111
RegisterSelfToLoadBalancer ( ) ;
112
+ #pragma warning restore CS4014
228
113
}
229
114
}
230
115
@@ -239,11 +124,8 @@ public async void UpdateLoadbalancerServers()
239
124
{
240
125
try
241
126
{
242
- using ( WebClient wc = new ( ) )
243
- {
244
- wc . Headers . Add ( "Authorization" , conf . LoadBalancerAuthKey ) ;
245
- await wc . DownloadStringTaskAsync ( $ "http://{ conf . LoadBalancerAddress } :{ conf . LoadBalancerPort } /api/roomsupdated") ;
246
- }
127
+ webClient . Headers . Add ( "Authorization" , conf . LoadBalancerAuthKey ) ;
128
+ await webClient . DownloadStringTaskAsync ( $ "http://{ conf . LoadBalancerAddress } :{ conf . LoadBalancerPort } /api/roomsupdated") ;
247
129
}
248
130
catch { } // LLB might be down, ignore.
249
131
}
@@ -279,13 +161,5 @@ private async Task<bool> RegisterSelfToLoadBalancer()
279
161
return false ;
280
162
}
281
163
}
282
-
283
- void CheckMethods ( Type type )
284
- {
285
- _awakeMethod = type . GetMethod ( "Awake" , BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic ) ;
286
- _startMethod = type . GetMethod ( "Start" , BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic ) ;
287
- _updateMethod = type . GetMethod ( "Update" , BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic ) ;
288
- _lateUpdateMethod = type . GetMethod ( "LateUpdate" , BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic ) ;
289
- }
290
164
}
291
165
}
0 commit comments