633 lines
16 KiB
C#
633 lines
16 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace UnityEngine.Networking
|
|
{
|
|
// Token: 0x02000043 RID: 67
|
|
[DisallowMultipleComponent]
|
|
[AddComponentMenu("Network/NetworkDiscovery")]
|
|
public class NetworkDiscovery : MonoBehaviour
|
|
{
|
|
// Token: 0x17000042 RID: 66
|
|
// (get) Token: 0x060001DF RID: 479 RVA: 0x0000AE38 File Offset: 0x00009038
|
|
// (set) Token: 0x060001E0 RID: 480 RVA: 0x0000AE53 File Offset: 0x00009053
|
|
public int broadcastPort
|
|
{
|
|
get
|
|
{
|
|
return this.m_BroadcastPort;
|
|
}
|
|
set
|
|
{
|
|
this.m_BroadcastPort = value;
|
|
}
|
|
}
|
|
|
|
// Token: 0x17000043 RID: 67
|
|
// (get) Token: 0x060001E1 RID: 481 RVA: 0x0000AE60 File Offset: 0x00009060
|
|
// (set) Token: 0x060001E2 RID: 482 RVA: 0x0000AE7B File Offset: 0x0000907B
|
|
public int broadcastKey
|
|
{
|
|
get
|
|
{
|
|
return this.m_BroadcastKey;
|
|
}
|
|
set
|
|
{
|
|
this.m_BroadcastKey = value;
|
|
}
|
|
}
|
|
|
|
// Token: 0x17000044 RID: 68
|
|
// (get) Token: 0x060001E3 RID: 483 RVA: 0x0000AE88 File Offset: 0x00009088
|
|
// (set) Token: 0x060001E4 RID: 484 RVA: 0x0000AEA3 File Offset: 0x000090A3
|
|
public int broadcastVersion
|
|
{
|
|
get
|
|
{
|
|
return this.m_BroadcastVersion;
|
|
}
|
|
set
|
|
{
|
|
this.m_BroadcastVersion = value;
|
|
}
|
|
}
|
|
|
|
// Token: 0x17000045 RID: 69
|
|
// (get) Token: 0x060001E5 RID: 485 RVA: 0x0000AEB0 File Offset: 0x000090B0
|
|
// (set) Token: 0x060001E6 RID: 486 RVA: 0x0000AECB File Offset: 0x000090CB
|
|
public int broadcastSubVersion
|
|
{
|
|
get
|
|
{
|
|
return this.m_BroadcastSubVersion;
|
|
}
|
|
set
|
|
{
|
|
this.m_BroadcastSubVersion = value;
|
|
}
|
|
}
|
|
|
|
// Token: 0x17000046 RID: 70
|
|
// (get) Token: 0x060001E7 RID: 487 RVA: 0x0000AED8 File Offset: 0x000090D8
|
|
// (set) Token: 0x060001E8 RID: 488 RVA: 0x0000AEF3 File Offset: 0x000090F3
|
|
public int broadcastInterval
|
|
{
|
|
get
|
|
{
|
|
return this.m_BroadcastInterval;
|
|
}
|
|
set
|
|
{
|
|
this.m_BroadcastInterval = value;
|
|
}
|
|
}
|
|
|
|
// Token: 0x17000047 RID: 71
|
|
// (get) Token: 0x060001E9 RID: 489 RVA: 0x0000AF00 File Offset: 0x00009100
|
|
// (set) Token: 0x060001EA RID: 490 RVA: 0x0000AF1B File Offset: 0x0000911B
|
|
public bool useNetworkManager
|
|
{
|
|
get
|
|
{
|
|
return this.m_UseNetworkManager;
|
|
}
|
|
set
|
|
{
|
|
this.m_UseNetworkManager = value;
|
|
}
|
|
}
|
|
|
|
// Token: 0x17000048 RID: 72
|
|
// (get) Token: 0x060001EB RID: 491 RVA: 0x0000AF28 File Offset: 0x00009128
|
|
// (set) Token: 0x060001EC RID: 492 RVA: 0x0000AF43 File Offset: 0x00009143
|
|
public string broadcastData
|
|
{
|
|
get
|
|
{
|
|
return this.m_BroadcastData;
|
|
}
|
|
set
|
|
{
|
|
this.m_BroadcastData = value;
|
|
this.m_MsgOutBuffer = NetworkDiscovery.StringToBytes(this.m_BroadcastData);
|
|
if (this.m_UseNetworkManager)
|
|
{
|
|
if (LogFilter.logWarn)
|
|
{
|
|
Debug.LogWarning("NetworkDiscovery broadcast data changed while using NetworkManager. This can prevent clients from finding the server. The format of the broadcast data must be 'NetworkManager:IPAddress:Port'.");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Token: 0x17000049 RID: 73
|
|
// (get) Token: 0x060001ED RID: 493 RVA: 0x0000AF84 File Offset: 0x00009184
|
|
// (set) Token: 0x060001EE RID: 494 RVA: 0x0000AF9F File Offset: 0x0000919F
|
|
public bool showGUI
|
|
{
|
|
get
|
|
{
|
|
return this.m_ShowGUI;
|
|
}
|
|
set
|
|
{
|
|
this.m_ShowGUI = value;
|
|
}
|
|
}
|
|
|
|
// Token: 0x1700004A RID: 74
|
|
// (get) Token: 0x060001EF RID: 495 RVA: 0x0000AFAC File Offset: 0x000091AC
|
|
// (set) Token: 0x060001F0 RID: 496 RVA: 0x0000AFC7 File Offset: 0x000091C7
|
|
public int offsetX
|
|
{
|
|
get
|
|
{
|
|
return this.m_OffsetX;
|
|
}
|
|
set
|
|
{
|
|
this.m_OffsetX = value;
|
|
}
|
|
}
|
|
|
|
// Token: 0x1700004B RID: 75
|
|
// (get) Token: 0x060001F1 RID: 497 RVA: 0x0000AFD4 File Offset: 0x000091D4
|
|
// (set) Token: 0x060001F2 RID: 498 RVA: 0x0000AFEF File Offset: 0x000091EF
|
|
public int offsetY
|
|
{
|
|
get
|
|
{
|
|
return this.m_OffsetY;
|
|
}
|
|
set
|
|
{
|
|
this.m_OffsetY = value;
|
|
}
|
|
}
|
|
|
|
// Token: 0x1700004C RID: 76
|
|
// (get) Token: 0x060001F3 RID: 499 RVA: 0x0000AFFC File Offset: 0x000091FC
|
|
// (set) Token: 0x060001F4 RID: 500 RVA: 0x0000B017 File Offset: 0x00009217
|
|
public int hostId
|
|
{
|
|
get
|
|
{
|
|
return this.m_HostId;
|
|
}
|
|
set
|
|
{
|
|
this.m_HostId = value;
|
|
}
|
|
}
|
|
|
|
// Token: 0x1700004D RID: 77
|
|
// (get) Token: 0x060001F5 RID: 501 RVA: 0x0000B024 File Offset: 0x00009224
|
|
// (set) Token: 0x060001F6 RID: 502 RVA: 0x0000B03F File Offset: 0x0000923F
|
|
public bool running
|
|
{
|
|
get
|
|
{
|
|
return this.m_Running;
|
|
}
|
|
set
|
|
{
|
|
this.m_Running = value;
|
|
}
|
|
}
|
|
|
|
// Token: 0x1700004E RID: 78
|
|
// (get) Token: 0x060001F7 RID: 503 RVA: 0x0000B04C File Offset: 0x0000924C
|
|
// (set) Token: 0x060001F8 RID: 504 RVA: 0x0000B067 File Offset: 0x00009267
|
|
public bool isServer
|
|
{
|
|
get
|
|
{
|
|
return this.m_IsServer;
|
|
}
|
|
set
|
|
{
|
|
this.m_IsServer = value;
|
|
}
|
|
}
|
|
|
|
// Token: 0x1700004F RID: 79
|
|
// (get) Token: 0x060001F9 RID: 505 RVA: 0x0000B074 File Offset: 0x00009274
|
|
// (set) Token: 0x060001FA RID: 506 RVA: 0x0000B08F File Offset: 0x0000928F
|
|
public bool isClient
|
|
{
|
|
get
|
|
{
|
|
return this.m_IsClient;
|
|
}
|
|
set
|
|
{
|
|
this.m_IsClient = value;
|
|
}
|
|
}
|
|
|
|
// Token: 0x17000050 RID: 80
|
|
// (get) Token: 0x060001FB RID: 507 RVA: 0x0000B09C File Offset: 0x0000929C
|
|
public Dictionary<string, NetworkBroadcastResult> broadcastsReceived
|
|
{
|
|
get
|
|
{
|
|
return this.m_BroadcastsReceived;
|
|
}
|
|
}
|
|
|
|
// Token: 0x060001FC RID: 508 RVA: 0x0000B0B8 File Offset: 0x000092B8
|
|
private static byte[] StringToBytes(string str)
|
|
{
|
|
byte[] array = new byte[str.Length * 2];
|
|
Buffer.BlockCopy(str.ToCharArray(), 0, array, 0, array.Length);
|
|
return array;
|
|
}
|
|
|
|
// Token: 0x060001FD RID: 509 RVA: 0x0000B0F0 File Offset: 0x000092F0
|
|
private static string BytesToString(byte[] bytes)
|
|
{
|
|
char[] array = new char[bytes.Length / 2];
|
|
Buffer.BlockCopy(bytes, 0, array, 0, bytes.Length);
|
|
return new string(array);
|
|
}
|
|
|
|
// Token: 0x060001FE RID: 510 RVA: 0x0000B124 File Offset: 0x00009324
|
|
public bool Initialize()
|
|
{
|
|
bool flag;
|
|
if (this.m_BroadcastData.Length >= 1024)
|
|
{
|
|
if (LogFilter.logError)
|
|
{
|
|
Debug.LogError("NetworkDiscovery Initialize - data too large. max is " + 1024);
|
|
}
|
|
flag = false;
|
|
}
|
|
else
|
|
{
|
|
if (!NetworkTransport.IsStarted)
|
|
{
|
|
NetworkTransport.Init();
|
|
}
|
|
if (this.m_UseNetworkManager && NetworkManager.singleton != null)
|
|
{
|
|
this.m_BroadcastData = string.Concat(new object[]
|
|
{
|
|
"NetworkManager:",
|
|
NetworkManager.singleton.networkAddress,
|
|
":",
|
|
NetworkManager.singleton.networkPort
|
|
});
|
|
if (LogFilter.logInfo)
|
|
{
|
|
Debug.Log("NetworkDiscovery set broadcast data to:" + this.m_BroadcastData);
|
|
}
|
|
}
|
|
this.m_MsgOutBuffer = NetworkDiscovery.StringToBytes(this.m_BroadcastData);
|
|
this.m_MsgInBuffer = new byte[1024];
|
|
this.m_BroadcastsReceived = new Dictionary<string, NetworkBroadcastResult>();
|
|
ConnectionConfig connectionConfig = new ConnectionConfig();
|
|
connectionConfig.AddChannel(QosType.Unreliable);
|
|
this.m_DefaultTopology = new HostTopology(connectionConfig, 1);
|
|
if (this.m_IsServer)
|
|
{
|
|
this.StartAsServer();
|
|
}
|
|
if (this.m_IsClient)
|
|
{
|
|
this.StartAsClient();
|
|
}
|
|
flag = true;
|
|
}
|
|
return flag;
|
|
}
|
|
|
|
// Token: 0x060001FF RID: 511 RVA: 0x0000B278 File Offset: 0x00009478
|
|
public bool StartAsClient()
|
|
{
|
|
bool flag;
|
|
if (this.m_HostId != -1 || this.m_Running)
|
|
{
|
|
if (LogFilter.logWarn)
|
|
{
|
|
Debug.LogWarning("NetworkDiscovery StartAsClient already started");
|
|
}
|
|
flag = false;
|
|
}
|
|
else if (this.m_MsgInBuffer == null)
|
|
{
|
|
if (LogFilter.logError)
|
|
{
|
|
Debug.LogError("NetworkDiscovery StartAsClient, NetworkDiscovery is not initialized");
|
|
}
|
|
flag = false;
|
|
}
|
|
else
|
|
{
|
|
this.m_HostId = NetworkTransport.AddHost(this.m_DefaultTopology, this.m_BroadcastPort);
|
|
if (this.m_HostId == -1)
|
|
{
|
|
if (LogFilter.logError)
|
|
{
|
|
Debug.LogError("NetworkDiscovery StartAsClient - addHost failed");
|
|
}
|
|
flag = false;
|
|
}
|
|
else
|
|
{
|
|
byte b;
|
|
NetworkTransport.SetBroadcastCredentials(this.m_HostId, this.m_BroadcastKey, this.m_BroadcastVersion, this.m_BroadcastSubVersion, out b);
|
|
this.m_Running = true;
|
|
this.m_IsClient = true;
|
|
if (LogFilter.logDebug)
|
|
{
|
|
Debug.Log("StartAsClient Discovery listening");
|
|
}
|
|
flag = true;
|
|
}
|
|
}
|
|
return flag;
|
|
}
|
|
|
|
// Token: 0x06000200 RID: 512 RVA: 0x0000B370 File Offset: 0x00009570
|
|
public bool StartAsServer()
|
|
{
|
|
bool flag;
|
|
if (this.m_HostId != -1 || this.m_Running)
|
|
{
|
|
if (LogFilter.logWarn)
|
|
{
|
|
Debug.LogWarning("NetworkDiscovery StartAsServer already started");
|
|
}
|
|
flag = false;
|
|
}
|
|
else
|
|
{
|
|
this.m_HostId = NetworkTransport.AddHost(this.m_DefaultTopology, 0);
|
|
byte b;
|
|
if (this.m_HostId == -1)
|
|
{
|
|
if (LogFilter.logError)
|
|
{
|
|
Debug.LogError("NetworkDiscovery StartAsServer - addHost failed");
|
|
}
|
|
flag = false;
|
|
}
|
|
else if (!NetworkTransport.StartBroadcastDiscovery(this.m_HostId, this.m_BroadcastPort, this.m_BroadcastKey, this.m_BroadcastVersion, this.m_BroadcastSubVersion, this.m_MsgOutBuffer, this.m_MsgOutBuffer.Length, this.m_BroadcastInterval, out b))
|
|
{
|
|
if (LogFilter.logError)
|
|
{
|
|
Debug.LogError("NetworkDiscovery StartBroadcast failed err: " + b);
|
|
}
|
|
flag = false;
|
|
}
|
|
else
|
|
{
|
|
this.m_Running = true;
|
|
this.m_IsServer = true;
|
|
if (LogFilter.logDebug)
|
|
{
|
|
Debug.Log("StartAsServer Discovery broadcasting");
|
|
}
|
|
Object.DontDestroyOnLoad(base.gameObject);
|
|
flag = true;
|
|
}
|
|
}
|
|
return flag;
|
|
}
|
|
|
|
// Token: 0x06000201 RID: 513 RVA: 0x0000B490 File Offset: 0x00009690
|
|
public void StopBroadcast()
|
|
{
|
|
if (this.m_HostId == -1)
|
|
{
|
|
if (LogFilter.logError)
|
|
{
|
|
Debug.LogError("NetworkDiscovery StopBroadcast not initialized");
|
|
}
|
|
}
|
|
else if (!this.m_Running)
|
|
{
|
|
Debug.LogWarning("NetworkDiscovery StopBroadcast not started");
|
|
}
|
|
else
|
|
{
|
|
if (this.m_IsServer)
|
|
{
|
|
NetworkTransport.StopBroadcastDiscovery();
|
|
}
|
|
NetworkTransport.RemoveHost(this.m_HostId);
|
|
this.m_HostId = -1;
|
|
this.m_Running = false;
|
|
this.m_IsServer = false;
|
|
this.m_IsClient = false;
|
|
this.m_MsgInBuffer = null;
|
|
this.m_BroadcastsReceived = null;
|
|
if (LogFilter.logDebug)
|
|
{
|
|
Debug.Log("Stopped Discovery broadcasting");
|
|
}
|
|
}
|
|
}
|
|
|
|
// Token: 0x06000202 RID: 514 RVA: 0x0000B540 File Offset: 0x00009740
|
|
private void Update()
|
|
{
|
|
if (this.m_HostId != -1)
|
|
{
|
|
if (!this.m_IsServer)
|
|
{
|
|
NetworkEventType networkEventType;
|
|
do
|
|
{
|
|
int num;
|
|
int num2;
|
|
int num3;
|
|
byte b;
|
|
networkEventType = NetworkTransport.ReceiveFromHost(this.m_HostId, out num, out num2, this.m_MsgInBuffer, 1024, out num3, out b);
|
|
if (networkEventType == NetworkEventType.BroadcastEvent)
|
|
{
|
|
NetworkTransport.GetBroadcastConnectionMessage(this.m_HostId, this.m_MsgInBuffer, 1024, out num3, out b);
|
|
string text;
|
|
int num4;
|
|
NetworkTransport.GetBroadcastConnectionInfo(this.m_HostId, out text, out num4, out b);
|
|
NetworkBroadcastResult networkBroadcastResult = default(NetworkBroadcastResult);
|
|
networkBroadcastResult.serverAddress = text;
|
|
networkBroadcastResult.broadcastData = new byte[num3];
|
|
Buffer.BlockCopy(this.m_MsgInBuffer, 0, networkBroadcastResult.broadcastData, 0, num3);
|
|
this.m_BroadcastsReceived[text] = networkBroadcastResult;
|
|
this.OnReceivedBroadcast(text, NetworkDiscovery.BytesToString(this.m_MsgInBuffer));
|
|
}
|
|
}
|
|
while (networkEventType != NetworkEventType.Nothing);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Token: 0x06000203 RID: 515 RVA: 0x0000B620 File Offset: 0x00009820
|
|
private void OnDestroy()
|
|
{
|
|
if (this.m_IsServer && this.m_Running && this.m_HostId != -1)
|
|
{
|
|
NetworkTransport.StopBroadcastDiscovery();
|
|
NetworkTransport.RemoveHost(this.m_HostId);
|
|
}
|
|
if (this.m_IsClient && this.m_Running && this.m_HostId != -1)
|
|
{
|
|
NetworkTransport.RemoveHost(this.m_HostId);
|
|
}
|
|
}
|
|
|
|
// Token: 0x06000204 RID: 516 RVA: 0x0000B693 File Offset: 0x00009893
|
|
public virtual void OnReceivedBroadcast(string fromAddress, string data)
|
|
{
|
|
}
|
|
|
|
// Token: 0x06000205 RID: 517 RVA: 0x0000B698 File Offset: 0x00009898
|
|
private void OnGUI()
|
|
{
|
|
if (this.m_ShowGUI)
|
|
{
|
|
int num = 10 + this.m_OffsetX;
|
|
int num2 = 40 + this.m_OffsetY;
|
|
if (Application.platform == RuntimePlatform.WebGLPlayer)
|
|
{
|
|
GUI.Box(new Rect((float)num, (float)num2, 200f, 20f), "( WebGL cannot broadcast )");
|
|
}
|
|
else if (this.m_MsgInBuffer == null)
|
|
{
|
|
if (GUI.Button(new Rect((float)num, (float)num2, 200f, 20f), "Initialize Broadcast"))
|
|
{
|
|
this.Initialize();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
string text = "";
|
|
if (this.m_IsServer)
|
|
{
|
|
text = " (server)";
|
|
}
|
|
if (this.m_IsClient)
|
|
{
|
|
text = " (client)";
|
|
}
|
|
GUI.Label(new Rect((float)num, (float)num2, 200f, 20f), "initialized" + text);
|
|
num2 += 24;
|
|
if (this.m_Running)
|
|
{
|
|
if (GUI.Button(new Rect((float)num, (float)num2, 200f, 20f), "Stop"))
|
|
{
|
|
this.StopBroadcast();
|
|
}
|
|
num2 += 24;
|
|
if (this.m_BroadcastsReceived != null)
|
|
{
|
|
foreach (string text2 in this.m_BroadcastsReceived.Keys)
|
|
{
|
|
NetworkBroadcastResult networkBroadcastResult = this.m_BroadcastsReceived[text2];
|
|
if (GUI.Button(new Rect((float)num, (float)(num2 + 20), 200f, 20f), "Game at " + text2) && this.m_UseNetworkManager)
|
|
{
|
|
string text3 = NetworkDiscovery.BytesToString(networkBroadcastResult.broadcastData);
|
|
string[] array = text3.Split(new char[] { ':' });
|
|
if (array.Length == 3 && array[0] == "NetworkManager")
|
|
{
|
|
if (NetworkManager.singleton != null && NetworkManager.singleton.client == null)
|
|
{
|
|
NetworkManager.singleton.networkAddress = array[1];
|
|
NetworkManager.singleton.networkPort = Convert.ToInt32(array[2]);
|
|
NetworkManager.singleton.StartClient();
|
|
}
|
|
}
|
|
}
|
|
num2 += 24;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (GUI.Button(new Rect((float)num, (float)num2, 200f, 20f), "Start Broadcasting"))
|
|
{
|
|
this.StartAsServer();
|
|
}
|
|
num2 += 24;
|
|
if (GUI.Button(new Rect((float)num, (float)num2, 200f, 20f), "Listen for Broadcast"))
|
|
{
|
|
this.StartAsClient();
|
|
}
|
|
num2 += 24;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Token: 0x0400010A RID: 266
|
|
private const int k_MaxBroadcastMsgSize = 1024;
|
|
|
|
// Token: 0x0400010B RID: 267
|
|
[SerializeField]
|
|
private int m_BroadcastPort = 47777;
|
|
|
|
// Token: 0x0400010C RID: 268
|
|
[SerializeField]
|
|
private int m_BroadcastKey = 2222;
|
|
|
|
// Token: 0x0400010D RID: 269
|
|
[SerializeField]
|
|
private int m_BroadcastVersion = 1;
|
|
|
|
// Token: 0x0400010E RID: 270
|
|
[SerializeField]
|
|
private int m_BroadcastSubVersion = 1;
|
|
|
|
// Token: 0x0400010F RID: 271
|
|
[SerializeField]
|
|
private int m_BroadcastInterval = 1000;
|
|
|
|
// Token: 0x04000110 RID: 272
|
|
[SerializeField]
|
|
private bool m_UseNetworkManager = false;
|
|
|
|
// Token: 0x04000111 RID: 273
|
|
[SerializeField]
|
|
private string m_BroadcastData = "HELLO";
|
|
|
|
// Token: 0x04000112 RID: 274
|
|
[SerializeField]
|
|
private bool m_ShowGUI = true;
|
|
|
|
// Token: 0x04000113 RID: 275
|
|
[SerializeField]
|
|
private int m_OffsetX;
|
|
|
|
// Token: 0x04000114 RID: 276
|
|
[SerializeField]
|
|
private int m_OffsetY;
|
|
|
|
// Token: 0x04000115 RID: 277
|
|
private int m_HostId = -1;
|
|
|
|
// Token: 0x04000116 RID: 278
|
|
private bool m_Running;
|
|
|
|
// Token: 0x04000117 RID: 279
|
|
private bool m_IsServer;
|
|
|
|
// Token: 0x04000118 RID: 280
|
|
private bool m_IsClient;
|
|
|
|
// Token: 0x04000119 RID: 281
|
|
private byte[] m_MsgOutBuffer;
|
|
|
|
// Token: 0x0400011A RID: 282
|
|
private byte[] m_MsgInBuffer;
|
|
|
|
// Token: 0x0400011B RID: 283
|
|
private HostTopology m_DefaultTopology;
|
|
|
|
// Token: 0x0400011C RID: 284
|
|
private Dictionary<string, NetworkBroadcastResult> m_BroadcastsReceived;
|
|
}
|
|
}
|