UdpServerEntryPoint.cs 2.9 KB

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