From 8295f15ab83709e73da5507d74a4fd8d819b9de9 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 7 May 2013 13:07:57 -0400 Subject: [PATCH 1/6] fixes #110 - Edit .ass file style --- .../MediaEncoder/MediaEncoder.cs | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs b/MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs index d02031fe6b..f42e9c6ae7 100644 --- a/MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs +++ b/MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs @@ -682,6 +682,7 @@ namespace MediaBrowser.Server.Implementations.MediaEncoder throw new ApplicationException(msg); } + await SetAssFont(outputPath).ConfigureAwait(false); } /// @@ -843,6 +844,33 @@ namespace MediaBrowser.Server.Implementations.MediaEncoder throw new ApplicationException(msg); } + + await SetAssFont(outputPath).ConfigureAwait(false); + } + + /// + /// Sets the ass font. + /// + /// The file. + /// Task. + private async Task SetAssFont(string file) + { + string text; + Encoding encoding; + + using (var reader = new StreamReader(file, detectEncodingFromByteOrderMarks: true)) + { + encoding = reader.CurrentEncoding; + + text = await reader.ReadToEndAsync().ConfigureAwait(false); + } + + text = text.Replace(",Arial,", ",Arial Unicode MS,"); + + using (var writer = new StreamWriter(file, false, encoding)) + { + writer.Write(text); + } } /// From de6acec5047e54e284025a3e9749ab9afa257d45 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 7 May 2013 14:49:42 -0400 Subject: [PATCH 2/6] fixes #224 - Exception trying to start server after a reboot --- .../ApplicationHost.cs | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/MediaBrowser.ServerApplication/ApplicationHost.cs b/MediaBrowser.ServerApplication/ApplicationHost.cs index 07e615b1da..309a2c3518 100644 --- a/MediaBrowser.ServerApplication/ApplicationHost.cs +++ b/MediaBrowser.ServerApplication/ApplicationHost.cs @@ -382,7 +382,8 @@ namespace MediaBrowser.ServerApplication HttpServer.Init(GetExports(false)); ServerManager.AddWebSocketListeners(GetExports(false)); - ServerManager.Start(); + + StartServer(true); }, () => LibraryManager.AddParts(GetExports(), GetExports(), GetExports(), GetExports(), GetExports()), @@ -405,6 +406,31 @@ namespace MediaBrowser.ServerApplication ); } + /// + /// Starts the server. + /// + /// if set to true [retry on failure]. + private void StartServer(bool retryOnFailure) + { + try + { + ServerManager.Start(); + } + catch + { + if (retryOnFailure) + { + RegisterServerWithAdministratorAccess(); + + StartServer(false); + } + else + { + throw; + } + } + } + /// /// Restarts this instance. /// From 156453cc42dff7e6c01ddd72eec01fe852dbeb22 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 7 May 2013 14:55:24 -0400 Subject: [PATCH 3/6] fixes #65 - When changing the port number server do not prompt for restart --- .../ServerManager/ServerManager.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs b/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs index 2131d90110..6b33f1145c 100644 --- a/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs +++ b/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs @@ -355,12 +355,12 @@ namespace MediaBrowser.Server.Implementations.ServerManager if (!string.Equals(HttpServer.UrlPrefix, _kernel.HttpServerUrlPrefix, StringComparison.OrdinalIgnoreCase)) { - ReloadHttpServer(); + _applicationHost.NotifyPendingRestart(); } - if (!SupportsNativeWebSocket && ExternalWebSocketServer != null && ExternalWebSocketServer.Port != ConfigurationManager.Configuration.LegacyWebSocketPortNumber) + else if (!SupportsNativeWebSocket && ExternalWebSocketServer != null && ExternalWebSocketServer.Port != ConfigurationManager.Configuration.LegacyWebSocketPortNumber) { - ReloadExternalWebSocketServer(); + _applicationHost.NotifyPendingRestart(); } } From baa779fb3170fe155f479cf96d5ce013e2a6431c Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 7 May 2013 14:57:27 -0400 Subject: [PATCH 4/6] improve stream disposal --- .../IO/DirectoryWatchers.cs | 16 +++++----------- .../Providers/ProviderManager.cs | 8 ++++---- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/MediaBrowser.Server.Implementations/IO/DirectoryWatchers.cs b/MediaBrowser.Server.Implementations/IO/DirectoryWatchers.cs index 5413cefc3d..beeb1c181c 100644 --- a/MediaBrowser.Server.Implementations/IO/DirectoryWatchers.cs +++ b/MediaBrowser.Server.Implementations/IO/DirectoryWatchers.cs @@ -410,11 +410,13 @@ namespace MediaBrowser.Server.Implementations.IO return false; } - FileStream stream = null; - try { - stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); + using (new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) + { + //file is not locked + return false; + } } catch { @@ -424,14 +426,6 @@ namespace MediaBrowser.Server.Implementations.IO //or does not exist (has already been processed) return true; } - finally - { - if (stream != null) - stream.Close(); - } - - //file is not locked - return false; } /// diff --git a/MediaBrowser.Server.Implementations/Providers/ProviderManager.cs b/MediaBrowser.Server.Implementations/Providers/ProviderManager.cs index 569fb8e022..12cc859129 100644 --- a/MediaBrowser.Server.Implementations/Providers/ProviderManager.cs +++ b/MediaBrowser.Server.Implementations/Providers/ProviderManager.cs @@ -457,9 +457,9 @@ namespace MediaBrowser.Server.Implementations.Providers { var ms = new MemoryStream(); - using (var input = dataToSave) + using (dataToSave) { - await input.CopyToAsync(ms).ConfigureAwait(false); + await dataToSave.CopyToAsync(ms).ConfigureAwait(false); } ms.Position = 0; @@ -470,9 +470,9 @@ namespace MediaBrowser.Server.Implementations.Providers { using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous)) { - using (var input = dataToSave) + using (dataToSave) { - await input.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, cancellationToken).ConfigureAwait(false); + await dataToSave.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, cancellationToken).ConfigureAwait(false); } } From f8263b8b69bac872f5b821e32f52fb502665976b Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 7 May 2013 15:07:51 -0400 Subject: [PATCH 5/6] improve restart code after port change --- .../BaseApplicationHost.cs | 4 ++-- .../ServerManager/ServerManager.cs | 14 ++----------- .../ApplicationHost.cs | 21 +++++++++++++++++++ 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/MediaBrowser.Common.Implementations/BaseApplicationHost.cs b/MediaBrowser.Common.Implementations/BaseApplicationHost.cs index c0959de85d..386da0ffaf 100644 --- a/MediaBrowser.Common.Implementations/BaseApplicationHost.cs +++ b/MediaBrowser.Common.Implementations/BaseApplicationHost.cs @@ -201,7 +201,7 @@ namespace MediaBrowser.Common.Implementations Task.Run(() => ConfigureAutoRunAtStartup()); - ConfigurationManager.ConfigurationUpdated += ConfigurationManager_ConfigurationUpdated; + ConfigurationManager.ConfigurationUpdated += OnConfigurationUpdated; }); } @@ -459,7 +459,7 @@ namespace MediaBrowser.Common.Implementations /// The source of the event. /// The instance containing the event data. /// - void ConfigurationManager_ConfigurationUpdated(object sender, EventArgs e) + protected virtual void OnConfigurationUpdated(object sender, EventArgs e) { ConfigureAutoRunAtStartup(); } diff --git a/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs b/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs index 6b33f1145c..a45804f69c 100644 --- a/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs +++ b/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs @@ -114,6 +114,8 @@ namespace MediaBrowser.Server.Implementations.ServerManager _applicationHost = applicationHost; ConfigurationManager = configurationManager; _kernel = kernel; + + ConfigurationManager.ConfigurationUpdated += ConfigurationUpdated; } /// @@ -127,8 +129,6 @@ namespace MediaBrowser.Server.Implementations.ServerManager { ReloadExternalWebSocketServer(); } - - ConfigurationManager.ConfigurationUpdated += ConfigurationUpdated; } /// @@ -352,16 +352,6 @@ namespace MediaBrowser.Server.Implementations.ServerManager void ConfigurationUpdated(object sender, EventArgs e) { HttpServer.EnableHttpRequestLogging = ConfigurationManager.Configuration.EnableHttpLevelLogging; - - if (!string.Equals(HttpServer.UrlPrefix, _kernel.HttpServerUrlPrefix, StringComparison.OrdinalIgnoreCase)) - { - _applicationHost.NotifyPendingRestart(); - } - - else if (!SupportsNativeWebSocket && ExternalWebSocketServer != null && ExternalWebSocketServer.Port != ConfigurationManager.Configuration.LegacyWebSocketPortNumber) - { - _applicationHost.NotifyPendingRestart(); - } } /// diff --git a/MediaBrowser.ServerApplication/ApplicationHost.cs b/MediaBrowser.ServerApplication/ApplicationHost.cs index 309a2c3518..37e6d1b8d7 100644 --- a/MediaBrowser.ServerApplication/ApplicationHost.cs +++ b/MediaBrowser.ServerApplication/ApplicationHost.cs @@ -431,6 +431,27 @@ namespace MediaBrowser.ServerApplication } } + /// + /// Called when [configuration updated]. + /// + /// The sender. + /// The instance containing the event data. + protected override void OnConfigurationUpdated(object sender, EventArgs e) + { + base.OnConfigurationUpdated(sender, e); + + if (!string.Equals(HttpServer.UrlPrefix, ServerKernel.HttpServerUrlPrefix, StringComparison.OrdinalIgnoreCase)) + { + NotifyPendingRestart(); + } + + else if (!ServerManager.SupportsNativeWebSocket && ServerManager.WebSocketPortNumber != ServerConfigurationManager.Configuration.LegacyWebSocketPortNumber) + { + NotifyPendingRestart(); + } + + } + /// /// Restarts this instance. /// From 0934a9ca766ae6794e7e8aa506ebf48c63a25526 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 7 May 2013 15:18:47 -0400 Subject: [PATCH 6/6] only update .ass file if contents will be different --- .../MediaEncoder/MediaEncoder.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs b/MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs index f42e9c6ae7..ce39ffc064 100644 --- a/MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs +++ b/MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs @@ -865,11 +865,14 @@ namespace MediaBrowser.Server.Implementations.MediaEncoder text = await reader.ReadToEndAsync().ConfigureAwait(false); } - text = text.Replace(",Arial,", ",Arial Unicode MS,"); + var newText = text.Replace(",Arial,", ",Arial Unicode MS,"); - using (var writer = new StreamWriter(file, false, encoding)) + if (!string.Equals(text, newText)) { - writer.Write(text); + using (var writer = new StreamWriter(file, false, encoding)) + { + writer.Write(newText); + } } }