Exemplo n.º 1
0
 public byte[] OutItemchache(string taskId)
 {
     _customSemphore[taskId].WaitOne();
     //手动抛出异常,结束过本次任务
     ChacheManager.Instence().ThrowException(taskId, ExceptionEnum.STOPEX.ToString());
     byte[] bytes = null;
     if (_itemTaskChache.ContainsKey(taskId))
     {
         bool result = _itemTaskChache[taskId].TryDequeue(out bytes);
     }
     try
     {
         _productSemaphore[taskId].Release();
     }
     catch (Exception ex)
     {
         Log.WriteLog("Release ProductSemEx:" + ex.Message);
     }
     return(bytes);
 }
Exemplo n.º 2
0
        public void InputItemTaskChache(string taskId, byte[] tmpbytes, int len, UploadInfo uploadInfo)
        {
            //手动抛出异常,结束过本次任务
            ChacheManager.Instence().ThrowException(taskId, ExceptionEnum.STOPEX.ToString());

            int  blockSize     = uploadInfo.BlockSize;
            long fileSize      = uploadInfo.FileSize;
            int  NUMBLOCKS     = uploadInfo.NumBlocks;
            int  LastBlockSize = uploadInfo.LastBlockSize;

            //构建块
            try
            {
                if (!_chache.ContainsKey(taskId))
                {
                    byte[] _chacheBytes = new byte[blockSize];
                    _chache.Add(taskId, _chacheBytes);
                    _offset.Add(taskId, 0);
                    _numBlocks.Add(taskId, 0);
                }

                if (_offset.ContainsKey(taskId))
                {
                    //剩余的字节
                    int needSize = blockSize - _offset[taskId];
                    if (len < needSize)
                    {
                        Array.Copy(tmpbytes, 0, _chache[taskId], _offset[taskId], len);
                        _offset[taskId] += len;
                    }
                    else
                    {
                        int leftSize = len - needSize;
                        //第一块
                        Array.Copy(tmpbytes, 0, _chache[taskId], _offset[taskId], needSize);
                        internalEnqueue(taskId, _chache[taskId], blockSize);

                        _chache[taskId] = new byte[blockSize];
                        _offset[taskId] = 0;

                        //剩余的整块数
                        int countBlock = leftSize / blockSize;
                        int fanilSize  = leftSize % blockSize;
                        for (int i = 0; i < countBlock; i++)
                        {
                            //多余的块
                            byte[] newBlockBytes = new byte[blockSize];
                            Array.Copy(tmpbytes, needSize + i * blockSize, newBlockBytes, 0, blockSize);
                            internalEnqueue(taskId, newBlockBytes, blockSize);
                        }
                        //最后剩余的块
                        int newoffset = needSize + countBlock * blockSize;
                        Array.Copy(tmpbytes, newoffset, _chache[taskId], 0, leftSize - countBlock * blockSize);
                        _offset[taskId] = len - newoffset;
                    }

                    //如果是最后一块
                    if (_numBlocks.ContainsKey(taskId) && _numBlocks[taskId] == NUMBLOCKS - 1)
                    {
                        Log.WriteLog("last block-------------" + "m:" + _offset[taskId] + "n:" + LastBlockSize);
                        if (_offset[taskId] == LastBlockSize)
                        {
                            Log.WriteLog("finished-------------");
                            byte[] newBytes = new byte[LastBlockSize];
                            Array.Copy(_chache[taskId], newBytes, LastBlockSize);
                            internalEnqueue(taskId, newBytes, LastBlockSize);
                            _chache.Remove(taskId);
                            _offset.Remove(taskId);
                            _numBlocks.Remove(taskId);
                            Log.WriteLog("download finished .....");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WriteLog("build block:" + ex.ToString());
            }
        }