UdpServerEntryPoint.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System.Threading;
  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 sealed 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. private CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
  32. private bool _disposed = false;
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="UdpServerEntryPoint" /> class.
  35. /// </summary>
  36. public UdpServerEntryPoint(
  37. ILogger<UdpServerEntryPoint> logger,
  38. IServerApplicationHost appHost)
  39. {
  40. _logger = logger;
  41. _appHost = appHost;
  42. }
  43. /// <inheritdoc />
  44. public async Task RunAsync()
  45. {
  46. _udpServer = new UdpServer(_logger, _appHost);
  47. _udpServer.Start(PortNumber, _cancellationTokenSource.Token);
  48. }
  49. /// <inheritdoc />
  50. public void Dispose()
  51. {
  52. if (_disposed)
  53. {
  54. return;
  55. }
  56. _cancellationTokenSource.Cancel();
  57. _udpServer.Dispose();
  58. _cancellationTokenSource = null;
  59. _udpServer = null;
  60. _disposed = true;
  61. }
  62. }
  63. }