From e2d6a5c05df874cb812cbc0b85e7deda22ce567a Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 17 May 2013 14:05:49 -0400 Subject: support static trailer streaming --- .../HttpClientManager/HttpClientManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'MediaBrowser.Common.Implementations') diff --git a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs index 4c51d1299..82a91fa46 100644 --- a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs +++ b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs @@ -97,7 +97,7 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager }; client = new HttpClient(handler); - client.Timeout = TimeSpan.FromSeconds(30); + client.Timeout = TimeSpan.FromSeconds(15); _httpClients.TryAdd(host, client); } -- cgit v1.2.3 From 5ecc276cb7c4188b149283722331ad776eeab906 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 18 May 2013 14:58:16 -0400 Subject: Dispose httprequestmessage --- .../HttpClientManager/HttpClientManager.cs | 166 +++++++++++---------- 1 file changed, 85 insertions(+), 81 deletions(-) (limited to 'MediaBrowser.Common.Implementations') diff --git a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs index 82a91fa46..7ea2d0adf 100644 --- a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs +++ b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs @@ -136,8 +136,8 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager { var now = DateTime.UtcNow; - var isCacheValid = (!cachedInfo.MustRevalidate && !string.IsNullOrEmpty(cachedInfo.Etag) && (now - cachedInfo.RequestDate).TotalDays < 7) - || (cachedInfo.Expires.HasValue && cachedInfo.Expires.Value > now); + var isCacheValid = cachedInfo.Expires.HasValue ? cachedInfo.Expires.Value > now : + !cachedInfo.MustRevalidate && !string.IsNullOrEmpty(cachedInfo.Etag) && (now - cachedInfo.RequestDate).TotalDays < 5; if (isCacheValid) { @@ -157,89 +157,90 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager options.CancellationToken.ThrowIfCancellationRequested(); - var message = GetHttpRequestMessage(options); - - if (options.EnableResponseCache && cachedInfo != null) + using (var message = GetHttpRequestMessage(options)) { - if (!string.IsNullOrEmpty(cachedInfo.Etag)) + if (options.EnableResponseCache && cachedInfo != null) { - message.Headers.Add("If-None-Match", cachedInfo.Etag); + if (!string.IsNullOrEmpty(cachedInfo.Etag)) + { + message.Headers.Add("If-None-Match", cachedInfo.Etag); + } + else if (cachedInfo.LastModified.HasValue) + { + message.Headers.IfModifiedSince = new DateTimeOffset(cachedInfo.LastModified.Value); + } } - else if (cachedInfo.LastModified.HasValue) + + if (options.ResourcePool != null) { - message.Headers.IfModifiedSince = new DateTimeOffset(cachedInfo.LastModified.Value); + await options.ResourcePool.WaitAsync(options.CancellationToken).ConfigureAwait(false); } - } - if (options.ResourcePool != null) - { - await options.ResourcePool.WaitAsync(options.CancellationToken).ConfigureAwait(false); - } - - _logger.Info("HttpClientManager.Get url: {0}", options.Url); + _logger.Info("HttpClientManager.Get url: {0}", options.Url); - try - { - options.CancellationToken.ThrowIfCancellationRequested(); + try + { + options.CancellationToken.ThrowIfCancellationRequested(); - var response = await GetHttpClient(GetHostFromUrl(options.Url)).SendAsync(message, HttpCompletionOption.ResponseHeadersRead, options.CancellationToken).ConfigureAwait(false); + var response = await GetHttpClient(GetHostFromUrl(options.Url)).SendAsync(message, HttpCompletionOption.ResponseHeadersRead, options.CancellationToken).ConfigureAwait(false); - if (options.EnableResponseCache) - { - if (response.StatusCode != HttpStatusCode.NotModified) + if (options.EnableResponseCache) { - EnsureSuccessStatusCode(response); - } + if (response.StatusCode != HttpStatusCode.NotModified) + { + EnsureSuccessStatusCode(response); + } - options.CancellationToken.ThrowIfCancellationRequested(); + options.CancellationToken.ThrowIfCancellationRequested(); - cachedInfo = UpdateInfoCache(cachedInfo, options.Url, cachedInfoPath, response); + cachedInfo = UpdateInfoCache(cachedInfo, options.Url, cachedInfoPath, response); - if (response.StatusCode == HttpStatusCode.NotModified) - { - _logger.Debug("Server indicates not modified for {0}. Returning cached result.", options.Url); - - return GetCachedResponse(cachedReponsePath); - } + if (response.StatusCode == HttpStatusCode.NotModified) + { + _logger.Debug("Server indicates not modified for {0}. Returning cached result.", options.Url); - if (!string.IsNullOrEmpty(cachedInfo.Etag) || cachedInfo.LastModified.HasValue || - (cachedInfo.Expires.HasValue && cachedInfo.Expires.Value > DateTime.UtcNow)) + return GetCachedResponse(cachedReponsePath); + } + + if (!string.IsNullOrEmpty(cachedInfo.Etag) || cachedInfo.LastModified.HasValue || + (cachedInfo.Expires.HasValue && cachedInfo.Expires.Value > DateTime.UtcNow)) + { + await UpdateResponseCache(response, cachedReponsePath).ConfigureAwait(false); + + return GetCachedResponse(cachedReponsePath); + } + } + else { - await UpdateResponseCache(response, cachedReponsePath).ConfigureAwait(false); + EnsureSuccessStatusCode(response); - return GetCachedResponse(cachedReponsePath); + options.CancellationToken.ThrowIfCancellationRequested(); } + + return await response.Content.ReadAsStreamAsync().ConfigureAwait(false); } - else + catch (OperationCanceledException ex) { - EnsureSuccessStatusCode(response); - - options.CancellationToken.ThrowIfCancellationRequested(); + throw GetCancellationException(options.Url, options.CancellationToken, ex); } + catch (HttpRequestException ex) + { + _logger.ErrorException("Error getting response from " + options.Url, ex); - return await response.Content.ReadAsStreamAsync().ConfigureAwait(false); - } - catch (OperationCanceledException ex) - { - throw GetCancellationException(options.Url, options.CancellationToken, ex); - } - catch (HttpRequestException ex) - { - _logger.ErrorException("Error getting response from " + options.Url, ex); - - throw new HttpException(ex.Message, ex); - } - catch (Exception ex) - { - _logger.ErrorException("Error getting response from " + options.Url, ex); + throw new HttpException(ex.Message, ex); + } + catch (Exception ex) + { + _logger.ErrorException("Error getting response from " + options.Url, ex); - throw; - } - finally - { - if (options.ResourcePool != null) + throw; + } + finally { - options.ResourcePool.Release(); + if (options.ResourcePool != null) + { + options.ResourcePool.Release(); + } } } } @@ -469,39 +470,42 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager { options.CancellationToken.ThrowIfCancellationRequested(); - using (var response = await GetHttpClient(GetHostFromUrl(options.Url)).SendAsync(GetHttpRequestMessage(options), HttpCompletionOption.ResponseHeadersRead, options.CancellationToken).ConfigureAwait(false)) + using (var message = GetHttpRequestMessage(options)) { - EnsureSuccessStatusCode(response); + using (var response = await GetHttpClient(GetHostFromUrl(options.Url)).SendAsync(message, HttpCompletionOption.ResponseHeadersRead, options.CancellationToken).ConfigureAwait(false)) + { + EnsureSuccessStatusCode(response); - options.CancellationToken.ThrowIfCancellationRequested(); + options.CancellationToken.ThrowIfCancellationRequested(); - var contentLength = GetContentLength(response); + var contentLength = GetContentLength(response); - if (!contentLength.HasValue) - { - // We're not able to track progress - using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false)) + if (!contentLength.HasValue) { - using (var fs = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous)) + // We're not able to track progress + using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false)) { - await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false); + using (var fs = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous)) + { + await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false); + } } } - } - else - { - using (var stream = ProgressStream.CreateReadProgressStream(await response.Content.ReadAsStreamAsync().ConfigureAwait(false), options.Progress.Report, contentLength.Value)) + else { - using (var fs = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous)) + using (var stream = ProgressStream.CreateReadProgressStream(await response.Content.ReadAsStreamAsync().ConfigureAwait(false), options.Progress.Report, contentLength.Value)) { - await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false); + using (var fs = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous)) + { + await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false); + } } } - } - options.Progress.Report(100); + options.Progress.Report(100); - options.CancellationToken.ThrowIfCancellationRequested(); + options.CancellationToken.ThrowIfCancellationRequested(); + } } } catch (Exception ex) -- cgit v1.2.3 From 407016a30702694e39505afbb599791ed7c2dcfe Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 18 May 2013 18:07:59 -0400 Subject: get to the tray icon a little quicker --- .../BaseApplicationHost.cs | 4 +- MediaBrowser.ServerApplication/App.xaml.cs | 13 ++++- MediaBrowser.ServerApplication/ApplicationHost.cs | 57 ++------------------ .../EntryPoints/UdpServerEntryPoint.cs | 61 ++++++++++++++++++++++ .../MediaBrowser.ServerApplication.csproj | 1 + 5 files changed, 77 insertions(+), 59 deletions(-) create mode 100644 MediaBrowser.ServerApplication/EntryPoints/UdpServerEntryPoint.cs (limited to 'MediaBrowser.Common.Implementations') diff --git a/MediaBrowser.Common.Implementations/BaseApplicationHost.cs b/MediaBrowser.Common.Implementations/BaseApplicationHost.cs index 34bdda94f..7451b6b97 100644 --- a/MediaBrowser.Common.Implementations/BaseApplicationHost.cs +++ b/MediaBrowser.Common.Implementations/BaseApplicationHost.cs @@ -180,8 +180,6 @@ namespace MediaBrowser.Common.Implementations await RegisterResources().ConfigureAwait(false); FindParts(); - - await RunStartupTasks().ConfigureAwait(false); } protected virtual void OnLoggerLoaded() @@ -193,7 +191,7 @@ namespace MediaBrowser.Common.Implementations /// Runs the startup tasks. /// /// Task. - protected virtual Task RunStartupTasks() + public virtual Task RunStartupTasks() { return Task.Run(() => { diff --git a/MediaBrowser.ServerApplication/App.xaml.cs b/MediaBrowser.ServerApplication/App.xaml.cs index 762eb4cd2..5a1d7505c 100644 --- a/MediaBrowser.ServerApplication/App.xaml.cs +++ b/MediaBrowser.ServerApplication/App.xaml.cs @@ -6,6 +6,7 @@ using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Entities; using MediaBrowser.Model.Logging; using MediaBrowser.Server.Implementations; +using MediaBrowser.ServerApplication.Splash; using Microsoft.Win32; using System; using System.Diagnostics; @@ -164,11 +165,19 @@ namespace MediaBrowser.ServerApplication Logger = CompositionRoot.LogManager.GetLogger("App"); + var splash = new SplashWindow(CompositionRoot.ApplicationVersion); + + splash.Show(); + await CompositionRoot.Init(); - var win = new MainWindow(CompositionRoot.LogManager, CompositionRoot, CompositionRoot.ServerConfigurationManager, CompositionRoot.UserManager, CompositionRoot.LibraryManager, CompositionRoot.JsonSerializer, CompositionRoot.DisplayPreferencesManager); + splash.Hide(); + + var task = CompositionRoot.RunStartupTasks(); + + new MainWindow(CompositionRoot.LogManager, CompositionRoot, CompositionRoot.ServerConfigurationManager, CompositionRoot.UserManager, CompositionRoot.LibraryManager, CompositionRoot.JsonSerializer, CompositionRoot.DisplayPreferencesManager).Show(); - win.Show(); + await task.ConfigureAwait(false); } catch (Exception ex) { diff --git a/MediaBrowser.ServerApplication/ApplicationHost.cs b/MediaBrowser.ServerApplication/ApplicationHost.cs index 4dd24d98f..35a9698c3 100644 --- a/MediaBrowser.ServerApplication/ApplicationHost.cs +++ b/MediaBrowser.ServerApplication/ApplicationHost.cs @@ -44,7 +44,6 @@ using MediaBrowser.Server.Implementations.Udp; using MediaBrowser.Server.Implementations.Updates; using MediaBrowser.Server.Implementations.WebSocket; using MediaBrowser.ServerApplication.Implementations; -using MediaBrowser.ServerApplication.Splash; using MediaBrowser.WebDashboard.Api; using System; using System.Collections.Generic; @@ -63,7 +62,7 @@ namespace MediaBrowser.ServerApplication /// public class ApplicationHost : BaseApplicationHost, IServerApplicationHost { - private const int UdpServerPort = 7359; + internal const int UdpServerPort = 7359; /// /// Gets the server kernel. @@ -139,11 +138,6 @@ namespace MediaBrowser.ServerApplication /// The HTTP server. private IHttpServer HttpServer { get; set; } - /// - /// Gets or sets the UDP server. - /// - /// The UDP server. - private UdpServer UdpServer { get; set; } /// /// Gets or sets the display preferences manager. /// @@ -175,26 +169,11 @@ namespace MediaBrowser.ServerApplication private Task _httpServerCreationTask; - /// - /// Inits this instance. - /// - /// Task. - public override async Task Init() - { - var win = new SplashWindow(ApplicationVersion); - - win.Show(); - - await base.Init(); - - win.Hide(); - } - /// /// Runs the startup tasks. /// /// Task. - protected override async Task RunStartupTasks() + public override async Task RunStartupTasks() { await base.RunStartupTasks().ConfigureAwait(false); @@ -390,21 +369,8 @@ namespace MediaBrowser.ServerApplication () => LibraryManager.AddParts(GetExports(), GetExports(), GetExports(), GetExports(), GetExports()), - () => ProviderManager.AddMetadataProviders(GetExports().ToArray()), + () => ProviderManager.AddMetadataProviders(GetExports().ToArray()) - () => - { - UdpServer = new UdpServer(Logger, NetworkManager, ServerConfigurationManager); - - try - { - UdpServer.Start(UdpServerPort); - } - catch (SocketException ex) - { - Logger.ErrorException("Failed to start UDP Server", ex); - } - } ); } @@ -471,23 +437,6 @@ namespace MediaBrowser.ServerApplication get { return ConfigurationManager.CommonConfiguration.EnableAutoUpdate; } } - /// - /// Releases unmanaged and - optionally - managed resources. - /// - /// true to release both managed and unmanaged resources; false to release only unmanaged resources. - protected override void Dispose(bool dispose) - { - if (dispose) - { - if (UdpServer != null) - { - UdpServer.Dispose(); - } - } - - base.Dispose(dispose); - } - /// /// Checks for update. /// diff --git a/MediaBrowser.ServerApplication/EntryPoints/UdpServerEntryPoint.cs b/MediaBrowser.ServerApplication/EntryPoints/UdpServerEntryPoint.cs new file mode 100644 index 000000000..af4ce84e3 --- /dev/null +++ b/MediaBrowser.ServerApplication/EntryPoints/UdpServerEntryPoint.cs @@ -0,0 +1,61 @@ +using MediaBrowser.Common.Net; +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Plugins; +using MediaBrowser.Model.Logging; +using MediaBrowser.Server.Implementations.Udp; +using System.Net.Sockets; + +namespace MediaBrowser.ServerApplication.EntryPoints +{ + public class UdpServerEntryPoint : IServerEntryPoint + { + /// + /// Gets or sets the UDP server. + /// + /// The UDP server. + private UdpServer UdpServer { get; set; } + + private readonly ILogger _logger; + private readonly INetworkManager _networkManager; + private readonly IServerConfigurationManager _serverConfigurationManager; + + public UdpServerEntryPoint(ILogger logger, INetworkManager networkManager, IServerConfigurationManager serverConfigurationManager) + { + _logger = logger; + _networkManager = networkManager; + _serverConfigurationManager = serverConfigurationManager; + } + + public void Run() + { + var udpServer = new UdpServer(_logger, _networkManager, _serverConfigurationManager); + + try + { + udpServer.Start(ApplicationHost.UdpServerPort); + + UdpServer = udpServer; + } + catch (SocketException ex) + { + _logger.ErrorException("Failed to start UDP Server", ex); + } + } + + public void Dispose() + { + Dispose(true); + } + + protected virtual void Dispose(bool dispose) + { + if (dispose) + { + if (UdpServer != null) + { + UdpServer.Dispose(); + } + } + } + } +} diff --git a/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj b/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj index 19b2d91ca..7c4eb128d 100644 --- a/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj +++ b/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj @@ -196,6 +196,7 @@ + SplashWindow.xaml -- cgit v1.2.3