Esempio n. 1
0
        public void GetBlock(byte[] buffer, int pieceIndex, int offset, int length, BlockReadDelegate callback,
                             object state)
        {
            try
            {
                var parts = GetParts(pieceIndex, offset, length, false);
                var block = new Block(buffer, pieceIndex, offset, length);
                var data  = _readCache.Get().Init(block, callback, length, state);
                if (parts.Count() > 1)
                {
                    Console.WriteLine(pieceIndex + " is split between " + parts.Count() + " files.");
                }

                foreach (var part in parts)
                {
                    if (part.FileStream != null)
                    {
                        DiskIo.QueueRead(part.FileStream, buffer, part.DataOffset, part.FileOffset, part.Length,
                                         EndGetBlock, data);
                    }
                    else
                    {
                        OnRaisedException(new TorrentException("Stream is null."));
                        callback(false, null, state);
                        return;
                    }
                }
            }
            catch (Exception e)
            {
                OnRaisedException(new TorrentException(e.Message, e));
                callback(false, null, state);
            }
        }
Esempio n. 2
0
 public void AddBlock(Block block, BlockWrittenDelegate callback, object state)
 {
     try
     {
         var parts    = GetParts(block.Info.Index, block.Info.Offset, block.Info.Length, true);
         var totalLen = parts.Sum(p => p.Length);
         var data     = _writeCache.Get().Init(callback, (int)totalLen, block, state);
         //Console.WriteLine(parts.Any());
         foreach (var part in parts)
         {
             DiskIo.QueueWrite(part.FileStream, block.Data, part.FileOffset, part.DataOffset, part.Length,
                               EndAddBlock, data);
         }
     }
     catch (Exception e)
     {
         OnRaisedException(new TorrentException(e.Message, e));
         callback(false, state);
     }
 }