class torrent_info { public:
torrent_info(); torrent_info(sha1_hash const& info_hash); torrent_info(entry const& torrent_file); //从一个文件取读数据后,经过bdecode函数转换成entry,之再转输入到这里
entry create_torrent() const; //根据本对象现存的信息,每成一个entry对象
void set_comment(char const* str); //设置注释
void set_piece_size(int size); //设置piece大小
void set_creator(char const* str); //设置创建程序名称
void set_hash(int index, sha1_hash const& h); //设置某一piece的SHA-1 digest
void add_tracker(std::string const& url, int tier = 0); //添加tracker服务器,tier代表优先级,0最高
void add_file(boost::filesystem::path file, size_type size); //添加一个文件信息
void add_url_seed(std::string const& url); //添加一个url seed地址
typedef std::vector<file_entry>::const_iterator file_iterator; typedef std::vector<file_entry>::const_reverse_iterator reverse_file_iterator;
file_iterator begin_files() const; //返回文件信息的迭代器
file_iterator end_files() const; reverse_file_iterator rbegin_files() const; //返回文件信息的逆向迭代器
reverse_file_iterator rend_files() const;
int num_files() const; //返回文件数量
file_entry const& file_at(int index) const; //根据文件信息录入的序号返回文件信息
std::vector<file_slice> map_block(int piece, size_type offset , int size) const; //??
peer_request map_file(int file_index, size_type file_offset , int size) const; //??
std::vector<announce_entry> const& trackers() const; //返回tracker列表
std::vector<std::string> const& url_seeds() const; //返回url seed 列表
size_type total_size() const; //返回所有文件大小的总和
size_type piece_length() const; //返回piece长度
int num_pieces() const; //返回piece数量
sha1_hash const& info_hash() const; //返回.torrent的info_hash
std::string const& name() const; //返回.torrent的name
std::string const& comment() const; //返回注释
std::string const& creator() const; //返回创建程序名称
boost::optional<boost::posix_time::ptime> creation_date() const; //返回创建时间
void print(std::ostream& os) const; //输出所有信息到标准输出流
size_type piece_size(unsigned int index) const; //返回指定piece的大小,只有最后一个piece的大小不同于其它piece
sha1_hash const& hash_for_piece(unsigned int index) const; //返回指定piece的SHA-1 hash
};
要用代码说明torrent_info如何使用,最好的代码莫过于libtorrent的例子make_torrent.cpp
#include <iostream> #include <fstream> #include <iterator> #include <iomanip>
#include "libtorrent/entry.hpp" #include "libtorrent/bencode.hpp" #include "libtorrent/torrent_info.hpp" #include "libtorrent/file.hpp" #include "libtorrent/storage.hpp" #include "libtorrent/hasher.hpp" #include "libtorrent/file_pool.hpp"
#include <boost/filesystem/operations.hpp> #include <boost/filesystem/path.hpp> #include <boost/filesystem/fstream.hpp>
using namespace boost::filesystem; using namespace libtorrent;
void add_files( torrent_info& t , path const& p , path const& l) { if (l.leaf()[0] == '.') return; path f(p / l); if (is_directory(f)) { for (directory_iterator i(f), end; i != end; ++i) add_files(t, p, l / i->leaf()); } else { std::cerr << "adding \"" << l.string() << "\"\n"; t.add_file(l, file_size(f)); } }
int main(int argc, char* argv[]) { using namespace libtorrent; using namespace boost::filesystem;
path::default_name_check(no_check);
if (argc != 4 && argc != 5) {
return 1; }
try { torrent_info t; path full_path = complete(path(argv[3])); ofstream out(complete(path(argv[1])), std::ios_base::binary);
int piece_size = 256 * 1024; char const* creator_str = "libtorrent";
add_files(t, full_path.branch_path(), full_path.leaf()); t.set_piece_size(piece_size);
file_pool fp; storage st(t, full_path.branch_path(), fp); t.add_tracker(argv[2]);
// calculate the hash for all pieces
int num = t.num_pieces(); std::vector<char> buf(piece_size); for (int i = 0; i < num; ++i) { st.read(&buf[0], i, 0, t.piece_size(i)); hasher h(&buf[0], t.piece_size(i)); t.set_hash(i, h.final()); std::cerr << (i+1) << "/" << num << "\r"; }
t.set_creator(creator_str);
if (argc == 5) t.add_url_seed(argv[4]);
// create the torrent and print it to out
entry e = t.create_torrent(); libtorrent::bencode(std::ostream_iterator<char>(out), e); } catch (std::exception& e) { std::cerr << e.what() << "\n"; }
return 0; }
|