| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.Networking; |
| | 4 | |
|
| | 5 | | namespace DCL |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// Our custom request async operation to be used with the WebRequestController. |
| | 9 | | /// </summary> |
| | 10 | | public class WebRequestAsyncOperation : CustomYieldInstruction |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// Event that will be invoked when the request has been completed. |
| | 14 | | /// </summary> |
| | 15 | | public event Action<WebRequestAsyncOperation> completed; |
| | 16 | |
|
| | 17 | | /// <summary> |
| | 18 | | /// WebRequest that is being managed. |
| | 19 | | /// </summary> |
| 0 | 20 | | public UnityWebRequest webRequest { get; private set; } |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Returns true after the request has finished communicating with the remote server. |
| | 24 | | /// </summary> |
| 0 | 25 | | public bool isDone { get; private set; } |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Returns true if the request was successfully finished. |
| | 29 | | /// </summary> |
| 0 | 30 | | public bool isSucceded { get; private set; } |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Returns true if webRequest has been disposed (webRequest = null). |
| | 34 | | /// </summary> |
| 0 | 35 | | public bool isDisposed { get { return webRequest == null; } } |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Set to true for disposing the request just after it has been completed. |
| | 39 | | /// </summary> |
| 0 | 40 | | public bool disposeOnCompleted { get; set; } |
| | 41 | |
|
| 759 | 42 | | public override bool keepWaiting { get { return !isDone; } } |
| | 43 | |
|
| 378 | 44 | | public WebRequestAsyncOperation(UnityWebRequest webRequest) |
| | 45 | | { |
| 378 | 46 | | this.webRequest = webRequest; |
| 378 | 47 | | isDone = false; |
| 378 | 48 | | isSucceded = false; |
| 378 | 49 | | disposeOnCompleted = true; |
| 378 | 50 | | } |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// Mark the request as completed and throw the corresponding event. |
| | 54 | | /// </summary> |
| | 55 | | /// <param name="success">True if the request was successfully ended.</param> |
| | 56 | | internal void SetAsCompleted(bool success) |
| | 57 | | { |
| 297 | 58 | | completed?.Invoke(this); |
| 297 | 59 | | isDone = true; |
| 297 | 60 | | isSucceded = success; |
| | 61 | |
|
| 297 | 62 | | if (disposeOnCompleted) |
| 101 | 63 | | Dispose(); |
| 297 | 64 | | } |
| | 65 | |
|
| | 66 | | /// <summary> |
| | 67 | | /// If in progress, halts the request as soon as possible. |
| | 68 | | /// </summary> |
| | 69 | | public void Abort() |
| | 70 | | { |
| 477 | 71 | | if (webRequest == null || isDone) |
| 372 | 72 | | return; |
| | 73 | |
|
| 105 | 74 | | webRequest.Abort(); |
| 105 | 75 | | } |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// Signals that this request is no longer being used, and should clean up any resources it is using (it aborts |
| | 79 | | /// </summary> |
| | 80 | | public void Dispose() |
| | 81 | | { |
| 477 | 82 | | Abort(); |
| | 83 | |
|
| 477 | 84 | | if (webRequest == null) |
| 103 | 85 | | return; |
| | 86 | |
|
| 374 | 87 | | webRequest.Dispose(); |
| 374 | 88 | | webRequest = null; |
| 374 | 89 | | } |
| | 90 | |
|
| 0 | 91 | | internal void SetNewWebRequest(UnityWebRequest newRequest) { webRequest = newRequest; } |
| | 92 | | } |
| | 93 | | } |