Basic Authenticator code organization and comments (#2369)

Co-authored-by: MrGadget1024 <chris@clevertech.net>
This commit is contained in:
MrGadget 2020-10-25 09:52:16 -04:00 committed by GitHub
parent 7f302dbe4c
commit 8f4ff423cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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
/// <summary>
/// Called on server from StartServer to initialize the Authenticator
/// <para>Server message handlers should be registered in this method.</para>
/// </summary>
public override void OnStartServer()
{
// register a handler for the authentication request we expect from client
NetworkServer.RegisterHandler<AuthRequestMessage>(OnAuthRequestMessage, false);
}
public override void OnStartClient()
{
// register a handler for the authentication response we expect from server
NetworkClient.RegisterHandler<AuthResponseMessage>(OnAuthResponseMessage, false);
}
/// <summary>
/// Called on server from OnServerAuthenticateInternal when a client needs to authenticate
/// </summary>
/// <param name="conn">Connection to client.</param>
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);
}
/// <summary>
/// Called on server when the client's AuthRequestMessage arrives
/// </summary>
/// <param name="conn">Connection to client.</param>
/// <param name="msg">The message payload</param>
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
/// <summary>
/// Called on client from StartClient to initialize the Authenticator
/// <para>Client message handlers should be registered in this method.</para>
/// </summary>
public override void OnStartClient()
{
// register a handler for the authentication response we expect from server
NetworkClient.RegisterHandler<AuthResponseMessage>(OnAuthResponseMessage, false);
}
/// <summary>
/// Called on client from OnClientAuthenticateInternal when a client needs to authenticate
/// </summary>
/// <param name="conn">Connection of the client.</param>
public override void OnClientAuthenticate(NetworkConnection conn)
{
AuthRequestMessage authRequestMessage = new AuthRequestMessage
{
authUsername = username,
authPassword = password
};
conn.Send(authRequestMessage);
}
/// <summary>
/// Called on client when the server's AuthResponseMessage arrives
/// </summary>
/// <param name="conn">Connection to client.</param>
/// <param name="msg">The message payload</param>
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
}
}