Skip to content

Feature/v3.x.x.x/1226 dynamic url testing 2 #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: dev/v3.x.x.x
Choose a base branch
from
Open
Show file tree
Hide file tree
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,8 @@
using System;
using System.IO;
using System.Net;
using System.Collections.Generic;
using System.Threading;
using JetBrains.Annotations;
using PatchKit.Logging;
using PatchKit.Network;
Expand All @@ -15,9 +17,9 @@ public sealed class BaseHttpDownloader : IBaseHttpDownloader
{
private class Handler : DownloadHandlerScript
{
Action<byte[], int> _receiveData;
DataAvailableHandler _receiveData;

public Handler(Action<byte[], int> receiveData)
public Handler(DataAvailableHandler receiveData)
{
_receiveData = receiveData;
}
Expand All @@ -32,25 +34,29 @@ protected override bool ReceiveData(byte[] data, int dataLength)

private readonly ILogger _logger;

private static readonly int BufferSize = 5 * (int) Units.MB;
private static readonly ulong DefaultBufferSize = 5 * (ulong)Units.MB;

private readonly string _url;
private readonly int _timeout;

private readonly ulong _bufferSize;
private readonly byte[] _buffer;

private bool _downloadHasBeenCalled;
private BytesRange? _bytesRange;

public event DataAvailableHandler DataAvailable;

public BaseHttpDownloader(string url, int timeout) :
this(url, timeout, PatcherLogManager.DefaultLogger)
this(url, timeout, PatcherLogManager.DefaultLogger, DefaultBufferSize)
{
}

public BaseHttpDownloader([NotNull] string url, int timeout, ulong bufferSize)
: this(url, timeout, PatcherLogManager.DefaultLogger, bufferSize)
{
}

public BaseHttpDownloader([NotNull] string url, int timeout,
[NotNull] ILogger logger)
[NotNull] ILogger logger, ulong bufferSize)
{
if (string.IsNullOrEmpty(url)) throw new ArgumentException("Value cannot be null or empty.", "url");
if (timeout <= 0) throw new ArgumentOutOfRangeException("timeout");
Expand All @@ -60,7 +66,8 @@ public BaseHttpDownloader([NotNull] string url, int timeout,
_timeout = timeout;
_logger = logger;

_buffer = new byte[BufferSize];
_bufferSize = bufferSize;
_buffer = new byte[_bufferSize];

ServicePointManager.ServerCertificateValidationCallback =
(sender, certificate, chain, errors) => true;
Expand All @@ -77,13 +84,101 @@ public void SetBytesRange(BytesRange? range)
}
}

public void Download(CancellationToken cancellationToken)
public IEnumerable<DataPacket> ReadPackets(CancellationToken cancellationToken)
{
CancellationTokenSource localCancel = new CancellationTokenSource();
cancellationToken.Register(() => localCancel.Cancel());

var packetQueue = new Queue<DataPacket>();
bool isDone = false;

Exception threadException = null;

var threadHandle = new Thread(_ =>
{
try
{
Download(localCancel.Token, (data, length) =>
{
lock (packetQueue)
{
packetQueue.Enqueue(new DataPacket
{
Data = data,
Length = length,
});
}
});
}
catch (OperationCanceledException e)
{
if (cancellationToken.IsCancelled)
{
threadException = e;
}
}
catch (Exception e)
{
threadException = e;
}

isDone = true;
});

Func<int> safeCount = () => {
lock (packetQueue)
{
return packetQueue.Count;
}
};

try
{
threadHandle.Start();
do
{
cancellationToken.ThrowIfCancellationRequested();
if (safeCount() > 0)
{
lock (packetQueue)
{
foreach (var packet in packetQueue)
{
yield return packet;
}
packetQueue.Clear();
}
}
else
{
Thread.Sleep(100);
}
} while (!isDone);
}
finally
{
localCancel.Cancel();
threadHandle.Join();

if (threadException != null)
{
throw threadException;
}
}
}

public void Download(CancellationToken cancellationToken, [NotNull] DataAvailableHandler onDataAvailable)
{
if (onDataAvailable == null)
{
throw new ArgumentNullException("onDataAvailable");
}

try
{
_logger.LogDebug("Downloading...");
_logger.LogTrace("url = " + _url);
_logger.LogTrace("bufferSize = " + BufferSize);
_logger.LogTrace("bufferSize = " + _bufferSize);
_logger.LogTrace("bytesRange = " + (_bytesRange.HasValue
? _bytesRange.Value.Start + "-" + _bytesRange.Value.End
: "(none)"));
Expand All @@ -93,33 +188,32 @@ public void Download(CancellationToken cancellationToken)

UnityWebRequest request = null;

UnityDispatcher.Invoke(() =>
UnityDispatcher.Invoke(() =>
{
request = new UnityWebRequest();
request.uri = new Uri(_url);
request.timeout = 30;

if (_bytesRange.HasValue)
{
var bytesRangeEndText =
var bytesRangeEndText =
_bytesRange.Value.End >= 0L ? _bytesRange.Value.End.ToString() : string.Empty;

request.SetRequestHeader(
"Range",
"Range",
"bytes=" + _bytesRange.Value.Start + "-" + bytesRangeEndText);
}

request.downloadHandler = new Handler(OnDataAvailable);

request.downloadHandler = new Handler(onDataAvailable);
}).WaitOne();

using (request)
{
using(request.downloadHandler)
using (request.downloadHandler)
{
UnityWebRequestAsyncOperation op = null;

UnityDispatcher.Invoke(() =>
UnityDispatcher.Invoke(() =>
{
op = request.SendWebRequest();
}).WaitOne();
Expand All @@ -128,7 +222,7 @@ public void Download(CancellationToken cancellationToken)

while (requestResponseCode <= 0)
{
UnityDispatcher.Invoke(() =>
UnityDispatcher.Invoke(() =>
{
requestResponseCode = request.responseCode;
}).WaitOne();
Expand All @@ -137,19 +231,19 @@ public void Download(CancellationToken cancellationToken)

System.Threading.Thread.Sleep(100);
}

_logger.LogDebug("Received response from server.");
_logger.LogTrace("statusCode = " + requestResponseCode);

if (Is2XXStatus((HttpStatusCode) requestResponseCode))
if (Is2XXStatus((HttpStatusCode)requestResponseCode))
{
_logger.LogDebug("Successful response. Reading response stream...");

bool opIsDone = false;

while (!opIsDone)
{
UnityDispatcher.Invoke(() =>
UnityDispatcher.Invoke(() =>
{
opIsDone = op.isDone;
}).WaitOne();
Expand All @@ -161,16 +255,16 @@ public void Download(CancellationToken cancellationToken)

_logger.LogDebug("Stream has been read.");
}
else if (Is4XXStatus((HttpStatusCode) requestResponseCode))
else if (Is4XXStatus((HttpStatusCode)requestResponseCode))
{
throw new DataNotAvailableException(string.Format(
"Request data for {0} is not available (status: {1})", _url, (HttpStatusCode) request.responseCode));
"Request data for {0} is not available (status: {1})", _url, (HttpStatusCode)request.responseCode));
}
else
{
throw new ServerErrorException(string.Format(
"Server has experienced some issues with request for {0} which resulted in {1} status code.",
_url, (HttpStatusCode) requestResponseCode));
_url, (HttpStatusCode)requestResponseCode));
}
}
}
Expand All @@ -190,22 +284,15 @@ public void Download(CancellationToken cancellationToken)
}
}

// ReSharper disable once InconsistentNaming
private static bool Is2XXStatus(HttpStatusCode statusCode)
{
return (int) statusCode >= 200 && (int) statusCode <= 299;
return (int)statusCode >= 200 && (int)statusCode <= 299;
}

// ReSharper disable once InconsistentNaming
private static bool Is4XXStatus(HttpStatusCode statusCode)
{
return (int) statusCode >= 400 && (int) statusCode <= 499;
}

private void OnDataAvailable(byte[] data, int length)
{
var handler = DataAvailable;
if (handler != null) handler(data, length);
return (int)statusCode >= 400 && (int)statusCode <= 499;
}
}
}
Loading