Async stream handling: Use interface instead of Func<Stream,Task>

No functional changes
This commit is contained in:
softworkz
2016-08-05 06:08:11 +02:00
parent 894d87fabb
commit 433254c498
9 changed files with 223 additions and 25 deletions

View File

@@ -4,38 +4,41 @@ using System.IO;
using System.Threading.Tasks;
using ServiceStack;
using ServiceStack.Web;
using MediaBrowser.Controller.Net;
namespace MediaBrowser.Server.Implementations.HttpServer
{
public class AsyncStreamWriterFunc : IStreamWriter, IAsyncStreamWriter, IHasOptions
public class AsyncStreamWriter : IStreamWriter, IAsyncStreamWriter, IHasOptions
{
/// <summary>
/// Gets or sets the source stream.
/// </summary>
/// <value>The source stream.</value>
private Func<Stream, Task> Writer { get; set; }
/// <summary>
/// Gets the options.
/// </summary>
/// <value>The options.</value>
public IDictionary<string, string> Options { get; private set; }
private IAsyncStreamSource _source;
public Action OnComplete { get; set; }
public Action OnError { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="StreamWriter" /> class.
/// Initializes a new instance of the <see cref="AsyncStreamWriter" /> class.
/// </summary>
public AsyncStreamWriterFunc(Func<Stream, Task> writer, IDictionary<string, string> headers)
public AsyncStreamWriter(IAsyncStreamSource source)
{
Writer = writer;
_source = source;
}
if (headers == null)
public IDictionary<string, string> Options
{
get
{
headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
var hasOptions = _source as IHasOptions;
if (hasOptions != null)
{
return hasOptions.Options;
}
return new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
}
Options = headers;
}
/// <summary>
@@ -44,13 +47,13 @@ namespace MediaBrowser.Server.Implementations.HttpServer
/// <param name="responseStream">The response stream.</param>
public void WriteTo(Stream responseStream)
{
var task = Writer(responseStream);
var task = _source.WriteToAsync(responseStream);
Task.WaitAll(task);
}
public async Task WriteToAsync(Stream responseStream)
{
await Writer(responseStream).ConfigureAwait(false);
await _source.WriteToAsync(responseStream).ConfigureAwait(false);
}
}
}

View File

@@ -0,0 +1,153 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using ServiceStack;
using ServiceStack.Web;
using MediaBrowser.Controller.Net;
namespace MediaBrowser.Server.Implementations.HttpServer
{
public class AsyncStreamWriterEx : AsyncStreamWriter, IHttpResult
{
/// <summary>
/// Gets or sets the source stream.
/// </summary>
/// <value>The source stream.</value>
private IAsyncStreamSource _source;
/// <summary>
/// Initializes a new instance of the <see cref="AsyncStreamWriter" /> class.
/// </summary>
public AsyncStreamWriterEx(IAsyncStreamSource source) : base(source)
{
_source = source;
}
public string ContentType
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public List<System.Net.Cookie> Cookies
{
get { throw new NotImplementedException(); }
}
public Dictionary<string, string> Headers
{
get { throw new NotImplementedException(); }
}
public int PaddingLength
{
get
{
return Result.PaddingLength;
}
set
{
Result.PaddingLength = value;
}
}
public IRequest RequestContext
{
get
{
return Result.RequestContext;
}
set
{
Result.RequestContext = value;
}
}
public object Response
{
get
{
return Result.Response;
}
set
{
Result.Response = value;
}
}
public IContentTypeWriter ResponseFilter
{
get
{
return Result.ResponseFilter;
}
set
{
Result.ResponseFilter = value;
}
}
public Func<IDisposable> ResultScope
{
get
{
return Result.ResultScope;
}
set
{
Result.ResultScope = value;
}
}
public int Status
{
get
{
return Result.Status;
}
set
{
Result.Status = value;
}
}
public System.Net.HttpStatusCode StatusCode
{
get
{
return Result.StatusCode;
}
set
{
Result.StatusCode = value;
}
}
public string StatusDescription
{
get
{
return Result.StatusDescription;
}
set
{
Result.StatusDescription = value;
}
}
private IHttpResult Result
{
get
{
return _source as IHttpResult;
}
}
}
}

View File

@@ -704,9 +704,14 @@ namespace MediaBrowser.Server.Implementations.HttpServer
throw error;
}
public object GetAsyncStreamWriter(Func<Stream, Task> streamWriter, IDictionary<string, string> responseHeaders = null)
public object GetAsyncStreamWriter(IAsyncStreamSource streamSource)
{
return new AsyncStreamWriterFunc(streamWriter, responseHeaders);
if (streamSource as IHttpResult != null)
{
return new AsyncStreamWriterEx(streamSource);
}
return new AsyncStreamWriter(streamSource);
}
}
}

View File

@@ -156,7 +156,8 @@
<Compile Include="EntryPoints\ServerEventNotifier.cs" />
<Compile Include="EntryPoints\UserDataChangeNotifier.cs" />
<Compile Include="FileOrganization\OrganizerScheduledTask.cs" />
<Compile Include="HttpServer\AsyncStreamWriterFunc.cs" />
<Compile Include="HttpServer\AsyncStreamWriterEx.cs" />
<Compile Include="HttpServer\AsyncStreamWriter.cs" />
<Compile Include="HttpServer\IHttpListener.cs" />
<Compile Include="HttpServer\Security\AuthorizationContext.cs" />
<Compile Include="HttpServer\ContainerAdapter.cs" />