UdpServerEntryPoint.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using Emby.Server.Implementations.Udp;
  3. using MediaBrowser.Controller;
  4. using MediaBrowser.Controller.Plugins;
  5. using MediaBrowser.Model.Net;
  6. using MediaBrowser.Model.Serialization;
  7. using Microsoft.Extensions.Logging;
  8. namespace Emby.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. private readonly ISocketFactory _socketFactory;
  25. private readonly IServerApplicationHost _appHost;
  26. private readonly IJsonSerializer _json;
  27. public const int PortNumber = 7359;
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="UdpServerEntryPoint" /> class.
  30. /// </summary>
  31. public UdpServerEntryPoint(ILogger logger, IServerApplicationHost appHost, IJsonSerializer json, ISocketFactory socketFactory)
  32. {
  33. _logger = logger;
  34. _appHost = appHost;
  35. _json = json;
  36. _socketFactory = socketFactory;
  37. }
  38. /// <summary>
  39. /// Runs this instance.
  40. /// </summary>
  41. public void Run()
  42. {
  43. var udpServer = new UdpServer(_logger, _appHost, _json, _socketFactory);
  44. try
  45. {
  46. udpServer.Start(PortNumber);
  47. UdpServer = udpServer;
  48. }
  49. catch (Exception ex)
  50. {
  51. _logger.LogError(ex, "Failed to start UDP Server");
  52. }
  53. }
  54. /// <summary>
  55. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  56. /// </summary>
  57. public void Dispose()
  58. {
  59. Dispose(true);
  60. }
  61. /// <summary>
  62. /// Releases unmanaged and - optionally - managed resources.
  63. /// </summary>
  64. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  65. protected virtual void Dispose(bool dispose)
  66. {
  67. if (dispose)
  68. {
  69. if (UdpServer != null)
  70. {
  71. UdpServer.Dispose();
  72. }
  73. }
  74. }
  75. }
  76. }