UdpServerEntryPoint.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Net;
  5. using MediaBrowser.Controller.Plugins;
  6. using MediaBrowser.Model.Logging;
  7. using MediaBrowser.Model.Serialization;
  8. using MediaBrowser.Server.Implementations.Udp;
  9. using System.Net.Sockets;
  10. namespace MediaBrowser.Server.Implementations.EntryPoints
  11. {
  12. /// <summary>
  13. /// Class UdpServerEntryPoint
  14. /// </summary>
  15. public class UdpServerEntryPoint : IServerEntryPoint
  16. {
  17. /// <summary>
  18. /// Gets or sets the UDP server.
  19. /// </summary>
  20. /// <value>The UDP server.</value>
  21. private UdpServer UdpServer { get; set; }
  22. /// <summary>
  23. /// The _logger
  24. /// </summary>
  25. private readonly ILogger _logger;
  26. /// <summary>
  27. /// The _network manager
  28. /// </summary>
  29. private readonly INetworkManager _networkManager;
  30. /// <summary>
  31. /// The _server configuration manager
  32. /// </summary>
  33. private readonly IServerConfigurationManager _serverConfigurationManager;
  34. /// <summary>
  35. /// The _HTTP server
  36. /// </summary>
  37. private readonly IHttpServer _httpServer;
  38. private readonly IServerApplicationHost _appHost;
  39. private readonly IJsonSerializer _json;
  40. public const int PortNumber = 7359;
  41. /// <summary>
  42. /// Initializes a new instance of the <see cref="UdpServerEntryPoint"/> class.
  43. /// </summary>
  44. /// <param name="logger">The logger.</param>
  45. /// <param name="networkManager">The network manager.</param>
  46. /// <param name="serverConfigurationManager">The server configuration manager.</param>
  47. /// <param name="httpServer">The HTTP server.</param>
  48. public UdpServerEntryPoint(ILogger logger, INetworkManager networkManager, IServerConfigurationManager serverConfigurationManager, IHttpServer httpServer, IServerApplicationHost appHost, IJsonSerializer json)
  49. {
  50. _logger = logger;
  51. _networkManager = networkManager;
  52. _serverConfigurationManager = serverConfigurationManager;
  53. _httpServer = httpServer;
  54. _appHost = appHost;
  55. _json = json;
  56. }
  57. /// <summary>
  58. /// Runs this instance.
  59. /// </summary>
  60. public void Run()
  61. {
  62. var udpServer = new UdpServer(_logger, _networkManager, _serverConfigurationManager, _httpServer, _appHost, _json);
  63. try
  64. {
  65. udpServer.Start(PortNumber);
  66. UdpServer = udpServer;
  67. }
  68. catch (SocketException ex)
  69. {
  70. _logger.ErrorException("Failed to start UDP Server", ex);
  71. }
  72. }
  73. /// <summary>
  74. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  75. /// </summary>
  76. public void Dispose()
  77. {
  78. Dispose(true);
  79. }
  80. /// <summary>
  81. /// Releases unmanaged and - optionally - managed resources.
  82. /// </summary>
  83. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  84. protected virtual void Dispose(bool dispose)
  85. {
  86. if (dispose)
  87. {
  88. if (UdpServer != null)
  89. {
  90. UdpServer.Dispose();
  91. }
  92. }
  93. }
  94. }
  95. }