当前位置: 首页 > 工具软件 > LibTorrent > 使用案例 >

java libtorrent_基于libtorrent最简单的BT下载程序 | 学步园

汪典
2023-12-01

/*****

libtorrent 测试程序

2010-04-27 aya 创建

*****/

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include "curl_get.h"

#include "curl_stack.h"

using namespace torrent;

using namespace core;

Poll* poll;

Object* object = NULL;

Download* download = NULL;

Object* createObjectFromTorrentFile (char* torrenFilePath);

Download* createDownloadObject (Object* object);

void tryCleanup ();

void recvShutdown (int s);

void

chunk_passed (Download* d) {

std::cout <

}

void

finished_download (Download* d) {

d->stop();

connection_manager()->listen_close();

}

void

hash_check_done (Download* d) {

std::cout << "Hash check completed." << std::endl;

d->start();

chunk_passed(d);

}

int main (int argc, char** args)

{

core::CurlStack curlStack;

if (argc != 2) {

return 0;

}

std::cout <

signal (SIGINT, recvShutdown);

signal (SIGTERM, recvShutdown);

poll = PollEPoll::create (512); /** 创建poll,供libtorrent使用**/

CurlStack::global_init(); /** 初始化curlstack **/

Http::set_factory (curlStack.get_http_factory());

initialize (poll); /** 初始化 **/

/** 打开监听端口 **/

if (!connection_manager()->listen_open(10000, 14000)) {

std::cout <

tryCleanup ();

return 0;

}

down_throttle_global ()->set_max_rate (0);

up_throttle_global ()->set_max_rate (16 * 1024);

/** 创建一个torrent 对象 **/

object = createObjectFromTorrentFile (args[1]);

if (!object) {

std::cout <

tryCleanup ();

return 0;

}

/** 根据torrent 对象创建一个下载对象 **/

download = createDownloadObject (object);

if (!download) {

std::cout <

tryCleanup ();

return 0;

}

download->info()->signal_initial_hash().connect (sigc::bind(sigc::ptr_fun(&hash_check_done), download));

download->info()->signal_download_done().connect (sigc::bind(sigc::ptr_fun(&finished_download), download));

download->open ();

download->hash_check (false);

std::cout <

while (1) {

perform();

rak::timer

timeout = next_timeout() + 1000;

static_cast(poll)->poll (timeout.usec()/1000);

curlStack.perform ();

torrent::perform ();

static_cast(poll)->perform ();

if (1024 > down_rate()->rate()) {

if (1024 > (download->bytes_done() / 1024)) {

std::cout <bytes_done() / 1024<

<rate()<rate()<

} else {

std::cout <bytes_done() / (1024 * 1024)<

<rate()<rate()<

}

} else {

if (1024 > (download->bytes_done() / 1024)) {

std::cout <bytes_done() / 1024<

<rate()/1024<rate()/1024<

} else {

std::cout <bytes_done() / (1024 * 1024)<

<rate()/1024<rate()/1024<

}

}

}

return 0;

}

void tryCleanup ()

{

download->stop();

connection_manager()->listen_close();

cleanup ();

core::CurlStack::global_cleanup();

delete poll;

if (download) {

delete download;

}

}

void recvShutdown (int s)

{

std::cout<

tryCleanup ();

signal (s, NULL);

raise (s);

}

Object* createObjectFromTorrentFile (char* torrenFilePath)

{

Object* obj;

std::fstream stream (torrenFilePath, std::ios::in | std::ios::binary);

if (!stream.is_open()) {

return NULL;

}

obj = new Object;

stream >> *obj;

if (!stream.good()) {

delete obj;

return NULL;

}

return obj;

}

Download* createDownloadObject (Object* object)

{

Download d = download_add(object);

return new Download (d);

}

 类似资料: