示例#1
0
 bool VerifyDownload(Block b)
 {
     if (b.content == null || b.content.LongLength != b.Size)
     {
         KnownPeerStore[b.Assignee].NumberOfTamperedBlocks += 1;
         return false;
     }
     foreach (long index in FileCheck[b.Position])
     {
         for (int i = 0; i < 2048; i++)
         {
             if (CheckFileAsByteArray[index + i] != b.content[index - b.StartAddress + i])
             {
                 KnownPeerStore[b.Assignee].NumberOfTamperedBlocks += 1;
                 return false;
             }
         }
     }
     KnownPeerStore[b.Assignee].NumberOfGoodBlocks += 1;
     KnownPeerStore[b.Assignee].NumberOfBytes += b.content.LongLength;
     KnownPeerStore[b.Assignee].LastSuccessfulTime = DateTime.UtcNow;
     return true;
 }
示例#2
0
        void FileIntegrityCheck(Block a)
        {
            int seed = 1;
            List<long> indices = new List<long>();
            using (RNGCryptoServiceProvider key = new RNGCryptoServiceProvider())
            {
                byte[] b = new byte[1];
                key.GetNonZeroBytes(b);
                Random rng = new Random(b[0]);
                b = new byte[rng.Next(5, 9)];//b can be 8 - 15 'bytes' (array length) long.
                for (int i = 0; i < a.Size; i += 40960)//40KB
                {
                    key.GetNonZeroBytes(b);
                    int mod = (a.Size - i >= 40960) ? 38913 : (int)a.Size - i - 2047;
                    //you can later modify this to select the last 5KB in the block.. duplicate downloads with the last byte possible
                    if (mod < 128) break; //So currently if last piece of block is less than 383 (255+128) dont download/check that part.
                    //e.g. if 5KB, 4864= 5*1024 - 256, max start index for a 256byte piece in a 5KB (5*1024) block; 4865 = 4864 + 1 to do mod so you can also get 4864; 4865= 5*1024-255
                    seed = Math.Abs(b.Aggregate(1, (x, y) => x * y)) % mod;
                    indices.Add(a.StartAddress + i + seed);
                }
            }
            FileCheck[a.Position] = indices.ToArray();

            //Code below not used because of the high possibility of error 429 (too many requests) from webserver. It is left because it will work in real life scenarios with actually different phones.
               /* while (this.InitiatorWorker.IsBusy)
            {
              //while waiting for the backgroundWorker to be free, inform the iniatiator and keep UI reponsive
                statusLbl.Text += "\nOne more block check download task pooled.";
                Application.DoEvents();
            } */
            /* or use this instead of above to search only if the chance is there:
            if (!InitiatorWorker.IsBusy)
            {
                InitiatorWorker.RunWorkerAsync(FileCheck[a.Position]);
                statusLbl.Text = "Block check download in background.";
            } */

            //so this is used instead in the simulation: a CheckFileAsByteArray which is the original file predownloaded to serve as a check. The reason is the Error 429 that will be received on trying to start so many requests to the server from the same application.
        }
示例#3
0
 void InitializeBlockList()
 {
     FileAsByteArray = new byte[FileSize];
     long div = FileSize / BlockSize;
     long count = (FileSize % BlockSize < BlockSize / 2) ? div : div + 1;
     BlockList = new Block[count];
     FileCheck = new long[count][];
     for (int i = 0; i < count-1; i++)
     {
         BlockList[i] = new Block();
         BlockList[i].BlockInit(DownloadLink, i, i * BlockSize, BlockSize);
         BlockList[i].BlockStatusChanged+=new EventHandler(Block_StatusChanged); //C#1.0 syntax
     }
     //could have checked from end in the for loop rather than doing this, but it's computationally wasteful.
     long rem =  (count - 1) * BlockSize;
     BlockList[count - 1] = new Block();
     BlockList[count - 1].BlockInit(DownloadLink,count - 1, rem, FileSize - rem);
     BlockList[count - 1].BlockStatusChanged += Block_StatusChanged; //C#2.0 syntax. equivalent to 1.0
 }
示例#4
0
文件: Peer.cs 项目: anuade/MScProject
 object[] DownloadBlock(Block block, BackgroundWorker worker, DoWorkEventArgs e)
 {
     object[] obj = new object[2];
     obj[0] = block.Position;
     if (worker.CancellationPending)
     {
         e.Cancel = true;
     }
     else
     {
         try
         {
             Thread.Sleep(2000);
             HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(block.Link);
             myHttpWebRequest.AddRange(block.StartAddress, block.StartAddress + block.Size - 1);
             myHttpWebRequest.Timeout = 30000;
             /*Debug.WriteLine("Call AddRange(50,150)");
             Debug.Write("Resulting Request Headers: ");
             Debug.WriteLine(myHttpWebRequest.Headers.ToString());*/
             using (HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse())
             /*Debug.Write("Resulting Response Headers: ");
             Debug.WriteLine(myHttpWebResponse.Headers.ToString());*/
             using (Stream streamResponse = myHttpWebResponse.GetResponseStream())
             using (MemoryStream ms = new MemoryStream())
             {
                 streamResponse.CopyTo(ms, 16384);
                 obj[1] = ms.ToArray();
                 myHttpWebResponse.Close();
             }
         }
         catch (Exception ex)
         {
             //notify peer of error
             Debug.WriteLine(ex.Message + " " + block.Position);
             //Thread.Sleep(2000);
             // e.Cancel = true;
             //MainForm.BlockList[block.Position].status = BlockStatus.NotDownloaded;
         }
     }
     return obj;
 }