fix: Added HashSet for disconnecting connections (#2981)

* fix: Added HashSet for disconnecting connections

* fixed code smells
This commit is contained in:
MrGadget 2021-10-31 10:12:18 -04:00 committed by GitHub
parent f83ed02a83
commit 803e804d87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Mirror.Authenticators
@ -7,12 +8,16 @@ namespace Mirror.Authenticators
[AddComponentMenu("Network/Authenticators/BasicAuthenticator")]
public class BasicAuthenticator : NetworkAuthenticator
{
[Header("Custom Properties")]
[Header("Server Credentials")]
public string serverUsername;
public string serverPassword;
// set these in the inspector
[Header("Client Credentials")]
public string username;
public string password;
readonly HashSet<NetworkConnection> connectionsPendingDisconnect = new HashSet<NetworkConnection>();
#region Messages
public struct AuthRequestMessage : NetworkMessage
@ -71,8 +76,10 @@ public void OnAuthRequestMessage(NetworkConnection conn, AuthRequestMessage msg)
{
// Debug.LogFormat(LogType.Log, "Authentication Request: {0} {1}", msg.authUsername, msg.authPassword);
if (connectionsPendingDisconnect.Contains(conn)) return;
// check the credentials by calling your web server, database table, playfab api, or any method appropriate.
if (msg.authUsername == username && msg.authPassword == password)
if (msg.authUsername == serverUsername && msg.authPassword == serverPassword)
{
// create and send msg to client so it knows to proceed
AuthResponseMessage authResponseMessage = new AuthResponseMessage
@ -88,6 +95,8 @@ public void OnAuthRequestMessage(NetworkConnection conn, AuthRequestMessage msg)
}
else
{
connectionsPendingDisconnect.Add(conn);
// create and send msg to client so it knows to disconnect
AuthResponseMessage authResponseMessage = new AuthResponseMessage
{
@ -111,6 +120,11 @@ IEnumerator DelayedDisconnect(NetworkConnection conn, float waitTime)
// Reject the unsuccessful authentication
ServerReject(conn);
yield return null;
// remove conn from pending connections
connectionsPendingDisconnect.Remove(conn);
}
#endregion