Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Net;
using System.Threading;
using JetBrains.Annotations;
using PatchKit.Logging;
using PatchKit.Network;
Expand Down Expand Up @@ -129,11 +130,39 @@ public void Download(CancellationToken cancellationToken)

private void ReadResponseStream(Stream responseStream, CancellationToken cancellationToken)
{
Func<byte[], int, int> tryReadData = (outBuffer, bufferSize) => {
const int threadAbortTimeout = 3 * 1000;
byte[] localBuffer = new byte[bufferSize];
int localBufferRead = -1;

var readDataThread = new Thread(() => {
localBufferRead = responseStream.Read(localBuffer, 0, BufferSize);
});

readDataThread.Start();
if (!readDataThread.Join(_timeout))
{
_logger.LogWarning("Read data thread timed out, trying to interrupt...");
readDataThread.Interrupt();

if (!readDataThread.Join(threadAbortTimeout))
{
_logger.LogWarning("Read data thread failed to terminate, trying to abort...");
readDataThread.Abort();
}

_logger.LogDebug("Throwing timeout exception");
throw new TimeoutException();
}

Array.ConstrainedCopy(localBuffer, 0, outBuffer, 0, localBufferRead);
return localBufferRead;
};

int bufferRead;
while ((bufferRead = responseStream.Read(_buffer, 0, BufferSize)) > 0)
while ((bufferRead = tryReadData(_buffer, BufferSize)) > 0)
{
cancellationToken.ThrowIfCancellationRequested();

OnDataAvailable(_buffer, bufferRead);
}
}
Expand Down