private void Update() { if (this.m_HostId == -1 || this.m_IsServer) { return; } NetworkEventType fromHost; do { int connectionId; int channelId; int receivedSize; byte error; fromHost = NetworkTransport.ReceiveFromHost(this.m_HostId, out connectionId, out channelId, this.m_MsgInBuffer, 1024, out receivedSize, out error); if (fromHost == NetworkEventType.BroadcastEvent) { NetworkTransport.GetBroadcastConnectionMessage(this.m_HostId, this.m_MsgInBuffer, 1024, out receivedSize, out error); string address; int port; NetworkTransport.GetBroadcastConnectionInfo(this.m_HostId, out address, out port, out error); NetworkBroadcastResult networkBroadcastResult = new NetworkBroadcastResult(); networkBroadcastResult.serverAddress = address; networkBroadcastResult.broadcastData = new byte[receivedSize]; Buffer.BlockCopy((Array)this.m_MsgInBuffer, 0, (Array)networkBroadcastResult.broadcastData, 0, receivedSize); this.m_BroadcastsReceived[address] = networkBroadcastResult; this.OnReceivedBroadcast(address, NetworkDiscovery.BytesToString(this.m_MsgInBuffer)); } }while (fromHost != NetworkEventType.Nothing); }
private void Init() { if (!this.m_Initialized || (this.m_BroadcastPortProperty == null)) { this.m_Initialized = true; this.m_Discovery = base.target as NetworkDiscovery; this.m_BroadcastPortProperty = base.serializedObject.FindProperty("m_BroadcastPort"); this.m_BroadcastKeyProperty = base.serializedObject.FindProperty("m_BroadcastKey"); this.m_BroadcastVersionProperty = base.serializedObject.FindProperty("m_BroadcastVersion"); this.m_BroadcastSubVersionProperty = base.serializedObject.FindProperty("m_BroadcastSubVersion"); this.m_BroadcastIntervalProperty = base.serializedObject.FindProperty("m_BroadcastInterval"); this.m_UseNetworkManagerProperty = base.serializedObject.FindProperty("m_UseNetworkManager"); this.m_BroadcastDataProperty = base.serializedObject.FindProperty("m_BroadcastData"); this.m_ShowGUIProperty = base.serializedObject.FindProperty("m_ShowGUI"); this.m_OffsetXProperty = base.serializedObject.FindProperty("m_OffsetX"); this.m_OffsetYProperty = base.serializedObject.FindProperty("m_OffsetY"); this.m_BroadcastPortLabel = new GUIContent("Broadcast Port", "The network port to broadcast to, and listen on."); this.m_BroadcastKeyLabel = new GUIContent("Broadcast Key", "The key to broadcast. This key typically identifies the application."); this.m_BroadcastVersionLabel = new GUIContent("Broadcast Version", "The version of the application to broadcast. This is used to match versions of the same application."); this.m_BroadcastSubVersionLabel = new GUIContent("Broadcast SubVersion", "The sub-version of the application to broadcast."); this.m_BroadcastIntervalLabel = new GUIContent("Broadcast Interval", "How often in milliseconds to broadcast when running as a server."); this.m_UseNetworkManagerLabel = new GUIContent("Use NetworkManager", "Broadcast information from the NetworkManager, and auto-join matching games using the NetworkManager."); this.m_BroadcastDataLabel = new GUIContent("Broadcast Data", "The data to broadcast when not using the NetworkManager"); } }
private void Update() { if (this.m_HostId == -1) { return; } if (this.m_IsServer) { return; } 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 value = default(NetworkBroadcastResult); value.serverAddress = text; value.broadcastData = new byte[num3]; Buffer.BlockCopy(this.m_MsgInBuffer, 0, value.broadcastData, 0, num3); this.m_BroadcastsReceived[text] = value; this.OnReceivedBroadcast(text, NetworkDiscovery.BytesToString(this.m_MsgInBuffer)); } }while (networkEventType != NetworkEventType.Nothing); }
public bool Initialize() { bool result; if (this.m_BroadcastData.Length >= 1024) { if (LogFilter.logError) { Debug.LogError("NetworkDiscovery Initialize - data too large. max is " + 1024); } result = 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(); } result = true; } return(result); }
/// <summary> /// <para>Initializes the NetworkDiscovery component.</para> /// </summary> /// <returns> /// <para>Return true if the network port was available.</para> /// </returns> public bool Initialize() { if (this.m_BroadcastData.Length >= 1024) { if (LogFilter.logError) { Debug.LogError((object)("NetworkDiscovery Initialize - data too large. max is " + (object)1024)); } return(false); } if (!NetworkTransport.IsStarted) { NetworkTransport.Init(); } if (this.m_UseNetworkManager && (UnityEngine.Object)NetworkManager.singleton != (UnityEngine.Object)null) { this.m_BroadcastData = "NetworkManager:" + NetworkManager.singleton.networkAddress + ":" + (object)NetworkManager.singleton.networkPort; if (LogFilter.logInfo) { Debug.Log((object)("NetwrokDiscovery set broadbast 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 defaultConfig = new ConnectionConfig(); int num = (int)defaultConfig.AddChannel(QosType.Unreliable); this.m_DefaultTopology = new HostTopology(defaultConfig, 1); if (this.m_IsServer) { this.StartAsServer(); } if (this.m_IsClient) { this.StartAsClient(); } return(true); }
private void OnGUI() { if (!this.m_ShowGUI) { return; } int num1 = 10 + this.m_OffsetX; int num2 = 40 + this.m_OffsetY; if (Application.platform == RuntimePlatform.WebGLPlayer) { GUI.Box(new Rect((float)num1, (float)num2, 200f, 20f), "( WebGL cannot broadcast )"); } else if (this.m_MsgInBuffer == null) { if (!GUI.Button(new Rect((float)num1, (float)num2, 200f, 20f), "Initialize Broadcast")) { return; } this.Initialize(); } else { string str = string.Empty; if (this.m_IsServer) { str = " (server)"; } if (this.m_IsClient) { str = " (client)"; } GUI.Label(new Rect((float)num1, (float)num2, 200f, 20f), "initialized" + str); int num3 = num2 + 24; if (this.m_Running) { if (GUI.Button(new Rect((float)num1, (float)num3, 200f, 20f), "Stop")) { this.StopBroadcast(); } int num4 = num3 + 24; if (this.m_BroadcastsReceived == null) { return; } using (Dictionary <string, NetworkBroadcastResult> .KeyCollection.Enumerator enumerator = this.m_BroadcastsReceived.Keys.GetEnumerator()) { while (enumerator.MoveNext()) { string current = enumerator.Current; NetworkBroadcastResult networkBroadcastResult = this.m_BroadcastsReceived[current]; if (GUI.Button(new Rect((float)num1, (float)(num4 + 20), 200f, 20f), "Game at " + current) && this.m_UseNetworkManager) { string[] strArray = NetworkDiscovery.BytesToString(networkBroadcastResult.broadcastData).Split(':'); if (strArray.Length == 3 && strArray[0] == "NetworkManager" && ((UnityEngine.Object)NetworkManager.singleton != (UnityEngine.Object)null && NetworkManager.singleton.client == null)) { NetworkManager.singleton.networkAddress = strArray[1]; NetworkManager.singleton.networkPort = Convert.ToInt32(strArray[2]); NetworkManager.singleton.StartClient(); } } num4 += 24; } } } else { if (GUI.Button(new Rect((float)num1, (float)num3, 200f, 20f), "Start Broadcasting")) { this.StartAsServer(); } int num4 = num3 + 24; if (GUI.Button(new Rect((float)num1, (float)num4, 200f, 20f), "Listen for Broadcast")) { this.StartAsClient(); } int num5 = num4 + 24; } } }
private void OnGUI() { if (!this.m_ShowGUI) { return; } int num = 10 + this.m_OffsetX; int num2 = 40 + this.m_OffsetY; if (this.m_MsgInBuffer == null) { if (GUI.Button(new Rect((float)num, (float)num2, 200f, 20f), "Initialize Broadcast")) { this.Initialize(); } return; } string str = string.Empty; if (this.m_IsServer) { str = " (server)"; } if (this.m_IsClient) { str = " (client)"; } GUI.Label(new Rect((float)num, (float)num2, 200f, 20f), "initialized" + str); 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 current in this.m_BroadcastsReceived.Keys) { NetworkBroadcastResult networkBroadcastResult = this.m_BroadcastsReceived[current]; if (GUI.Button(new Rect((float)num, (float)(num2 + 20), 200f, 20f), "Game at " + current) && this.m_UseNetworkManager) { string text = NetworkDiscovery.BytesToString(networkBroadcastResult.broadcastData); string[] array = text.Split(new char[] { ':' }); if (array.Length == 3 && array[0] == "NetworkManager" && 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; } }