-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathConnectionApprovalComponent.cs
More file actions
250 lines (218 loc) · 8.47 KB
/
ConnectionApprovalComponent.cs
File metadata and controls
250 lines (218 loc) · 8.47 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
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using Unity.Netcode;
namespace TestProject.ManualTests
{
/// <summary>
/// This component demonstrates how to use the Netcode for GameObjects connection approval feature
/// </summary>
public class ConnectionApprovalComponent : NetworkBehaviour
{
[SerializeField]
private string m_ApprovalToken;
[SerializeField]
private uint m_GlobalObjectIdHashOverride;
[SerializeField]
private Text m_ConnectionMessageToDisplay;
[SerializeField]
private Toggle m_SimulateFailure;
[SerializeField]
private Toggle m_PlayerPrefabOverride;
[SerializeField]
private Button m_ClientDisconnectButton;
[SerializeField]
private ConnectionModeScript m_ConnectionModeButtons;
private class MessageEntry
{
public string Message;
public float TimeOut;
}
private List<MessageEntry> m_Messages = new List<MessageEntry>();
private void Start()
{
if (m_PlayerPrefabOverride)
{
m_PlayerPrefabOverride.gameObject.SetActive(false);
}
if (m_SimulateFailure)
{
m_SimulateFailure.gameObject.SetActive(false);
}
if (m_ConnectionMessageToDisplay)
{
m_ConnectionMessageToDisplay.gameObject.SetActive(false);
}
if (m_ClientDisconnectButton)
{
m_ClientDisconnectButton.gameObject.SetActive(false);
}
if (NetworkManager != null && NetworkManager.NetworkConfig.ConnectionApproval)
{
NetworkManager.ConnectionApprovalCallback = ConnectionApprovalCallback;
if (m_ApprovalToken != string.Empty)
{
NetworkManager.NetworkConfig.ConnectionData = Encoding.ASCII.GetBytes(m_ApprovalToken);
}
else
{
Debug.LogError($"You need to set the {nameof(m_ApprovalToken)} to a value first!");
}
}
NetworkManager.OnClientDisconnectCallback += NetworkManager_OnClientDisconnectCallback;
NetworkManager.OnClientConnectedCallback += NetworkManager_OnClientConnectedCallback;
}
/// <summary>
/// When a client connects we display a message and if we are not the server
/// we display a disconnect button for ease of testing.
/// </summary>
private void NetworkManager_OnClientConnectedCallback(ulong clientId)
{
if (m_ClientDisconnectButton)
{
m_ClientDisconnectButton.gameObject.SetActive(!IsServer);
}
AddNewMessage($"Client {clientId} was connected.");
}
/// <summary>
/// When a client is disconnected we display a message and if we
/// are not listening and not the server we reset the UI Connection
/// mode buttons
/// </summary>
private void NetworkManager_OnClientDisconnectCallback(ulong clientId)
{
AddNewMessage($"Client {clientId} was disconnected!");
if (!NetworkManager.IsListening && !NetworkManager.IsServer)
{
m_ConnectionModeButtons.Reset();
}
if (m_ClientDisconnectButton)
{
m_ClientDisconnectButton.gameObject.SetActive(false);
}
}
/// <summary>
/// Just display certain check boxes only when we are in a network session
/// </summary>
public override void OnNetworkSpawn()
{
if (m_SimulateFailure)
{
m_SimulateFailure.gameObject.SetActive(IsServer);
}
if (m_PlayerPrefabOverride)
{
m_PlayerPrefabOverride.gameObject.SetActive(IsServer);
}
}
/// <summary>
/// Used for the client when the disconnect button is pressed
/// </summary>
public void OnDisconnectClient()
{
if (NetworkManager != null && NetworkManager.IsListening && !NetworkManager.IsServer)
{
NetworkManager.Shutdown();
m_ConnectionModeButtons.Reset();
if (m_ClientDisconnectButton)
{
m_ClientDisconnectButton.gameObject.SetActive(false);
}
}
}
/// <summary>
/// Invoked only on the server, this will handle the various connection approval combinations
/// </summary>
/// <param name="request">The connection approval request</param>
/// <returns>ConnectionApprovalResult with the approval decision, with parameters</returns>
private void ConnectionApprovalCallback(NetworkManager.ConnectionApprovalRequest request, NetworkManager.ConnectionApprovalResponse response)
{
string approvalToken = Encoding.ASCII.GetString(request.Payload);
var isTokenValid = approvalToken == m_ApprovalToken;
if (m_SimulateFailure && m_SimulateFailure.isOn && IsServer && request.ClientNetworkId != NetworkManager.LocalClientId)
{
isTokenValid = false;
}
if (m_GlobalObjectIdHashOverride != 0 && m_PlayerPrefabOverride && m_PlayerPrefabOverride.isOn)
{
response.Approved = isTokenValid;
response.PlayerPrefabHash = m_GlobalObjectIdHashOverride;
response.Position = null;
response.Rotation = null;
response.CreatePlayerObject = true;
}
else
{
response.Approved = isTokenValid;
response.PlayerPrefabHash = null;
response.Position = null;
response.Rotation = null;
response.CreatePlayerObject = true;
}
if (m_ConnectionMessageToDisplay)
{
if (isTokenValid)
{
AddNewMessage($"Client id {request.ClientNetworkId} is authorized!");
}
else
{
AddNewMessage($"Client id {request.ClientNetworkId} failed authorization!");
}
m_ConnectionMessageToDisplay.gameObject.SetActive(true);
}
}
/// <summary>
/// Adds a new message to be displayed and if our display coroutine is not running start it.
/// </summary>
/// <param name="msg">message to add to the list of messages to be displayed</param>
private void AddNewMessage(string msg)
{
m_Messages.Add(new MessageEntry() { Message = msg, TimeOut = Time.realtimeSinceStartup + 8.0f });
if (!m_ConnectionMessageToDisplay.gameObject.activeInHierarchy)
{
StartCoroutine(DisplayMessatesUntilEmpty());
if (m_ConnectionMessageToDisplay)
{
m_ConnectionMessageToDisplay.gameObject.SetActive(true);
}
}
}
/// <summary>
/// Coroutine that displays messages until there are no more messages to be displayed.
/// </summary>
/// <returns></returns>
private IEnumerator DisplayMessatesUntilEmpty()
{
var messagesToRemove = new List<MessageEntry>();
while (m_Messages.Count > 0)
{
m_ConnectionMessageToDisplay.text = string.Empty;
foreach (var message in m_Messages)
{
if (message.TimeOut > Time.realtimeSinceStartup)
{
m_ConnectionMessageToDisplay.text += message.Message + "\n";
}
else
{
messagesToRemove.Add(message);
}
}
yield return new WaitForSeconds(0.5f);
foreach (var message in messagesToRemove)
{
m_Messages.Remove(message);
}
messagesToRemove.Clear();
}
if (m_ConnectionMessageToDisplay)
{
m_ConnectionMessageToDisplay.gameObject.SetActive(false);
}
yield return null;
}
}
}