Beispiel #1
0
 /// <summary>
 /// 读取或写入区块
 /// </summary>
 /// <param name="blockIndex">区块序号</param>
 public FileBlock this[int blockIndex]
 {
     get
     {
         FileBlock output;
         if (EnabledIoBuffer)
         {
             bool isInBuf;
             lock (IoBuffer)
                 isInBuf = IoBuffer.TryGetValue(blockIndex, out output);
             if (isInBuf)
             {
                 return(output);
             }
             output = new FileBlock(Task, blockIndex, true);
             BeginFillIoBuffer(blockIndex + 1, null, null);
         }
         else
         {
             output = new FileBlock(Task, blockIndex, true);
         }
         return(output);
     }
     set
     {
         if (blockIndex != value.Index)
         {
             throw new FileBlockException("Bad Index!", FileBlockException.ErrorCode.BadIndex);
         }
         Write(value);
     }
 }
Beispiel #2
0
        /// <summary>
        /// 校验文件
        /// </summary>
        /// <returns>损坏或尚未下载的区块序号列表</returns>
        public List <int> HashFile()
        {
            FileStream.Position = 0;
            ExistBlock.Clear();
            for (int count = 0; FileStream.Position < FileStream.Length && count < TotalBlock; count++)
            {
//校验已存在的区块
                var testBlock = new FileBlock(this, count, true);
                SendString(string.Format("GET BlockHash {0}", count));
                string[] msg = ReceiveString().Split(' ');
                if (msg[0] == "BlockHash")
                {
                    if (Convert.ToInt32(msg[1]) == count)
                    {
                        if (BitConverter.ToInt32(testBlock.DataHash, 0) == Convert.ToInt32(msg[2]))
                        {
                            ExistBlock.Add(count);
                        }
                    }
                }
                if (BlockHashed != null)
                {
                    BlockHashed(this, new BlockFinishedEventArgs(count));
                }
            }
            int maxExistBlockIndex; //已存在的区块最大序号

            try
            {
                maxExistBlockIndex = ExistBlock.Max();
            }
            catch
            {
                maxExistBlockIndex = 0;
            }
            var blockRemaining = new List <int>();

            for (int index = 0; index < TotalBlock;)
            {
//计算仍需传输的区块
                if (index <= maxExistBlockIndex)
                {
                    if (ExistBlock.Exists(a => a == index))
                    {
                        index++;
                        continue;
                    }
                }
                blockRemaining.Add(index++);
            }
            return(blockRemaining);
        }
Beispiel #3
0
 /// <summary>
 /// 写入区块
 /// </summary>
 /// <param name="value">区块对象</param>
 public void Write(FileBlock value)
 {
     if (EnabledIoBuffer)
     {
         if (IoBuffer.Count >= IoBufferSize)
         {
             WriteAllBlock();
         }
         lock (IoBuffer)
             IoBuffer.Add(value.Index, value);
     }
     else
     {
         value.Write();
     }
 }