generated from RageAgainstThePixel/upm-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added dataReceivedEventCallback overloads to capture streamed data - Added GetUrl eueryParamters overload - Added missing docs - Formalized error responses and logging - Updated deps
- Loading branch information
1 parent
3f637b9
commit 28b5b3a
Showing
6 changed files
with
234 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using System; | ||
using System.IO; | ||
using UnityEngine; | ||
using UnityEngine.Networking; | ||
using Utilities.Async; | ||
|
||
namespace Utilities.WebRequestRest | ||
{ | ||
internal class DownloadHandlerCallback : DownloadHandlerScript | ||
{ | ||
internal const int kEventChunkSize = 512; | ||
|
||
private readonly MemoryStream stream = new MemoryStream(); | ||
|
||
private long streamPosition; | ||
|
||
private long StreamOffset => stream.Length - streamPosition; | ||
|
||
private int eventChunkSize = kEventChunkSize; | ||
|
||
public int EventChunkSize | ||
{ | ||
get => eventChunkSize; | ||
set | ||
{ | ||
if (value < 1) | ||
{ | ||
throw new InvalidOperationException($"{nameof(EventChunkSize)} must be greater than 1!"); | ||
} | ||
|
||
eventChunkSize = value; | ||
} | ||
} | ||
|
||
public UnityWebRequest UnityWebRequest { get; set; } | ||
|
||
public Action<UnityWebRequest, byte[]> OnDataReceived { get; set; } | ||
|
||
protected override bool ReceiveData(byte[] unprocessedData, int dataLength) | ||
{ | ||
var offset = unprocessedData.Length - dataLength; | ||
|
||
try | ||
{ | ||
stream.Position = stream.Length; | ||
stream.Write(unprocessedData, offset, dataLength); | ||
|
||
if (StreamOffset >= EventChunkSize) | ||
{ | ||
var multiplier = StreamOffset / EventChunkSize; | ||
var bytesToRead = EventChunkSize * multiplier; | ||
stream.Position = streamPosition; | ||
var buffer = new byte[bytesToRead]; | ||
var bytesRead = stream.Read(buffer, 0, (int)bytesToRead); | ||
streamPosition += bytesRead; | ||
OnDataReceived?.Invoke(UnityWebRequest, buffer); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
Debug.LogError(e); | ||
} | ||
|
||
return base.ReceiveData(unprocessedData, dataLength); | ||
} | ||
|
||
protected override void CompleteContent() | ||
{ | ||
try | ||
{ | ||
if (StreamOffset > 0) | ||
{ | ||
stream.Position = streamPosition; | ||
var buffer = new byte[StreamOffset]; | ||
var bytesRead = stream.Read(buffer); | ||
streamPosition += bytesRead; | ||
OnDataReceived.Invoke(UnityWebRequest, buffer); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
Debug.LogError(e); | ||
} | ||
|
||
base.CompleteContent(); | ||
} | ||
|
||
public override void Dispose() | ||
{ | ||
stream.Dispose(); | ||
base.Dispose(); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.