public void GetBlock(byte[] buffer, int pieceIndex, int offset, int length, BlockReadDelegate callback, object state) { try { IEnumerable <BlockPartInfo> parts = GetParts(pieceIndex, offset, length, false); var block = new Block(buffer, pieceIndex, offset, length); BlockReadState data = readCache.Get().Init(block, callback, length, state); if (parts.Count() > 1) { Trace.WriteLine(pieceIndex + " is split between " + parts.Count() + " files."); } foreach (BlockPartInfo 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); } }
public void AddBlock(Block block, BlockWrittenDelegate callback, object state) { try { IEnumerable <BlockPartInfo> parts = GetParts(block.Info.Index, block.Info.Offset, block.Info.Length, true); var totalLen = parts.Sum(p => p.Length); BlockWriteState data = writeCache.Get().Init(callback, (int)totalLen, block, state); Trace.Assert(parts.Any()); foreach (BlockPartInfo 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); } }