UdpServerEntryPoint.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Plugins;
  4. using MediaBrowser.Model.Logging;
  5. using MediaBrowser.Model.Serialization;
  6. using Emby.Server.Implementations.Udp;
  7. using MediaBrowser.Model.Net;
  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.ErrorException("Failed to start UDP Server", ex);
  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. GC.SuppressFinalize(this);
  61. }
  62. /// <summary>
  63. /// Releases unmanaged and - optionally - managed resources.
  64. /// </summary>
  65. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  66. protected virtual void Dispose(bool dispose)
  67. {
  68. if (dispose)
  69. {
  70. if (UdpServer != null)
  71. {
  72. UdpServer.Dispose();
  73. }
  74. }
  75. }
  76. }
  77. }