aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsoftworkz <softworkz@hotmail.com>2016-08-05 06:08:11 +0200
committersoftworkz <softworkz@hotmail.com>2016-08-07 02:00:36 +0200
commit433254c498d2e43acfd34e5c4fcee2fdcc2e767b (patch)
treee04ddf9e4202800f5f4658d0bfe9e2f01675bfeb
parent894d87fabb812152c76443afcf1739fda7c578ff (diff)
Async stream handling: Use interface instead of Func<Stream,Task>
No functional changes
-rw-r--r--MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs4
-rw-r--r--MediaBrowser.Api/Playback/Progressive/ProgressiveStreamWriter.cs23
-rw-r--r--MediaBrowser.Controller/MediaBrowser.Controller.csproj1
-rw-r--r--MediaBrowser.Controller/Net/IAsyncStreamSource.cs18
-rw-r--r--MediaBrowser.Controller/Net/IHttpResultFactory.cs2
-rw-r--r--MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriter.cs (renamed from MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriterFunc.cs)35
-rw-r--r--MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriterEx.cs153
-rw-r--r--MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs9
-rw-r--r--MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj3
9 files changed, 223 insertions, 25 deletions
diff --git a/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs b/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs
index 4649499c4..5a5cb8000 100644
--- a/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs
+++ b/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs
@@ -362,9 +362,9 @@ namespace MediaBrowser.Api.Playback.Progressive
outputHeaders[item.Key] = item.Value;
}
- Func<Stream, Task> streamWriter = stream => new ProgressiveFileCopier(FileSystem, job, Logger).StreamFile(outputPath, stream, CancellationToken.None);
+ var streamSource = new ProgressiveFileCopier(FileSystem, outputPath, outputHeaders, job, Logger, CancellationToken.None);
- return ResultFactory.GetAsyncStreamWriter(streamWriter, outputHeaders);
+ return ResultFactory.GetAsyncStreamWriter(streamSource);
}
finally
{
diff --git a/MediaBrowser.Api/Playback/Progressive/ProgressiveStreamWriter.cs b/MediaBrowser.Api/Playback/Progressive/ProgressiveStreamWriter.cs
index 63d71b85e..8c4e23a39 100644
--- a/MediaBrowser.Api/Playback/Progressive/ProgressiveStreamWriter.cs
+++ b/MediaBrowser.Api/Playback/Progressive/ProgressiveStreamWriter.cs
@@ -4,28 +4,45 @@ using System.IO;
using System.Threading;
using System.Threading.Tasks;
using CommonIO;
+using MediaBrowser.Controller.Net;
+using System.Collections.Generic;
+using ServiceStack.Web;
namespace MediaBrowser.Api.Playback.Progressive
{
- public class ProgressiveFileCopier
+ public class ProgressiveFileCopier : IAsyncStreamSource, IHasOptions
{
private readonly IFileSystem _fileSystem;
private readonly TranscodingJob _job;
private readonly ILogger _logger;
+ private readonly string _path;
+ private readonly CancellationToken _cancellationToken;
+ private readonly Dictionary<string, string> _outputHeaders;
// 256k
private const int BufferSize = 81920;
private long _bytesWritten = 0;
- public ProgressiveFileCopier(IFileSystem fileSystem, TranscodingJob job, ILogger logger)
+ public ProgressiveFileCopier(IFileSystem fileSystem, string path, Dictionary<string, string> outputHeaders, TranscodingJob job, ILogger logger, CancellationToken cancellationToken)
{
_fileSystem = fileSystem;
+ _path = path;
+ _outputHeaders = outputHeaders;
_job = job;
_logger = logger;
+ _cancellationToken = cancellationToken;
}
- public async Task StreamFile(string path, Stream outputStream, CancellationToken cancellationToken)
+ public IDictionary<string, string> Options
+ {
+ get
+ {
+ return _outputHeaders;
+ }
+ }
+
+ public async Task WriteToAsync(Stream outputStream)
{
try
{
diff --git a/MediaBrowser.Controller/MediaBrowser.Controller.csproj b/MediaBrowser.Controller/MediaBrowser.Controller.csproj
index 0462117cb..e7eaa1dc0 100644
--- a/MediaBrowser.Controller/MediaBrowser.Controller.csproj
+++ b/MediaBrowser.Controller/MediaBrowser.Controller.csproj
@@ -236,6 +236,7 @@
<Compile Include="Net\IAuthorizationContext.cs" />
<Compile Include="Net\IAuthService.cs" />
<Compile Include="Net\IHasAuthorization.cs" />
+ <Compile Include="Net\IAsyncStreamSource.cs" />
<Compile Include="Net\IHasResultFactory.cs" />
<Compile Include="Net\IHasSession.cs" />
<Compile Include="Net\IHttpResultFactory.cs" />
diff --git a/MediaBrowser.Controller/Net/IAsyncStreamSource.cs b/MediaBrowser.Controller/Net/IAsyncStreamSource.cs
new file mode 100644
index 000000000..0f41f60a5
--- /dev/null
+++ b/MediaBrowser.Controller/Net/IAsyncStreamSource.cs
@@ -0,0 +1,18 @@
+using ServiceStack.Web;
+using System.IO;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Controller.Net
+{
+ /// <summary>
+ /// Interface IAsyncStreamSource
+ /// Enables asynchronous writing to http resonse streams
+ /// </summary>
+ public interface IAsyncStreamSource
+ {
+ /// <summary>
+ /// Asynchronously write to the response stream.
+ /// </summary>
+ Task WriteToAsync(Stream responseStream);
+ }
+}
diff --git a/MediaBrowser.Controller/Net/IHttpResultFactory.cs b/MediaBrowser.Controller/Net/IHttpResultFactory.cs
index 49d4614d8..8fdb1ce37 100644
--- a/MediaBrowser.Controller/Net/IHttpResultFactory.cs
+++ b/MediaBrowser.Controller/Net/IHttpResultFactory.cs
@@ -28,7 +28,7 @@ namespace MediaBrowser.Controller.Net
/// <returns>System.Object.</returns>
object GetResult(object content, string contentType, IDictionary<string,string> responseHeaders = null);
- object GetAsyncStreamWriter(Func<Stream,Task> streamWriter, IDictionary<string, string> responseHeaders = null);
+ object GetAsyncStreamWriter(IAsyncStreamSource streamSource);
/// <summary>
/// Gets the optimized result.
diff --git a/MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriterFunc.cs b/MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriter.cs
index 5aa01c706..e44b0c6af 100644
--- a/MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriterFunc.cs
+++ b/MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriter.cs
@@ -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);
}
}
}
diff --git a/MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriterEx.cs b/MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriterEx.cs
new file mode 100644
index 000000000..b98addb31
--- /dev/null
+++ b/MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriterEx.cs
@@ -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;
+ }
+ }
+ }
+}
diff --git a/MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs b/MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs
index c0a2a5eb3..f234674d8 100644
--- a/MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs
+++ b/MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs
@@ -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);
}
}
} \ No newline at end of file
diff --git a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj
index dca753193..8025e3594 100644
--- a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj
+++ b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj
@@ -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" />