| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.Networking; |
| | 4 | |
|
| | 5 | | namespace DCL |
| | 6 | | { |
| | 7 | | public interface IWebRequestAsyncOperation |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Event that will be invoked when the request has been completed. |
| | 11 | | /// </summary> |
| | 12 | | event Action<WebRequestAsyncOperation> completed; |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// WebRequest that is being managed. |
| | 16 | | /// </summary> |
| | 17 | | UnityWebRequest webRequest { get; } |
| | 18 | |
|
| | 19 | | UnityWebRequestAsyncOperation asyncOp { get; } |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Returns true after the request has finished communicating with the remote server. |
| | 23 | | /// </summary> |
| | 24 | | bool isDone { get; } |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Returns true if the request was successfully finished. |
| | 28 | | /// </summary> |
| | 29 | | bool isSucceded { get; } |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Returns true if webRequest has been disposed (webRequest = null). |
| | 33 | | /// </summary> |
| | 34 | | bool isDisposed { get; } |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Set to true for disposing the request just after it has been completed. |
| | 38 | | /// </summary> |
| | 39 | | bool disposeOnCompleted { get; } |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Returns the data that has been downloaded as a byte array, if not done or success it will return an empty ar |
| | 43 | | /// </summary> |
| | 44 | | byte[] GetResultData(); |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// If in progress, halts the request as soon as possible. |
| | 48 | | /// </summary> |
| | 49 | | void Abort(); |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Signals that this request is no longer being used, and should clean up any resources it is using (it aborts |
| | 53 | | /// </summary> |
| | 54 | | void Dispose(); |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Mark the request as completed and throw the corresponding event. |
| | 58 | | /// </summary> |
| | 59 | | /// <param name="success">True if the request was successfully ended.</param> |
| | 60 | | void SetAsCompleted(bool success); |
| | 61 | | } |
| | 62 | |
|
| | 63 | | /// <summary> |
| | 64 | | /// Our custom request async operation to be used with the WebRequestController. |
| | 65 | | /// </summary> |
| | 66 | | public class WebRequestAsyncOperation : CustomYieldInstruction, IWebRequestAsyncOperation |
| | 67 | | { |
| | 68 | | /// <summary> |
| | 69 | | /// Event that will be invoked when the request has been completed. |
| | 70 | | /// </summary> |
| | 71 | | public event Action<WebRequestAsyncOperation> completed; |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// WebRequest that is being managed. |
| | 75 | | /// </summary> |
| 134 | 76 | | public UnityWebRequest webRequest { get; private set; } |
| | 77 | |
|
| 0 | 78 | | public UnityWebRequestAsyncOperation asyncOp { get; private set; } |
| | 79 | |
|
| | 80 | | /// <summary> |
| | 81 | | /// Returns true after the request has finished communicating with the remote server. |
| | 82 | | /// </summary> |
| 9 | 83 | | public bool isDone { get; private set; } |
| | 84 | |
|
| | 85 | | /// <summary> |
| | 86 | | /// Returns true if the request was successfully finished. |
| | 87 | | /// </summary> |
| 9 | 88 | | public bool isSucceded { get; private set; } |
| | 89 | |
|
| | 90 | | /// <summary> |
| | 91 | | /// Returns true if webRequest has been disposed (webRequest = null). |
| | 92 | | /// </summary> |
| 0 | 93 | | public bool isDisposed { get { return webRequest == null; } } |
| | 94 | |
|
| | 95 | | /// <summary> |
| | 96 | | /// Set to true for disposing the request just after it has been completed. |
| | 97 | | /// </summary> |
| 0 | 98 | | public bool disposeOnCompleted { get; set; } |
| | 99 | |
|
| 57 | 100 | | public override bool keepWaiting { get { return !isDone; } } |
| | 101 | |
|
| 341 | 102 | | public WebRequestAsyncOperation(UnityWebRequest webRequest) |
| | 103 | | { |
| 341 | 104 | | this.webRequest = webRequest; |
| 341 | 105 | | isDone = false; |
| 341 | 106 | | isSucceded = false; |
| 341 | 107 | | disposeOnCompleted = true; |
| 341 | 108 | | } |
| | 109 | |
|
| | 110 | | /// <summary> |
| | 111 | | /// Mark the request as completed and throw the corresponding event. |
| | 112 | | /// </summary> |
| | 113 | | /// <param name="success">True if the request was successfully ended.</param> |
| | 114 | | public void SetAsCompleted(bool success) |
| | 115 | | { |
| 315 | 116 | | completed?.Invoke(this); |
| 315 | 117 | | isDone = true; |
| 315 | 118 | | isSucceded = success; |
| | 119 | |
|
| 315 | 120 | | if (disposeOnCompleted) |
| 129 | 121 | | Dispose(); |
| 315 | 122 | | } |
| | 123 | |
|
| | 124 | | /// <summary> |
| | 125 | | /// If in progress, halts the request as soon as possible. |
| | 126 | | /// </summary> |
| | 127 | | public void Abort() |
| | 128 | | { |
| 401 | 129 | | if (webRequest == null || isDone) |
| 322 | 130 | | return; |
| | 131 | |
|
| 79 | 132 | | webRequest.Abort(); |
| 79 | 133 | | } |
| | 134 | |
|
| | 135 | | /// <summary> |
| | 136 | | /// Signals that this request is no longer being used, and should clean up any resources it is using (it aborts |
| | 137 | | /// </summary> |
| | 138 | | public void Dispose() |
| | 139 | | { |
| 401 | 140 | | Abort(); |
| | 141 | |
|
| 401 | 142 | | if (webRequest == null) |
| 70 | 143 | | return; |
| | 144 | |
|
| 331 | 145 | | webRequest.Dispose(); |
| 331 | 146 | | webRequest = null; |
| 331 | 147 | | } |
| | 148 | |
|
| | 149 | | /// <summary> |
| | 150 | | /// Returns the data that has been downloaded as a byte array, if not done or success it will return an empty ar |
| | 151 | | /// </summary> |
| | 152 | | public byte[] GetResultData() |
| | 153 | | { |
| 0 | 154 | | return webRequest.downloadHandler.data; |
| | 155 | | } |
| | 156 | |
|
| 0 | 157 | | internal void SetNewWebRequest(UnityWebRequest newRequest) { webRequest = newRequest; } |
| | 158 | | public UnityWebRequestAsyncOperation SendWebRequest() |
| | 159 | | { |
| 341 | 160 | | asyncOp = webRequest.SendWebRequest(); |
| 341 | 161 | | return asyncOp; |
| | 162 | | } |
| | 163 | | } |
| | 164 | | } |