UdpServerEntryPoint.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using Emby.Server.Implementations.Udp;
  5. using MediaBrowser.Controller;
  6. using MediaBrowser.Controller.Plugins;
  7. using MediaBrowser.Model.Net;
  8. using MediaBrowser.Model.Serialization;
  9. using Microsoft.Extensions.Logging;
  10. namespace Emby.Server.Implementations.EntryPoints
  11. {
  12. /// <summary>
  13. /// Class UdpServerEntryPoint.
  14. /// </summary>
  15. public sealed class UdpServerEntryPoint : IServerEntryPoint
  16. {
  17. /// <summary>
  18. /// The port of the UDP server.
  19. /// </summary>
  20. public const int PortNumber = 7359;
  21. /// <summary>
  22. /// The logger.
  23. /// </summary>
  24. private readonly ILogger _logger;
  25. private readonly ISocketFactory _socketFactory;
  26. private readonly IServerApplicationHost _appHost;
  27. private readonly IJsonSerializer _json;
  28. /// <summary>
  29. /// The UDP server.
  30. /// </summary>
  31. private UdpServer _udpServer;
  32. private CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
  33. private bool _disposed = false;
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="UdpServerEntryPoint" /> class.
  36. /// </summary>
  37. public UdpServerEntryPoint(
  38. ILogger<UdpServerEntryPoint> logger,
  39. IServerApplicationHost appHost)
  40. {
  41. _logger = logger;
  42. _appHost = appHost;
  43. }
  44. /// <inheritdoc />
  45. public async Task RunAsync()
  46. {
  47. _udpServer = new UdpServer(_logger, _appHost);
  48. _udpServer.Start(PortNumber, _cancellationTokenSource.Token);
  49. }
  50. /// <inheritdoc />
  51. public void Dispose()
  52. {
  53. if (_disposed)
  54. {
  55. return;
  56. }
  57. _cancellationTokenSource.Cancel();
  58. _udpServer.Dispose();
  59. _cancellationTokenSource = null;
  60. _udpServer = null;
  61. _disposed = true;
  62. }
  63. }
  64. }