@@ -18,8 +18,6 @@ public class AutoJoinSessionAndRoom : Singleton<AutoJoinSessionAndRoom>
1818 /// </summary>
1919 private long roomID = 1 ;
2020
21- private Coroutine autoConnect ;
22-
2321 private static bool ShouldLocalUserCreateRoom
2422 {
2523 get
@@ -52,26 +50,23 @@ private void Start()
5250 // SharingStage should be valid at this point, but we may not be connected.
5351 if ( SharingStage . Instance . IsConnected )
5452 {
55- Connected ( ) ;
53+ SharingManagerConnected ( ) ;
5654 }
5755 else
5856 {
59- Disconnected ( ) ;
57+ SessionTrackerDisconnected ( ) ;
6058 }
6159 }
6260
6361 protected override void OnDestroy ( )
6462 {
6563 if ( SharingStage . Instance != null )
6664 {
67- SharingStage . Instance . SharingManagerConnected -= Connected ;
68- SharingStage . Instance . SessionsTracker . ServerDisconnected -= Disconnected ;
65+ SharingStage . Instance . SharingManagerConnected -= SharingManagerConnected ;
66+ SharingStage . Instance . SessionsTracker . ServerDisconnected -= SessionTrackerDisconnected ;
6967 }
7068
71- if ( autoConnect != null )
72- {
73- StopCoroutine ( autoConnect ) ;
74- }
69+ StopCoroutine ( AutoConnect ( ) ) ;
7570
7671 base . OnDestroy ( ) ;
7772 }
@@ -81,68 +76,38 @@ protected override void OnDestroy()
8176 /// </summary>
8277 /// <param name="sender">Sender.</param>
8378 /// <param name="e">Events Arguments.</param>
84- private void Connected ( object sender = null , EventArgs e = null )
79+ private void SharingManagerConnected ( object sender = null , EventArgs e = null )
8580 {
86- if ( autoConnect != null )
87- {
88- StopCoroutine ( autoConnect ) ;
89- }
90-
91- SharingStage . Instance . SharingManagerConnected -= Connected ;
92- SharingStage . Instance . SessionsTracker . ServerDisconnected += Disconnected ;
81+ SharingStage . Instance . SharingManagerConnected -= SharingManagerConnected ;
82+ SharingStage . Instance . SessionsTracker . ServerDisconnected += SessionTrackerDisconnected ;
9383 }
9484
9585 /// <summary>
9686 /// Called when the Session Tracker connects to a server.
9787 /// </summary>
98- private void Disconnected ( )
88+ private void SessionTrackerDisconnected ( )
9989 {
100- SharingStage . Instance . SessionsTracker . ServerDisconnected -= Disconnected ;
101- SharingStage . Instance . SharingManagerConnected += Connected ;
90+ SharingStage . Instance . SharingManagerConnected += SharingManagerConnected ;
91+ SharingStage . Instance . SessionsTracker . ServerDisconnected -= SessionTrackerDisconnected ;
10292
10393 if ( SharingStage . Instance . ClientRole == ClientRole . Primary )
10494 {
105- autoConnect = StartCoroutine ( AutoConnect ( ) ) ;
95+ StartCoroutine ( AutoConnect ( ) ) ;
10696 }
10797 }
10898
10999 private IEnumerator AutoConnect ( )
110100 {
111101 if ( SharingStage . Instance . ShowDetailedLogs )
112102 {
113- Debug . Log ( "[AutoJoinSession] Attempting to connect..." ) ;
114- }
115-
116- if ( SharingStage . Instance . SessionsTracker == null )
117- {
118- if ( SharingStage . Instance . ShowDetailedLogs )
119- {
120- Debug . LogWarning ( "[AutoJoinSession] Sharing Manager is not ready! Attempting to reinitialize..." ) ;
121- }
122-
123- SharingStage . Instance . ConnectToServer ( ) ;
124- }
125-
126- while ( SharingStage . Instance . SessionsTracker == null )
127- {
128- yield return null ;
103+ Debug . Log ( "[AutoJoinSessionAndRoom] Attempting to connect..." ) ;
129104 }
130105
131- if ( SharingStage . Instance . SessionsTracker . IsServerConnected )
106+ if ( ! SharingStage . Instance . SessionsTracker . IsServerConnected )
132107 {
133108 if ( SharingStage . Instance . ShowDetailedLogs )
134109 {
135- Debug . LogFormat ( "[AutoJoinSession] Looking for {0}..." , SharingStage . Instance . SessionName ) ;
136-
137- Debug . LogFormat ( "[AutoJoinSession] Successfully connected to server with {0} Sessions." ,
138- SharingStage . Instance . SessionsTracker . Sessions . Count . ToString ( ) ) ;
139- }
140- }
141- else
142- {
143- if ( SharingStage . Instance . ShowDetailedLogs )
144- {
145- Debug . LogWarning ( "[AutoJoinSession] Disconnected from server. Waiting for a connection... " ) ;
110+ Debug . LogWarning ( "[AutoJoinSessionAndRoom] Disconnected from server. Waiting for a connection... " ) ;
146111 }
147112
148113 while ( ! SharingStage . Instance . SessionsTracker . IsServerConnected )
@@ -152,137 +117,126 @@ private IEnumerator AutoConnect()
152117
153118 if ( SharingStage . Instance . ShowDetailedLogs )
154119 {
155- Debug . Log ( "[AutoJoinSession ] Connected!" ) ;
120+ Debug . Log ( "[AutoJoinSessionAndRoom ] Connected!" ) ;
156121 }
157122 }
158123
159- // If we are a Primary Client and can join sessions...
160- // Check to see if we aren't already in the desired session
161- Session currentSession = SharingStage . Instance . SessionsTracker . GetCurrentSession ( ) ;
162-
163- // We're not in a valid session.
164- if ( currentSession == null )
124+ if ( SharingStage . Instance . ShowDetailedLogs )
165125 {
166- if ( SharingStage . Instance . ShowDetailedLogs )
167- {
168- Debug . Log ( "[AutoJoinSession] Didn't find the session, making a new one..." ) ;
169- }
170-
171- yield return SharingStage . Instance . SessionsTracker . CreateSession ( new XString ( SharingStage . Instance . SessionName ) ) ;
126+ Debug . LogFormat ( "[AutoJoinSessionAndRoom] Looking for {0}..." , SharingStage . Instance . SessionName ) ;
172127
173- currentSession = SharingStage . Instance . SessionsTracker . GetCurrentSession ( ) ;
128+ Debug . LogFormat ( "[AutoJoinSessionAndRoom] Successfully connected to server with {0} Sessions." ,
129+ SharingStage . Instance . SessionsTracker . Sessions . Count . ToString ( ) ) ;
174130 }
175131
176- // We're already in the Session we're searching for.
177- if ( currentSession != null && currentSession . GetName ( ) . GetString ( ) == SharingStage . Instance . SessionName )
132+ yield return new WaitForEndOfFrame ( ) ;
133+
134+ bool sessionExists = false ;
135+
136+ for ( int i = 0 ; i < SharingStage . Instance . SessionsTracker . Sessions . Count ; ++ i )
178137 {
179- if ( SharingStage . Instance . ShowDetailedLogs )
138+ if ( SharingStage . Instance . SessionsTracker . Sessions [ i ] . GetName ( ) . GetString ( ) ==
139+ SharingStage . Instance . SessionName )
180140 {
181- Debug . LogFormat ( "[AutoJoinSession] We're already in the session we're attempting to join." ) ;
182- }
141+ sessionExists = true ;
142+ if ( SharingStage . Instance . ShowDetailedLogs )
143+ {
144+ Debug . LogFormat ( "[AutoJoinSessionAndRoom] Joining session {0}..." , SharingStage . Instance . SessionName ) ;
145+ }
183146
184- autoConnect = null ;
185- yield break ;
147+ yield return SharingStage . Instance . SessionsTracker . JoinSession ( SharingStage . Instance . SessionsTracker . Sessions [ i ] ) ;
148+
149+ yield return new WaitForEndOfFrame ( ) ;
150+ break ;
151+ }
186152 }
187153
188- // We're already connected to the session.
189- if ( currentSession != null && currentSession . GetMachineSessionState ( ) != MachineSessionState . DISCONNECTED )
154+ if ( ! sessionExists )
190155 {
191156 if ( SharingStage . Instance . ShowDetailedLogs )
192157 {
193- Debug . LogFormat ( "[AutoJoinSession] We're joining or we've already joined the session." ) ;
158+ Debug . LogFormat ( "[AutoJoinSessionAndRoom] Didn't find session {0}, making a new one..." , SharingStage . Instance . SessionName ) ;
194159 }
195160
196- autoConnect = null ;
197- yield break ;
198- }
161+ yield return SharingStage . Instance . SessionsTracker . CreateSession ( SharingStage . Instance . SessionName ) ;
199162
200- for ( int i = 0 ; i < SharingStage . Instance . SessionsTracker . Sessions . Count ; ++ i )
201- {
202- if ( SharingStage . Instance . SessionsTracker . Sessions [ i ] . GetName ( ) . GetString ( ) != SharingStage . Instance . SessionName )
203- {
204- continue ;
205- }
163+ yield return new WaitForEndOfFrame ( ) ;
206164
207- if ( SharingStage . Instance . ShowDetailedLogs )
165+ while ( SharingStage . Instance . SessionsTracker . GetCurrentSession ( ) == null )
208166 {
209- Debug . LogFormat ( "[AutoJoinSession] Joining session {0}..." , SharingStage . Instance . SessionName ) ;
167+ yield return null ;
210168 }
211-
212- yield return SharingStage . Instance . SessionsTracker . JoinSession ( SharingStage . Instance . SessionsTracker . Sessions [ i ] ) ;
213169 }
214170
215- while ( currentSession != null && currentSession . GetMachineSessionState ( ) != MachineSessionState . JOINED )
171+ while ( SharingStage . Instance . SessionsTracker . GetCurrentSession ( ) . GetMachineSessionState ( ) != MachineSessionState . JOINED )
216172 {
217173 yield return null ;
218174 }
219175
220176 if ( SharingStage . Instance . ShowDetailedLogs )
221177 {
222- Debug . LogFormat ( "[AutoJoinSession ] Joined session {0} successfully!" , SharingStage . Instance . SessionName ) ;
178+ Debug . LogFormat ( "[AutoJoinSessionAndRoom ] Joined session {0} successfully!" , SharingStage . Instance . SessionName ) ;
223179 }
224180
225- // First check if there is a current room
226- var currentRoom = SharingStage . Instance . CurrentRoom ;
227-
228- while ( SharingStage . Instance . CurrentRoom != null )
229- {
230- yield return null ;
231- }
181+ yield return new WaitForEndOfFrame ( ) ;
232182
233- if ( currentRoom == null || SharingStage . Instance . CurrentRoomManager . GetRoomCount ( ) == 0 )
183+ if ( SharingStage . Instance . CurrentRoomManager . GetRoomCount ( ) == 0 )
234184 {
235185 // If we are the user with the lowest user ID, we will create the room.
236186 if ( ShouldLocalUserCreateRoom )
237187 {
238188 if ( SharingStage . Instance . ShowDetailedLogs )
239189 {
240- Debug . LogFormat ( "[AutoJoinSession ] Creating room {0}..." , SharingStage . Instance . RoomName ) ;
190+ Debug . LogFormat ( "[AutoJoinSessionAndRoom ] Creating room {0}..." , SharingStage . Instance . RoomName ) ;
241191 }
242192
243193 // To keep anchors alive even if all users have left the session...
244194 // Pass in true instead of false in CreateRoom.
245- currentRoom = SharingStage . Instance . CurrentRoomManager . CreateRoom (
195+ SharingStage . Instance . CurrentRoomManager . CreateRoom (
246196 new XString ( SharingStage . Instance . RoomName ) ,
247197 roomID ,
248198 SharingStage . Instance . KeepRoomAlive ) ;
249199 }
250200 }
251201 else if ( SharingStage . Instance . CurrentRoomManager . GetRoomCount ( ) > 0 )
252202 {
203+ if ( SharingStage . Instance . CurrentRoom != null )
204+ {
205+ SharingStage . Instance . CurrentRoomManager . LeaveRoom ( ) ;
206+ }
207+
208+ yield return new WaitForEndOfFrame ( ) ;
209+
253210 // Look through the existing rooms and join the one that matches the room name provided.
254211 for ( int i = 0 ; i < SharingStage . Instance . CurrentRoomManager . GetRoomCount ( ) ; i ++ )
255212 {
256- if ( SharingStage . Instance . CurrentRoomManager . GetRoom ( i ) . GetName ( ) . GetString ( ) . Equals ( SharingStage . Instance . RoomName ,
257- StringComparison . OrdinalIgnoreCase ) )
213+ if ( SharingStage . Instance . CurrentRoomManager . GetRoom ( i ) . GetName ( ) . GetString ( ) . Equals ( SharingStage . Instance . RoomName , StringComparison . OrdinalIgnoreCase ) )
258214 {
259- currentRoom = SharingStage . Instance . CurrentRoomManager . GetRoom ( i ) ;
260- SharingStage . Instance . CurrentRoomManager . JoinRoom ( currentRoom ) ;
215+ SharingStage . Instance . CurrentRoomManager . JoinRoom ( SharingStage . Instance . CurrentRoomManager . GetRoom ( i ) ) ;
261216
262217 if ( SharingStage . Instance . ShowDetailedLogs )
263218 {
264- Debug . LogFormat ( "[AutoJoinSession ] Joining room {0}..." , currentRoom . GetName ( ) . GetString ( ) ) ;
219+ Debug . LogFormat ( "[AutoJoinSessionAndRoom ] Joining room {0}..." , SharingStage . Instance . CurrentRoomManager . GetRoom ( i ) . GetName ( ) . GetString ( ) ) ;
265220 }
266221
267222 break ;
268223 }
269224 }
270225 }
271226
272- if ( currentRoom == null )
227+ while ( SharingStage . Instance . CurrentRoom == null )
273228 {
274- Debug . LogError ( "[AutoJoinSession] Unable to create or join a room!" ) ;
275- yield break ;
229+ yield return null ;
276230 }
277231
278232 if ( SharingStage . Instance . ShowDetailedLogs )
279233 {
280- Debug . LogFormat ( "[AutoJoinSession ] Joined room {0} successfully!" , currentRoom . GetName ( ) . GetString ( ) ) ;
234+ Debug . LogFormat ( "[AutoJoinSessionAndRoom ] Joined room {0} successfully!" , SharingStage . Instance . CurrentRoom . GetName ( ) . GetString ( ) ) ;
281235 }
282236
283- SharingWorldAnchorManager . Instance . AttachAnchor ( gameObject ) ;
237+ yield return new WaitForEndOfFrame ( ) ;
284238
285- autoConnect = null ;
239+ SharingWorldAnchorManager . Instance . AttachAnchor ( gameObject ) ;
286240 }
287241 }
288242}
0 commit comments