1- using UnityEngine ;
1+ using System ;
2+ using UnityEngine ;
23
34namespace HoloToolkit . Sharing
45{
56 public class SharingStage : MonoBehaviour
67 {
8+ /// <summary>
9+ /// SharingManagerConnected event notifies when the sharing manager is created and connected.
10+ /// </summary>
11+ public event EventHandler SharingManagerConnected ;
12+
713 public static SharingStage Instance = null ;
814
915 /// <summary>
@@ -24,45 +30,81 @@ public class SharingStage : MonoBehaviour
2430 /// </summary>
2531 public bool IsAudioEndpoint = true ;
2632
33+ public bool AutoDiscoverServer = false ;
34+
35+ [ Tooltip ( "Determines how often the discovery service should ping the network in search of a server." ) ]
36+ public float PingIntervalSec = 2 ;
37+
2738 /// <summary>
2839 /// Pipes XTools console output to Unity's output window for debugging
2940 /// </summary>
3041 private ConsoleLogWriter logWriter ;
3142
43+ /// <summary>
44+ /// Enables Server Discovery on the network
45+ /// </summary>
46+ private DiscoveryClient discoveryClient ;
47+ /// <summary>
48+ /// Provides callbacks when server is discovered or lost.
49+ /// </summary>
50+ private DiscoveryClientAdapter discoveryClientAdapter ;
51+
52+ private float pingIntervalCurrent = 0 ;
53+ private bool isTryingToFindServer = false ;
54+
3255 private void Awake ( )
3356 {
3457 Instance = this ;
3558
3659 this . logWriter = new ConsoleLogWriter ( ) ;
3760
38- ClientConfig config = new ClientConfig ( this . ClientRole ) ;
39- config . SetIsAudioEndpoint ( this . IsAudioEndpoint ) ;
40- config . SetLogWriter ( this . logWriter ) ;
41- config . SetServerAddress ( this . ServerAddress ) ;
42- config . SetServerPort ( this . ServerPort ) ;
43- config . SetProfilerEnabled ( false ) ;
44-
45- this . sharingMgr = SharingManager . Create ( config ) ;
61+ if ( AutoDiscoverServer )
62+ {
63+ AutoDiscoverInit ( ) ;
64+ }
65+ else
66+ {
67+ Connect ( ) ;
68+ }
4669 }
4770
4871 protected void OnDestroy ( )
4972 {
5073 Instance = null ;
5174
52- // Force a disconnection so that we can stop and start Unity without connections hanging around
53- this . sharingMgr . GetPairedConnection ( ) . Disconnect ( ) ;
54- this . sharingMgr . GetServerConnection ( ) . Disconnect ( ) ;
75+ if ( this . discoveryClient != null )
76+ {
77+ discoveryClient . RemoveListener ( discoveryClientAdapter ) ;
78+ discoveryClient . Dispose ( ) ;
79+ discoveryClient = null ;
80+
81+ if ( discoveryClientAdapter != null )
82+ {
83+ discoveryClientAdapter . Dispose ( ) ;
84+ discoveryClientAdapter = null ;
85+ }
86+ }
5587
56- // Release the XTools manager so that it cleans up the C++ copy
57- this . sharingMgr . Dispose ( ) ;
58- this . sharingMgr = null ;
88+ if ( this . sharingMgr != null )
89+ {
90+ // Force a disconnection so that we can stop and start Unity without connections hanging around
91+ this . sharingMgr . GetPairedConnection ( ) . Disconnect ( ) ;
92+ this . sharingMgr . GetServerConnection ( ) . Disconnect ( ) ;
5993
94+ // Release the XTools manager so that it cleans up the C++ copy
95+ this . sharingMgr . Dispose ( ) ;
96+ this . sharingMgr = null ;
97+ }
6098 // Forces a garbage collection to try to clean up any additional reference to SWIG-wrapped objects
6199 System . GC . Collect ( ) ;
62100 }
63101
64102 private void LateUpdate ( )
65103 {
104+ if ( this . isTryingToFindServer )
105+ {
106+ AutoDiscoverUpdate ( ) ;
107+ }
66108 if ( this . sharingMgr != null )
67109 {
68110 // Update the XToolsManager to processes any network messages that have arrived
@@ -80,6 +122,75 @@ private void OnDisable()
80122 Application . logMessageReceived -= HandleLog ;
81123 }
82124
125+ private void Connect ( )
126+ {
127+ ClientConfig config = new ClientConfig ( this . ClientRole ) ;
128+ config . SetIsAudioEndpoint ( this . IsAudioEndpoint ) ;
129+ config . SetLogWriter ( this . logWriter ) ;
130+ config . SetServerAddress ( this . ServerAddress ) ;
131+ config . SetServerPort ( this . ServerPort ) ;
132+ config . SetProfilerEnabled ( false ) ;
133+
134+ this . sharingMgr = SharingManager . Create ( config ) ;
135+
136+ //delay sending notification so everything is initialized properly
137+ Invoke ( "SendConnectedNotification" , 1 ) ;
138+ }
139+
140+ private void SendConnectedNotification ( )
141+ {
142+ if ( Manager . GetServerConnection ( ) . IsConnected ( ) )
143+ {
144+ //Send notification that we're connected
145+ EventHandler connectedEvent = SharingManagerConnected ;
146+ if ( connectedEvent != null )
147+ {
148+ connectedEvent ( this , EventArgs . Empty ) ;
149+ }
150+ }
151+ else
152+ {
153+ Log . Error ( string . Format ( "Cannot connect to server {0}:{1}" , ServerAddress , ServerPort ) ) ;
154+ }
155+ }
156+
157+ private void AutoDiscoverInit ( )
158+ {
159+ discoveryClientAdapter = new DiscoveryClientAdapter ( ) ;
160+ discoveryClientAdapter . DiscoveredEvent += OnSystemDiscovered ;
161+
162+ discoveryClient = DiscoveryClient . Create ( ) ;
163+ discoveryClient . AddListener ( discoveryClientAdapter ) ;
164+
165+ //Start Finding Server
166+ isTryingToFindServer = true ;
167+ }
168+
169+ private void AutoDiscoverUpdate ( )
170+ {
171+ //Searching Enabled-> Update DiscoveryClient to check results, Wait Interval then Ping network.
172+ pingIntervalCurrent += Time . deltaTime ;
173+ if ( pingIntervalCurrent > PingIntervalSec )
174+ {
175+ pingIntervalCurrent = 0 ;
176+ discoveryClient . Ping ( ) ;
177+ }
178+ discoveryClient . Update ( ) ;
179+ }
180+
181+ private void OnSystemDiscovered ( DiscoveredSystem obj )
182+ {
183+ if ( obj . GetRole ( ) == SystemRole . SessionDiscoveryServerRole )
184+ {
185+ //Found a server. Stop pinging the network and connect
186+ isTryingToFindServer = false ;
187+ this . ServerAddress = obj . GetAddress ( ) ;
188+ Debug . Log ( "System Discovered at: " + this . ServerAddress ) ;
189+ Connect ( ) ;
190+ Debug . Log ( string . Format ( "Connected to: {0}:{1}" , this . ServerAddress , this . ServerPort ) ) ;
191+ }
192+ }
193+
83194 void HandleLog ( string logString , string stackTrace , LogType type )
84195 {
85196 switch ( type )
0 commit comments