|
|
|
@ -5,6 +5,7 @@ using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.Net;
|
|
|
|
|
using MediaBrowser.Model.Text;
|
|
|
|
|
using SocketHttpListener.Primitives;
|
|
|
|
@ -26,8 +27,9 @@ namespace SocketHttpListener.Net
|
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
|
private readonly IAcceptSocket _socket;
|
|
|
|
|
private readonly bool _supportsDirectSocketAccess;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
internal ResponseStream(Stream stream, HttpListenerResponse response, IMemoryStreamFactory memoryStreamFactory, ITextEncoding textEncoding, IFileSystem fileSystem, IAcceptSocket socket, bool supportsDirectSocketAccess)
|
|
|
|
|
internal ResponseStream(Stream stream, HttpListenerResponse response, IMemoryStreamFactory memoryStreamFactory, ITextEncoding textEncoding, IFileSystem fileSystem, IAcceptSocket socket, bool supportsDirectSocketAccess, ILogger logger)
|
|
|
|
|
{
|
|
|
|
|
this.response = response;
|
|
|
|
|
_memoryStreamFactory = memoryStreamFactory;
|
|
|
|
@ -35,6 +37,7 @@ namespace SocketHttpListener.Net
|
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
|
_socket = socket;
|
|
|
|
|
_supportsDirectSocketAccess = supportsDirectSocketAccess;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
this.stream = stream;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -309,29 +312,34 @@ namespace SocketHttpListener.Net
|
|
|
|
|
|
|
|
|
|
public Task TransmitFile(string path, long offset, long count, FileShareMode fileShareMode, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
//if (_supportsDirectSocketAccess && offset == 0 && count == 0 && !response.SendChunked)
|
|
|
|
|
//if (_supportsDirectSocketAccess && offset == 0 && count == 0 && !response.SendChunked && response.ContentLength64 > 8192)
|
|
|
|
|
//{
|
|
|
|
|
// return TransmitFileOverSocket(path, offset, count, cancellationToken);
|
|
|
|
|
// return TransmitFileOverSocket(path, offset, count, fileShareMode, cancellationToken);
|
|
|
|
|
//}
|
|
|
|
|
return TransmitFileManaged(path, offset, count, fileShareMode, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly byte[] _emptyBuffer = new byte[] { };
|
|
|
|
|
private async Task TransmitFileOverSocket(string path, long offset, long count, CancellationToken cancellationToken)
|
|
|
|
|
private Task TransmitFileOverSocket(string path, long offset, long count, FileShareMode fileShareMode, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
MemoryStream ms = GetHeaders(response, _memoryStreamFactory, false);
|
|
|
|
|
|
|
|
|
|
var buffer = new byte[] {};
|
|
|
|
|
byte[] buffer;
|
|
|
|
|
if (ms != null)
|
|
|
|
|
{
|
|
|
|
|
ms.Position = 0;
|
|
|
|
|
|
|
|
|
|
byte[] msBuffer;
|
|
|
|
|
_memoryStreamFactory.TryGetBuffer(ms, out msBuffer);
|
|
|
|
|
buffer = msBuffer;
|
|
|
|
|
using (var msCopy = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
ms.CopyTo(msCopy);
|
|
|
|
|
buffer = msCopy.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return TransmitFileManaged(path, offset, count, fileShareMode, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await _socket.SendFile(path, buffer, _emptyBuffer, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
_logger.Info("Socket sending file {0} {1}", path, response.ContentLength64);
|
|
|
|
|
return _socket.SendFile(path, buffer, _emptyBuffer, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task TransmitFileManaged(string path, long offset, long count, FileShareMode fileShareMode, CancellationToken cancellationToken)
|
|
|
|
|