From 8f4ff423cbc3e03da947a85638d36ccf517229d8 Mon Sep 17 00:00:00 2001 From: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Date: Sun, 25 Oct 2020 09:52:16 -0400 Subject: [PATCH] Basic Authenticator code organization and comments (#2369) Co-authored-by: MrGadget1024 --- .../Authenticators/BasicAuthenticator.cs | 76 ++++++++++++++----- 1 file changed, 57 insertions(+), 19 deletions(-) diff --git a/Assets/Mirror/Authenticators/BasicAuthenticator.cs b/Assets/Mirror/Authenticators/BasicAuthenticator.cs index f53539b43..41c4528bc 100644 --- a/Assets/Mirror/Authenticators/BasicAuthenticator.cs +++ b/Assets/Mirror/Authenticators/BasicAuthenticator.cs @@ -14,7 +14,9 @@ public class BasicAuthenticator : NetworkAuthenticator public string username; public string password; - public struct AuthRequestMessage : NetworkMessage + #region Messages + + public struct AuthRequestMessage : NetworkMessage { // use whatever credentials make sense for your game // for example, you might want to pass the accessToken if using oauth @@ -28,34 +30,34 @@ public struct AuthResponseMessage : NetworkMessage public string message; } + #endregion + + #region Server + + /// + /// Called on server from StartServer to initialize the Authenticator + /// Server message handlers should be registered in this method. + /// public override void OnStartServer() { // register a handler for the authentication request we expect from client NetworkServer.RegisterHandler(OnAuthRequestMessage, false); } - public override void OnStartClient() - { - // register a handler for the authentication response we expect from server - NetworkClient.RegisterHandler(OnAuthResponseMessage, false); - } - + /// + /// Called on server from OnServerAuthenticateInternal when a client needs to authenticate + /// + /// Connection to client. public override void OnServerAuthenticate(NetworkConnection conn) { // do nothing...wait for AuthRequestMessage from client } - public override void OnClientAuthenticate(NetworkConnection conn) - { - AuthRequestMessage authRequestMessage = new AuthRequestMessage - { - authUsername = username, - authPassword = password - }; - - conn.Send(authRequestMessage); - } - + /// + /// Called on server when the client's AuthRequestMessage arrives + /// + /// Connection to client. + /// The message payload public void OnAuthRequestMessage(NetworkConnection conn, AuthRequestMessage msg) { if (logger.LogEnabled()) logger.LogFormat(LogType.Log, "Authentication Request: {0} {1}", msg.authUsername, msg.authPassword); @@ -94,12 +96,46 @@ public void OnAuthRequestMessage(NetworkConnection conn, AuthRequestMessage msg) } } - public IEnumerator DelayedDisconnect(NetworkConnection conn, float waitTime) + IEnumerator DelayedDisconnect(NetworkConnection conn, float waitTime) { yield return new WaitForSeconds(waitTime); conn.Disconnect(); } + #endregion + + #region Client + + /// + /// Called on client from StartClient to initialize the Authenticator + /// Client message handlers should be registered in this method. + /// + public override void OnStartClient() + { + // register a handler for the authentication response we expect from server + NetworkClient.RegisterHandler(OnAuthResponseMessage, false); + } + + /// + /// Called on client from OnClientAuthenticateInternal when a client needs to authenticate + /// + /// Connection of the client. + public override void OnClientAuthenticate(NetworkConnection conn) + { + AuthRequestMessage authRequestMessage = new AuthRequestMessage + { + authUsername = username, + authPassword = password + }; + + conn.Send(authRequestMessage); + } + + /// + /// Called on client when the server's AuthResponseMessage arrives + /// + /// Connection to client. + /// The message payload public void OnAuthResponseMessage(NetworkConnection conn, AuthResponseMessage msg) { if (msg.code == 100) @@ -120,5 +156,7 @@ public void OnAuthResponseMessage(NetworkConnection conn, AuthResponseMessage ms conn.Disconnect(); } } + + #endregion } }