123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using COSXML.Model;
- using COSXML.Common;
- using COSXML.Model.Object;
- using COSXML.CosException;
- using System.Threading.Tasks;
- namespace COSXML.Transfer
- {
- public abstract class COSXMLTask
- {
- public COSXML.Callback.OnProgressCallback progressCallback;
- public COSXML.Callback.OnSuccessCallback<CosResult> successCallback;
- public COSXML.Callback.OnFailedCallback failCallback;
- public OnInternalHandleBeforExcute onInternalHandle;
- public OnState onState;
- public bool throwExceptionIfCancelled = false;
- protected CosXml cosXmlServer;
- protected string bucket;
- protected string region;
- protected string key;
- protected Dictionary<string, string> customHeaders;
- protected TaskState taskState;
- protected Object syncTaskState = new Object();
- public void InitCosXmlServer(CosXml cosXml)
- {
- cosXmlServer = cosXml;
- if (this.region == null)
- {
- this.region = cosXml.GetConfig().Region;
- }
- }
- public COSXMLTask(string bucket, string region, string key)
- {
- this.bucket = bucket;
- this.region = region;
- this.key = key;
- }
- public COSXMLTask(string bucket, string key)
- {
- this.bucket = bucket;
- this.key = key;
- }
- public abstract void Pause();
- public abstract void Cancel();
- public abstract void Resume();
- public TaskState State()
- {
- return taskState;
- }
- protected void SetHeaders(Dictionary<string, string> headers)
- {
- this.customHeaders = headers;
- }
- protected bool UpdateTaskState(TaskState newTaskState)
- {
- bool result = false;
- lock (syncTaskState)
- {
- switch (newTaskState)
- {
- case TaskState.Waiting:
- taskState = newTaskState;
- result = true;
- break;
- case TaskState.Running:
- if (taskState == TaskState.Waiting)
- {
- taskState = newTaskState;
- result = true;
- }
- break;
- case TaskState.Completed:
- if (taskState == TaskState.Running)
- {
- taskState = newTaskState;
- result = true;
- }
- break;
- case TaskState.Failed:
- if (taskState == TaskState.Waiting || taskState == TaskState.Running)
- {
- taskState = newTaskState;
- result = true;
- }
- break;
- case TaskState.Pause:
- if (taskState == TaskState.Waiting || taskState == TaskState.Running)
- {
- taskState = newTaskState;
- result = true;
- }
- break;
- case TaskState.Cancel:
- if (taskState != TaskState.Completed || taskState != TaskState.Cancel)
- {
- taskState = newTaskState;
- result = true;
- }
- break;
- case TaskState.Resume:
- if (taskState == TaskState.Pause || taskState == TaskState.Failed)
- {
- taskState = newTaskState;
- result = true;
- }
- break;
- }
- }
- if (result && onState != null)
- {
- onState(taskState);
- }
- return result;
- }
- /// <summary>
- /// 等待任务
- /// </summary>
- /// <param name="task"></param>
- /// <typeparam name="T"></typeparam>
- /// <returns></returns>
- public Task<T> AsyncTask<T>() where T : CosResult
- {
- return NewTaskCompletion<T>().Task;
- }
- private TaskCompletionSource<T> NewTaskCompletion<T>() where T : CosResult
- {
- var t = new TaskCompletionSource<T>();
- successCallback = delegate (CosResult cosResult)
- {
- t.TrySetResult(cosResult as T);
- };
- failCallback = delegate (CosClientException clientException, CosServerException serverException)
- {
- if (clientException != null)
- {
- t.TrySetException(clientException);
- }
- else
- {
- t.TrySetException(serverException);
- }
- };
- return t;
- }
- }
- internal class SliceStruct
- {
- public int partNumber;
- public bool isAlreadyUpload;
- public long sliceStart;
- public long sliceEnd;
- public long sliceLength;
- public string eTag;
- }
- public class DownloadSliceStruct
- {
- public int partNumber;
- public long sliceStart;
- public long sliceEnd;
- public string eTag;
- }
- public enum TaskState
- {
- Waiting = 0,
- Running,
- Completed,
- Failed,
- Cancel,
- Pause,
- Resume,
- }
- public delegate void OnState(TaskState taskState);
- public delegate void OnInternalHandleBeforExcute(CosRequest cosRequest);
- internal interface IOnMultipartUploadStateListener
- {
- void OnInit();
- void OnPart();
- void OnCompleted(CompleteMultipartUploadResult result);
- void OnFailed(CosClientException clientEx, CosServerException serverEx);
- }
- }
|