private bool CheckPiece(int pieceIndex) { bool result; if (this._pieceCheckedReusltsDictionary.TryGetValue(pieceIndex, out result)) { return(result); } ulong startPos = (ulong)pieceIndex * (ulong)this._torrent.PieceLength; ulong pos = 0; ulong writenLength = 0; var filePieces = new List <FileLinkPiece>(); foreach (TorrentFileLink fileLink in this._torrentFileLinks) { if (pos + (ulong)fileLink.TorrentFile.Length >= startPos) { ulong readPos = startPos - pos; ulong readLength = (ulong)fileLink.TorrentFile.Length - readPos; if (writenLength + readLength > (ulong)this._torrent.PieceLength) { readLength = (ulong)this._torrent.PieceLength - writenLength; } var filePiece = new FileLinkPiece { FileLink = fileLink, ReadLength = readLength, StartPos = readPos }; filePieces.Add(filePiece); writenLength += readLength; startPos += readLength; if (writenLength == (ulong)this._torrent.PieceLength) { break; } } pos += (ulong)fileLink.TorrentFile.Length; } var hash = new HashFileLinkPieces(this._torrent, pieceIndex, filePieces); string pattern = hash.Run(); if (string.IsNullOrEmpty(pattern)) { foreach (FileLinkPiece piece in filePieces) { if (piece.FileLink.State == LinkState.NeedConfirm) { piece.FileLink.State = LinkState.Fail; } } this._pieceCheckedReusltsDictionary.Add(pieceIndex, false); return(false); } for (int i = 0; i < filePieces.Count; i++) { if (filePieces[i].FileLink.State == LinkState.NeedConfirm) { filePieces[i].FileLink.LinkedFsFileIndex = int.Parse(pattern.Split(',')[i]); filePieces[i].FileLink.LinkedFsFileInfo.Located = true; filePieces[i].FileLink.State = LinkState.Located; } } this._pieceCheckedReusltsDictionary.Add(pieceIndex, true); return(true); }
private bool CheckPiece(int pieceIndex) { bool result; if (this._pieceCheckedReusltsDictionary.TryGetValue(pieceIndex, out result)) { return result; } ulong startPos = (ulong)pieceIndex * (ulong)this._torrent.PieceLength; ulong pos = 0; ulong writenLength = 0; var filePieces = new List<FileLinkPiece>(); foreach (TorrentFileLink fileLink in this._torrentFileLinks) { if (pos + (ulong)fileLink.TorrentFile.Length >= startPos) { ulong readPos = startPos - pos; ulong readLength = (ulong)fileLink.TorrentFile.Length - readPos; if (writenLength + readLength > (ulong)this._torrent.PieceLength) { readLength = (ulong)this._torrent.PieceLength - writenLength; } var filePiece = new FileLinkPiece { FileLink = fileLink, ReadLength = readLength, StartPos = readPos }; filePieces.Add(filePiece); writenLength += readLength; startPos += readLength; if (writenLength == (ulong)this._torrent.PieceLength) { break; } } pos += (ulong)fileLink.TorrentFile.Length; } var hash = new HashFileLinkPieces(this._torrent, pieceIndex, filePieces); string pattern = hash.Run(); if (string.IsNullOrEmpty(pattern)) { foreach (FileLinkPiece piece in filePieces) { if (piece.FileLink.State == LinkState.NeedConfirm) { piece.FileLink.State = LinkState.Fail; } } this._pieceCheckedReusltsDictionary.Add(pieceIndex, false); return false; } for (int i = 0; i < filePieces.Count; i++) { if (filePieces[i].FileLink.State == LinkState.NeedConfirm) { filePieces[i].FileLink.LinkedFsFileIndex = int.Parse(pattern.Split(',')[i]); filePieces[i].FileLink.LinkedFsFileInfo.Located = true; filePieces[i].FileLink.State = LinkState.Located; } } this._pieceCheckedReusltsDictionary.Add(pieceIndex, true); return true; }