NetworkDiscovery.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Net;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. namespace Mirror.Discovery
  6. {
  7. [Serializable]
  8. public class ServerFoundUnityEvent : UnityEvent<ServerResponse> {};
  9. [DisallowMultipleComponent]
  10. [AddComponentMenu("Network/NetworkDiscovery")]
  11. public class NetworkDiscovery : NetworkDiscoveryBase<ServerRequest, ServerResponse>
  12. {
  13. #region Server
  14. public long ServerId { get; private set; }
  15. [Tooltip("Transport to be advertised during discovery")]
  16. public Transport transport;
  17. [Tooltip("Invoked when a server is found")]
  18. public ServerFoundUnityEvent OnServerFound;
  19. public override void Start()
  20. {
  21. ServerId = RandomLong();
  22. // active transport gets initialized in awake
  23. // so make sure we set it here in Start() (after awakes)
  24. // Or just let the user assign it in the inspector
  25. if (transport == null)
  26. transport = Transport.activeTransport;
  27. base.Start();
  28. }
  29. /// <summary>
  30. /// Process the request from a client
  31. /// </summary>
  32. /// <remarks>
  33. /// Override if you wish to provide more information to the clients
  34. /// such as the name of the host player
  35. /// </remarks>
  36. /// <param name="request">Request coming from client</param>
  37. /// <param name="endpoint">Address of the client that sent the request</param>
  38. /// <returns>The message to be sent back to the client or null</returns>
  39. protected override ServerResponse ProcessRequest(ServerRequest request, IPEndPoint endpoint)
  40. {
  41. // In this case we don't do anything with the request
  42. // but other discovery implementations might want to use the data
  43. // in there, This way the client can ask for
  44. // specific game mode or something
  45. try
  46. {
  47. // this is an example reply message, return your own
  48. // to include whatever is relevant for your game
  49. return new ServerResponse
  50. {
  51. serverId = ServerId,
  52. uri = transport.ServerUri()
  53. };
  54. }
  55. catch (NotImplementedException)
  56. {
  57. Debug.LogError($"Transport {transport} does not support network discovery");
  58. throw;
  59. }
  60. }
  61. #endregion
  62. #region Client
  63. /// <summary>
  64. /// Create a message that will be broadcasted on the network to discover servers
  65. /// </summary>
  66. /// <remarks>
  67. /// Override if you wish to include additional data in the discovery message
  68. /// such as desired game mode, language, difficulty, etc... </remarks>
  69. /// <returns>An instance of ServerRequest with data to be broadcasted</returns>
  70. protected override ServerRequest GetRequest() => new ServerRequest();
  71. /// <summary>
  72. /// Process the answer from a server
  73. /// </summary>
  74. /// <remarks>
  75. /// A client receives a reply from a server, this method processes the
  76. /// reply and raises an event
  77. /// </remarks>
  78. /// <param name="response">Response that came from the server</param>
  79. /// <param name="endpoint">Address of the server that replied</param>
  80. protected override void ProcessResponse(ServerResponse response, IPEndPoint endpoint)
  81. {
  82. // we received a message from the remote endpoint
  83. response.EndPoint = endpoint;
  84. // although we got a supposedly valid url, we may not be able to resolve
  85. // the provided host
  86. // However we know the real ip address of the server because we just
  87. // received a packet from it, so use that as host.
  88. UriBuilder realUri = new UriBuilder(response.uri)
  89. {
  90. Host = response.EndPoint.Address.ToString()
  91. };
  92. response.uri = realUri.Uri;
  93. OnServerFound.Invoke(response);
  94. }
  95. #endregion
  96. }
  97. }