NetworkDiscovery.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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<TResponseType> : UnityEvent<TResponseType> {};
  9. [DisallowMultipleComponent]
  10. [AddComponentMenu("Network/Network Discovery")]
  11. public class NetworkDiscovery : NetworkDiscoveryBase<ServerRequest, ServerResponse>
  12. {
  13. #region Server
  14. /// <summary>
  15. /// Process the request from a client
  16. /// </summary>
  17. /// <remarks>
  18. /// Override if you wish to provide more information to the clients
  19. /// such as the name of the host player
  20. /// </remarks>
  21. /// <param name="request">Request coming from client</param>
  22. /// <param name="endpoint">Address of the client that sent the request</param>
  23. /// <returns>The message to be sent back to the client or null</returns>
  24. protected override ServerResponse ProcessRequest(ServerRequest request, IPEndPoint endpoint)
  25. {
  26. // In this case we don't do anything with the request
  27. // but other discovery implementations might want to use the data
  28. // in there, This way the client can ask for
  29. // specific game mode or something
  30. try
  31. {
  32. // this is an example reply message, return your own
  33. // to include whatever is relevant for your game
  34. return new ServerResponse
  35. {
  36. serverId = ServerId,
  37. uri = transport.ServerUri()
  38. };
  39. }
  40. catch (NotImplementedException)
  41. {
  42. Debug.LogError($"Transport {transport} does not support network discovery");
  43. throw;
  44. }
  45. }
  46. #endregion
  47. #region Client
  48. /// <summary>
  49. /// Create a message that will be broadcasted on the network to discover servers
  50. /// </summary>
  51. /// <remarks>
  52. /// Override if you wish to include additional data in the discovery message
  53. /// such as desired game mode, language, difficulty, etc... </remarks>
  54. /// <returns>An instance of ServerRequest with data to be broadcasted</returns>
  55. protected override ServerRequest GetRequest() => new ServerRequest();
  56. /// <summary>
  57. /// Process the answer from a server
  58. /// </summary>
  59. /// <remarks>
  60. /// A client receives a reply from a server, this method processes the
  61. /// reply and raises an event
  62. /// </remarks>
  63. /// <param name="response">Response that came from the server</param>
  64. /// <param name="endpoint">Address of the server that replied</param>
  65. protected override void ProcessResponse(ServerResponse response, IPEndPoint endpoint)
  66. {
  67. // we received a message from the remote endpoint
  68. response.EndPoint = endpoint;
  69. // although we got a supposedly valid url, we may not be able to resolve
  70. // the provided host
  71. // However we know the real ip address of the server because we just
  72. // received a packet from it, so use that as host.
  73. UriBuilder realUri = new UriBuilder(response.uri)
  74. {
  75. Host = response.EndPoint.Address.ToString()
  76. };
  77. response.uri = realUri.Uri;
  78. OnServerFound.Invoke(response);
  79. }
  80. #endregion
  81. }
  82. }