From 31bb5b5cb349f33842994415dd1d876385b599bb Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Wed, 27 Mar 2013 18:13:46 -0400 Subject: removed udp server layer --- .../IO/DirectoryWatchers.cs | 4 + .../MediaBrowser.Server.Implementations.csproj | 2 +- .../ServerManager/RegisterServer.bat | 28 ---- .../ServerManager/ServerManager.cs | 162 +-------------------- .../Udp/UdpServer.cs | 47 ++++-- 5 files changed, 43 insertions(+), 200 deletions(-) delete mode 100644 MediaBrowser.Server.Implementations/ServerManager/RegisterServer.bat (limited to 'MediaBrowser.Server.Implementations') diff --git a/MediaBrowser.Server.Implementations/IO/DirectoryWatchers.cs b/MediaBrowser.Server.Implementations/IO/DirectoryWatchers.cs index 14d413f1a4..c33975a649 100644 --- a/MediaBrowser.Server.Implementations/IO/DirectoryWatchers.cs +++ b/MediaBrowser.Server.Implementations/IO/DirectoryWatchers.cs @@ -454,6 +454,10 @@ namespace MediaBrowser.Server.Implementations.IO // Should we remove it from it's parent? Logger.ErrorException("Error refreshing {0}", ex, i.Name); } + catch (Exception ex) + { + Logger.ErrorException("Error refreshing {0}", ex, i.Name); + } }))).ConfigureAwait(false); } diff --git a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj index 0a20370515..9aac84f29f 100644 --- a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj +++ b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj @@ -167,6 +167,7 @@ + @@ -244,7 +245,6 @@ - diff --git a/MediaBrowser.Server.Implementations/ServerManager/RegisterServer.bat b/MediaBrowser.Server.Implementations/ServerManager/RegisterServer.bat deleted file mode 100644 index d762dfaf76..0000000000 --- a/MediaBrowser.Server.Implementations/ServerManager/RegisterServer.bat +++ /dev/null @@ -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 \ No newline at end of file diff --git a/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs b/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs index c1453cd8ba..991ac2e3b5 100644 --- a/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs +++ b/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs @@ -7,13 +7,8 @@ using MediaBrowser.Model.Net; using MediaBrowser.Model.Serialization; using System; using System.Collections.Generic; -using System.Diagnostics; -using System.IO; using System.Linq; using System.Net; -using System.Net.Sockets; -using System.Reflection; -using System.Text; using System.Threading; using System.Threading.Tasks; @@ -22,14 +17,8 @@ namespace MediaBrowser.Server.Implementations.ServerManager /// /// Manages the Http Server, Udp Server and WebSocket connections /// - public class ServerManager : IServerManager, IDisposable + public class ServerManager : IServerManager { - /// - /// This is the udp server used for server discovery by clients - /// - /// The UDP server. - private IUdpServer UdpServer { get; set; } - /// /// 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. @@ -65,11 +54,6 @@ namespace MediaBrowser.Server.Implementations.ServerManager /// private readonly ILogger _logger; - /// - /// The _network manager - /// - private readonly INetworkManager _networkManager; - /// /// The _application host /// @@ -106,26 +90,22 @@ namespace MediaBrowser.Server.Implementations.ServerManager private readonly List _webSocketListeners = new List(); private readonly Kernel _kernel; - + /// /// Initializes a new instance of the class. /// /// The application host. - /// The network manager. /// The json serializer. /// The logger. /// The configuration manager. + /// The kernel. /// applicationHost - 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) { throw new ArgumentNullException("applicationHost"); } - if (networkManager == null) - { - throw new ArgumentNullException("networkManager"); - } if (jsonSerializer == null) { throw new ArgumentNullException("jsonSerializer"); @@ -138,7 +118,6 @@ namespace MediaBrowser.Server.Implementations.ServerManager _logger = logger; _jsonSerializer = jsonSerializer; _applicationHost = applicationHost; - _networkManager = networkManager; ConfigurationManager = configurationManager; _kernel = kernel; } @@ -148,13 +127,6 @@ namespace MediaBrowser.Server.Implementations.ServerManager /// public void Start() { - if (_applicationHost.IsFirstRun) - { - RegisterServerWithAdministratorAccess(); - } - - ReloadUdpServer(); - ReloadHttpServer(); if (!SupportsNativeWebSocket) @@ -162,7 +134,7 @@ namespace MediaBrowser.Server.Implementations.ServerManager ReloadExternalWebSocketServer(); } - ConfigurationManager.ConfigurationUpdated += _kernel_ConfigurationUpdated; + ConfigurationManager.ConfigurationUpdated += ConfigurationUpdated; } /// @@ -181,8 +153,7 @@ namespace MediaBrowser.Server.Implementations.ServerManager /// /// Restarts the Http Server, or starts it if not currently running /// - /// if set to true [register server on failure]. - private void ReloadHttpServer(bool registerServerOnFailure = true) + private void ReloadHttpServer() { // 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)) @@ -204,16 +175,6 @@ namespace MediaBrowser.Server.Implementations.ServerManager { _logger.ErrorException("Error starting Http Server", ex); - if (registerServerOnFailure) - { - RegisterServerWithAdministratorAccess(); - - // Don't get stuck in a loop - ReloadHttpServer(false); - - return; - } - throw; } @@ -253,60 +214,6 @@ namespace MediaBrowser.Server.Implementations.ServerManager await Task.WhenAll(tasks).ConfigureAwait(false); } - /// - /// Starts or re-starts the udp server - /// - 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(); - - _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; - } - - /// - /// Handles the MessageReceived event of the UdpServer control. - /// - /// The source of the event. - /// The instance containing the event data. - 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); - } - } - /// /// Sends a message to all clients currently connected via a web socket /// @@ -387,20 +294,6 @@ namespace MediaBrowser.Server.Implementations.ServerManager } } - /// - /// Disposes the udp server - /// - private void DisposeUdpServer() - { - if (UdpServer != null) - { - _logger.Info("Disposing UdpServer"); - - UdpServer.MessageReceived -= UdpServer_MessageReceived; - UdpServer.Dispose(); - } - } - /// /// Disposes the current HttpServer /// @@ -428,46 +321,6 @@ namespace MediaBrowser.Server.Implementations.ServerManager DisposeExternalWebSocketServer(); } - /// - /// Registers the server with administrator access. - /// - 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(); - } - } - /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// @@ -485,7 +338,6 @@ namespace MediaBrowser.Server.Implementations.ServerManager { if (dispose) { - DisposeUdpServer(); DisposeHttpServer(); } } @@ -508,7 +360,7 @@ namespace MediaBrowser.Server.Implementations.ServerManager /// The source of the event. /// The instance containing the event data. /// - void _kernel_ConfigurationUpdated(object sender, EventArgs e) + void ConfigurationUpdated(object sender, EventArgs e) { HttpServer.EnableHttpRequestLogging = ConfigurationManager.Configuration.EnableHttpLevelLogging; diff --git a/MediaBrowser.Server.Implementations/Udp/UdpServer.cs b/MediaBrowser.Server.Implementations/Udp/UdpServer.cs index 375d9274ef..718a25a0d6 100644 --- a/MediaBrowser.Server.Implementations/Udp/UdpServer.cs +++ b/MediaBrowser.Server.Implementations/Udp/UdpServer.cs @@ -1,5 +1,7 @@ -using MediaBrowser.Common.Implementations.NetworkManagement; +using System.Linq; +using MediaBrowser.Common.Implementations.NetworkManagement; using MediaBrowser.Common.Net; +using MediaBrowser.Controller.Configuration; using MediaBrowser.Model.Logging; using System; using System.Net; @@ -13,36 +15,49 @@ namespace MediaBrowser.Server.Implementations.Udp /// /// Provides a Udp Server /// - public class UdpServer : IUdpServer + public class UdpServer : IDisposable { /// - /// Occurs when [message received]. + /// The _logger /// - public event EventHandler MessageReceived; + private readonly ILogger _logger; - /// - /// Gets or sets the logger. - /// - /// The logger. - private ILogger Logger { get; set; } + private readonly INetworkManager _networkManager; + private readonly IServerConfigurationManager _serverConfigurationManager; + /// /// Initializes a new instance of the class. /// /// The logger. - public UdpServer(ILogger logger) + /// The network manager. + public UdpServer(ILogger logger, INetworkManager networkManager, IServerConfigurationManager serverConfigurationManager) { - Logger = logger; + _logger = logger; + _networkManager = networkManager; + _serverConfigurationManager = serverConfigurationManager; } /// /// Raises the event. /// /// The instance containing the event data. - protected virtual void OnMessageReceived(UdpMessageReceivedEventArgs e) + private async void OnMessageReceived(UdpMessageReceivedEventArgs e) { - EventHandler handler = MessageReceived; - if (handler != null) handler(this, 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(), _serverConfigurationManager.Configuration.HttpServerPortNumber); + + await SendAsync(Encoding.UTF8.GetBytes(response), e.RemoteEndPoint); + } } /// @@ -82,7 +97,7 @@ namespace MediaBrowser.Server.Implementations.Udp } 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))); } }) @@ -201,7 +216,7 @@ namespace MediaBrowser.Server.Implementations.Udp 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); } } -- cgit v1.2.3