Example #1
0
        private void ComputeSliceNums()
        {
            int count = (int)(sendContentLength / sliceSize);

            sliceList = new List <SliceStruct>(count > 0 ? count : 1);
            // partNumber >= 1
            // partNumber >= 1
            int i = 1;

            for (; i < count; i++)
            {
                SliceStruct sliceStruct = new SliceStruct();

                sliceStruct.partNumber      = i;
                sliceStruct.isAlreadyUpload = false;
                sliceStruct.sliceStart      = sendOffset + (i - 1) * sliceSize;
                sliceStruct.sliceLength     = sliceSize;
                sliceStruct.sliceEnd        = sendOffset + i * sliceSize - 1;
                sliceList.Add(sliceStruct);
            }

            SliceStruct lastSliceStruct = new SliceStruct();

            lastSliceStruct.partNumber      = i;
            lastSliceStruct.isAlreadyUpload = false;
            lastSliceStruct.sliceStart      = sendOffset + (i - 1) * sliceSize;
            lastSliceStruct.sliceLength     = sendContentLength - (i - 1) * sliceSize;
            lastSliceStruct.sliceEnd        = sendOffset + sendContentLength - 1;
            sliceList.Add(lastSliceStruct);
        }
        private void ComputeSliceNums()
        {
            int count = (int)(sourceSize / sliceSize);

            sliceList = new List <SliceStruct>(count > 0 ? count : 1);
            int i = 1;


            for (; i < count; i++)
            {
                SliceStruct sliceStruct = new SliceStruct();

                sliceStruct.partNumber      = i;
                sliceStruct.isAlreadyUpload = false;
                sliceStruct.sliceStart      = (i - 1) * sliceSize;
                sliceStruct.sliceEnd        = i * sliceSize - 1;
                sliceList.Add(sliceStruct);
            }

            SliceStruct lastSliceStruct = new SliceStruct();

            lastSliceStruct.partNumber      = i;
            lastSliceStruct.isAlreadyUpload = false;
            lastSliceStruct.sliceStart      = (i - 1) * sliceSize;
            lastSliceStruct.sliceEnd        = sourceSize - 1;
            sliceList.Add(lastSliceStruct);
        }
Example #3
0
        private void UpdateSliceNums(ListPartsResult listPartsResult)
        {
            try
            {
                if (listPartsResult.listParts.parts != null)
                {
                    //获取原来的parts并提取partNumber
                    Dictionary <int, SliceStruct> sourceParts = new Dictionary <int, SliceStruct>(sliceList.Count);

                    foreach (SliceStruct sliceStruct in sliceList)
                    {
                        sourceParts.Add(sliceStruct.partNumber, sliceStruct);
                    }

                    foreach (ListParts.Part part in listPartsResult.listParts.parts)
                    {
                        int partNumber = -1;

                        bool parse = int.TryParse(part.partNumber, out partNumber);

                        if (!parse)
                        {
                            throw new ArgumentException("ListParts.Part parse error");
                        }

                        SliceStruct sliceStruct = sourceParts[partNumber];

                        sliceStruct.isAlreadyUpload = true;
                        sliceStruct.eTag            = part.eTag;
                        lock (syncProgress)
                        {
                            long size = 0L;

                            long.TryParse(part.size, out size);
                            hasReceiveDataLength += size;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                lock (syncExit)
                {
                    if (isExit)
                    {
                        return;
                    }
                }

                if (UpdateTaskState(TaskState.Failed))
                {
                    OnFailed(new CosClientException((int)CosClientError.InternalError, ex.Message, ex), null);
                }
            }
        }
Example #4
0
        private void UploadPart()
        {
            activieTasks = 0;
            int size = sliceList.Count;

            sliceCount            = size;
            uploadPartRequestMap  = new Dictionary <UploadPartRequest, long>(size);
            uploadPartRequestList = new List <UploadPartRequest>(size);

            AutoResetEvent resetEvent = new AutoResetEvent(false);


            for (int i = 0; i < size; i++)
            {
                if (activieTasks > MaxConcurrent)
                {
                    resetEvent.WaitOne();
                }

                lock (syncExit)
                {
                    if (isExit)
                    {
                        return;
                    }
                }

                SliceStruct sliceStruct = sliceList[i];

                if (!sliceStruct.isAlreadyUpload)
                {
                    UploadPartRequest uploadPartRequest = new UploadPartRequest(bucket, key, sliceStruct.partNumber, uploadId, srcPath,
                                                                                sliceStruct.sliceStart, sliceStruct.sliceLength);

                    if (customHeaders != null && customHeaders.ContainsKey(CosRequestHeaderKey.X_COS_TRAFFIC_LIMIT))
                    {
                        string trafficLimit = customHeaders[CosRequestHeaderKey.X_COS_TRAFFIC_LIMIT];

                        uploadPartRequest.LimitTraffic(Convert.ToInt64(trafficLimit));
                    }

                    //打印进度
                    uploadPartRequest.SetCosProgressCallback(
                        delegate(long completed, long total)
                    {
                        lock (syncProgress)
                        {
                            long dataLen = hasReceiveDataLength + completed - uploadPartRequestMap[uploadPartRequest];

                            UpdateProgress(dataLen, sendContentLength, false);
                            hasReceiveDataLength = dataLen;
                            uploadPartRequestMap[uploadPartRequest] = completed;
                        }
                    }
                        );

                    uploadPartRequestMap.Add(uploadPartRequest, 0);
                    uploadPartRequestList.Add(uploadPartRequest);


                    Interlocked.Increment(ref activieTasks);

                    cosXmlServer.UploadPart(uploadPartRequest, delegate(CosResult result)
                    {
                        Interlocked.Decrement(ref activieTasks);
                        UploadPartResult uploadPartResult = result as UploadPartResult;

                        sliceStruct.eTag = uploadPartResult.eTag;
                        lock (syncPartCopyCount)
                        {
                            sliceCount--;

                            if (sliceCount == 0)
                            {
                                OnPart();
                            }
                        }

                        resetEvent.Set();
                    }, delegate(CosClientException clientEx, CosServerException serverEx)
                    {
                        Interlocked.Decrement(ref activieTasks);

                        if (UpdateTaskState(TaskState.Failed))
                        {
                            OnFailed(clientEx, serverEx);
                        }

                        resetEvent.Set();
                    });
                }
                else
                {
                    lock (syncPartCopyCount)
                    {
                        sliceCount--;

                        if (sliceCount == 0)
                        {
                            OnPart();

                            return;
                        }
                    }
                }
            }
        }
        private void PartCopy()
        {
            int size = sliceList.Count;

            sliceCount = size;
            uploadCopyCopyRequestList = new List <UploadPartCopyRequest>(size);
            for (int i = 0; i < size; i++)
            {
                if (isExit)
                {
                    return;
                }
                SliceStruct sliceStruct = sliceList[i];
                if (!sliceStruct.isAlreadyUpload)
                {
                    UploadPartCopyRequest uploadPartCopyRequest = new UploadPartCopyRequest(bucket, key, sliceStruct.partNumber, uploadId);
                    uploadPartCopyRequest.SetCopySource(copySource);
                    uploadPartCopyRequest.SetCopyRange(sliceStruct.sliceStart, sliceStruct.sliceEnd);
                    uploadCopyCopyRequestList.Add(uploadPartCopyRequest);
                    cosXmlServer.PartCopy(uploadPartCopyRequest, delegate(CosResult result)
                    {
                        lock (syncExit)
                        {
                            if (isExit)
                            {
                                return;
                            }
                        }
                        UploadPartCopyResult uploadPartCopyResult = result as UploadPartCopyResult;
                        sliceStruct.eTag = uploadPartCopyResult.copyObject.eTag;
                        lock (syncPartCopyCount)
                        {
                            sliceCount--;
                            if (sliceCount == 0)
                            {
                                OnPart();
                                return;
                            }
                        }
                    }, delegate(CosClientException clientEx, CosServerException serverEx)
                    {
                        lock (syncExit)
                        {
                            if (isExit)
                            {
                                return;
                            }
                        }
                        if (UpdateTaskState(TaskState.FAILED))
                        {
                            OnFailed(clientEx, serverEx);
                        }
                        return;
                    });
                }
                else
                {
                    lock (syncPartCopyCount)
                    {
                        sliceCount--;
                        if (sliceCount == 0)
                        {
                            OnPart();
                            return;
                        }
                    }
                }
            }
        }
        private void UploadPart()
        {
            activieTasks = 0;
            int size = sliceList.Count;

            sliceCount            = size;
            uploadPartRequestMap  = new Dictionary <UploadPartRequest, long>(size);
            uploadPartRequestList = new List <UploadPartRequest>(size);
            for (int i = 0; i < size; i++)
            {
                lock (syncExit)
                {
                    if (isExit)
                    {
                        return;
                    }
                }
                SliceStruct sliceStruct = sliceList[i];
                while (activieTasks > MAX_ACTIVIE_TASKS)
                {
                    Thread.Sleep(5);
                }
                if (!sliceStruct.isAlreadyUpload)
                {
                    UploadPartRequest uploadPartRequest = new UploadPartRequest(bucket, key, sliceStruct.partNumber, uploadId, srcPath, sliceStruct.sliceStart,
                                                                                sliceStruct.sliceLength);

                    //打印进度
                    uploadPartRequest.SetCosProgressCallback(delegate(long completed, long total)
                    {
                        lock (syncProgress)
                        {
                            long dataLen = hasReceiveDataLength + completed - uploadPartRequestMap[uploadPartRequest];
                            UpdateProgress(dataLen, sendContentLength, false);
                            hasReceiveDataLength = dataLen;
                            uploadPartRequestMap[uploadPartRequest] = completed;
                        }
                    });

                    uploadPartRequestMap.Add(uploadPartRequest, 0);
                    uploadPartRequestList.Add(uploadPartRequest);


                    activieTasks++;

                    cosXmlServer.UploadPart(uploadPartRequest, delegate(CosResult result)
                    {
                        lock (syncExit)
                        {
                            if (isExit)
                            {
                                return;
                            }
                        }
                        activieTasks--;
                        UploadPartResult uploadPartResult = result as UploadPartResult;
                        sliceStruct.eTag = uploadPartResult.eTag;
                        lock (syncPartCopyCount)
                        {
                            sliceCount--;
                            if (sliceCount == 0)
                            {
                                OnPart();
                                return;
                            }
                        }
                    }, delegate(CosClientException clientEx, CosServerException serverEx)
                    {
                        lock (syncExit)
                        {
                            if (isExit)
                            {
                                return;
                            }
                        }
                        activieTasks--;
                        if (UpdateTaskState(TaskState.FAILED))
                        {
                            OnFailed(clientEx, serverEx);
                        }
                        return;
                    });
                }
                else
                {
                    lock (syncPartCopyCount)
                    {
                        sliceCount--;
                        if (sliceCount == 0)
                        {
                            OnPart();
                            return;
                        }
                    }
                }
            }
        }