AsyncStreamWriter.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading.Tasks;
  5. using ServiceStack;
  6. using ServiceStack.Web;
  7. using MediaBrowser.Controller.Net;
  8. namespace MediaBrowser.Server.Implementations.HttpServer
  9. {
  10. public class AsyncStreamWriter : IStreamWriter, IAsyncStreamWriter, IHasOptions
  11. {
  12. /// <summary>
  13. /// Gets or sets the source stream.
  14. /// </summary>
  15. /// <value>The source stream.</value>
  16. private IAsyncStreamSource _source;
  17. public Action OnComplete { get; set; }
  18. public Action OnError { get; set; }
  19. /// <summary>
  20. /// Initializes a new instance of the <see cref="AsyncStreamWriter" /> class.
  21. /// </summary>
  22. public AsyncStreamWriter(IAsyncStreamSource source)
  23. {
  24. _source = source;
  25. }
  26. public IDictionary<string, string> Options
  27. {
  28. get
  29. {
  30. var hasOptions = _source as IHasOptions;
  31. if (hasOptions != null)
  32. {
  33. return hasOptions.Options;
  34. }
  35. return new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  36. }
  37. }
  38. /// <summary>
  39. /// Writes to.
  40. /// </summary>
  41. /// <param name="responseStream">The response stream.</param>
  42. public void WriteTo(Stream responseStream)
  43. {
  44. var task = _source.WriteToAsync(responseStream);
  45. Task.WaitAll(task);
  46. }
  47. public async Task WriteToAsync(Stream responseStream)
  48. {
  49. await _source.WriteToAsync(responseStream).ConfigureAwait(false);
  50. }
  51. }
  52. }