@@ -164,6 +164,8 @@ public bool IsConnected
164164 get { return Manager != null && Connection != null && Connection . IsConnected ( ) ; }
165165 }
166166
167+ #region Unity Methods
168+
167169 protected override void Awake ( )
168170 {
169171 base . Awake ( ) ;
@@ -177,13 +179,13 @@ protected override void Awake()
177179 }
178180 else
179181 {
180- Connect ( ) ;
182+ ManagerInit ( ) ;
181183 }
182184 }
183185
184186 private void OnEnable ( )
185187 {
186- Application . logMessageReceived += HandleLog ;
188+ Application . logMessageReceived += OnLogReceived ;
187189 }
188190
189191 private void LateUpdate ( )
@@ -202,7 +204,7 @@ private void LateUpdate()
202204
203205 private void OnDisable ( )
204206 {
205- Application . logMessageReceived -= HandleLog ;
207+ Application . logMessageReceived -= OnLogReceived ;
206208 }
207209
208210 protected override void OnDestroy ( )
@@ -259,7 +261,77 @@ protected override void OnDestroy()
259261 base . OnDestroy ( ) ;
260262 }
261263
262- private void Connect ( )
264+ #endregion // Unity Methods
265+
266+ #region Callbacks
267+
268+ private void OnNetworkConnectionChanged ( NetworkConnection networkConnection )
269+ {
270+ if ( IsConnected )
271+ {
272+ if ( SharingManagerConnected != null )
273+ {
274+ SharingManagerConnected ( this , EventArgs . Empty ) ;
275+ }
276+ }
277+ else
278+ {
279+ if ( SharingManagerDisconnected != null )
280+ {
281+ SharingManagerDisconnected ( this , EventArgs . Empty ) ;
282+ }
283+ }
284+ }
285+
286+ private void OnSystemDiscovered ( DiscoveredSystem system )
287+ {
288+ if ( system . GetRole ( ) != SystemRole . SessionDiscoveryServerRole ) { return ; }
289+
290+ // Found a server. Stop pinging the network and connect.
291+ isTryingToFindServer = false ;
292+ ServerAddress = system . GetAddress ( ) ;
293+
294+ if ( ShowDetailedLogs )
295+ {
296+ Debug . Log ( "Server discovered at: " + ServerAddress ) ;
297+ }
298+
299+ ManagerInit ( ) ;
300+
301+ if ( ShowDetailedLogs )
302+ {
303+ Debug . LogFormat ( "Connected to: {0}:{1}" , ServerAddress , ServerPort . ToString ( ) ) ;
304+ }
305+ }
306+
307+ private void OnLogReceived ( string logString , string stackTrace , LogType type )
308+ {
309+ switch ( type )
310+ {
311+ case LogType . Error :
312+ case LogType . Assert :
313+ case LogType . Exception :
314+ Log . Error ( string . Format ( "{0} \n {1}" , logString , stackTrace ) ) ;
315+ break ;
316+
317+ case LogType . Warning :
318+ Log . Warning ( string . Format ( "{0} \n {1}" , logString , stackTrace ) ) ;
319+ break ;
320+
321+ case LogType . Log :
322+ if ( ShowDetailedLogs )
323+ {
324+ Log . Info ( logString ) ;
325+ }
326+ break ;
327+ default :
328+ throw new ArgumentOutOfRangeException ( "type" , type , "Invalid Message Type" ) ;
329+ }
330+ }
331+
332+ #endregion // Callbacks
333+
334+ private void ManagerInit ( )
263335 {
264336 var config = new ClientConfig ( ClientRole ) ;
265337 config . SetIsAudioEndpoint ( IsAudioEndpoint ) ;
@@ -276,8 +348,8 @@ private void Connect()
276348
277349 // Set up callbacks so that we know when we've connected successfully.
278350 networkConnectionAdapter = new NetworkConnectionAdapter ( ) ;
279- networkConnectionAdapter . ConnectedCallback += NetworkConnectionAdapter_ConnectedCallback ;
280- networkConnectionAdapter . DisconnectedCallback += NetworkConnectionAdapter_ConnectedCallback ;
351+ networkConnectionAdapter . ConnectedCallback += OnNetworkConnectionChanged ;
352+ networkConnectionAdapter . DisconnectedCallback += OnNetworkConnectionChanged ;
281353 Connection . AddListener ( ( byte ) MessageID . StatusOnly , networkConnectionAdapter ) ;
282354
283355 SyncStateListener = new SyncStateListener ( ) ;
@@ -306,29 +378,6 @@ private void Connect()
306378 }
307379 }
308380
309- private void NetworkConnectionAdapter_ConnectedCallback ( NetworkConnection obj )
310- {
311- SendConnectedNotification ( ) ;
312- }
313-
314- private void SendConnectedNotification ( )
315- {
316- if ( IsConnected )
317- {
318- if ( SharingManagerConnected != null )
319- {
320- SharingManagerConnected ( this , EventArgs . Empty ) ;
321- }
322- }
323- else
324- {
325- if ( SharingManagerDisconnected != null )
326- {
327- SharingManagerDisconnected ( this , EventArgs . Empty ) ;
328- }
329- }
330- }
331-
332381 private void AutoDiscoverInit ( )
333382 {
334383 if ( ShowDetailedLogs )
@@ -363,27 +412,6 @@ private void AutoDiscoverUpdate()
363412 discoveryClient . Update ( ) ;
364413 }
365414
366- private void OnSystemDiscovered ( DiscoveredSystem obj )
367- {
368- if ( obj . GetRole ( ) != SystemRole . SessionDiscoveryServerRole ) { return ; }
369-
370- // Found a server. Stop pinging the network and connect.
371- isTryingToFindServer = false ;
372- ServerAddress = obj . GetAddress ( ) ;
373-
374- if ( ShowDetailedLogs )
375- {
376- Debug . Log ( "Server discovered at: " + ServerAddress ) ;
377- }
378-
379- Connect ( ) ;
380-
381- if ( ShowDetailedLogs )
382- {
383- Debug . LogFormat ( "Connected to: {0}:{1}" , ServerAddress , ServerPort . ToString ( ) ) ;
384- }
385- }
386-
387415 public void ConnectToServer ( string serverAddress , int port )
388416 {
389417 ServerAddress = serverAddress ;
@@ -395,30 +423,5 @@ public void ConnectToServer()
395423 {
396424 Manager . SetServerConnectionInfo ( ServerAddress , ( uint ) ServerPort ) ;
397425 }
398-
399- private void HandleLog ( string logString , string stackTrace , LogType type )
400- {
401- switch ( type )
402- {
403- case LogType . Error :
404- case LogType . Assert :
405- case LogType . Exception :
406- Log . Error ( string . Format ( "{0} \n {1}" , logString , stackTrace ) ) ;
407- break ;
408-
409- case LogType . Warning :
410- Log . Warning ( string . Format ( "{0} \n {1}" , logString , stackTrace ) ) ;
411- break ;
412-
413- case LogType . Log :
414- if ( ShowDetailedLogs )
415- {
416- Log . Info ( logString ) ;
417- }
418- break ;
419- default :
420- throw new ArgumentOutOfRangeException ( "type" , type , "Invalid Message Type" ) ;
421- }
422- }
423426 }
424427}
0 commit comments