AlchemyServer.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using Alchemy;
  2. using Alchemy.Classes;
  3. using MediaBrowser.Common.Net;
  4. using MediaBrowser.Model.Logging;
  5. using System;
  6. using System.Net;
  7. #if __MonoCS__
  8. using Mono.Unix.Native;
  9. #endif
  10. namespace MediaBrowser.Server.Implementations.WebSocket
  11. {
  12. /// <summary>
  13. /// Class AlchemyServer
  14. /// </summary>
  15. public class AlchemyServer : IWebSocketServer
  16. {
  17. /// <summary>
  18. /// Occurs when [web socket connected].
  19. /// </summary>
  20. public event EventHandler<WebSocketConnectEventArgs> WebSocketConnected;
  21. /// <summary>
  22. /// Gets or sets the web socket server.
  23. /// </summary>
  24. /// <value>The web socket server.</value>
  25. private WebSocketServer WebSocketServer { get; set; }
  26. /// <summary>
  27. /// The _logger
  28. /// </summary>
  29. private readonly ILogger _logger;
  30. private bool _hasStopped;
  31. /// <summary>
  32. /// Initializes a new instance of the <see cref="AlchemyServer" /> class.
  33. /// </summary>
  34. /// <param name="logger">The logger.</param>
  35. /// <exception cref="System.ArgumentNullException">logger</exception>
  36. public AlchemyServer(ILogger logger)
  37. {
  38. if (logger == null)
  39. {
  40. throw new ArgumentNullException("logger");
  41. }
  42. _logger = logger;
  43. }
  44. /// <summary>
  45. /// Gets the port.
  46. /// </summary>
  47. /// <value>The port.</value>
  48. public int Port { get; private set; }
  49. /// <summary>
  50. /// Starts the specified port number.
  51. /// </summary>
  52. /// <param name="portNumber">The port number.</param>
  53. public void Start(int portNumber)
  54. {
  55. _logger.Info("Starting Alchemy web socket server on port {0}", portNumber);
  56. try
  57. {
  58. WebSocketServer = new WebSocketServer(portNumber, IPAddress.Any)
  59. {
  60. OnConnected = OnAlchemyWebSocketClientConnected,
  61. TimeOut = TimeSpan.FromHours(24)
  62. };
  63. #if __MonoCS__
  64. //Linux: port below 1024 require root or cap_net_bind_service
  65. if (Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX)
  66. {
  67. if (Syscall.getuid() == 0)
  68. {
  69. WebSocketServer.FlashAccessPolicyEnabled = true;
  70. }
  71. else
  72. {
  73. WebSocketServer.FlashAccessPolicyEnabled = false;
  74. }
  75. }
  76. #endif
  77. WebSocketServer.Start();
  78. }
  79. catch (Exception ex)
  80. {
  81. _logger.ErrorException("The web socket server is unable to start on port {0} due to a Socket error. This can occasionally happen when the operating system takes longer than usual to release the IP bindings from the previous session. This can take up to five minutes. Please try waiting or rebooting the system.", ex, portNumber);
  82. throw;
  83. }
  84. Port = portNumber;
  85. _logger.Info("Alchemy Web Socket Server started");
  86. }
  87. /// <summary>
  88. /// Called when [alchemy web socket client connected].
  89. /// </summary>
  90. /// <param name="context">The context.</param>
  91. private void OnAlchemyWebSocketClientConnected(UserContext context)
  92. {
  93. if (_hasStopped)
  94. {
  95. return;
  96. }
  97. if (WebSocketConnected != null)
  98. {
  99. var socket = new AlchemyWebSocket(context, _logger);
  100. WebSocketConnected(this, new WebSocketConnectEventArgs
  101. {
  102. WebSocket = socket,
  103. Endpoint = context.ClientAddress.ToString()
  104. });
  105. }
  106. }
  107. /// <summary>
  108. /// Stops this instance.
  109. /// </summary>
  110. public void Stop()
  111. {
  112. if (WebSocketServer != null)
  113. {
  114. WebSocketServer.Stop();
  115. }
  116. }
  117. /// <summary>
  118. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  119. /// </summary>
  120. public void Dispose()
  121. {
  122. Dispose(true);
  123. GC.SuppressFinalize(this);
  124. }
  125. private readonly object _syncLock = new object();
  126. /// <summary>
  127. /// Releases unmanaged and - optionally - managed resources.
  128. /// </summary>
  129. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  130. protected virtual void Dispose(bool dispose)
  131. {
  132. _hasStopped = true;
  133. lock (_syncLock)
  134. {
  135. if (WebSocketServer != null)
  136. {
  137. // Calling dispose will also call stop
  138. _logger.Debug("Disposing alchemy server");
  139. WebSocketServer.Stop();
  140. WebSocketServer = null;
  141. }
  142. }
  143. }
  144. }
  145. }