diff --git a/Assets/HoloToolkit-Examples/SharingWithUNET/Scripts/GenericNetworkTransmitter.cs b/Assets/HoloToolkit-Examples/SharingWithUNET/Scripts/GenericNetworkTransmitter.cs index e180909d1bf..307d6697256 100644 --- a/Assets/HoloToolkit-Examples/SharingWithUNET/Scripts/GenericNetworkTransmitter.cs +++ b/Assets/HoloToolkit-Examples/SharingWithUNET/Scripts/GenericNetworkTransmitter.cs @@ -1,7 +1,6 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -using System; using UnityEngine; using HoloToolkit.Unity; @@ -46,6 +45,23 @@ public class GenericNetworkTransmitter : Singleton /// private byte[] mostRecentDataBuffer; +#if !UNITY_EDITOR && UNITY_WSA + /// + /// Tracks the network connection to the remote machine we are sending meshes to. + /// + private StreamSocket networkConnection; + + /// + /// If we are running as the server, this is the listener the server will use. + /// + private StreamSocketListener networkListener; + + /// + /// If we cannot connect to the server, this is how long we will wait before retrying. + /// + private float timeToDeferFailedConnections = 10.0f; +#endif + /// /// If someone connects to us, this is the data we will send them. /// @@ -58,10 +74,10 @@ public void SetData(byte[] data) /// /// Tells us who to contact if we need data. /// - /// - public void SetServerIp(string _serverIp) + /// + public void SetServerIp(string newServerIp) { - serverIp = _serverIp.Trim(); + serverIp = newServerIp.Trim(); } /// @@ -73,28 +89,12 @@ public void RequestAndGetData() ConnectListener(); } - // A lot of the work done in this class can only be done in UWP. The editor is not a UWP app. -#if !UNITY_EDITOR && UNITY_WSA - /// - /// Tracks the network connection to the remote machine we are sending meshes to. - /// - private StreamSocket networkConnection; - - /// - /// If we are running as the server, this is the listener the server will use. - /// - private StreamSocketListener networkListener; - - /// - /// If we cannot connect to the server, this is how long we will wait before retrying. - /// - private float timeToDeferFailedConnections = 10.0f; - /// /// Configures the network transmitter as the source. /// public void ConfigureAsServer() { +#if !UNITY_EDITOR && UNITY_WSA Task t = new Task(() => { networkListener = new StreamSocketListener(); @@ -103,8 +103,36 @@ public void ConfigureAsServer() } ); t.Start(); +#else + Debug.Log("This script is not intended to be run from the Unity Editor"); + // In order to avoid compiler warnings in the Unity Editor we have to access a few of our fields. + Debug.Log(string.Format("serverIP = {0} waitingForConnection = {1} mostRecentDataBuffer = {2}", serverIp, waitingForConnection, mostRecentDataBuffer == null ? "No there" : "there")); +#endif } + /// + /// Connects to the server and requests data. + /// + private void ConnectListener() + { +#if !UNITY_EDITOR && UNITY_WSA + if (waitingForConnection) + { + return; + } + + waitingForConnection = true; + Debug.Log("Connecting to " + serverIp); + HostName networkHost = new HostName(serverIp); + networkConnection = new StreamSocket(); + + IAsyncAction outstandingAction = networkConnection.ConnectAsync(networkHost, SendConnectionPort.ToString()); + AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler(RcvNetworkConnectedHandler); + outstandingAction.Completed = aach; +#endif + } + +#if !UNITY_EDITOR && UNITY_WSA /// /// When a connection is made to us, this call back gets called and /// we send our data. @@ -131,26 +159,6 @@ private void NetworkListener_ConnectionReceived(StreamSocketListener sender, Str } } - /// - /// Connects to the server and requests data. - /// - private void ConnectListener() - { - if (waitingForConnection) - { - return; - } - - Debug.Log("Connecting to " + serverIP); - waitingForConnection = true; - HostName networkHost = new HostName(serverIP); - networkConnection = new StreamSocket(); - - IAsyncAction outstandingAction = networkConnection.ConnectAsync(networkHost, SendConnectionPort.ToString()); - AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler(RcvNetworkConnectedHandler); - outstandingAction.Completed = aach; - } - /// /// When a connection to the server is established and we can start reading the data, this will be called. /// @@ -187,7 +195,7 @@ private async void RcvNetworkConnectedHandler(IAsyncAction asyncInfo, AsyncStatu networkDataReader.ReadBytes(mostRecentDataBuffer); // And fire our data ready event. - dataReadyEvent?.Invoke(mostRecentDataBuffer); + DataReadyEvent?.Invoke(mostRecentDataBuffer); } } else @@ -198,15 +206,6 @@ private async void RcvNetworkConnectedHandler(IAsyncAction asyncInfo, AsyncStatu networkConnection.Dispose(); waitingForConnection = false; } - -#else - public void ConfigureAsServer() - { - Debug.Log("This script is not intended to be run from the Unity Editor"); - // In order to avoid compiler warnings in the Unity Editor we have to access a few of our fields. - Debug.Log(string.Format("serverIP = {0} waitingForConnection = {1} mostRecentDataBuffer = {2}", serverIp, waitingForConnection, mostRecentDataBuffer == null ? "No there" : "there")); - } - private void ConnectListener() {} #endif } } \ No newline at end of file diff --git a/Assets/HoloToolkit-Examples/SharingWithUNET/Scripts/NetworkDiscoveryWithAnchors.cs b/Assets/HoloToolkit-Examples/SharingWithUNET/Scripts/NetworkDiscoveryWithAnchors.cs index cd68a4ffe66..5fad62dc616 100644 --- a/Assets/HoloToolkit-Examples/SharingWithUNET/Scripts/NetworkDiscoveryWithAnchors.cs +++ b/Assets/HoloToolkit-Examples/SharingWithUNET/Scripts/NetworkDiscoveryWithAnchors.cs @@ -18,10 +18,10 @@ namespace HoloToolkit.Examples.SharingWithUNET public class NetworkDiscoveryWithAnchors : NetworkDiscovery { /// - /// This flag gets set when we recieve a broadcast. + /// This flag gets set when we receive a broadcast. /// if this flag is set then we should not create a server. /// - public bool receivedBroadcast { get; private set; } + public bool ReceivedBroadcast { get; private set; } /// /// Controls how often a broadcast should be sent to clients @@ -79,8 +79,8 @@ private void Start() // We randomize how long we wait so that we reduce the chances that everyone joins at // once and decides that they are the server. // An alternative would be to create UI for managing who hosts. - float InvokeWaitTime = 3 * BroadcastInterval + Random.value * 3 * BroadcastInterval; - Invoke("MaybeInitAsServer", InvokeWaitTime * 0.001f); + float invokeWaitTime = 3 * BroadcastInterval + Random.value * 3 * BroadcastInterval; + Invoke("MaybeInitAsServer", invokeWaitTime * 0.001f); } /// @@ -89,8 +89,8 @@ private void Start() /// private void MaybeInitAsServer() { - // If we Recieved a broadcast then we should not start as a server. - if (receivedBroadcast) + // If we receive a broadcast then we should not start as a server. + if (ReceivedBroadcast) { return; } @@ -134,18 +134,18 @@ private IEnumerator InitAsServer() /// /// When the broadcast came from /// The data in the broad cast. Not currently used, but could - /// be used for differntiating rooms or similar. + /// be used for differentiating rooms or similar. public override void OnReceivedBroadcast(string fromAddress, string data) { - // If we've already recieved a broadcast then we've already set everything up. - if (receivedBroadcast) + // If we've already received a broadcast then we've already set everything up. + if (ReceivedBroadcast) { return; } Debug.Log("Acting as client"); - receivedBroadcast = true; + ReceivedBroadcast = true; // Stop listening for more broadcasts. StopBroadcast(); @@ -158,9 +158,9 @@ public override void OnReceivedBroadcast(string fromAddress, string data) #if !UNITY_EDITOR // Tell the network transmitter the IP to request anchor data from if needed. - GenericNetworkTransmitter.Instance.SetServerIP(ServerIp); + GenericNetworkTransmitter.Instance.SetServerIp(ServerIp); #else - Debug.LogWarning("This script will need modification to work in the Unity Editor"); + Debug.LogWarning("This script will need modification to work in the Unity Editor"); #endif // And join the networked experience as a client. NetworkManager.singleton.StartClient();