11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT License. See LICENSE in the project root for license information.
33
4- using System ;
54using UnityEngine ;
65using HoloToolkit . Unity ;
76
@@ -46,6 +45,23 @@ public class GenericNetworkTransmitter : Singleton<GenericNetworkTransmitter>
4645 /// </summary>
4746 private byte [ ] mostRecentDataBuffer ;
4847
48+ #if ! UNITY_EDITOR && UNITY_WSA
49+ /// <summary>
50+ /// Tracks the network connection to the remote machine we are sending meshes to.
51+ /// </summary>
52+ private StreamSocket networkConnection ;
53+
54+ /// <summary>
55+ /// If we are running as the server, this is the listener the server will use.
56+ /// </summary>
57+ private StreamSocketListener networkListener ;
58+
59+ /// <summary>
60+ /// If we cannot connect to the server, this is how long we will wait before retrying.
61+ /// </summary>
62+ private float timeToDeferFailedConnections = 10.0f ;
63+ #endif
64+
4965 /// <summary>
5066 /// If someone connects to us, this is the data we will send them.
5167 /// </summary>
@@ -58,10 +74,10 @@ public void SetData(byte[] data)
5874 /// <summary>
5975 /// Tells us who to contact if we need data.
6076 /// </summary>
61- /// <param name="_serverIp "></param>
62- public void SetServerIp ( string _serverIp )
77+ /// <param name="newServerIp "></param>
78+ public void SetServerIp ( string newServerIp )
6379 {
64- serverIp = _serverIp . Trim ( ) ;
80+ serverIp = newServerIp . Trim ( ) ;
6581 }
6682
6783 /// <summary>
@@ -73,28 +89,12 @@ public void RequestAndGetData()
7389 ConnectListener ( ) ;
7490 }
7591
76- // A lot of the work done in this class can only be done in UWP. The editor is not a UWP app.
77- #if ! UNITY_EDITOR && UNITY_WSA
78- /// <summary>
79- /// Tracks the network connection to the remote machine we are sending meshes to.
80- /// </summary>
81- private StreamSocket networkConnection ;
82-
83- /// <summary>
84- /// If we are running as the server, this is the listener the server will use.
85- /// </summary>
86- private StreamSocketListener networkListener ;
87-
88- /// <summary>
89- /// If we cannot connect to the server, this is how long we will wait before retrying.
90- /// </summary>
91- private float timeToDeferFailedConnections = 10.0f ;
92-
9392 /// <summary>
9493 /// Configures the network transmitter as the source.
9594 /// </summary>
9695 public void ConfigureAsServer ( )
9796 {
97+ #if ! UNITY_EDITOR && UNITY_WSA
9898 Task t = new Task ( ( ) =>
9999 {
100100 networkListener = new StreamSocketListener ( ) ;
@@ -103,8 +103,36 @@ public void ConfigureAsServer()
103103 }
104104 ) ;
105105 t . Start ( ) ;
106+ #else
107+ Debug . Log ( "This script is not intended to be run from the Unity Editor" ) ;
108+ // In order to avoid compiler warnings in the Unity Editor we have to access a few of our fields.
109+ Debug . Log ( string . Format ( "serverIP = {0} waitingForConnection = {1} mostRecentDataBuffer = {2}" , serverIp , waitingForConnection , mostRecentDataBuffer == null ? "No there" : "there" ) ) ;
110+ #endif
106111 }
107112
113+ /// <summary>
114+ /// Connects to the server and requests data.
115+ /// </summary>
116+ private void ConnectListener ( )
117+ {
118+ #if ! UNITY_EDITOR && UNITY_WSA
119+ if ( waitingForConnection )
120+ {
121+ return ;
122+ }
123+
124+ waitingForConnection = true ;
125+ Debug . Log ( "Connecting to " + serverIp ) ;
126+ HostName networkHost = new HostName ( serverIp ) ;
127+ networkConnection = new StreamSocket ( ) ;
128+
129+ IAsyncAction outstandingAction = networkConnection . ConnectAsync ( networkHost , SendConnectionPort . ToString ( ) ) ;
130+ AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler ( RcvNetworkConnectedHandler ) ;
131+ outstandingAction . Completed = aach ;
132+ #endif
133+ }
134+
135+ #if ! UNITY_EDITOR && UNITY_WSA
108136 /// <summary>
109137 /// When a connection is made to us, this call back gets called and
110138 /// we send our data.
@@ -131,26 +159,6 @@ private void NetworkListener_ConnectionReceived(StreamSocketListener sender, Str
131159 }
132160 }
133161
134- /// <summary>
135- /// Connects to the server and requests data.
136- /// </summary>
137- private void ConnectListener ( )
138- {
139- if ( waitingForConnection )
140- {
141- return ;
142- }
143-
144- Debug . Log ( "Connecting to " + serverIP ) ;
145- waitingForConnection = true ;
146- HostName networkHost = new HostName ( serverIP ) ;
147- networkConnection = new StreamSocket ( ) ;
148-
149- IAsyncAction outstandingAction = networkConnection . ConnectAsync ( networkHost , SendConnectionPort . ToString ( ) ) ;
150- AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler ( RcvNetworkConnectedHandler ) ;
151- outstandingAction . Completed = aach ;
152- }
153-
154162 /// <summary>
155163 /// When a connection to the server is established and we can start reading the data, this will be called.
156164 /// </summary>
@@ -187,7 +195,7 @@ private async void RcvNetworkConnectedHandler(IAsyncAction asyncInfo, AsyncStatu
187195 networkDataReader . ReadBytes ( mostRecentDataBuffer ) ;
188196
189197 // And fire our data ready event.
190- dataReadyEvent ? . Invoke ( mostRecentDataBuffer ) ;
198+ DataReadyEvent ? . Invoke ( mostRecentDataBuffer ) ;
191199 }
192200 }
193201 else
@@ -198,15 +206,6 @@ private async void RcvNetworkConnectedHandler(IAsyncAction asyncInfo, AsyncStatu
198206 networkConnection . Dispose ( ) ;
199207 waitingForConnection = false ;
200208 }
201-
202- #else
203- public void ConfigureAsServer ( )
204- {
205- Debug . Log ( "This script is not intended to be run from the Unity Editor" ) ;
206- // In order to avoid compiler warnings in the Unity Editor we have to access a few of our fields.
207- Debug . Log ( string . Format ( "serverIP = {0} waitingForConnection = {1} mostRecentDataBuffer = {2}" , serverIp , waitingForConnection , mostRecentDataBuffer == null ? "No there" : "there" ) ) ;
208- }
209- private void ConnectListener ( ) { }
210209#endif
211210 }
212211}
0 commit comments