From 35f40993b2b85efc6fbb677d373b337aebfe0465 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Wed, 22 Oct 2014 01:06:19 -0400 Subject: [PATCH] update http server --- MediaBrowser.Dlna/Profiles/XboxOneProfile.cs | 2 - .../HttpServer/HttpListenerHost.cs | 4 +- .../HttpServer/RangeRequestWriter.cs | 59 ++++++++++++++++++- .../HttpServer/StreamWriter.cs | 8 +-- 4 files changed, 62 insertions(+), 11 deletions(-) diff --git a/MediaBrowser.Dlna/Profiles/XboxOneProfile.cs b/MediaBrowser.Dlna/Profiles/XboxOneProfile.cs index 8d480e5394..fea65438a8 100644 --- a/MediaBrowser.Dlna/Profiles/XboxOneProfile.cs +++ b/MediaBrowser.Dlna/Profiles/XboxOneProfile.cs @@ -12,8 +12,6 @@ namespace MediaBrowser.Dlna.Profiles Name = "Xbox One"; TimelineOffsetSeconds = 40; - RequiresPlainFolders = true; - RequiresPlainVideoItems = true; Identification = new DeviceIdentification { diff --git a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs index 98f895616b..0e297816d9 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -205,8 +205,8 @@ namespace MediaBrowser.Server.Implementations.HttpServer HostContext.Config.HandlerFactoryPath = ListenerRequest.GetHandlerPathIfAny(UrlPrefixes.First()); _listener = _supportsNativeWebSocket && NativeWebSocket.IsSupported - ? _listener = new HttpListenerServer(_logger, OnRequestReceived) - //? _listener = new WebSocketSharpListener(_logger, OnRequestReceived) + //? _listener = new HttpListenerServer(_logger, OnRequestReceived) + ? _listener = new WebSocketSharpListener(_logger, OnRequestReceived) : _listener = new WebSocketSharpListener(_logger, OnRequestReceived); _listener.WebSocketHandler = WebSocketHandler; diff --git a/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs b/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs index 7a143c8d96..cdd4c6d7ca 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs @@ -173,9 +173,64 @@ namespace MediaBrowser.Server.Implementations.HttpServer ThrottleCallback = ThrottleCallback }; } - var task = WriteToAsync(responseStream); + WriteToInternal(responseStream); + } + + /// + /// Writes to async. + /// + /// The response stream. + /// Task. + private void WriteToInternal(Stream responseStream) + { + try + { + // Headers only + if (IsHeadRequest) + { + return; + } + + using (var source = SourceStream) + { + // If the requested range is "0-", we can optimize by just doing a stream copy + if (RangeEnd >= TotalContentLength - 1) + { + source.CopyTo(responseStream); + } + else + { + CopyToInternal(source, responseStream, Convert.ToInt32(RangeLength)); + } + } + } + finally + { + if (OnComplete != null) + { + OnComplete(); + } + } + } - Task.WaitAll(task); + private void CopyToInternal(Stream source, Stream destination, int copyLength) + { + const int bufferSize = 81920; + var array = new byte[bufferSize]; + int count; + while ((count = source.Read(array, 0, array.Length)) != 0) + { + var bytesToCopy = Math.Min(count, copyLength); + + destination.Write(array, 0, bytesToCopy); + + copyLength -= bytesToCopy; + + if (copyLength <= 0) + { + break; + } + } } /// diff --git a/MediaBrowser.Server.Implementations/HttpServer/StreamWriter.cs b/MediaBrowser.Server.Implementations/HttpServer/StreamWriter.cs index 1ca5f1204b..29b703e252 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/StreamWriter.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/StreamWriter.cs @@ -91,9 +91,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer ThrottleCallback = ThrottleCallback }; } - var task = WriteToAsync(responseStream); - - Task.WaitAll(task); + WriteToAsync(responseStream); } /// @@ -101,13 +99,13 @@ namespace MediaBrowser.Server.Implementations.HttpServer /// /// The response stream. /// Task. - private async Task WriteToAsync(Stream responseStream) + private void WriteToAsync(Stream responseStream) { try { using (var src = SourceStream) { - await src.CopyToAsync(responseStream).ConfigureAwait(false); + src.CopyTo(responseStream); } } catch (Exception ex)