-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathUnityNetworking.cs
More file actions
607 lines (468 loc) · 22.9 KB
/
UnityNetworking.cs
File metadata and controls
607 lines (468 loc) · 22.9 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
using System.Collections;
using Engine;
using Engine.Events;
using Engine.Utility;
using UnityEngine;
namespace Engine.Networking {
public class NetworkMessages {
public static string NetworkConnected = "networkConnected";
public static string NetworkDisconnected = "networkDisconnected";
public static string NetworkNatTestComplete = "networkNatTestComplete";
}
public class UnityNetworking : GameObjectBehavior {
#if NETWORK_USE_UNITY
// TODO move to downloadable config.
public string masterserverGameName = "defaultgame";
public int defaultServerPort = 50666;
public int connectTimeoutValue = 30;
public int connectTestTimeValue = 9;
public int masterServerPort = 25010;
public int connectionTesterPort = 25011;
public int natFacilitatorPort = 25011;
public string masterserveriPAddressOrDns = "matchup.test.com";
private bool awaitingHostList = false;
// CONNECTING
private bool connecting = false;
private HostData lastConnectHostData;
private string[] lastConnectIP = null;
private int lastConnectPort;
private string lastConnectPW = "";
private float lastConnectStarted;
private bool lastConnectUsedNAT = true;
private VoidDelegate lastConnectionFailDelegate;
private bool lastConnectMayRetry = true;
private NetworkConnectionError lastConnectionError;
private static bool hasTestedNAT = false;
private static bool testedUseNat = false;
// MASTERSERVER
private float lastMSRetry = 0;
private int msRetries = 0;
/*
* METHODS AVAILABLE
*
public bool ReadyLoading()
Is NAT tested & Masterserver loaded?
public void HostDataConnect(HostData hData, string password, bool doRetryConnection, VoidDelegate failDelegate)
public void DirectConnect(string IP, int port, string password, bool doRetryConnection, VoidDelegate failDelegate)
public void CancelConnection()
public bool IsConnecting()
public FetchHostList()
Request new hostlist, you need to manually call this!
public void SetHostListDelegate(VoidDelegate newD)
Required! Set this to a void function of your own to be notified when the new hostlist has arrived.
public HostData[] GetHostData()
Get this list in your HostList delegate function!
public bool HasReceivedHostList()
Set TRUE after a succesfull Hostlist response, call FetchHostlist first
public void StartServer(string password, int port, int maxConnections, bool enableSecurity)
public void RegisterServer(string serverTitle, string comment)
public void UnRegisterServer()
public bool GetNATStatus()
public string ConnectingToAddress()
IP:port..i.e.: 127.0.0.1:25005
public string[] LastIP()
public int LastPort()
public NetworkConnectionError LastConnectionError()
public float TimeSinceLastConnect()
public static byte[] StringToBytes(string str)
Convert string to byte[] to workaround the 4096 character limit
public static string BytesToString(byte[] by)
*/
public static UnityNetworking Instance;
public delegate void VoidDelegate();
private static bool hasLoadedMasterserverSettings = false;
private static HostData[] hostData = null; //Latest cached hostData result
public void Awake() {
if (Instance != null && this != Instance) {
//There is already a copy of this script running
Destroy(this);
return;
}
Instance = this;
StartCoroutine(TestConnection());
//DontDestroyOnLoad(this); //enable this to make this an persistent GO
}
public void SetupMasterServer() {
StartCoroutine(SetupMasterServerRoutine());
}
public bool ReadyLoading() {
return (hasLoadedMasterserverSettings && hasTestedNAT);
}
private IEnumerator SetupMasterServerRoutine() {
// Set ports & IP
// Try to resolve the masterserver IP instead of hardcoding an IP.
// Do note that a DNS resolve adds some plugins to the webplayer.
// Call a webpage containing the right IP(s)ipt to reduce filesize
MasterServer.port = masterServerPort;
Network.connectionTesterPort = connectionTesterPort;
Network.natFacilitatorPort = natFacilitatorPort;
Network.natFacilitatorIP = Network.connectionTesterIP = MasterServer.ipAddress = masterserveriPAddressOrDns;
//Network.natFacilitatorIP = MasterServer.ipAddress = masterserveriPAddressOrDns;
//Network.natFacilitatorIP = MasterServer.ipAddress = masterserveriPAddressOrDns;
// If we don't set any IP/ports we use the public&free Unity masterserver.
hasLoadedMasterserverSettings = true;
FetchHostList();
yield break;
}
// CONNECTING
public void HostDataConnect(HostData hData, string password, bool doRetryConnection, VoidDelegate failDelegate) {
StartedConnecting();
lastConnectHostData = hData;
lastConnectUsedNAT = lastConnectHostData.useNat;
lastConnectMayRetry = doRetryConnection;
if (password == "")
Network.Connect(hData);
else
Network.Connect(hData, password);
Invoke("ConnectTimeout", connectTimeoutValue);
lastConnectIP = hData.ip;
lastConnectPort = hData.port;
lastConnectPW = password;
lastConnectStarted = Time.time;
if (failDelegate != null)
lastConnectionFailDelegate = failDelegate;
connecting = true;
}
public void DirectConnect(string IP, int port, string password, bool doRetryConnection, VoidDelegate failDelegate) {
string[] ips = new string[1];
ips[0] = IP;
DirectConnect(ips, port, password, doRetryConnection, failDelegate);
}
public void DirectConnect(string[] IP, int port, string password, bool doRetryConnection, VoidDelegate failDelegate) {
StartedConnecting();
lastConnectMayRetry = doRetryConnection;
lastConnectUsedNAT = false;
if (password == "")
Network.Connect(IP, port);
else
Network.Connect(IP, port, password);
Invoke("ConnectTimeout", connectTimeoutValue);
connecting = true;
lastConnectHostData = null;
lastConnectIP = IP;
lastConnectPort = port;
lastConnectPW = password;
lastConnectStarted = Time.time;
if (failDelegate != null)
lastConnectionFailDelegate = failDelegate;
}
private void StartedConnecting() {
CancelInvoke("ConnectTimeout");
connecting = true;
lastConnectStarted = Time.realtimeSinceStartup;
}
public void CancelConnection() {
CancelInvoke("ConnectTimeout");
lastConnectMayRetry = false;
connecting = false;
}
public void ConnectTimeout() {
LogUtil.Log("Connect timeout");
OnFailedToConnect(NetworkConnectionError.NoError);
}
private void OnFailedToConnect(NetworkConnectionError info) {
CancelInvoke("ConnectTimeout");
if (lastConnectIP != null) {
LogUtil.Log("Failed to connect ["
+ lastConnectIP[0]
+ ":"
+ lastConnectPort
+ " ] info:"
+ info);
StartCoroutine(FailedConnectRetry(info));
}
else {
LogUtil.Log("Failed to connect, no data: " + info);
connecting = false;
}
}
public void OnConnectedToServer() {
CancelInvoke("ConnectTimeout");
}
//Try again with some different settings..mainly try connecting without NAT
private IEnumerator FailedConnectRetry(NetworkConnectionError info) {
lastConnectionError = info;
if (!lastConnectMayRetry
|| info == NetworkConnectionError.TooManyConnectedPlayers
|| info == NetworkConnectionError.InvalidPassword) {
//Stop retrying
}
else if (lastConnectUsedNAT
|| lastConnectPort != defaultServerPort) {
//Retry (without NAT) on default port!
yield return 0; //Workaround against "too many open connections"
DirectConnect(lastConnectIP,
UnityNetworking.Instance.defaultServerPort,
lastConnectPW,
true,
lastConnectionFailDelegate);
yield break;
}
//Finished: failed
connecting = false;
if (lastConnectionFailDelegate != null) {
lastConnectionFailDelegate();
}
}
public bool IsConnecting() {
return connecting;
}
// MASTERSERVER
public void OnFailedToConnectToMasterServer(NetworkConnectionError info) {
//Two possible causes: FetchHostList OR RegisterHost failed to connect
LogUtil.Log("OnFailedToConnectToMasterServer: " + info);
// 5,10,20,50,.. to not overload the masterserver when its down
int retryTime = 5 + 5 * msRetries * msRetries;
LogUtil.Log("UnityNetworking: OnFailedToConnectToMasterServer - msRetries: " + msRetries);
LogUtil.Log("UnityNetworking: OnFailedToConnectToMasterServer - retryTime: " + retryTime);
if (lastMSRetry < Time.time - retryTime) {
LogUtil.Log("UnityNetworking: OnFailedToConnectToMasterServer - lastMSRetry: " + lastMSRetry);
lastMSRetry = Time.time;
FetchHostList();
}
}
private float lastHostListRequest = -999;
public void FetchHostList() {
LogUtil.Log("UnityNetworking: FetchHostList: " + masterserverGameName);
if (!hasLoadedMasterserverSettings) {
LogUtil.LogError("Calling FetchHostList but we havent loaded MS settings yet");
return;
}
hostData = MasterServer.PollHostList();
LogUtil.Log("UnityNetworking: FetchHostList - PollHostList: ");
LogUtil.Log("UnityNetworking: FetchHostList - hostData.Length: " + hostData.Length);
if (lastHostListRequest < Time.realtimeSinceStartup - 1) {
LogUtil.Log("UnityNetworking: FetchHostList - lastHostListRequest: " + lastHostListRequest);
LogUtil.Log("UnityNetworking: FetchHostList - Time.realtimeSinceStartup: " + Time.realtimeSinceStartup);
lastHostListRequest = Time.realtimeSinceStartup;
MasterServer.RequestHostList(masterserverGameName);
LogUtil.Log("UnityNetworking: FetchHostList - RequestHostList: " + masterserverGameName);
}
}
private VoidDelegate currentHostListDelegate;
public void SetHostListDelegate(VoidDelegate newD) {
currentHostListDelegate = newD;
}
public HostData[] GetHostData() {
return hostData;
}
private bool hasReceivedHostListResponse = false;
public bool HasReceivedHostList() {
return hasReceivedHostListResponse;
}
public void OnMasterServerEvent(MasterServerEvent msEvent) {
LogUtil.Log(Time.realtimeSinceStartup + " OnMasterEvent: " + msEvent);
switch (msEvent) {
case MasterServerEvent.HostListReceived:
//WARNING: It does 1 call per item in the full host list!
//A list of 100 items generates 100 calls. The 34th call contains 34 items total etc.
LogUtil.Log("UnityNetworking: OnMasterServerEvent - HostListReceived: ");
StartCoroutine(WaitForAllHostData());
break;
case MasterServerEvent.RegistrationFailedGameName:
LogUtil.Log("UnityNetworking: OnMasterServerEvent - RegistrationFailedGameName: ");
break;
case MasterServerEvent.RegistrationFailedGameType:
LogUtil.Log("UnityNetworking: OnMasterServerEvent - RegistrationFailedGameType: ");
break;
case MasterServerEvent.RegistrationFailedNoServer:
LogUtil.Log("Masterserver error: " + msEvent);
LogUtil.Log("UnityNetworking: OnMasterServerEvent - RegistrationFailedNoServer: ");
break;
case MasterServerEvent.RegistrationSucceeded:
LogUtil.Log("UnityNetworking: OnMasterServerEvent - RegistrationSucceeded: ");
break;
default:
break;
}
}
private IEnumerator WaitForAllHostData() {
LogUtil.Log("MasterServer polling...");
if (awaitingHostList)
yield break;
LogUtil.Log("MasterServer awaitingHostList...");
awaitingHostList = true;
hostData = MasterServer.PollHostList();
LogUtil.Log("MasterServer hostData.Length: " + hostData.Length);
while (true) {
yield return new WaitForSeconds(0.2f);
LogUtil.Log("MasterServer .PollHostList().Length: " + MasterServer.PollHostList().Length);
LogUtil.Log("MasterServer hostData.Length: " + hostData.Length);
if (MasterServer.PollHostList().Length == hostData.Length) {
break;
}
hostData = MasterServer.PollHostList();
for (int i = 0; i < hostData.Length; i++) {
LogUtil.Log("Masterserver hostdata #" + i.ToString() + " gameName: " + hostData[i].gameName);
LogUtil.Log("Masterserver hostdata #" + i.ToString() + " gameType: " + hostData[i].gameType);
LogUtil.Log("Masterserver hostdata #" + i.ToString() + " playerLimit: " + hostData[i].playerLimit);
LogUtil.Log("Masterserver hostdata #" + i.ToString() + " ip: " + hostData[i].ip);
LogUtil.Log("Masterserver hostdata #" + i.ToString() + " connectedPlayers: " + hostData[i].connectedPlayers);
LogUtil.Log("Masterserver hostdata #" + i.ToString() + " useNat: " + hostData[i].useNat);
}
LogUtil.Log("MasterServer poll loop...");
}
if (currentHostListDelegate != null) {
currentHostListDelegate();
}
hasReceivedHostListResponse = true;
awaitingHostList = false;
}
// SERVER
public void StartServer(string password, int port, int maxConnections, bool enableSecurity) {
if (port <= 1024) {
LogUtil.LogError("StartServer tries to use port <=1024. This will probably not work and is illegal! Use a different port!");
}
if (password != "") {
Network.incomingPassword = password;
}
if (enableSecurity) {
try {
if (Network.connections.Length == 0)
Network.InitializeSecurity();
}
catch (System.Exception e) {
LogUtil.Log("Error InitializeSecurity:" + e.Message + e.StackTrace);
}
}
Network.InitializeServer(maxConnections, port, GetNATStatus());
}
public void RegisterHost(string serverTitle, string comment) {
if (!Network.isServer) {
LogUtil.LogError("RegisterServer: is not a server!");
return;
}
if (!ReadyLoading())
LogUtil.Log("RegisterHost; wasn't ready loading network settings yet though!");
MasterServer.RegisterHost(masterserverGameName, serverTitle, comment);
}
public void UnregisterHost() {
if (!Network.isServer) {
LogUtil.LogError("RegisterServer: is not a server!");
return;
}
MasterServer.UnregisterHost();
}
public void OnDisconnectedFromServer(NetworkDisconnection info) {
if (Network.isServer)
UnregisterHost();
SendMessageUpwards("OnDisconnectedSession", SendMessageOptions.DontRequireReceiver);
Messenger<NetworkDisconnection>.Broadcast(NetworkMessages.NetworkDisconnected, info);
}
// CONNECTIONTESTER
public bool GetNATStatus() {
if (!hasTestedNAT)
LogUtil.Log("Calling GetNATStatus, but we havent finished testing yet!");
//return testedUseNat;
return true;
}
private IEnumerator TestConnection() {
if (hasTestedNAT)
yield break;
while (!hasLoadedMasterserverSettings)
yield return 0;
testedUseNat = !Network.HavePublicAddress();
ConnectionTesterStatus connectionTestResult = ConnectionTesterStatus.Undetermined;
float timeoutAt = Time.realtimeSinceStartup + connectTestTimeValue;
float timer = 0;
bool probingPublicIP = false;
string testMessage = "";
while (!hasTestedNAT) {
yield return 0;
if (Time.realtimeSinceStartup >= timeoutAt) {
LogUtil.Log("TestConnect NAT test aborted; timeout");
break;
}
connectionTestResult = Network.TestConnection();
switch (connectionTestResult) {
case ConnectionTesterStatus.Error:
testMessage = "Problem determining NAT capabilities";
hasTestedNAT = false;
break;
case ConnectionTesterStatus.Undetermined:
testMessage = "Undetermined NAT capabilities";
hasTestedNAT = false;
break;
case ConnectionTesterStatus.PublicIPIsConnectable:
testMessage = "Directly connectable public IP address.";
testedUseNat = false;
hasTestedNAT = true;
break;
// This case is a bit special as we now need to check if we can
// circumvent the blocking by using NAT punchthrough
case ConnectionTesterStatus.PublicIPPortBlocked:
testMessage = "Non-connectble public IP address (port " + defaultServerPort + " blocked), running a server is impossible.";
hasTestedNAT = false;
// If no NAT punchthrough test has been performed on this public IP, force a test
if (!probingPublicIP) {
LogUtil.Log("Testing if firewall can be circumvented");
connectionTestResult = Network.TestConnectionNAT();
probingPublicIP = true;
timer = Time.time + 10;
}
// NAT punchthrough test was performed but we still get blocked
else if (Time.time > timer) {
probingPublicIP = false; // reset
testedUseNat = true;
hasTestedNAT = true;
}
break;
case ConnectionTesterStatus.PublicIPNoServerStarted:
testMessage = "Public IP address but server not initialized, it must be started to check server accessibility. Restart connection test when ready.";
break;
case ConnectionTesterStatus.LimitedNATPunchthroughPortRestricted:
testMessage = "Limited NAT punchthrough capabilities. Cannot connect to all types of NAT servers. Running a server is ill adviced as not everyone can connect.";
testedUseNat = true;
hasTestedNAT = true;
break;
case ConnectionTesterStatus.LimitedNATPunchthroughSymmetric:
testMessage = "Limited NAT punchthrough capabilities. Cannot connect to all types of NAT servers. Running a server is ill adviced as not everyone can connect.";
testedUseNat = true;
hasTestedNAT = true;
break;
case ConnectionTesterStatus.NATpunchthroughAddressRestrictedCone:
case ConnectionTesterStatus.NATpunchthroughFullCone:
testMessage = "NAT punchthrough capable. Can connect to all servers and receive connections from all clients. Enabling NAT punchthrough functionality.";
testedUseNat = true;
hasTestedNAT = true;
break;
default:
testMessage = "Error in test routine, got " + connectionTestResult;
break;
}
}
hasTestedNAT = true;
//Messenger<bool, UnityEngine.ConnectionTesterStatus, bool, bool, string>.Broadcast(NetworkMessages.NetworkNatTestComplete, testedUseNat, connectionTestResult, probingPublicIP, hasTestedNAT, testMessage);
LogUtil.Log("TestConnection result: testedUseNat=" + testedUseNat + " connectionTestResult=" + connectionTestResult + " probingPublicIP=" + probingPublicIP + " hasTestedNAT=" + hasTestedNAT + " testMessage=" + testMessage);
}
public void SetNetworkEnabled(int groupIndex, bool enabled) {
Network.isMessageQueueRunning = enabled;
Network.SetSendingEnabled(groupIndex, enabled);
//Network.SetReceivingEnabled(Network.player, groupIndex, enabled);
}
// PROPERTIES / HELPERS
public string ConnectingToAddress() {
return lastConnectIP[0] + ":" + lastConnectPort;
}
public string[] LastIP() {
return lastConnectIP;
}
public int LastPort() {
return lastConnectPort;
}
public NetworkConnectionError LastConnectionError() {
return lastConnectionError;
}
public float TimeSinceLastConnect() {
return Time.realtimeSinceStartup - lastConnectStarted;
}
//Send/receive large amounds of data via byte[]
public static byte[] StringToBytes(string str) {
return System.Text.Encoding.UTF8.GetBytes(str);
}
public static string BytesToString(byte[] by) {
return System.Text.Encoding.UTF8.GetString(by);
}
#endif
}
}