forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteMeshSource.cs
More file actions
202 lines (174 loc) · 7.54 KB
/
RemoteMeshSource.cs
File metadata and controls
202 lines (174 loc) · 7.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using UnityEngine;
#if !UNITY_EDITOR && UNITY_WSA
using System.Collections.Generic;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
using Windows.Networking;
using Windows.Foundation;
#endif
namespace HoloToolkit.Unity.SpatialMapping
{
/// <summary>
/// RemoteMeshSource will try to send meshes from the HoloLens to a remote system that is running the Unity editor.
/// </summary>
public class RemoteMeshSource : Singleton<RemoteMeshSource>
{
[Tooltip("The IPv4 Address of the machine running the Unity editor. Copy and paste this value from RemoteMeshTarget.")]
public string ServerIP;
[Tooltip("The connection port on the machine to use.")]
public int ConnectionPort = 11000;
#if !UNITY_EDITOR && UNITY_WSA
/// <summary>
/// Tracks the network connection to the remote machine we are sending meshes to.
/// </summary>
private StreamSocket networkConnection;
/// <summary>
/// Tracks if we are currently sending a mesh.
/// </summary>
private bool Sending = false;
/// <summary>
/// Temporary buffer for the data we are sending.
/// </summary>
private byte[] nextDataBufferToSend;
/// <summary>
/// A queue of data buffers to send.
/// </summary>
private Queue<byte[]> dataQueue = new Queue<byte[]>();
/// <summary>
/// If we cannot connect to the server, we will wait before trying to reconnect.
/// </summary>
private float deferTime = 0.0f;
/// <summary>
/// If we cannot connect to the server, this is how long we will wait before retrying.
/// </summary>
private float timeToDeferFailedConnections = 10.0f;
public void Update()
{
// Check to see if deferTime has been set.
// DeferUpdates will set the Sending flag to true for
// deferTime seconds.
if (deferTime > 0.0f)
{
DeferUpdates(deferTime);
deferTime = 0.0f;
}
// If we aren't sending a mesh, but we have a mesh to send, send it.
if (!Sending && dataQueue.Count > 0)
{
byte[] nextPacket = dataQueue.Dequeue();
SendDataOverNetwork(nextPacket);
}
}
/// <summary>
/// Handles waiting for some amount of time before trying to reconnect.
/// </summary>
/// <param name="timeout">Time in seconds to wait.</param>
void DeferUpdates(float timeout)
{
Sending = true;
Invoke("EnableUpdates", timeout);
}
/// <summary>
/// Stops waiting to reconnect.
/// </summary>
void EnableUpdates()
{
Sending = false;
}
/// <summary>
/// Queues up a data buffer to send over the network.
/// </summary>
/// <param name="dataBufferToSend">The data buffer to send.</param>
public void SendData(byte[] dataBufferToSend)
{
dataQueue.Enqueue(dataBufferToSend);
}
/// <summary>
/// Sends the data over the network.
/// </summary>
/// <param name="dataBufferToSend">The data buffer to send.</param>
private void SendDataOverNetwork(byte[] dataBufferToSend)
{
if (Sending)
{
// This shouldn't happen, but just in case.
Debug.Log("one at a time please");
return;
}
// Track that we are sending a data buffer.
Sending = true;
// Set the next buffer to send when the connection is made.
nextDataBufferToSend = dataBufferToSend;
// Setup a connection to the server.
HostName networkHost = new HostName(ServerIP.Trim());
networkConnection = new StreamSocket();
// Connections are asynchronous.
// !!! NOTE These do not arrive on the main Unity Thread. Most Unity operations will throw in the callback !!!
IAsyncAction outstandingAction = networkConnection.ConnectAsync(networkHost, ConnectionPort.ToString());
AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler(NetworkConnectedHandler);
outstandingAction.Completed = aach;
}
/// <summary>
/// Called when a connection attempt complete, successfully or not.
/// !!! NOTE These do not arrive on the main Unity Thread. Most Unity operations will throw in the callback !!!
/// </summary>
/// <param name="asyncInfo">Data about the async operation.</param>
/// <param name="status">The status of the operation.</param>
public void NetworkConnectedHandler(IAsyncAction asyncInfo, AsyncStatus status)
{
// Status completed is successful.
if (status == AsyncStatus.Completed)
{
DataWriter networkDataWriter;
// Since we are connected, we can send the data we set aside when establishing the connection.
using(networkDataWriter = new DataWriter(networkConnection.OutputStream))
{
// Write how much data we are sending.
networkDataWriter.WriteInt32(nextDataBufferToSend.Length);
// Then write the data.
networkDataWriter.WriteBytes(nextDataBufferToSend);
// Again, this is an async operation, so we'll set a callback.
DataWriterStoreOperation dswo = networkDataWriter.StoreAsync();
dswo.Completed = new AsyncOperationCompletedHandler<uint>(DataSentHandler);
}
}
else
{
Debug.Log("Failed to establish connection. Error Code: " + asyncInfo.ErrorCode);
// In the failure case we'll requeue the data and wait before trying again.
networkConnection.Dispose();
// Didn't send, so requeue the data.
dataQueue.Enqueue(nextDataBufferToSend);
// And set the defer time so the update loop can do the 'Unity things'
// on the main Unity thread.
deferTime = timeToDeferFailedConnections;
}
}
/// <summary>
/// Called when sending data has completed.
/// !!! NOTE These do not arrive on the main Unity Thread. Most Unity operations will throw in the callback !!!
/// </summary>
/// <param name="operation">The operation in flight.</param>
/// <param name="status">The status of the operation.</param>
public void DataSentHandler(IAsyncOperation<uint> operation, AsyncStatus status)
{
// If we failed, requeue the data and set the deferral time.
if (status == AsyncStatus.Error)
{
// didn't send, so requeue
dataQueue.Enqueue(nextDataBufferToSend);
deferTime = timeToDeferFailedConnections;
}
else
{
// If we succeeded, clear the sending flag so we can send another mesh
Sending = false;
}
// Always disconnect here since we will reconnect when sending the next mesh.
networkConnection.Dispose();
}
#endif
}
}