2013-02-21 02:33:05 +01:00
|
|
|
|
using Alchemy;
|
|
|
|
|
using Alchemy.Classes;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
|
using MediaBrowser.Common.Serialization;
|
|
|
|
|
using MediaBrowser.Model.Configuration;
|
2013-02-21 22:39:53 +01:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Net.WebSockets;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Common.Kernel
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Manages the Http Server, Udp Server and WebSocket connections
|
|
|
|
|
/// </summary>
|
2013-02-23 18:54:51 +01:00
|
|
|
|
public class TcpManager : IDisposable
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This is the udp server used for server discovery by clients
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The UDP server.</value>
|
|
|
|
|
private UdpServer UdpServer { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the UDP listener.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The UDP listener.</value>
|
|
|
|
|
private IDisposable UdpListener { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Both the Ui and server will have a built-in HttpServer.
|
|
|
|
|
/// People will inevitably want remote control apps so it's needed in the Ui too.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The HTTP server.</value>
|
|
|
|
|
public HttpServer HttpServer { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This subscribes to HttpListener requests and finds the appropriate BaseHandler to process it
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The HTTP listener.</value>
|
|
|
|
|
private IDisposable HttpListener { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The web socket connections
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly List<WebSocketConnection> _webSocketConnections = new List<WebSocketConnection>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the external web socket server.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The external web socket server.</value>
|
|
|
|
|
private WebSocketServer ExternalWebSocketServer { get; set; }
|
|
|
|
|
|
2013-02-21 22:39:53 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _logger
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILogger _logger;
|
2013-02-22 05:23:06 +01:00
|
|
|
|
|
2013-02-23 18:54:51 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _network manager
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly INetworkManager _networkManager;
|
|
|
|
|
|
2013-02-22 05:23:06 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _application host
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IApplicationHost _applicationHost;
|
2013-02-21 22:39:53 +01:00
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _supports native web socket
|
|
|
|
|
/// </summary>
|
|
|
|
|
private bool? _supportsNativeWebSocket;
|
|
|
|
|
|
2013-02-23 18:54:51 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _kernel
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IKernel _kernel;
|
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a value indicating whether [supports web socket].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value><c>true</c> if [supports web socket]; otherwise, <c>false</c>.</value>
|
|
|
|
|
internal bool SupportsNativeWebSocket
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (!_supportsNativeWebSocket.HasValue)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
new ClientWebSocket();
|
|
|
|
|
|
|
|
|
|
_supportsNativeWebSocket = true;
|
|
|
|
|
}
|
|
|
|
|
catch (PlatformNotSupportedException)
|
|
|
|
|
{
|
|
|
|
|
_supportsNativeWebSocket = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _supportsNativeWebSocket.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the web socket port number.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The web socket port number.</value>
|
|
|
|
|
public int WebSocketPortNumber
|
|
|
|
|
{
|
2013-02-23 18:54:51 +01:00
|
|
|
|
get { return SupportsNativeWebSocket ? _kernel.Configuration.HttpServerPortNumber : _kernel.Configuration.LegacyWebSocketPortNumber; }
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="TcpManager" /> class.
|
|
|
|
|
/// </summary>
|
2013-02-22 05:23:06 +01:00
|
|
|
|
/// <param name="applicationHost">The application host.</param>
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <param name="kernel">The kernel.</param>
|
2013-02-23 18:54:51 +01:00
|
|
|
|
/// <param name="networkManager">The network manager.</param>
|
2013-02-21 22:39:53 +01:00
|
|
|
|
/// <param name="logger">The logger.</param>
|
2013-02-23 18:54:51 +01:00
|
|
|
|
public TcpManager(IApplicationHost applicationHost, IKernel kernel, INetworkManager networkManager, ILogger logger)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-02-23 18:54:51 +01:00
|
|
|
|
if (applicationHost == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("applicationHost");
|
|
|
|
|
}
|
|
|
|
|
if (kernel == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("kernel");
|
|
|
|
|
}
|
|
|
|
|
if (networkManager == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("networkManager");
|
|
|
|
|
}
|
|
|
|
|
if (logger == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("logger");
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 22:39:53 +01:00
|
|
|
|
_logger = logger;
|
2013-02-23 18:54:51 +01:00
|
|
|
|
_kernel = kernel;
|
2013-02-22 05:23:06 +01:00
|
|
|
|
_applicationHost = applicationHost;
|
2013-02-23 18:54:51 +01:00
|
|
|
|
_networkManager = networkManager;
|
2013-02-21 22:39:53 +01:00
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
if (kernel.IsFirstRun)
|
|
|
|
|
{
|
|
|
|
|
RegisterServerWithAdministratorAccess();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReloadUdpServer();
|
|
|
|
|
ReloadHttpServer();
|
|
|
|
|
|
|
|
|
|
if (!SupportsNativeWebSocket)
|
|
|
|
|
{
|
|
|
|
|
ReloadExternalWebSocketServer();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts the external web socket server.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void ReloadExternalWebSocketServer()
|
|
|
|
|
{
|
|
|
|
|
// Avoid windows firewall prompts in the ui
|
2013-02-23 18:54:51 +01:00
|
|
|
|
if (_kernel.KernelContext != KernelContext.Server)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DisposeExternalWebSocketServer();
|
|
|
|
|
|
2013-02-23 18:54:51 +01:00
|
|
|
|
ExternalWebSocketServer = new WebSocketServer(_kernel.Configuration.LegacyWebSocketPortNumber, IPAddress.Any)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
OnConnected = OnAlchemyWebSocketClientConnected,
|
|
|
|
|
TimeOut = TimeSpan.FromMinutes(60)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ExternalWebSocketServer.Start();
|
|
|
|
|
|
2013-02-21 22:39:53 +01:00
|
|
|
|
_logger.Info("Alchemy Web Socket Server started");
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called when [alchemy web socket client connected].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="context">The context.</param>
|
|
|
|
|
private void OnAlchemyWebSocketClientConnected(UserContext context)
|
|
|
|
|
{
|
2013-02-21 22:39:53 +01:00
|
|
|
|
var connection = new WebSocketConnection(new AlchemyWebSocket(context, _logger), context.ClientAddress, ProcessWebSocketMessageReceived, _logger);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
_webSocketConnections.Add(connection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Restarts the Http Server, or starts it if not currently running
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="registerServerOnFailure">if set to <c>true</c> [register server on failure].</param>
|
|
|
|
|
public void ReloadHttpServer(bool registerServerOnFailure = true)
|
|
|
|
|
{
|
|
|
|
|
// Only reload if the port has changed, so that we don't disconnect any active users
|
2013-02-23 18:54:51 +01:00
|
|
|
|
if (HttpServer != null && HttpServer.UrlPrefix.Equals(_kernel.HttpServerUrlPrefix, StringComparison.OrdinalIgnoreCase))
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DisposeHttpServer();
|
|
|
|
|
|
2013-02-21 22:39:53 +01:00
|
|
|
|
_logger.Info("Loading Http Server");
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-02-23 18:54:51 +01:00
|
|
|
|
HttpServer = new HttpServer(_kernel.HttpServerUrlPrefix, "Media Browser", _applicationHost, _kernel, _logger);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
catch (HttpListenerException ex)
|
|
|
|
|
{
|
2013-02-21 22:39:53 +01:00
|
|
|
|
_logger.ErrorException("Error starting Http Server", ex);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
if (registerServerOnFailure)
|
|
|
|
|
{
|
|
|
|
|
RegisterServerWithAdministratorAccess();
|
|
|
|
|
|
|
|
|
|
// Don't get stuck in a loop
|
|
|
|
|
ReloadHttpServer(false);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HttpServer.WebSocketConnected += HttpServer_WebSocketConnected;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles the WebSocketConnected event of the HttpServer control.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">The source of the event.</param>
|
|
|
|
|
/// <param name="e">The <see cref="WebSocketConnectEventArgs" /> instance containing the event data.</param>
|
|
|
|
|
void HttpServer_WebSocketConnected(object sender, WebSocketConnectEventArgs e)
|
|
|
|
|
{
|
2013-02-21 22:39:53 +01:00
|
|
|
|
var connection = new WebSocketConnection(e.WebSocket, e.Endpoint, ProcessWebSocketMessageReceived, _logger);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
_webSocketConnections.Add(connection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Processes the web socket message received.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="result">The result.</param>
|
|
|
|
|
private async void ProcessWebSocketMessageReceived(WebSocketMessageInfo result)
|
|
|
|
|
{
|
2013-02-23 18:54:51 +01:00
|
|
|
|
var tasks = _kernel.WebSocketListeners.Select(i => Task.Run(async () =>
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await i.ProcessMessage(result).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2013-02-21 22:39:53 +01:00
|
|
|
|
_logger.ErrorException("{0} failed processing WebSocket message {1}", ex, i.GetType().Name, result.MessageType);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
await Task.WhenAll(tasks).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts or re-starts the udp server
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void ReloadUdpServer()
|
|
|
|
|
{
|
|
|
|
|
// For now, there's no reason to keep reloading this over and over
|
|
|
|
|
if (UdpServer != null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Avoid windows firewall prompts in the ui
|
2013-02-23 18:54:51 +01:00
|
|
|
|
if (_kernel.KernelContext != KernelContext.Server)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DisposeUdpServer();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// The port number can't be in configuration because we don't want it to ever change
|
2013-02-23 18:54:51 +01:00
|
|
|
|
UdpServer = new UdpServer(new IPEndPoint(IPAddress.Any, _kernel.UdpServerPortNumber));
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
catch (SocketException ex)
|
|
|
|
|
{
|
2013-02-21 22:39:53 +01:00
|
|
|
|
_logger.ErrorException("Failed to start UDP Server", ex);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UdpListener = UdpServer.Subscribe(async res =>
|
|
|
|
|
{
|
2013-02-23 18:54:51 +01:00
|
|
|
|
var expectedMessage = String.Format("who is MediaBrowser{0}?", _kernel.KernelContext);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
var expectedMessageBytes = Encoding.UTF8.GetBytes(expectedMessage);
|
|
|
|
|
|
|
|
|
|
if (expectedMessageBytes.SequenceEqual(res.Buffer))
|
|
|
|
|
{
|
2013-02-21 22:39:53 +01:00
|
|
|
|
_logger.Info("Received UDP server request from " + res.RemoteEndPoint.ToString());
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
// Send a response back with our ip address and port
|
2013-02-23 18:54:51 +01:00
|
|
|
|
var response = String.Format("MediaBrowser{0}|{1}:{2}", _kernel.KernelContext, _networkManager.GetLocalIpAddress(), _kernel.UdpServerPortNumber);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
await UdpServer.SendAsync(response, res.RemoteEndPoint);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends a message to all clients currently connected via a web socket
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="messageType">Type of the message.</param>
|
|
|
|
|
/// <param name="data">The data.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
public void SendWebSocketMessage<T>(string messageType, T data)
|
|
|
|
|
{
|
|
|
|
|
SendWebSocketMessage(messageType, () => data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends a message to all clients currently connected via a web socket
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="messageType">Type of the message.</param>
|
|
|
|
|
/// <param name="dataFunction">The function that generates the data to send, if there are any connected clients</param>
|
|
|
|
|
public void SendWebSocketMessage<T>(string messageType, Func<T> dataFunction)
|
|
|
|
|
{
|
|
|
|
|
Task.Run(async () => await SendWebSocketMessageAsync(messageType, dataFunction, CancellationToken.None).ConfigureAwait(false));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends a message to all clients currently connected via a web socket
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="messageType">Type of the message.</param>
|
|
|
|
|
/// <param name="dataFunction">The function that generates the data to send, if there are any connected clients</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">messageType</exception>
|
|
|
|
|
public async Task SendWebSocketMessageAsync<T>(string messageType, Func<T> dataFunction, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(messageType))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("messageType");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dataFunction == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("dataFunction");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cancellationToken == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("cancellationToken");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
var connections = _webSocketConnections.Where(s => s.State == WebSocketState.Open).ToList();
|
|
|
|
|
|
|
|
|
|
if (connections.Count > 0)
|
|
|
|
|
{
|
2013-02-21 22:39:53 +01:00
|
|
|
|
_logger.Info("Sending web socket message {0}", messageType);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
var message = new WebSocketMessage<T> { MessageType = messageType, Data = dataFunction() };
|
|
|
|
|
var bytes = JsonSerializer.SerializeToBytes(message);
|
|
|
|
|
|
|
|
|
|
var tasks = connections.Select(s => Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
s.SendAsync(bytes, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2013-02-21 22:39:53 +01:00
|
|
|
|
_logger.ErrorException("Error sending web socket message {0} to {1}", ex, messageType, s.RemoteEndPoint);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
await Task.WhenAll(tasks).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disposes the udp server
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void DisposeUdpServer()
|
|
|
|
|
{
|
|
|
|
|
if (UdpServer != null)
|
|
|
|
|
{
|
|
|
|
|
UdpServer.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (UdpListener != null)
|
|
|
|
|
{
|
|
|
|
|
UdpListener.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disposes the current HttpServer
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void DisposeHttpServer()
|
|
|
|
|
{
|
|
|
|
|
foreach (var socket in _webSocketConnections)
|
|
|
|
|
{
|
|
|
|
|
// Dispose the connection
|
|
|
|
|
socket.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_webSocketConnections.Clear();
|
|
|
|
|
|
|
|
|
|
if (HttpServer != null)
|
|
|
|
|
{
|
2013-02-21 22:39:53 +01:00
|
|
|
|
_logger.Info("Disposing Http Server");
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
HttpServer.WebSocketConnected -= HttpServer_WebSocketConnected;
|
|
|
|
|
HttpServer.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (HttpListener != null)
|
|
|
|
|
{
|
|
|
|
|
HttpListener.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DisposeExternalWebSocketServer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registers the server with administrator access.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void RegisterServerWithAdministratorAccess()
|
|
|
|
|
{
|
|
|
|
|
// Create a temp file path to extract the bat file to
|
2013-02-23 18:54:51 +01:00
|
|
|
|
var tmpFile = Path.Combine(_kernel.ApplicationPaths.TempDirectory, Guid.NewGuid() + ".bat");
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
// Extract the bat file
|
|
|
|
|
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MediaBrowser.Common.Kernel.RegisterServer.bat"))
|
|
|
|
|
{
|
|
|
|
|
using (var fileStream = File.Create(tmpFile))
|
|
|
|
|
{
|
|
|
|
|
stream.CopyTo(fileStream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var startInfo = new ProcessStartInfo
|
|
|
|
|
{
|
|
|
|
|
FileName = tmpFile,
|
|
|
|
|
|
2013-02-23 18:54:51 +01:00
|
|
|
|
Arguments = string.Format("{0} {1} {2} {3}", _kernel.Configuration.HttpServerPortNumber,
|
|
|
|
|
_kernel.HttpServerUrlPrefix,
|
|
|
|
|
_kernel.UdpServerPortNumber,
|
|
|
|
|
_kernel.Configuration.LegacyWebSocketPortNumber),
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
CreateNoWindow = true,
|
|
|
|
|
WindowStyle = ProcessWindowStyle.Hidden,
|
|
|
|
|
Verb = "runas",
|
|
|
|
|
ErrorDialog = false
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using (var process = Process.Start(startInfo))
|
|
|
|
|
{
|
|
|
|
|
process.WaitForExit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-23 18:54:51 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Releases unmanaged and - optionally - managed resources.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
2013-02-23 18:54:51 +01:00
|
|
|
|
protected virtual void Dispose(bool dispose)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
if (dispose)
|
|
|
|
|
{
|
|
|
|
|
DisposeUdpServer();
|
|
|
|
|
DisposeHttpServer();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disposes the external web socket server.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void DisposeExternalWebSocketServer()
|
|
|
|
|
{
|
|
|
|
|
if (ExternalWebSocketServer != null)
|
|
|
|
|
{
|
|
|
|
|
ExternalWebSocketServer.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called when [application configuration changed].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="oldConfig">The old config.</param>
|
|
|
|
|
/// <param name="newConfig">The new config.</param>
|
|
|
|
|
public void OnApplicationConfigurationChanged(BaseApplicationConfiguration oldConfig, BaseApplicationConfiguration newConfig)
|
|
|
|
|
{
|
|
|
|
|
if (oldConfig.HttpServerPortNumber != newConfig.HttpServerPortNumber)
|
|
|
|
|
{
|
|
|
|
|
ReloadHttpServer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!SupportsNativeWebSocket && oldConfig.LegacyWebSocketPortNumber != newConfig.LegacyWebSocketPortNumber)
|
|
|
|
|
{
|
|
|
|
|
ReloadExternalWebSocketServer();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|