COSXMLTask.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using COSXML.Model;
  5. using COSXML.Common;
  6. using COSXML.Model.Object;
  7. using COSXML.CosException;
  8. using System.Threading.Tasks;
  9. namespace COSXML.Transfer
  10. {
  11. public abstract class COSXMLTask
  12. {
  13. public COSXML.Callback.OnProgressCallback progressCallback;
  14. public COSXML.Callback.OnSuccessCallback<CosResult> successCallback;
  15. public COSXML.Callback.OnFailedCallback failCallback;
  16. public OnInternalHandleBeforExcute onInternalHandle;
  17. public OnState onState;
  18. public bool throwExceptionIfCancelled = false;
  19. protected CosXml cosXmlServer;
  20. protected string bucket;
  21. protected string region;
  22. protected string key;
  23. protected Dictionary<string, string> customHeaders;
  24. protected TaskState taskState;
  25. protected Object syncTaskState = new Object();
  26. public void InitCosXmlServer(CosXml cosXml)
  27. {
  28. cosXmlServer = cosXml;
  29. if (this.region == null)
  30. {
  31. this.region = cosXml.GetConfig().Region;
  32. }
  33. }
  34. public COSXMLTask(string bucket, string region, string key)
  35. {
  36. this.bucket = bucket;
  37. this.region = region;
  38. this.key = key;
  39. }
  40. public COSXMLTask(string bucket, string key)
  41. {
  42. this.bucket = bucket;
  43. this.key = key;
  44. }
  45. public abstract void Pause();
  46. public abstract void Cancel();
  47. public abstract void Resume();
  48. public TaskState State()
  49. {
  50. return taskState;
  51. }
  52. protected void SetHeaders(Dictionary<string, string> headers)
  53. {
  54. this.customHeaders = headers;
  55. }
  56. protected bool UpdateTaskState(TaskState newTaskState)
  57. {
  58. bool result = false;
  59. lock (syncTaskState)
  60. {
  61. switch (newTaskState)
  62. {
  63. case TaskState.Waiting:
  64. taskState = newTaskState;
  65. result = true;
  66. break;
  67. case TaskState.Running:
  68. if (taskState == TaskState.Waiting)
  69. {
  70. taskState = newTaskState;
  71. result = true;
  72. }
  73. break;
  74. case TaskState.Completed:
  75. if (taskState == TaskState.Running)
  76. {
  77. taskState = newTaskState;
  78. result = true;
  79. }
  80. break;
  81. case TaskState.Failed:
  82. if (taskState == TaskState.Waiting || taskState == TaskState.Running)
  83. {
  84. taskState = newTaskState;
  85. result = true;
  86. }
  87. break;
  88. case TaskState.Pause:
  89. if (taskState == TaskState.Waiting || taskState == TaskState.Running)
  90. {
  91. taskState = newTaskState;
  92. result = true;
  93. }
  94. break;
  95. case TaskState.Cancel:
  96. if (taskState != TaskState.Completed || taskState != TaskState.Cancel)
  97. {
  98. taskState = newTaskState;
  99. result = true;
  100. }
  101. break;
  102. case TaskState.Resume:
  103. if (taskState == TaskState.Pause || taskState == TaskState.Failed)
  104. {
  105. taskState = newTaskState;
  106. result = true;
  107. }
  108. break;
  109. }
  110. }
  111. if (result && onState != null)
  112. {
  113. onState(taskState);
  114. }
  115. return result;
  116. }
  117. /// <summary>
  118. /// 等待任务
  119. /// </summary>
  120. /// <param name="task"></param>
  121. /// <typeparam name="T"></typeparam>
  122. /// <returns></returns>
  123. public Task<T> AsyncTask<T>() where T : CosResult
  124. {
  125. return NewTaskCompletion<T>().Task;
  126. }
  127. private TaskCompletionSource<T> NewTaskCompletion<T>() where T : CosResult
  128. {
  129. var t = new TaskCompletionSource<T>();
  130. successCallback = delegate (CosResult cosResult)
  131. {
  132. t.TrySetResult(cosResult as T);
  133. };
  134. failCallback = delegate (CosClientException clientException, CosServerException serverException)
  135. {
  136. if (clientException != null)
  137. {
  138. t.TrySetException(clientException);
  139. }
  140. else
  141. {
  142. t.TrySetException(serverException);
  143. }
  144. };
  145. return t;
  146. }
  147. }
  148. internal class SliceStruct
  149. {
  150. public int partNumber;
  151. public bool isAlreadyUpload;
  152. public long sliceStart;
  153. public long sliceEnd;
  154. public long sliceLength;
  155. public string eTag;
  156. }
  157. public class DownloadSliceStruct
  158. {
  159. public int partNumber;
  160. public long sliceStart;
  161. public long sliceEnd;
  162. public string eTag;
  163. }
  164. public enum TaskState
  165. {
  166. Waiting = 0,
  167. Running,
  168. Completed,
  169. Failed,
  170. Cancel,
  171. Pause,
  172. Resume,
  173. }
  174. public delegate void OnState(TaskState taskState);
  175. public delegate void OnInternalHandleBeforExcute(CosRequest cosRequest);
  176. internal interface IOnMultipartUploadStateListener
  177. {
  178. void OnInit();
  179. void OnPart();
  180. void OnCompleted(CompleteMultipartUploadResult result);
  181. void OnFailed(CosClientException clientEx, CosServerException serverEx);
  182. }
  183. }