Skip to content

Commit

Permalink
Merge pull request #967 from StephenHodgson/MRTK-NetworkFix
Browse files Browse the repository at this point in the history
Examples regression fix
  • Loading branch information
StephenHodgson authored Sep 13, 2017
2 parents e491011 + 8d4af16 commit 15df22e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -46,6 +45,23 @@ public class GenericNetworkTransmitter : Singleton<GenericNetworkTransmitter>
/// </summary>
private byte[] mostRecentDataBuffer;

#if !UNITY_EDITOR && UNITY_WSA
/// <summary>
/// Tracks the network connection to the remote machine we are sending meshes to.
/// </summary>
private StreamSocket networkConnection;

/// <summary>
/// If we are running as the server, this is the listener the server will use.
/// </summary>
private StreamSocketListener networkListener;

/// <summary>
/// If we cannot connect to the server, this is how long we will wait before retrying.
/// </summary>
private float timeToDeferFailedConnections = 10.0f;
#endif

/// <summary>
/// If someone connects to us, this is the data we will send them.
/// </summary>
Expand All @@ -58,10 +74,10 @@ public void SetData(byte[] data)
/// <summary>
/// Tells us who to contact if we need data.
/// </summary>
/// <param name="_serverIp"></param>
public void SetServerIp(string _serverIp)
/// <param name="newServerIp"></param>
public void SetServerIp(string newServerIp)
{
serverIp = _serverIp.Trim();
serverIp = newServerIp.Trim();
}

/// <summary>
Expand All @@ -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
/// <summary>
/// Tracks the network connection to the remote machine we are sending meshes to.
/// </summary>
private StreamSocket networkConnection;

/// <summary>
/// If we are running as the server, this is the listener the server will use.
/// </summary>
private StreamSocketListener networkListener;

/// <summary>
/// If we cannot connect to the server, this is how long we will wait before retrying.
/// </summary>
private float timeToDeferFailedConnections = 10.0f;

/// <summary>
/// Configures the network transmitter as the source.
/// </summary>
public void ConfigureAsServer()
{
#if !UNITY_EDITOR && UNITY_WSA
Task t = new Task(() =>
{
networkListener = new StreamSocketListener();
Expand All @@ -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
}

/// <summary>
/// Connects to the server and requests data.
/// </summary>
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
/// <summary>
/// When a connection is made to us, this call back gets called and
/// we send our data.
Expand All @@ -131,26 +159,6 @@ private void NetworkListener_ConnectionReceived(StreamSocketListener sender, Str
}
}

/// <summary>
/// Connects to the server and requests data.
/// </summary>
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;
}

/// <summary>
/// When a connection to the server is established and we can start reading the data, this will be called.
/// </summary>
Expand Down Expand Up @@ -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
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ namespace HoloToolkit.Examples.SharingWithUNET
public class NetworkDiscoveryWithAnchors : NetworkDiscovery
{
/// <summary>
/// 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.
/// </summary>
public bool receivedBroadcast { get; private set; }
public bool ReceivedBroadcast { get; private set; }

/// <summary>
/// Controls how often a broadcast should be sent to clients
Expand Down Expand Up @@ -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);
}

/// <summary>
Expand All @@ -89,8 +89,8 @@ private void Start()
/// </summary>
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;
}
Expand Down Expand Up @@ -134,18 +134,18 @@ private IEnumerator InitAsServer()
/// </summary>
/// <param name="fromAddress">When the broadcast came from</param>
/// <param name="data">The data in the broad cast. Not currently used, but could
/// be used for differntiating rooms or similar.</param>
/// be used for differentiating rooms or similar.</param>
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();
Expand All @@ -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();
Expand Down

0 comments on commit 15df22e

Please sign in to comment.