forked from microsoft/OpenXR-Unity-MixedReality-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppRemotingSample.cs
More file actions
298 lines (256 loc) · 11.7 KB
/
AppRemotingSample.cs
File metadata and controls
298 lines (256 loc) · 11.7 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Collections;
using UnityEngine.UI;
using UnityEngine;
using UnityEngine.XR;
using UnityEngine.XR.Management;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
namespace Microsoft.MixedReality.OpenXR.BasicSample
{
/// <summary>
/// Helper script for automatically connecting an OpenXR app to a specific remote device.
/// </summary>
public class AppRemotingSample : MonoBehaviour
{
[SerializeField, Tooltip("The UI to be displayed in a 2D app window, when the remote session hasn't yet been established.")]
private GameObject flatUI = null;
[SerializeField, Tooltip("The UI to be displayed in the 3D app session, when remoting and XR have both been established.")]
private GameObject immersiveUI = null;
[SerializeField, Tooltip("A text field to input the IP address of the remote device, such as a HoloLens.")]
private UnityEngine.UI.InputField textInput = null;
[SerializeField, Tooltip("A text field to display log information when an incorrect address is provided.")]
private UnityEngine.UI.Text outputText = null;
[SerializeField, Tooltip("The configuration information for the remote connection.")]
private Remoting.RemotingConfiguration remotingConfiguration = new Remoting.RemotingConfiguration { RemotePort = 8265, MaxBitrateKbps = 20000 };
[SerializeField, Tooltip("The configuration information for listening to remote connection.")]
private Remoting.RemotingListenConfiguration remotingListenConfiguration = new Remoting.RemotingListenConfiguration { ListenInterface = "0.0.0.0", HandshakeListenPort = 8265, TransportListenPort = 8266, MaxBitrateKbps = 20000 };
private static readonly List<XRDisplaySubsystem> XRDisplaySubsystems = new List<XRDisplaySubsystem>();
private Remoting.ConnectionState m_connectionState = Remoting.ConnectionState.Disconnected;
private Remoting.DisconnectReason m_disconnectReason = Remoting.DisconnectReason.None;
private AppRemotingMode m_appRemotingMode = AppRemotingMode.none;
private bool m_disconnectedOnListenMode = false;
private void Awake()
{
// This is intended for app remoting and shouldn't run in the editor
if (Application.isEditor)
{
DisableConnection2DUI();
return;
}
SubsystemManager.GetInstances(XRDisplaySubsystems);
foreach (XRDisplaySubsystem xrDisplaySubsystem in XRDisplaySubsystems)
{
// If a running XR display is found, assume an XR headset is attached.
// In this case, don't display the UI, since the app has already launched
// into an XR experience and it's too late to connect remoting.
if (xrDisplaySubsystem.running)
{
var connectionValid = Remoting.AppRemoting.TryGetConnectionState(out Remoting.ConnectionState connectionState, out Remoting.DisconnectReason disconnectReason);
if (!connectionValid || connectionState == Remoting.ConnectionState.Disconnected)
{
DisableConnection2DUI();
}
else
{
HideConnection2DUI();
}
return;
}
}
ShowConnection2DUI();
}
private void Update()
{
var ip = textInput.text;
var hostIp = GetLocalIPAddress();
var connectPort = remotingConfiguration.RemotePort;
var listenPort = remotingListenConfiguration.TransportListenPort;
var connectionStateValid = Remoting.AppRemoting.TryGetConnectionState(out Remoting.ConnectionState connectionState, out Remoting.DisconnectReason disconnectReason);
if (connectionStateValid)
{
if (m_connectionState != connectionState || disconnectReason != m_disconnectReason)
{
m_connectionState = connectionState;
m_disconnectReason = disconnectReason;
if(m_appRemotingMode == AppRemotingMode.connect)
{
Debug.Log($"Connection state changed : {ip}:{connectPort}, {connectionState}, {m_disconnectReason}");
}
else if (m_appRemotingMode == AppRemotingMode.listen)
{
Debug.Log($"Connection state changed : {hostIp}:{listenPort}, {connectionState}, {m_disconnectReason}");
}
switch (m_connectionState)
{
case Remoting.ConnectionState.Connected:
HideConnection2DUI();
break;
case Remoting.ConnectionState.Connecting:
ShowConnection2DUI();
break;
case Remoting.ConnectionState.Disconnected:
ShowConnection2DUI();
break;
}
}
}
else
{
m_connectionState = Remoting.ConnectionState.Disconnected;
}
string commonMessage = "Welcome to App Remoting! Provide Ip address & click Connect or click Listen";
string connectMessage = string.IsNullOrWhiteSpace(ip)
? $"No IP address was provided to {nameof(Remoting.AppRemoting)}."
: m_connectionState == Remoting.ConnectionState.Connected
? $"Connected to {ip}:{connectPort}."
: m_connectionState == Remoting.ConnectionState.Connecting
? $"Connecting to {ip}:{connectPort}..."
: $"Disconnected to {ip}:{connectPort}. Reason is {m_disconnectReason}";
string listenMessage = m_connectionState == Remoting.ConnectionState.Connected
? $"Connected on {hostIp}."
: m_connectionState == Remoting.ConnectionState.Disconnected && m_disconnectedOnListenMode
? $"Disconnected on {hostIp}:{listenPort}. Reason is {m_disconnectReason}"
: $"Listening to incoming connection on {hostIp}";
switch(m_appRemotingMode)
{
case AppRemotingMode.none:
outputText.text = commonMessage;
break;
case AppRemotingMode.connect:
outputText.text = connectMessage;
break;
case AppRemotingMode.listen:
outputText.text = listenMessage;
break;
}
}
/// <summary>
/// Connects to
/// 1. the IP address parameter, if one is passed in
/// 2. the serialized input field's text, if no IP address is passed in and the input field exists
/// 3. the remote host name in the remoting configuration, if no input field exists
/// </summary>
/// <param name="address">The (optional) address to connect to.</param>
public void ConnectToRemote(string address = null)
{
m_appRemotingMode = AppRemotingMode.connect;
if (!string.IsNullOrWhiteSpace(address))
{
remotingConfiguration.RemoteHostName = address;
}
else if (textInput != null)
{
remotingConfiguration.RemoteHostName = textInput.text;
}
if (string.IsNullOrWhiteSpace(remotingConfiguration.RemoteHostName))
{
Debug.LogWarning($"No IP address was provided to {nameof(Remoting.AppRemoting)}. Returning without connecting.");
return;
}
StartCoroutine(Remoting.AppRemoting.Connect(remotingConfiguration));
}
/// <summary>
/// Listens to the incoming connections as specified in the remotinglistenconfiguration
/// </summary>
public void ListenToRemote()
{
m_appRemotingMode = AppRemotingMode.listen;
StartCoroutine(Remoting.AppRemoting.Listen(remotingListenConfiguration));
}
/// <summary>
/// Disconnects from the remote session.
/// </summary>
public void DisconnectFromRemote()
{
Remoting.AppRemoting.Disconnect();
if (m_appRemotingMode == AppRemotingMode.listen)
{
m_disconnectedOnListenMode = true;
}
ShowConnection2DUI();
}
/*private string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
throw new System.Exception("No network adapters with an IPv4 address in the system!");
}*/
private string GetLocalIPAddress()
{
UnicastIPAddressInformation mostSuitableIp = null;
var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (var network in networkInterfaces)
{
if (network.OperationalStatus != OperationalStatus.Up)
continue;
var properties = network.GetIPProperties();
if (properties.GatewayAddresses.Count == 0)
continue;
foreach (var address in properties.UnicastAddresses)
{
if (address.Address.AddressFamily != AddressFamily.InterNetwork)
continue;
if (IPAddress.IsLoopback(address.Address))
continue;
if (!address.IsDnsEligible)
{
if (mostSuitableIp == null)
mostSuitableIp = address;
continue;
}
// The best IP is the IP got from DHCP server
if (address.PrefixOrigin != PrefixOrigin.Dhcp)
{
if (mostSuitableIp == null || !mostSuitableIp.IsDnsEligible)
mostSuitableIp = address;
continue;
}
return address.Address.ToString();
}
}
return mostSuitableIp != null
? mostSuitableIp.Address.ToString()
: "";
}
private void ShowConnection2DUI()
{
SetObjectActive(immersiveUI, false);
SetObjectActive(flatUI, true);
}
private void HideConnection2DUI()
{
SetObjectActive(flatUI, false);
SetObjectActive(immersiveUI, true);
}
private void DisableConnection2DUI()
{
SetObjectActive(gameObject, false);
SetObjectActive(immersiveUI, false);
SetObjectActive(flatUI, false);
}
private void SetObjectActive(GameObject @object, bool active)
{
if (@object != null && @object.activeSelf != active)
{
@object.SetActive(active);
}
}
}
public enum AppRemotingMode
{
none = 0,
connect = 1,
listen = 2
}
}