UdpServerEntryPoint.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Threading.Tasks;
  3. using Emby.Server.Implementations.Udp;
  4. using MediaBrowser.Controller;
  5. using MediaBrowser.Controller.Plugins;
  6. using MediaBrowser.Model.Net;
  7. using MediaBrowser.Model.Serialization;
  8. using Microsoft.Extensions.Logging;
  9. namespace Emby.Server.Implementations.EntryPoints
  10. {
  11. /// <summary>
  12. /// Class UdpServerEntryPoint.
  13. /// </summary>
  14. public class UdpServerEntryPoint : IServerEntryPoint
  15. {
  16. /// <summary>
  17. /// The port of the UDP server.
  18. /// </summary>
  19. public const int PortNumber = 7359;
  20. /// <summary>
  21. /// The logger.
  22. /// </summary>
  23. private readonly ILogger _logger;
  24. private readonly ISocketFactory _socketFactory;
  25. private readonly IServerApplicationHost _appHost;
  26. private readonly IJsonSerializer _json;
  27. /// <summary>
  28. /// The UDP server.
  29. /// </summary>
  30. private UdpServer _udpServer;
  31. /// <summary>
  32. /// Initializes a new instance of the <see cref="UdpServerEntryPoint" /> class.
  33. /// </summary>
  34. public UdpServerEntryPoint(
  35. ILogger logger,
  36. IServerApplicationHost appHost,
  37. IJsonSerializer json,
  38. ISocketFactory socketFactory)
  39. {
  40. _logger = logger;
  41. _appHost = appHost;
  42. _json = json;
  43. _socketFactory = socketFactory;
  44. }
  45. /// <inheritdoc />
  46. public Task RunAsync()
  47. {
  48. var udpServer = new UdpServer(_logger, _appHost, _json, _socketFactory);
  49. try
  50. {
  51. udpServer.Start(PortNumber);
  52. _udpServer = udpServer;
  53. }
  54. catch (Exception ex)
  55. {
  56. _logger.LogError(ex, "Failed to start UDP Server");
  57. }
  58. return Task.CompletedTask;
  59. }
  60. /// <inheritdoc />
  61. public void Dispose()
  62. {
  63. Dispose(true);
  64. GC.SuppressFinalize(this);
  65. }
  66. /// <summary>
  67. /// Releases unmanaged and - optionally - managed resources.
  68. /// </summary>
  69. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  70. protected virtual void Dispose(bool dispose)
  71. {
  72. if (dispose)
  73. {
  74. if (_udpServer != null)
  75. {
  76. _udpServer.Dispose();
  77. }
  78. }
  79. }
  80. }
  81. }