forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathManualIpConfiguration.cs
211 lines (175 loc) · 5.79 KB
/
ManualIpConfiguration.cs
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
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
namespace HoloToolkit.Sharing.Utilities
{
/// <summary>
/// Utility for connecting to Sharing Service by IP Address from inside application at runtime.
/// </summary>
public class ManualIpConfiguration : MonoBehaviour
{
/// <summary>
/// The maximum length of characters in a IPv4 address.
/// <remarks>000.000.000.000</remarks>
/// </summary>
private const int MaximumCharacterLength = 15;
public string IpAddress { get { return ipAddress.text; } }
/// <summary>
/// Hides the UI when connection is made.
/// </summary>
[Tooltip("Hides the UI when connection is made.")]
public bool HideWhenConnected;
/// <summary>
/// Hides the UI after this many seconds.
/// </summary>
[Range(0.1f, 5f)]
[Tooltip("Hides the UI after this many seconds.")]
public float HideAfterSeconds = 1f;
/// <summary>
/// How many seconds before server connection times out.
/// </summary>
[Range(1, 30)]
[Tooltip("How many seconds before server connection times out.")]
public int Timeout = 5;
[SerializeField]
private Text ipAddress;
[SerializeField]
private Image connectionIndicator;
private bool timerRunning;
private float timer;
private bool isTryingToConnect;
private bool firstRun;
private void Awake()
{
ipAddress.text = PlayerPrefs.GetString("SharingServerIp", "Not Connected");
firstRun = true;
}
private void Start()
{
if (SharingStage.Instance != null)
{
SharingStage.Instance.SharingManagerConnected += OnConnected;
SharingStage.Instance.SharingManagerDisconnected += OnDisconnected;
}
else
{
Debug.LogError("Unable to subscribe to Sharing Stage!");
}
}
private void OnEnable()
{
if (firstRun)
{
firstRun = false;
isTryingToConnect = true;
ConnectToSharingService();
}
else
{
isTryingToConnect = false;
}
}
private void Update()
{
if (timerRunning && timer - Time.time < 0)
{
if (isTryingToConnect)
{
isTryingToConnect = false;
OnDisconnected();
PlayerPrefs.SetString("SharingServerIp", "Not Connected");
}
}
}
private void OnDestroy()
{
if (SharingStage.Instance != null)
{
SharingStage.Instance.SharingManagerConnected -= OnConnected;
SharingStage.Instance.SharingManagerDisconnected -= OnDisconnected;
}
}
private void CheckConnection()
{
// SharingStage should be valid at this point, but we may not be connected.
if (SharingStage.Instance.IsConnected)
{
OnConnected();
}
else if (!timerRunning)
{
timer = Time.time + Timeout;
timerRunning = true;
}
}
private void OnConnected(object sender = null, EventArgs e = null)
{
timerRunning = false;
connectionIndicator.color = Color.green;
ipAddress.text = SharingStage.Instance.Connection.GetRemoteAddress().ToString();
PlayerPrefs.SetString("SharingServerIp", ipAddress.text);
if (HideWhenConnected && isTryingToConnect)
{
StartCoroutine(Hide());
}
isTryingToConnect = false;
}
private void OnDisconnected(object sender = null, EventArgs e = null)
{
timerRunning = false;
if (!isTryingToConnect)
{
connectionIndicator.color = Color.red;
ipAddress.text = "Not Connected";
}
}
public void InputString(string input)
{
timerRunning = false;
isTryingToConnect = false;
if (ipAddress.text.Contains("Connected") || ipAddress.text.Contains("127.0.0.1"))
{
ipAddress.text = string.Empty;
}
if (ipAddress.text.Length < MaximumCharacterLength)
{
ipAddress.text += input;
}
}
public void DeleteLastCharacter()
{
timerRunning = false;
isTryingToConnect = false;
if (!string.IsNullOrEmpty(ipAddress.text))
{
ipAddress.text = ipAddress.text.Substring(0, ipAddress.text.Length - 1);
}
}
public void ClearIpAddressString()
{
timerRunning = false;
isTryingToConnect = false;
ipAddress.text = "";
}
public void ConnectToSharingService()
{
timerRunning = false;
isTryingToConnect = false;
if (SharingStage.Instance == null || ipAddress.text.Contains("Connected"))
{
OnDisconnected();
return;
}
isTryingToConnect = true;
connectionIndicator.color = Color.yellow;
SharingStage.Instance.ConnectToServer(ipAddress.text, SharingStage.Instance.ServerPort);
CheckConnection();
}
private IEnumerator Hide()
{
yield return new WaitForSeconds(HideAfterSeconds);
gameObject.SetActive(false);
}
}
}