-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
TimeoutAuthenticator.cs
85 lines (69 loc) · 2.74 KB
/
TimeoutAuthenticator.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
using System;
using System.Collections;
using System.Collections.Generic;
using Mirage.Logging;
using UnityEngine;
namespace Mirage.Authenticators
{
/// <summary>
/// An authenticator that disconnects connections if they don't
/// authenticate within a specified time limit.
/// </summary>
[AddComponentMenu("Network/Authenticators/TimeoutAuthenticator")]
public class TimeoutAuthenticator : NetworkAuthenticator
{
static readonly ILogger logger = LogFactory.GetLogger(typeof(TimeoutAuthenticator));
public NetworkAuthenticator Authenticator;
[Range(0, 600), Tooltip("Timeout to auto-disconnect in seconds. Set to 0 for no timeout.")]
public float Timeout = 60;
public void Awake()
{
Authenticator.OnClientAuthenticated += HandleClientAuthenticated;
Authenticator.OnServerAuthenticated += HandleServerAuthenticated;
}
private readonly HashSet<INetworkPlayer> pendingAuthentication = new HashSet<INetworkPlayer>();
private void HandleServerAuthenticated(INetworkPlayer player)
{
pendingAuthentication.Remove(player);
ServerAccept(player);
}
private void HandleClientAuthenticated(INetworkPlayer player)
{
pendingAuthentication.Remove(player);
ClientAccept(player);
}
public override void ServerAuthenticate(INetworkPlayer player)
{
pendingAuthentication.Add(player);
Authenticator.ServerAuthenticate(player);
if (Timeout > 0)
StartCoroutine(BeginAuthentication(player, ServerReject));
}
public override void ClientAuthenticate(INetworkPlayer player)
{
pendingAuthentication.Add(player);
Authenticator.ClientAuthenticate(player);
if (Timeout > 0)
StartCoroutine(BeginAuthentication(player, ClientReject));
}
public override void ServerSetup(NetworkServer server)
{
Authenticator.ServerSetup(server);
}
public override void ClientSetup(NetworkClient client)
{
Authenticator.ClientSetup(client);
}
IEnumerator BeginAuthentication(INetworkPlayer player, Action<INetworkPlayer> reject)
{
if (logger.LogEnabled()) logger.Log($"Authentication countdown started {player} {Timeout}");
yield return new WaitForSecondsRealtime(Timeout);
if (pendingAuthentication.Contains(player))
{
if (logger.LogEnabled()) logger.Log($"Authentication Timeout {player}");
pendingAuthentication.Remove(player);
reject.Invoke(player);
}
}
}
}