How would I go about reading bittorrent pieces?
I'm currently developing a torrent metainfo management library for Ruby.
I'm having trouble reading the pieces from the files. I just don't understand how I'm supposed to go about i开发者_开发知识库t. I know I'm supposed to SHA1 digest piece length bytes of a file once (or read piece length bytes multiple times, or what?)
I'm counting on your help. Pseudo / Python / Ruby / PHP code preferred.
Thanks in advance.
C#
// Open the file
using (var file = File.Open(...))
{
    // Move to the relevant place in the file where the piece begins
    file.Seek(piece * pieceLength, SeekOrigin.Begin);
    // Attempt to read up to pieceLength bytes from the file into a buffer
    byte[] buffer = new byte[pieceLength];
    int totalRead = 0;
    while (totalRead < pieceLength)
    {
        var read = stream.Read(buffer, totalRead, pieceLength-totalRead);
        if (read == 0)
        {
            // the piece is smaller than the pieceLength,
            // because it’s the last in the file
            Array.Resize(ref buffer, totalRead);
            break;
        }
        totalRead += read;
    }
    // If you want the raw data for the piece:
    return buffer;
    // If you want the SHA1 hashsum:
    return SHA1.Create().ComputeHash(buffer);
}
Please taker a look at this distribution here:
http://prdownload.berlios.de/torrentparse/TorrentParse.GTK.0.21.zip
Written in PHP, it contains an Encoder and Decoder and the in's and out I believe!
 加载中,请稍侯......
      
精彩评论