NetworkAuthenticator.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Events;
  4. namespace Mirror
  5. {
  6. [Serializable] public class UnityEventNetworkConnection : UnityEvent<NetworkConnection> {}
  7. /// <summary>Base class for implementing component-based authentication during the Connect phase</summary>
  8. [HelpURL("https://mirror-networking.gitbook.io/docs/components/network-authenticators")]
  9. public abstract class NetworkAuthenticator : MonoBehaviour
  10. {
  11. /// <summary>Notify subscribers on the server when a client is authenticated</summary>
  12. [Header("Event Listeners (optional)")]
  13. [Tooltip("Mirror has an internal subscriber to this event. You can add your own here.")]
  14. public UnityEventNetworkConnection OnServerAuthenticated = new UnityEventNetworkConnection();
  15. /// <summary>Notify subscribers on the client when the client is authenticated</summary>
  16. [Tooltip("Mirror has an internal subscriber to this event. You can add your own here.")]
  17. public UnityEventNetworkConnection OnClientAuthenticated = new UnityEventNetworkConnection();
  18. /// <summary>Called when server starts, used to register message handlers if needed.</summary>
  19. public virtual void OnStartServer() {}
  20. /// <summary>Called when server stops, used to unregister message handlers if needed.</summary>
  21. public virtual void OnStopServer() {}
  22. /// <summary>Called on server from OnServerAuthenticateInternal when a client needs to authenticate</summary>
  23. public abstract void OnServerAuthenticate(NetworkConnection conn);
  24. protected void ServerAccept(NetworkConnection conn)
  25. {
  26. OnServerAuthenticated.Invoke(conn);
  27. }
  28. protected void ServerReject(NetworkConnection conn)
  29. {
  30. conn.Disconnect();
  31. }
  32. /// <summary>Called when client starts, used to register message handlers if needed.</summary>
  33. public virtual void OnStartClient() {}
  34. /// <summary>Called when client stops, used to unregister message handlers if needed.</summary>
  35. public virtual void OnStopClient() {}
  36. /// <summary>Called on client from OnClientAuthenticateInternal when a client needs to authenticate</summary>
  37. public abstract void OnClientAuthenticate();
  38. protected void ClientAccept()
  39. {
  40. OnClientAuthenticated.Invoke(NetworkClient.connection);
  41. }
  42. protected void ClientReject()
  43. {
  44. // Set this on the client for local reference
  45. NetworkClient.connection.isAuthenticated = false;
  46. // disconnect the client
  47. NetworkClient.connection.Disconnect();
  48. }
  49. void OnValidate()
  50. {
  51. #if UNITY_EDITOR
  52. // automatically assign authenticator field if we add this to NetworkManager
  53. NetworkManager manager = GetComponent<NetworkManager>();
  54. if (manager != null && manager.authenticator == null)
  55. {
  56. manager.authenticator = this;
  57. UnityEditor.Undo.RecordObject(gameObject, "Assigned NetworkManager authenticator");
  58. }
  59. #endif
  60. }
  61. }
  62. }