removed udp server layer
This commit is contained in:
parent
ecbec1f6fd
commit
31bb5b5cb3
|
@ -80,12 +80,10 @@
|
||||||
<Compile Include="Net\IHttpServer.cs" />
|
<Compile Include="Net\IHttpServer.cs" />
|
||||||
<Compile Include="Net\INetworkManager.cs" />
|
<Compile Include="Net\INetworkManager.cs" />
|
||||||
<Compile Include="Net\IRestfulService.cs" />
|
<Compile Include="Net\IRestfulService.cs" />
|
||||||
<Compile Include="Net\IUdpServer.cs" />
|
|
||||||
<Compile Include="Net\IWebSocket.cs" />
|
<Compile Include="Net\IWebSocket.cs" />
|
||||||
<Compile Include="Net\IWebSocketConnection.cs" />
|
<Compile Include="Net\IWebSocketConnection.cs" />
|
||||||
<Compile Include="Net\IWebSocketServer.cs" />
|
<Compile Include="Net\IWebSocketServer.cs" />
|
||||||
<Compile Include="Net\MimeTypes.cs" />
|
<Compile Include="Net\MimeTypes.cs" />
|
||||||
<Compile Include="Net\UdpMessageReceivedEventArgs.cs" />
|
|
||||||
<Compile Include="Net\WebSocketConnectEventArgs.cs" />
|
<Compile Include="Net\WebSocketConnectEventArgs.cs" />
|
||||||
<Compile Include="Plugins\IPlugin.cs" />
|
<Compile Include="Plugins\IPlugin.cs" />
|
||||||
<Compile Include="Plugins\IUIPlugin.cs" />
|
<Compile Include="Plugins\IUIPlugin.cs" />
|
||||||
|
|
|
@ -1,46 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace MediaBrowser.Common.Net
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Interface IUdpServer
|
|
||||||
/// </summary>
|
|
||||||
public interface IUdpServer : IDisposable
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Occurs when [message received].
|
|
||||||
/// </summary>
|
|
||||||
event EventHandler<UdpMessageReceivedEventArgs> MessageReceived;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Starts the specified port.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="port">The port.</param>
|
|
||||||
void Start(int port);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Stops this instance.
|
|
||||||
/// </summary>
|
|
||||||
void Stop();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Sends the async.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="bytes">The bytes.</param>
|
|
||||||
/// <param name="remoteEndPoint">The remote end point.</param>
|
|
||||||
/// <returns>Task.</returns>
|
|
||||||
/// <exception cref="System.ArgumentNullException">data</exception>
|
|
||||||
Task SendAsync(byte[] bytes, string remoteEndPoint);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Sends the async.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="bytes">The bytes.</param>
|
|
||||||
/// <param name="ipAddress">The ip address.</param>
|
|
||||||
/// <param name="port">The port.</param>
|
|
||||||
/// <returns>Task.</returns>
|
|
||||||
/// <exception cref="System.ArgumentNullException">bytes</exception>
|
|
||||||
Task SendAsync(byte[] bytes, string ipAddress, int port);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,21 +0,0 @@
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace MediaBrowser.Common.Net
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Class UdpMessageReceivedEventArgs
|
|
||||||
/// </summary>
|
|
||||||
public class UdpMessageReceivedEventArgs : EventArgs
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the bytes.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>The bytes.</value>
|
|
||||||
public byte[] Bytes { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the remote end point.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>The remote end point.</value>
|
|
||||||
public string RemoteEndPoint { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -124,15 +124,6 @@ namespace MediaBrowser.Controller
|
||||||
/// <value>The user data repository.</value>
|
/// <value>The user data repository.</value>
|
||||||
public IUserDataRepository UserDataRepository { get; set; }
|
public IUserDataRepository UserDataRepository { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the UDP server port number.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>The UDP server port number.</value>
|
|
||||||
public int UdpServerPortNumber
|
|
||||||
{
|
|
||||||
get { return 7359; }
|
|
||||||
}
|
|
||||||
|
|
||||||
private readonly IServerConfigurationManager _configurationManager;
|
private readonly IServerConfigurationManager _configurationManager;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -454,6 +454,10 @@ namespace MediaBrowser.Server.Implementations.IO
|
||||||
// Should we remove it from it's parent?
|
// Should we remove it from it's parent?
|
||||||
Logger.ErrorException("Error refreshing {0}", ex, i.Name);
|
Logger.ErrorException("Error refreshing {0}", ex, i.Name);
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Logger.ErrorException("Error refreshing {0}", ex, i.Name);
|
||||||
|
}
|
||||||
|
|
||||||
}))).ConfigureAwait(false);
|
}))).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
|
@ -167,6 +167,7 @@
|
||||||
<Compile Include="Sqlite\SQLiteRepository.cs" />
|
<Compile Include="Sqlite\SQLiteRepository.cs" />
|
||||||
<Compile Include="Sqlite\SQLiteUserDataRepository.cs" />
|
<Compile Include="Sqlite\SQLiteUserDataRepository.cs" />
|
||||||
<Compile Include="Sqlite\SQLiteUserRepository.cs" />
|
<Compile Include="Sqlite\SQLiteUserRepository.cs" />
|
||||||
|
<Compile Include="Udp\UdpMessageReceivedEventArgs.cs" />
|
||||||
<Compile Include="Udp\UdpServer.cs" />
|
<Compile Include="Udp\UdpServer.cs" />
|
||||||
<Compile Include="Updates\InstallationManager.cs" />
|
<Compile Include="Updates\InstallationManager.cs" />
|
||||||
<Compile Include="WebSocket\AlchemyServer.cs" />
|
<Compile Include="WebSocket\AlchemyServer.cs" />
|
||||||
|
@ -244,7 +245,6 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="packages.config" />
|
<None Include="packages.config" />
|
||||||
<EmbeddedResource Include="ServerManager\RegisterServer.bat" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup />
|
<ItemGroup />
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
|
|
|
@ -1,28 +0,0 @@
|
||||||
rem %1 = http server port
|
|
||||||
rem %2 = http server url
|
|
||||||
rem %3 = udp server port
|
|
||||||
rem %4 = tcp server port (web socket)
|
|
||||||
|
|
||||||
if [%1]==[] GOTO DONE
|
|
||||||
|
|
||||||
netsh advfirewall firewall delete rule name="Port %1" protocol=TCP localport=%1
|
|
||||||
netsh advfirewall firewall add rule name="Port %1" dir=in action=allow protocol=TCP localport=%1
|
|
||||||
|
|
||||||
if [%2]==[] GOTO DONE
|
|
||||||
|
|
||||||
netsh http del urlacl url="%2" user="NT AUTHORITY\Authenticated Users"
|
|
||||||
netsh http add urlacl url="%2" user="NT AUTHORITY\Authenticated Users"
|
|
||||||
|
|
||||||
if [%3]==[] GOTO DONE
|
|
||||||
|
|
||||||
netsh advfirewall firewall delete rule name="Port %3" protocol=UDP localport=%3
|
|
||||||
netsh advfirewall firewall add rule name="Port %3" dir=in action=allow protocol=UDP localport=%3
|
|
||||||
|
|
||||||
if [%4]==[] GOTO DONE
|
|
||||||
|
|
||||||
netsh advfirewall firewall delete rule name="Port %4" protocol=TCP localport=%4
|
|
||||||
netsh advfirewall firewall add rule name="Port %4" dir=in action=allow protocol=TCP localport=%4
|
|
||||||
|
|
||||||
|
|
||||||
:DONE
|
|
||||||
Exit
|
|
|
@ -7,13 +7,8 @@ using MediaBrowser.Model.Net;
|
||||||
using MediaBrowser.Model.Serialization;
|
using MediaBrowser.Model.Serialization;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Sockets;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
@ -22,14 +17,8 @@ namespace MediaBrowser.Server.Implementations.ServerManager
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Manages the Http Server, Udp Server and WebSocket connections
|
/// Manages the Http Server, Udp Server and WebSocket connections
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ServerManager : IServerManager, IDisposable
|
public class ServerManager : IServerManager
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// This is the udp server used for server discovery by clients
|
|
||||||
/// </summary>
|
|
||||||
/// <value>The UDP server.</value>
|
|
||||||
private IUdpServer UdpServer { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Both the Ui and server will have a built-in HttpServer.
|
/// 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.
|
/// People will inevitably want remote control apps so it's needed in the Ui too.
|
||||||
|
@ -65,11 +54,6 @@ namespace MediaBrowser.Server.Implementations.ServerManager
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The _network manager
|
|
||||||
/// </summary>
|
|
||||||
private readonly INetworkManager _networkManager;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The _application host
|
/// The _application host
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -106,26 +90,22 @@ namespace MediaBrowser.Server.Implementations.ServerManager
|
||||||
private readonly List<IWebSocketListener> _webSocketListeners = new List<IWebSocketListener>();
|
private readonly List<IWebSocketListener> _webSocketListeners = new List<IWebSocketListener>();
|
||||||
|
|
||||||
private readonly Kernel _kernel;
|
private readonly Kernel _kernel;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="ServerManager" /> class.
|
/// Initializes a new instance of the <see cref="ServerManager" /> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="applicationHost">The application host.</param>
|
/// <param name="applicationHost">The application host.</param>
|
||||||
/// <param name="networkManager">The network manager.</param>
|
|
||||||
/// <param name="jsonSerializer">The json serializer.</param>
|
/// <param name="jsonSerializer">The json serializer.</param>
|
||||||
/// <param name="logger">The logger.</param>
|
/// <param name="logger">The logger.</param>
|
||||||
/// <param name="configurationManager">The configuration manager.</param>
|
/// <param name="configurationManager">The configuration manager.</param>
|
||||||
|
/// <param name="kernel">The kernel.</param>
|
||||||
/// <exception cref="System.ArgumentNullException">applicationHost</exception>
|
/// <exception cref="System.ArgumentNullException">applicationHost</exception>
|
||||||
public ServerManager(IApplicationHost applicationHost, INetworkManager networkManager, IJsonSerializer jsonSerializer, ILogger logger, IServerConfigurationManager configurationManager, Kernel kernel)
|
public ServerManager(IApplicationHost applicationHost, IJsonSerializer jsonSerializer, ILogger logger, IServerConfigurationManager configurationManager, Kernel kernel)
|
||||||
{
|
{
|
||||||
if (applicationHost == null)
|
if (applicationHost == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException("applicationHost");
|
throw new ArgumentNullException("applicationHost");
|
||||||
}
|
}
|
||||||
if (networkManager == null)
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException("networkManager");
|
|
||||||
}
|
|
||||||
if (jsonSerializer == null)
|
if (jsonSerializer == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException("jsonSerializer");
|
throw new ArgumentNullException("jsonSerializer");
|
||||||
|
@ -138,7 +118,6 @@ namespace MediaBrowser.Server.Implementations.ServerManager
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_jsonSerializer = jsonSerializer;
|
_jsonSerializer = jsonSerializer;
|
||||||
_applicationHost = applicationHost;
|
_applicationHost = applicationHost;
|
||||||
_networkManager = networkManager;
|
|
||||||
ConfigurationManager = configurationManager;
|
ConfigurationManager = configurationManager;
|
||||||
_kernel = kernel;
|
_kernel = kernel;
|
||||||
}
|
}
|
||||||
|
@ -148,13 +127,6 @@ namespace MediaBrowser.Server.Implementations.ServerManager
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Start()
|
public void Start()
|
||||||
{
|
{
|
||||||
if (_applicationHost.IsFirstRun)
|
|
||||||
{
|
|
||||||
RegisterServerWithAdministratorAccess();
|
|
||||||
}
|
|
||||||
|
|
||||||
ReloadUdpServer();
|
|
||||||
|
|
||||||
ReloadHttpServer();
|
ReloadHttpServer();
|
||||||
|
|
||||||
if (!SupportsNativeWebSocket)
|
if (!SupportsNativeWebSocket)
|
||||||
|
@ -162,7 +134,7 @@ namespace MediaBrowser.Server.Implementations.ServerManager
|
||||||
ReloadExternalWebSocketServer();
|
ReloadExternalWebSocketServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
ConfigurationManager.ConfigurationUpdated += _kernel_ConfigurationUpdated;
|
ConfigurationManager.ConfigurationUpdated += ConfigurationUpdated;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -181,8 +153,7 @@ namespace MediaBrowser.Server.Implementations.ServerManager
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Restarts the Http Server, or starts it if not currently running
|
/// Restarts the Http Server, or starts it if not currently running
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="registerServerOnFailure">if set to <c>true</c> [register server on failure].</param>
|
private void ReloadHttpServer()
|
||||||
private void ReloadHttpServer(bool registerServerOnFailure = true)
|
|
||||||
{
|
{
|
||||||
// Only reload if the port has changed, so that we don't disconnect any active users
|
// Only reload if the port has changed, so that we don't disconnect any active users
|
||||||
if (HttpServer != null && HttpServer.UrlPrefix.Equals(_kernel.HttpServerUrlPrefix, StringComparison.OrdinalIgnoreCase))
|
if (HttpServer != null && HttpServer.UrlPrefix.Equals(_kernel.HttpServerUrlPrefix, StringComparison.OrdinalIgnoreCase))
|
||||||
|
@ -204,16 +175,6 @@ namespace MediaBrowser.Server.Implementations.ServerManager
|
||||||
{
|
{
|
||||||
_logger.ErrorException("Error starting Http Server", ex);
|
_logger.ErrorException("Error starting Http Server", ex);
|
||||||
|
|
||||||
if (registerServerOnFailure)
|
|
||||||
{
|
|
||||||
RegisterServerWithAdministratorAccess();
|
|
||||||
|
|
||||||
// Don't get stuck in a loop
|
|
||||||
ReloadHttpServer(false);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -253,60 +214,6 @@ namespace MediaBrowser.Server.Implementations.ServerManager
|
||||||
await Task.WhenAll(tasks).ConfigureAwait(false);
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
DisposeUdpServer();
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// The port number can't be in configuration because we don't want it to ever change
|
|
||||||
UdpServer = _applicationHost.Resolve<IUdpServer>();
|
|
||||||
|
|
||||||
_logger.Info("Starting udp server");
|
|
||||||
|
|
||||||
UdpServer.Start(_kernel.UdpServerPortNumber);
|
|
||||||
}
|
|
||||||
catch (SocketException ex)
|
|
||||||
{
|
|
||||||
_logger.ErrorException("Failed to start UDP Server", ex);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
UdpServer.MessageReceived += UdpServer_MessageReceived;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Handles the MessageReceived event of the UdpServer control.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="sender">The source of the event.</param>
|
|
||||||
/// <param name="e">The <see cref="UdpMessageReceivedEventArgs" /> instance containing the event data.</param>
|
|
||||||
async void UdpServer_MessageReceived(object sender, UdpMessageReceivedEventArgs e)
|
|
||||||
{
|
|
||||||
var context = "Server";
|
|
||||||
|
|
||||||
var expectedMessage = String.Format("who is MediaBrowser{0}?", context);
|
|
||||||
var expectedMessageBytes = Encoding.UTF8.GetBytes(expectedMessage);
|
|
||||||
|
|
||||||
if (expectedMessageBytes.SequenceEqual(e.Bytes))
|
|
||||||
{
|
|
||||||
_logger.Info("Received UDP server request from " + e.RemoteEndPoint);
|
|
||||||
|
|
||||||
// Send a response back with our ip address and port
|
|
||||||
var response = String.Format("MediaBrowser{0}|{1}:{2}", context, _networkManager.GetLocalIpAddress(), ConfigurationManager.Configuration.HttpServerPortNumber);
|
|
||||||
|
|
||||||
await UdpServer.SendAsync(Encoding.UTF8.GetBytes(response), e.RemoteEndPoint);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends a message to all clients currently connected via a web socket
|
/// Sends a message to all clients currently connected via a web socket
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -387,20 +294,6 @@ namespace MediaBrowser.Server.Implementations.ServerManager
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Disposes the udp server
|
|
||||||
/// </summary>
|
|
||||||
private void DisposeUdpServer()
|
|
||||||
{
|
|
||||||
if (UdpServer != null)
|
|
||||||
{
|
|
||||||
_logger.Info("Disposing UdpServer");
|
|
||||||
|
|
||||||
UdpServer.MessageReceived -= UdpServer_MessageReceived;
|
|
||||||
UdpServer.Dispose();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Disposes the current HttpServer
|
/// Disposes the current HttpServer
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -428,46 +321,6 @@ namespace MediaBrowser.Server.Implementations.ServerManager
|
||||||
DisposeExternalWebSocketServer();
|
DisposeExternalWebSocketServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Registers the server with administrator access.
|
|
||||||
/// </summary>
|
|
||||||
private void RegisterServerWithAdministratorAccess()
|
|
||||||
{
|
|
||||||
_logger.Info("Requesting administrative access to authorize http server");
|
|
||||||
|
|
||||||
// Create a temp file path to extract the bat file to
|
|
||||||
var tmpFile = Path.Combine(ConfigurationManager.CommonApplicationPaths.TempDirectory, Guid.NewGuid() + ".bat");
|
|
||||||
|
|
||||||
// Extract the bat file
|
|
||||||
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MediaBrowser.Server.Implementations.ServerManager.RegisterServer.bat"))
|
|
||||||
{
|
|
||||||
using (var fileStream = File.Create(tmpFile))
|
|
||||||
{
|
|
||||||
stream.CopyTo(fileStream);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var startInfo = new ProcessStartInfo
|
|
||||||
{
|
|
||||||
FileName = tmpFile,
|
|
||||||
|
|
||||||
Arguments = string.Format("{0} {1} {2} {3}", ConfigurationManager.Configuration.HttpServerPortNumber,
|
|
||||||
_kernel.HttpServerUrlPrefix,
|
|
||||||
_kernel.UdpServerPortNumber,
|
|
||||||
ConfigurationManager.Configuration.LegacyWebSocketPortNumber),
|
|
||||||
|
|
||||||
CreateNoWindow = true,
|
|
||||||
WindowStyle = ProcessWindowStyle.Hidden,
|
|
||||||
Verb = "runas",
|
|
||||||
ErrorDialog = false
|
|
||||||
};
|
|
||||||
|
|
||||||
using (var process = Process.Start(startInfo))
|
|
||||||
{
|
|
||||||
process.WaitForExit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -485,7 +338,6 @@ namespace MediaBrowser.Server.Implementations.ServerManager
|
||||||
{
|
{
|
||||||
if (dispose)
|
if (dispose)
|
||||||
{
|
{
|
||||||
DisposeUdpServer();
|
|
||||||
DisposeHttpServer();
|
DisposeHttpServer();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -508,7 +360,7 @@ namespace MediaBrowser.Server.Implementations.ServerManager
|
||||||
/// <param name="sender">The source of the event.</param>
|
/// <param name="sender">The source of the event.</param>
|
||||||
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
|
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
|
||||||
/// <exception cref="System.NotImplementedException"></exception>
|
/// <exception cref="System.NotImplementedException"></exception>
|
||||||
void _kernel_ConfigurationUpdated(object sender, EventArgs e)
|
void ConfigurationUpdated(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
HttpServer.EnableHttpRequestLogging = ConfigurationManager.Configuration.EnableHttpLevelLogging;
|
HttpServer.EnableHttpRequestLogging = ConfigurationManager.Configuration.EnableHttpLevelLogging;
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
using MediaBrowser.Common.Implementations.NetworkManagement;
|
using System.Linq;
|
||||||
|
using MediaBrowser.Common.Implementations.NetworkManagement;
|
||||||
using MediaBrowser.Common.Net;
|
using MediaBrowser.Common.Net;
|
||||||
|
using MediaBrowser.Controller.Configuration;
|
||||||
using MediaBrowser.Model.Logging;
|
using MediaBrowser.Model.Logging;
|
||||||
using System;
|
using System;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
@ -13,36 +15,49 @@ namespace MediaBrowser.Server.Implementations.Udp
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Provides a Udp Server
|
/// Provides a Udp Server
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class UdpServer : IUdpServer
|
public class UdpServer : IDisposable
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Occurs when [message received].
|
/// The _logger
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public event EventHandler<UdpMessageReceivedEventArgs> MessageReceived;
|
private readonly ILogger _logger;
|
||||||
|
|
||||||
/// <summary>
|
private readonly INetworkManager _networkManager;
|
||||||
/// Gets or sets the logger.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>The logger.</value>
|
|
||||||
private ILogger Logger { get; set; }
|
|
||||||
|
|
||||||
|
private readonly IServerConfigurationManager _serverConfigurationManager;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="UdpServer" /> class.
|
/// Initializes a new instance of the <see cref="UdpServer" /> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="logger">The logger.</param>
|
/// <param name="logger">The logger.</param>
|
||||||
public UdpServer(ILogger logger)
|
/// <param name="networkManager">The network manager.</param>
|
||||||
|
public UdpServer(ILogger logger, INetworkManager networkManager, IServerConfigurationManager serverConfigurationManager)
|
||||||
{
|
{
|
||||||
Logger = logger;
|
_logger = logger;
|
||||||
|
_networkManager = networkManager;
|
||||||
|
_serverConfigurationManager = serverConfigurationManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Raises the <see cref="E:MessageReceived" /> event.
|
/// Raises the <see cref="E:MessageReceived" /> event.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="e">The <see cref="UdpMessageReceivedEventArgs" /> instance containing the event data.</param>
|
/// <param name="e">The <see cref="UdpMessageReceivedEventArgs" /> instance containing the event data.</param>
|
||||||
protected virtual void OnMessageReceived(UdpMessageReceivedEventArgs e)
|
private async void OnMessageReceived(UdpMessageReceivedEventArgs e)
|
||||||
{
|
{
|
||||||
EventHandler<UdpMessageReceivedEventArgs> handler = MessageReceived;
|
var context = "Server";
|
||||||
if (handler != null) handler(this, e);
|
|
||||||
|
var expectedMessage = String.Format("who is MediaBrowser{0}?", context);
|
||||||
|
var expectedMessageBytes = Encoding.UTF8.GetBytes(expectedMessage);
|
||||||
|
|
||||||
|
if (expectedMessageBytes.SequenceEqual(e.Bytes))
|
||||||
|
{
|
||||||
|
_logger.Info("Received UDP server request from " + e.RemoteEndPoint);
|
||||||
|
|
||||||
|
// Send a response back with our ip address and port
|
||||||
|
var response = String.Format("MediaBrowser{0}|{1}:{2}", context, _networkManager.GetLocalIpAddress(), _serverConfigurationManager.Configuration.HttpServerPortNumber);
|
||||||
|
|
||||||
|
await SendAsync(Encoding.UTF8.GetBytes(response), e.RemoteEndPoint);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -82,7 +97,7 @@ namespace MediaBrowser.Server.Implementations.Udp
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Logger.ErrorException("Error receiving udp message", ex);
|
_logger.ErrorException("Error receiving udp message", ex);
|
||||||
return Task.FromResult(new UdpReceiveResult(new byte[] { }, new IPEndPoint(IPAddress.Any, 0)));
|
return Task.FromResult(new UdpReceiveResult(new byte[] { }, new IPEndPoint(IPAddress.Any, 0)));
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -201,7 +216,7 @@ namespace MediaBrowser.Server.Implementations.Udp
|
||||||
|
|
||||||
await _udpClient.SendAsync(bytes, bytes.Length, new NetworkManager().Parse(remoteEndPoint)).ConfigureAwait(false);
|
await _udpClient.SendAsync(bytes, bytes.Length, new NetworkManager().Parse(remoteEndPoint)).ConfigureAwait(false);
|
||||||
|
|
||||||
Logger.Info("Udp message sent to {0}", remoteEndPoint);
|
_logger.Info("Udp message sent to {0}", remoteEndPoint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,8 @@
|
||||||
using MediaBrowser.Api;
|
using System.Diagnostics;
|
||||||
|
using System.Net.Cache;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using MediaBrowser.Api;
|
||||||
using MediaBrowser.Common;
|
using MediaBrowser.Common;
|
||||||
using MediaBrowser.Common.Configuration;
|
using MediaBrowser.Common.Configuration;
|
||||||
using MediaBrowser.Common.Constants;
|
using MediaBrowser.Common.Constants;
|
||||||
|
@ -55,6 +59,8 @@ namespace MediaBrowser.ServerApplication
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ApplicationHost : BaseApplicationHost<ServerApplicationPaths>, IServerApplicationHost
|
public class ApplicationHost : BaseApplicationHost<ServerApplicationPaths>, IServerApplicationHost
|
||||||
{
|
{
|
||||||
|
private const int UdpServerPort = 7359;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the server kernel.
|
/// Gets the server kernel.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -129,6 +135,12 @@ namespace MediaBrowser.ServerApplication
|
||||||
/// <value>The HTTP server.</value>
|
/// <value>The HTTP server.</value>
|
||||||
private IHttpServer HttpServer { get; set; }
|
private IHttpServer HttpServer { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the UDP server.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>The UDP server.</value>
|
||||||
|
private UdpServer UdpServer { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The full path to our startmenu shortcut
|
/// The full path to our startmenu shortcut
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -172,7 +184,6 @@ namespace MediaBrowser.ServerApplication
|
||||||
RegisterSingleInstance(ServerConfigurationManager);
|
RegisterSingleInstance(ServerConfigurationManager);
|
||||||
|
|
||||||
RegisterSingleInstance<IWebSocketServer>(() => new AlchemyServer(Logger));
|
RegisterSingleInstance<IWebSocketServer>(() => new AlchemyServer(Logger));
|
||||||
RegisterSingleInstance<IUdpServer>(new UdpServer(Logger), false);
|
|
||||||
|
|
||||||
RegisterSingleInstance<IIsoManager>(new PismoIsoManager(Logger));
|
RegisterSingleInstance<IIsoManager>(new PismoIsoManager(Logger));
|
||||||
RegisterSingleInstance<IBlurayExaminer>(new BdInfoExaminer());
|
RegisterSingleInstance<IBlurayExaminer>(new BdInfoExaminer());
|
||||||
|
@ -183,7 +194,7 @@ namespace MediaBrowser.ServerApplication
|
||||||
HttpServer = ServerFactory.CreateServer(this, ProtobufSerializer, Logger, "Media Browser", "index.html");
|
HttpServer = ServerFactory.CreateServer(this, ProtobufSerializer, Logger, "Media Browser", "index.html");
|
||||||
RegisterSingleInstance(HttpServer, false);
|
RegisterSingleInstance(HttpServer, false);
|
||||||
|
|
||||||
ServerManager = new ServerManager(this, NetworkManager, JsonSerializer, Logger, ServerConfigurationManager, ServerKernel);
|
ServerManager = new ServerManager(this, JsonSerializer, Logger, ServerConfigurationManager, ServerKernel);
|
||||||
RegisterSingleInstance(ServerManager);
|
RegisterSingleInstance(ServerManager);
|
||||||
|
|
||||||
UserManager = new UserManager(ServerKernel, Logger, ServerConfigurationManager);
|
UserManager = new UserManager(ServerKernel, Logger, ServerConfigurationManager);
|
||||||
|
@ -247,6 +258,11 @@ namespace MediaBrowser.ServerApplication
|
||||||
{
|
{
|
||||||
base.FindParts();
|
base.FindParts();
|
||||||
|
|
||||||
|
//if (IsFirstRun)
|
||||||
|
{
|
||||||
|
RegisterServerWithAdministratorAccess();
|
||||||
|
}
|
||||||
|
|
||||||
Parallel.Invoke(
|
Parallel.Invoke(
|
||||||
|
|
||||||
() =>
|
() =>
|
||||||
|
@ -261,6 +277,17 @@ namespace MediaBrowser.ServerApplication
|
||||||
|
|
||||||
() => ProviderManager.AddMetadataProviders(GetExports<BaseMetadataProvider>().OrderBy(e => e.Priority).ToArray())
|
() => ProviderManager.AddMetadataProviders(GetExports<BaseMetadataProvider>().OrderBy(e => e.Priority).ToArray())
|
||||||
);
|
);
|
||||||
|
|
||||||
|
UdpServer = new UdpServer(Logger, NetworkManager, ServerConfigurationManager);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
UdpServer.Start(UdpServerPort);
|
||||||
|
}
|
||||||
|
catch (SocketException ex)
|
||||||
|
{
|
||||||
|
Logger.ErrorException("Failed to start UDP Server", ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -280,6 +307,23 @@ namespace MediaBrowser.ServerApplication
|
||||||
get { return ConfigurationManager.CommonConfiguration.EnableAutoUpdate; }
|
get { return ConfigurationManager.CommonConfiguration.EnableAutoUpdate; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
protected override void Dispose(bool dispose)
|
||||||
|
{
|
||||||
|
if (dispose)
|
||||||
|
{
|
||||||
|
if (UdpServer != null)
|
||||||
|
{
|
||||||
|
UdpServer.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
base.Dispose(dispose);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Checks for update.
|
/// Checks for update.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -363,6 +407,46 @@ namespace MediaBrowser.ServerApplication
|
||||||
public override void Shutdown()
|
public override void Shutdown()
|
||||||
{
|
{
|
||||||
App.Instance.Dispatcher.Invoke(App.Instance.Shutdown);
|
App.Instance.Dispatcher.Invoke(App.Instance.Shutdown);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Registers the server with administrator access.
|
||||||
|
/// </summary>
|
||||||
|
private void RegisterServerWithAdministratorAccess()
|
||||||
|
{
|
||||||
|
Logger.Info("Requesting administrative access to authorize http server");
|
||||||
|
|
||||||
|
// Create a temp file path to extract the bat file to
|
||||||
|
var tmpFile = Path.Combine(ConfigurationManager.CommonApplicationPaths.TempDirectory, Guid.NewGuid() + ".bat");
|
||||||
|
|
||||||
|
// Extract the bat file
|
||||||
|
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MediaBrowser.ServerApplication.RegisterServer.bat"))
|
||||||
|
{
|
||||||
|
using (var fileStream = File.Create(tmpFile))
|
||||||
|
{
|
||||||
|
stream.CopyTo(fileStream);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var startInfo = new ProcessStartInfo
|
||||||
|
{
|
||||||
|
FileName = tmpFile,
|
||||||
|
|
||||||
|
Arguments = string.Format("{0} {1} {2} {3}", ServerConfigurationManager.Configuration.HttpServerPortNumber,
|
||||||
|
ServerKernel.HttpServerUrlPrefix,
|
||||||
|
UdpServerPort,
|
||||||
|
ServerConfigurationManager.Configuration.LegacyWebSocketPortNumber),
|
||||||
|
|
||||||
|
CreateNoWindow = true,
|
||||||
|
WindowStyle = ProcessWindowStyle.Hidden,
|
||||||
|
Verb = "runas",
|
||||||
|
ErrorDialog = false
|
||||||
|
};
|
||||||
|
|
||||||
|
using (var process = Process.Start(startInfo))
|
||||||
|
{
|
||||||
|
process.WaitForExit();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,13 @@
|
||||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:tb="http://www.hardcodet.net/taskbar"
|
xmlns:tb="http://www.hardcodet.net/taskbar"
|
||||||
Title="Media Browser Server" Height="350" Width="525" AllowsTransparency="True" Background="Transparent" WindowStyle="None" ShowInTaskbar="False">
|
Title="Media Browser Server"
|
||||||
|
Height="350"
|
||||||
|
Width="525"
|
||||||
|
AllowsTransparency="True"
|
||||||
|
Background="Transparent"
|
||||||
|
WindowStyle="None"
|
||||||
|
ShowInTaskbar="False">
|
||||||
<Grid>
|
<Grid>
|
||||||
<tb:TaskbarIcon Name="MbTaskbarIcon" ToolTipText="Media Browser Server">
|
<tb:TaskbarIcon Name="MbTaskbarIcon" ToolTipText="Media Browser Server">
|
||||||
|
|
||||||
|
|
|
@ -258,6 +258,7 @@
|
||||||
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
|
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
|
||||||
</None>
|
</None>
|
||||||
<AppDesigner Include="Properties\" />
|
<AppDesigner Include="Properties\" />
|
||||||
|
<EmbeddedResource Include="RegisterServer.bat" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="App.config">
|
<None Include="App.config">
|
||||||
|
|
Loading…
Reference in New Issue
Block a user