WebSocketSharpResponse.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using MediaBrowser.Model.IO;
  8. using MediaBrowser.Model.Services;
  9. using Microsoft.AspNetCore.Http;
  10. using Microsoft.Extensions.Logging;
  11. using IRequest = MediaBrowser.Model.Services.IRequest;
  12. namespace Emby.Server.Implementations.SocketSharp
  13. {
  14. public class WebSocketSharpResponse : IResponse
  15. {
  16. private readonly ILogger _logger;
  17. private readonly HttpResponse _response;
  18. public WebSocketSharpResponse(ILogger logger, HttpResponse response, IRequest request)
  19. {
  20. _logger = logger;
  21. this._response = response;
  22. Items = new Dictionary<string, object>();
  23. Request = request;
  24. }
  25. public IRequest Request { get; private set; }
  26. public Dictionary<string, object> Items { get; private set; }
  27. public object OriginalResponse => _response;
  28. public int StatusCode
  29. {
  30. get => this._response.StatusCode;
  31. set => this._response.StatusCode = value;
  32. }
  33. public string StatusDescription { get; set; }
  34. public string ContentType
  35. {
  36. get => _response.ContentType;
  37. set => _response.ContentType = value;
  38. }
  39. public IHeaderDictionary Headers => _response.Headers;
  40. public void AddHeader(string name, string value)
  41. {
  42. if (string.Equals(name, "Content-Type", StringComparison.OrdinalIgnoreCase))
  43. {
  44. ContentType = value;
  45. return;
  46. }
  47. _response.Headers.Add(name, value);
  48. }
  49. public string GetHeader(string name)
  50. {
  51. return _response.Headers[name];
  52. }
  53. public void Redirect(string url)
  54. {
  55. _response.Redirect(url);
  56. }
  57. public Stream OutputStream => _response.Body;
  58. public bool IsClosed { get; set; }
  59. public bool SendChunked { get; set; }
  60. const int StreamCopyToBufferSize = 81920;
  61. public async Task TransmitFile(string path, long offset, long count, FileShareMode fileShareMode, IFileSystem fileSystem, IStreamHelper streamHelper, CancellationToken cancellationToken)
  62. {
  63. // TODO
  64. // return _response.TransmitFile(path, offset, count, fileShareMode, cancellationToken);
  65. var allowAsync = !RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
  66. //if (count <= 0)
  67. //{
  68. // allowAsync = true;
  69. //}
  70. var fileOpenOptions = FileOpenOptions.SequentialScan;
  71. if (allowAsync)
  72. {
  73. fileOpenOptions |= FileOpenOptions.Asynchronous;
  74. }
  75. // use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039
  76. using (var fs = fileSystem.GetFileStream(path, FileOpenMode.Open, FileAccessMode.Read, fileShareMode, fileOpenOptions))
  77. {
  78. if (offset > 0)
  79. {
  80. fs.Position = offset;
  81. }
  82. if (count > 0)
  83. {
  84. await streamHelper.CopyToAsync(fs, OutputStream, count, cancellationToken).ConfigureAwait(false);
  85. }
  86. else
  87. {
  88. await fs.CopyToAsync(OutputStream, StreamCopyToBufferSize, cancellationToken).ConfigureAwait(false);
  89. }
  90. }
  91. }
  92. }
  93. }