Skip to content

Commit 915cd7b

Browse files
authored
Merge pull request #306 from Holo-Krzysztof/sharing-fixes
Sharing fixes
2 parents 82fc644 + 5b8ab53 commit 915cd7b

File tree

2 files changed

+78
-44
lines changed

2 files changed

+78
-44
lines changed

Assets/HoloToolkit/Sharing/Scripts/SharingStage.cs

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
using System;
1+
using HoloToolkit.Unity;
2+
using System;
23
using UnityEngine;
34

45
namespace HoloToolkit.Sharing
56
{
6-
public class SharingStage : MonoBehaviour
7+
public class SharingStage : Singleton<SharingStage>
78
{
89
/// <summary>
910
/// SharingManagerConnected event notifies when the sharing manager is created and connected.
1011
/// </summary>
1112
public event EventHandler SharingManagerConnected;
1213

13-
public static SharingStage Instance = null;
14-
1514
/// <summary>
1615
/// Set whether this app should be a Primary or Secondary client.
1716
/// Primary: Connects directly to the Session Server, can create/join/leave sessions
@@ -48,30 +47,39 @@ public class SharingStage : MonoBehaviour
4847
/// Provides callbacks when server is discovered or lost.
4948
/// </summary>
5049
private DiscoveryClientAdapter discoveryClientAdapter;
50+
51+
/// <summary>
52+
/// Provides callbacks for when we connect to a server.
53+
/// </summary>
54+
private NetworkConnectionAdapter networkConnectionAdapter = null;
55+
56+
private NetworkConnection networkConnection = null;
5157

5258
private float pingIntervalCurrent = 0;
5359
private bool isTryingToFindServer = false;
5460

5561
private void Awake()
5662
{
57-
Instance = this;
58-
5963
this.logWriter = new ConsoleLogWriter();
6064

6165
if (AutoDiscoverServer)
6266
{
6367
AutoDiscoverInit();
6468
}
65-
else
69+
70+
networkConnectionAdapter = new NetworkConnectionAdapter();
71+
}
72+
73+
private void Start()
74+
{
75+
if (!AutoDiscoverServer)
6676
{
6777
Connect();
6878
}
6979
}
7080

7181
protected void OnDestroy()
7282
{
73-
Instance = null;
74-
7583
if (this.discoveryClient != null)
7684
{
7785
discoveryClient.RemoveListener(discoveryClientAdapter);
@@ -85,6 +93,19 @@ protected void OnDestroy()
8593
}
8694
}
8795

96+
if (this.networkConnection != null)
97+
{
98+
networkConnection.RemoveListener((byte)MessageID.StatusOnly, networkConnectionAdapter);
99+
networkConnection.Dispose();
100+
networkConnection = null;
101+
102+
if (networkConnectionAdapter != null)
103+
{
104+
networkConnectionAdapter.Dispose();
105+
networkConnectionAdapter = null;
106+
}
107+
}
108+
88109
if (this.sharingMgr != null)
89110
{
90111
// Force a disconnection so that we can stop and start Unity without connections hanging around
@@ -133,8 +154,16 @@ private void Connect()
133154

134155
this.sharingMgr = SharingManager.Create(config);
135156

136-
//delay sending notification so everything is initialized properly
137-
Invoke("SendConnectedNotification", 1);
157+
//set up callbacks so that we know when we've connected successfully
158+
this.networkConnection = sharingMgr.GetServerConnection();
159+
this.networkConnectionAdapter = new NetworkConnectionAdapter();
160+
networkConnectionAdapter.ConnectedCallback += NetworkConnectionAdapter_ConnectedCallback;
161+
networkConnection.AddListener((byte)MessageID.StatusOnly, networkConnectionAdapter);
162+
}
163+
164+
private void NetworkConnectionAdapter_ConnectedCallback(NetworkConnection obj)
165+
{
166+
SendConnectedNotification();
138167
}
139168

140169
private void SendConnectedNotification()

Assets/HoloToolkit/Sharing/Tests/ImportExportAnchorManager.cs

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class ImportExportAnchorManager : Singleton<ImportExportAnchorManager>
1818
/// <summary>
1919
/// Enum to track the progress through establishing a shared coordinate system.
2020
/// </summary>
21-
enum ImportExportState
21+
private enum ImportExportState
2222
{
2323
// Overall states
2424
Start,
@@ -39,7 +39,7 @@ enum ImportExportState
3939
Importing
4040
}
4141

42-
ImportExportState currentState = ImportExportState.Start;
42+
private ImportExportState currentState = ImportExportState.Start;
4343

4444
public string StateName
4545
{
@@ -60,78 +60,87 @@ public bool AnchorEstablished
6060
/// <summary>
6161
/// WorldAnchorTransferBatch is the primary object in serializing/deserializing anchors.
6262
/// </summary>
63-
WorldAnchorTransferBatch sharedAnchorInterface;
63+
private WorldAnchorTransferBatch sharedAnchorInterface;
6464

6565
/// <summary>
6666
/// Keeps track of stored anchor data blob.
6767
/// </summary>
68-
byte[] rawAnchorData = null;
68+
private byte[] rawAnchorData = null;
6969

7070
/// <summary>
7171
/// Keeps track of locally stored anchors.
7272
/// </summary>
73-
WorldAnchorStore anchorStore = null;
73+
private WorldAnchorStore anchorStore = null;
7474

7575
/// <summary>
7676
/// Keeps track of the name of the anchor we are exporting.
7777
/// </summary>
78-
string exportingAnchorName { get; set; }
78+
private string exportingAnchorName { get; set; }
7979

8080
/// <summary>
8181
/// The datablob of the anchor.
8282
/// </summary>
83-
List<byte> exportingAnchorBytes = new List<byte>();
83+
private List<byte> exportingAnchorBytes = new List<byte>();
8484

8585
/// <summary>
8686
/// Keeps track of if the sharing service is ready.
8787
/// We need the sharing service to be ready so we can
8888
/// upload and download data for sharing anchors.
8989
/// </summary>
90-
bool sharingServiceReady = false;
90+
private bool sharingServiceReady = false;
9191

9292
/// <summary>
9393
/// The room manager API for the sharing service.
9494
/// </summary>
95-
RoomManager roomManager;
95+
private RoomManager roomManager;
9696

9797
/// <summary>
9898
/// Keeps track of the current room we are connected to. Anchors
9999
/// are kept in rooms.
100100
/// </summary>
101-
Room currentRoom;
101+
private Room currentRoom;
102102

103103
/// <summary>
104104
/// Sometimes we'll see a really small anchor blob get generated.
105105
/// These tend to not work, so we have a minimum trustable size.
106106
/// </summary>
107-
const uint minTrustworthySerializedAnchorDataSize = 100000;
107+
private const uint minTrustworthySerializedAnchorDataSize = 100000;
108108

109109
/// <summary>
110110
/// Some room ID for indicating which room we are in.
111111
/// </summary>
112-
const long roomID = 8675309;
112+
private const long roomID = 8675309;
113113

114114
/// <summary>
115115
/// Provides updates when anchor data is uploaded/downloaded.
116116
/// </summary>
117-
RoomManagerAdapter roomManagerCallbacks;
117+
private RoomManagerAdapter roomManagerCallbacks;
118118

119-
void Start()
119+
private void Awake()
120120
{
121121
Debug.Log("Import Export Manager starting");
122-
123-
currentState = ImportExportState.Ready;
124-
125122
// We need to get our local anchor store started up.
126123
currentState = ImportExportState.AnchorStore_Initializing;
127124
WorldAnchorStore.GetAsync(AnchorStoreReady);
125+
}
128126

127+
private void Start()
128+
{
129129
//Wait for a notification that the sharing manager has been initialized (connected to sever)
130130
SharingStage.Instance.SharingManagerConnected += SharingManagerConnected;
131+
132+
// We will register for session joined to indicate when the sharing service
133+
// is ready for us to make room related requests.
134+
SharingSessionTracker.Instance.SessionJoined += Instance_SessionJoined;
131135
}
132136

133-
void OnDestroy()
137+
private void OnDestroy()
134138
{
139+
if (SharingStage.Instance != null)
140+
{
141+
SharingStage.Instance.SharingManagerConnected -= SharingManagerConnected;
142+
}
143+
135144
if (roomManagerCallbacks != null)
136145
{
137146
roomManagerCallbacks.AnchorsDownloadedEvent -= RoomManagerCallbacks_AnchorsDownloaded;
@@ -153,10 +162,6 @@ private void SharingManagerConnected(object sender, EventArgs e)
153162
roomManagerCallbacks.AnchorsDownloadedEvent += RoomManagerCallbacks_AnchorsDownloaded;
154163
roomManagerCallbacks.AnchorUploadedEvent += RoomManagerCallbacks_AnchorUploaded;
155164
roomManager.AddListener(roomManagerCallbacks);
156-
157-
// We will register for session joined to indicate when the sharing service
158-
// is ready for us to make room related requests.
159-
SharingSessionTracker.Instance.SessionJoined += Instance_SessionJoined;
160165
}
161166

162167
/// <summary>
@@ -202,7 +207,7 @@ private void RoomManagerCallbacks_AnchorsDownloaded(bool successful, AnchorDownl
202207
/// Called when the local anchor store is ready.
203208
/// </summary>
204209
/// <param name="store"></param>
205-
void AnchorStoreReady(WorldAnchorStore store)
210+
private void AnchorStoreReady(WorldAnchorStore store)
206211
{
207212
anchorStore = store;
208213
currentState = ImportExportState.AnchorStore_Initialized;
@@ -224,15 +229,15 @@ private void Instance_SessionJoined(object sender, SharingSessionTracker.Session
224229
Invoke("MarkSharingServiceReady", 5);
225230
}
226231

227-
void MarkSharingServiceReady()
232+
private void MarkSharingServiceReady()
228233
{
229234
sharingServiceReady = true;
230235
}
231236

232237
/// <summary>
233238
/// Initializes the room api.
234239
/// </summary>
235-
void InitRoomApi()
240+
private void InitRoomApi()
236241
{
237242
// If we have a room, we'll join the first room we see.
238243
// If we are the user with the lowest user ID, we will create the room.
@@ -262,7 +267,7 @@ void InitRoomApi()
262267
}
263268
}
264269

265-
bool LocalUserHasLowestUserId()
270+
private bool LocalUserHasLowestUserId()
266271
{
267272
long localUserId = CustomMessages.Instance.localUserID;
268273
foreach (long userid in SharingSessionTracker.Instance.UserIds)
@@ -279,7 +284,7 @@ bool LocalUserHasLowestUserId()
279284
/// <summary>
280285
/// Kicks off the process of creating the shared space.
281286
/// </summary>
282-
void StartAnchorProcess()
287+
private void StartAnchorProcess()
283288
{
284289
// First, are there any anchors in this room?
285290
int anchorCount = currentRoom.GetAnchorCount();
@@ -306,7 +311,7 @@ void StartAnchorProcess()
306311
/// <summary>
307312
/// Kicks off getting the datablob required to import the shared anchor.
308313
/// </summary>
309-
void MakeAnchorDataRequest()
314+
private void MakeAnchorDataRequest()
310315
{
311316
if (roomManager.DownloadAnchor(currentRoom, currentRoom.GetAnchorName(0)))
312317
{
@@ -319,7 +324,7 @@ void MakeAnchorDataRequest()
319324
}
320325
}
321326

322-
void Update()
327+
private void Update()
323328
{
324329
switch (currentState)
325330
{
@@ -353,7 +358,7 @@ void Update()
353358
/// <summary>
354359
/// Starts establishing a new anchor.
355360
/// </summary>
356-
void CreateAnchorLocally()
361+
private void CreateAnchorLocally()
357362
{
358363
WorldAnchor anchor = GetComponent<WorldAnchor>();
359364
if (anchor == null)
@@ -394,7 +399,7 @@ private void Anchor_OnTrackingChanged_InitialAnchor(WorldAnchor self, bool locat
394399
/// Attempts to attach to an anchor by anchorName in the local store..
395400
/// </summary>
396401
/// <returns>True if it attached, false if it could not attach</returns>
397-
bool AttachToCachedAnchor(string AnchorName)
402+
private bool AttachToCachedAnchor(string AnchorName)
398403
{
399404
Debug.Log("Looking for " + AnchorName);
400405
string[] ids = anchorStore.GetAllIds();
@@ -446,7 +451,7 @@ private void ImportExportAnchorManager_OnTrackingChanged_Attaching(WorldAnchor s
446451
/// </summary>
447452
/// <param name="status"></param>
448453
/// <param name="wat"></param>
449-
void ImportComplete(SerializationCompletionReason status, WorldAnchorTransferBatch wat)
454+
private void ImportComplete(SerializationCompletionReason status, WorldAnchorTransferBatch wat)
450455
{
451456
if (status == SerializationCompletionReason.Succeeded && wat.GetAllIds().Length > 0)
452457
{
@@ -469,7 +474,7 @@ void ImportComplete(SerializationCompletionReason status, WorldAnchorTransferBat
469474
/// <summary>
470475
/// Exports the currently created anchor.
471476
/// </summary>
472-
void Export()
477+
private void Export()
473478
{
474479
WorldAnchor anchor = GetComponent<WorldAnchor>();
475480

0 commit comments

Comments
 (0)