aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriterFunc.cs
diff options
context:
space:
mode:
authorLuke Pulverenti <luke.pulverenti@gmail.com>2016-07-15 13:13:55 -0400
committerLuke Pulverenti <luke.pulverenti@gmail.com>2016-07-15 13:13:55 -0400
commit2e91d69d20e49f971d9890674d3016351ee87ccd (patch)
tree5ce0f330f639434127ada9ccae290bbf5cf400ba /MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriterFunc.cs
parent61aa25c3585d5f7acf1b88ddaf30a8b04e0c466e (diff)
update async stream writing
Diffstat (limited to 'MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriterFunc.cs')
-rw-r--r--MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriterFunc.cs56
1 files changed, 56 insertions, 0 deletions
diff --git a/MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriterFunc.cs b/MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriterFunc.cs
new file mode 100644
index 000000000..4f8b18319
--- /dev/null
+++ b/MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriterFunc.cs
@@ -0,0 +1,56 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Threading.Tasks;
+using ServiceStack;
+using ServiceStack.Web;
+
+namespace MediaBrowser.Server.Implementations.HttpServer
+{
+ public class AsyncStreamWriterFunc : 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; }
+
+ public Action OnComplete { get; set; }
+ public Action OnError { get; set; }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="StreamWriter" /> class.
+ /// </summary>
+ public AsyncStreamWriterFunc(Func<Stream, Task> writer, IDictionary<string, string> headers)
+ {
+ Writer = writer;
+
+ if (headers == null)
+ {
+ headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
+ }
+ Options = headers;
+ }
+
+ /// <summary>
+ /// Writes to.
+ /// </summary>
+ /// <param name="responseStream">The response stream.</param>
+ public void WriteTo(Stream responseStream)
+ {
+ var task = Writer(responseStream);
+ Task.WaitAll(task);
+ }
+
+ public async Task WriteToAsync(Stream responseStream)
+ {
+ await Writer(responseStream).ConfigureAwait(false);
+ }
+ }
+}