[英]How would I go about reading bittorrent pieces?
I'm currently developing a torrent metainfo management library for Ruby. 我目前正在为Ruby开发torrent元信息管理库。
I'm having trouble reading the pieces from the files. 我无法读取文件中的内容。 I just don't understand how I'm supposed to go about it. 我只是不明白我应该怎么做。 I know I'm supposed to SHA1 digest piece length bytes of a file once (or read piece length bytes multiple times, or what?) 我知道我应该一次对文件的片段长度字节进行SHA1提取(或者多次读取片段长度字节,或者什么?)
I'm counting on your help. 我指望您的帮助。 Pseudo / Python / Ruby / PHP code preferred. 首选Pseudo / Python / Ruby / PHP代码。
Thanks in advance. 提前致谢。
// 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 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! 用PHP编写,它包含一个Encoder和Decoder以及输入和输出,我相信!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.