-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathNetworkDiscovery.cs
534 lines (409 loc) · 13.5 KB
/
NetworkDiscovery.cs
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
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using UnityEngine.Profiling;
using UnityEngine.SceneManagement;
using System.Globalization;
namespace NetworkDiscoveryUnity
{
public class NetworkDiscovery : MonoBehaviour
{
public class DiscoveryInfo
{
public readonly IPEndPoint EndPoint;
public readonly IReadOnlyDictionary<string, string> KeyValuePairs;
private float m_timeWhenReceived = 0f;
public float TimeSinceReceived => Time.realtimeSinceStartup - m_timeWhenReceived;
public DiscoveryInfo (IPEndPoint endPoint, Dictionary<string, string> keyValuePairs)
{
this.EndPoint = endPoint;
this.KeyValuePairs = keyValuePairs;
m_timeWhenReceived = Time.realtimeSinceStartup;
}
public ushort GetGameServerPort() => ushort.Parse(this.KeyValuePairs[kPortKey], CultureInfo.InvariantCulture);
public bool TryGetGameServerPort(out ushort port)
{
port = 0;
return this.KeyValuePairs.TryGetValue(kPortKey, out string portString)
&& ushort.TryParse(portString, NumberStyles.None, CultureInfo.InvariantCulture, out port);
}
}
public UnityEngine.Events.UnityEvent<DiscoveryInfo> onReceivedServerResponse =
new UnityEngine.Events.UnityEvent<DiscoveryInfo>();
// server sends this data as a response to broadcast
readonly Dictionary<string, string> m_responseData =
new Dictionary<string, string> (System.StringComparer.InvariantCulture);
public static NetworkDiscovery Instance { get ; private set ; }
public const string kSignatureKey = "Signature", kPortKey = "Port", kMapNameKey = "Map";
public const int kDefaultServerPort = 18418;
[SerializeField] int m_serverPort = kDefaultServerPort;
UdpClient m_serverUdpCl = null;
UdpClient m_clientUdpCl = null;
public static bool SupportedOnThisPlatform { get { return Application.platform != RuntimePlatform.WebGLPlayer; } }
public int gameServerPortNumber = 7777;
private static string s_cachedSignature = null;
void Awake ()
{
if (null == Instance)
Instance = this;
RegisterResponseData(kSignatureKey, GetCachedSignature());
RegisterResponseData(kPortKey, this.gameServerPortNumber.ToString(CultureInfo.InvariantCulture));
RegisterResponseData(kMapNameKey, SceneManager.GetActiveScene().name);
}
void OnEnable()
{
SceneManager.activeSceneChanged += OnActiveSceneChanged;
}
void OnDisable ()
{
SceneManager.activeSceneChanged -= OnActiveSceneChanged;
ShutdownUdpClients ();
}
void OnActiveSceneChanged(Scene oldScene, Scene newScene)
{
RegisterResponseData(kMapNameKey, SceneManager.GetActiveScene().name);
}
void Update()
{
UpdateServer();
UpdateClient();
}
public void EnsureServerIsInitialized()
{
if (m_serverUdpCl != null)
return;
m_serverUdpCl = new UdpClient (m_serverPort);
RunSafe( () => { m_serverUdpCl.EnableBroadcast = true; } );
RunSafe( () => { m_serverUdpCl.MulticastLoopback = false; } );
}
void EnsureClientIsInitialized()
{
if (m_clientUdpCl != null)
return;
m_clientUdpCl = new UdpClient (0);
RunSafe( () => { m_clientUdpCl.EnableBroadcast = true; } );
// turn off receiving from our IP
RunSafe( () => { m_clientUdpCl.MulticastLoopback = false; } );
}
void ShutdownUdpClients()
{
CloseServerUdpClient();
CloseClientUdpClient();
}
public void CloseServerUdpClient()
{
if (m_serverUdpCl != null) {
m_serverUdpCl.Close ();
m_serverUdpCl = null;
}
}
void CloseClientUdpClient()
{
if (m_clientUdpCl != null) {
m_clientUdpCl.Close ();
m_clientUdpCl = null;
}
}
static DiscoveryInfo ReadDataFromUdpClient(UdpClient udpClient)
{
// only proceed if there is available data in network buffer, or otherwise Receive() will block
// average time for UdpClient.Available : 10 us
if (udpClient.Available <= 0)
return null;
Profiler.BeginSample("UdpClient.Receive");
IPEndPoint remoteEP = new IPEndPoint (IPAddress.Any, 0);
byte[] receivedBytes = udpClient.Receive (ref remoteEP);
Profiler.EndSample ();
if (remoteEP != null && receivedBytes != null && receivedBytes.Length > 0) {
Profiler.BeginSample ("Convert data");
var dict = ConvertByteArrayToDictionary (receivedBytes);
Profiler.EndSample ();
return new DiscoveryInfo(remoteEP, dict);
}
return null;
}
void UpdateServer()
{
if (null == m_serverUdpCl)
return;
// average time for this (including data receiving and processing): less than 100 us
Profiler.BeginSample ("Receive broadcast");
// var timer = System.Diagnostics.Stopwatch.StartNew ();
var info = ReadDataFromUdpClient(m_serverUdpCl);
if (info != null)
OnReceivedBroadcast(info);
// Debug.Log("receive broadcast time: " + timer.GetElapsedMicroSeconds () + " us");
Profiler.EndSample ();
}
void OnReceivedBroadcast(DiscoveryInfo info)
{
if(info.KeyValuePairs.ContainsKey(kSignatureKey) && info.KeyValuePairs[kSignatureKey] == GetCachedSignature())
{
// signature matches
// send response
Profiler.BeginSample("Send response");
byte[] bytes = ConvertDictionaryToByteArray( m_responseData );
m_serverUdpCl.Send( bytes, bytes.Length, info.EndPoint );
Profiler.EndSample();
}
}
void UpdateClient()
{
if (null == m_clientUdpCl)
return;
var info = ReadDataFromUdpClient(m_clientUdpCl);
if (info != null)
OnReceivedServerResponse(info);
}
public static byte[] GetDiscoveryRequestData()
{
Profiler.BeginSample("ConvertDictionaryToByteArray");
var dict = new Dictionary<string, string>(System.StringComparer.InvariantCulture) {{kSignatureKey, GetCachedSignature()}};
byte[] buffer = ConvertDictionaryToByteArray (dict);
Profiler.EndSample();
return buffer;
}
public void SendBroadcast()
{
if (!SupportedOnThisPlatform)
return;
byte[] buffer = GetDiscoveryRequestData();
// We can't just send packet to 255.255.255.255 - the OS will only broadcast it to the network interface
// which the socket is bound to.
// We need to broadcast packet on every network interface.
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, m_serverPort);
foreach(var address in GetBroadcastAdresses())
{
endPoint.Address = address;
SendDiscoveryRequest(endPoint, buffer);
}
}
public void SendDiscoveryRequest(IPEndPoint endPoint)
{
SendDiscoveryRequest(endPoint, GetDiscoveryRequestData());
}
void SendDiscoveryRequest(IPEndPoint endPoint, byte[] buffer)
{
if (!SupportedOnThisPlatform)
return;
EnsureClientIsInitialized();
if (null == m_clientUdpCl)
return;
Profiler.BeginSample("UdpClient.Send");
try {
m_clientUdpCl.Send (buffer, buffer.Length, endPoint);
} catch(SocketException ex) {
if(ex.ErrorCode == 10051) {
// Network is unreachable
// ignore this error
} else {
throw;
}
}
Profiler.EndSample();
}
public static IPAddress[] GetBroadcastAdresses()
{
// try multiple methods - because some of them may fail on some devices, especially if IL2CPP comes into play
IPAddress[] ips = null;
RunSafe(() => ips = GetBroadcastAdressesFromNetworkInterfaces(), false);
if (null == ips || ips.Length < 1)
{
// try another method
RunSafe(() => ips = GetBroadcastAdressesFromHostEntry(), false);
}
if (null == ips || ips.Length < 1)
{
// all methods failed, or there is no network interface on this device
// just use full-broadcast address
ips = new IPAddress[]{IPAddress.Broadcast};
}
return ips;
}
static IPAddress[] GetBroadcastAdressesFromNetworkInterfaces()
{
List<IPAddress> ips = new List<IPAddress>();
var nifs = NetworkInterface.GetAllNetworkInterfaces()
.Where(nif => nif.OperationalStatus == OperationalStatus.Up)
.Where(nif => nif.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || nif.NetworkInterfaceType == NetworkInterfaceType.Ethernet);
foreach (var nif in nifs)
{
foreach (UnicastIPAddressInformation ipInfo in nif.GetIPProperties().UnicastAddresses)
{
var ip = ipInfo.Address;
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
if(ToBroadcastAddress(ref ip, ipInfo.IPv4Mask))
ips.Add(ip);
}
}
}
return ips.ToArray();
}
static IPAddress[] GetBroadcastAdressesFromHostEntry()
{
var ips = new List<IPAddress> ();
IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
foreach(var address in hostEntry.AddressList)
{
if (address.AddressFamily == AddressFamily.InterNetwork)
{
// this is IPv4 address
// convert it to broadcast address
// use default subnet
var subnetMask = GetDefaultSubnetMask(address);
if (subnetMask != null)
{
var broadcastAddress = address;
if (ToBroadcastAddress(ref broadcastAddress, subnetMask))
ips.Add( broadcastAddress );
}
}
}
if (ips.Count > 0)
{
// if we found at least 1 ip, then also add full-broadcast address
// this will compensate in case we used a wrong subnet mask
ips.Add(IPAddress.Broadcast);
}
return ips.ToArray();
}
static bool ToBroadcastAddress(ref IPAddress ip, IPAddress subnetMask)
{
if (ip.AddressFamily != AddressFamily.InterNetwork || subnetMask.AddressFamily != AddressFamily.InterNetwork)
return false;
byte[] bytes = ip.GetAddressBytes();
byte[] subnetMaskBytes = subnetMask.GetAddressBytes();
for(int i=0; i < 4; i++)
{
// on places where subnet mask has 1s, address bits are copied,
// and on places where subnet mask has 0s, address bits are 1
bytes[i] = (byte) ((~subnetMaskBytes[i]) | bytes[i]);
}
ip = new IPAddress(bytes);
return true;
}
static IPAddress GetDefaultSubnetMask(IPAddress ip)
{
if (ip.AddressFamily != AddressFamily.InterNetwork)
return null;
IPAddress subnetMask;
byte[] bytes = ip.GetAddressBytes();
byte firstByte = bytes[0];
if (firstByte >= 0 && firstByte <= 127)
subnetMask = new IPAddress(new byte[]{255, 0, 0, 0});
else if (firstByte >= 128 && firstByte <= 191)
subnetMask = new IPAddress(new byte[]{255, 255, 0, 0});
else if (firstByte >= 192 && firstByte <= 223)
subnetMask = new IPAddress(new byte[]{255, 255, 255, 0});
else // undefined subnet
subnetMask = null;
return subnetMask;
}
void OnReceivedServerResponse(DiscoveryInfo info) {
// check if data is valid
if(!IsDataFromServerValid(info))
return;
// invoke event
this.onReceivedServerResponse?.Invoke(info);
}
public static bool IsDataFromServerValid(DiscoveryInfo data)
{
// data must contain signature which matches, and port number
return data.KeyValuePairs.ContainsKey(kSignatureKey) && data.KeyValuePairs[kSignatureKey] == GetCachedSignature()
&& data.KeyValuePairs.ContainsKey(kPortKey);
}
public void RegisterResponseData( string key, string value )
{
m_responseData[key] = value;
}
public void UnRegisterResponseData( string key )
{
m_responseData.Remove (key);
}
static string GetCachedSignature()
{
if (s_cachedSignature != null)
return s_cachedSignature;
s_cachedSignature = CalculateGameSignature();
return s_cachedSignature;
}
/// Signature identifies this game among others.
public static string CalculateGameSignature()
{
string[] strings = new string[]{ Application.companyName, Application.productName,
Application.unityVersion };
string signature = "";
foreach (string str in strings)
{
// only use it's hash code
signature += str.GetHashCode() + ".";
}
return signature;
}
public static string ConvertDictionaryToString( Dictionary<string, string> dict )
{
return string.Join( "\n", dict.Select( pair => pair.Key + ": " + pair.Value ) );
}
public static Dictionary<string, string> ConvertStringToDictionary( string str )
{
var dict = new Dictionary<string, string>(System.StringComparer.InvariantCulture);
string[] lines = str.Split("\n".ToCharArray(), System.StringSplitOptions.RemoveEmptyEntries);
foreach(string line in lines)
{
int index = line.IndexOf(": ");
if(index < 0)
continue;
dict[line.Substring(0, index)] = line.Substring(index + 2, line.Length - index - 2);
}
return dict;
}
public static byte[] ConvertDictionaryToByteArray( Dictionary<string, string> dict )
{
return ConvertStringToPacketData( ConvertDictionaryToString( dict ) );
}
public static Dictionary<string, string> ConvertByteArrayToDictionary( byte[] data )
{
return ConvertStringToDictionary( ConvertPacketDataToString( data ) );
}
public static byte[] ConvertStringToPacketData(string str)
{
byte[] data = new byte[str.Length * 2];
for (int i = 0; i < str.Length; i++)
{
ushort c = str[i];
data[i * 2] = (byte) ((c & 0xff00) >> 8);
data[i * 2 + 1] = (byte) (c & 0x00ff);
}
return data;
}
public static string ConvertPacketDataToString(byte[] data)
{
char[] arr = new char[data.Length / 2];
for (int i = 0; i < arr.Length; i++)
{
ushort b1 = data[i * 2];
ushort b2 = data[i * 2 + 1];
arr[i] = (char)((b1 << 8) | b2);
}
return new string(arr);
}
static bool RunSafe(System.Action action, bool logException = true)
{
try
{
action();
return true;
}
catch(System.Exception ex)
{
if (logException)
Debug.LogException(ex);
return false;
}
}
}
}