您的位置:首页 > 其它

基于libtorrent最简单的BT下载程序

2010-04-27 11:57 393 查看
/*****
libtorrent 测试程序
2010-04-27  aya 创建
*****/
#include <iostream>
#include <fstream>
#include <torrent/torrent.h>
#include <torrent/poll_epoll.h>
#include <torrent/rate.h>
#include <torrent/throttle.h>
#include <torrent/object.h>
#include <torrent/object_stream.h>
#include <torrent/http.h>
#include <torrent/connection_manager.h>
#include <rak/timer.h>
#include <torrent/download_info.h>
#include <signal.h>
#include <sigc++/bind.h>
#include <sigc++/hide.h>
#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 <<"chunk passed" << std::endl;
}

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 <<"程序启动 libtorrent 版本 "<<version ()<<std::endl;
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 <<"打开监听端口失败/n"<<std::endl;
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 <<"创建下载对象失败/n"<<std::endl;
tryCleanup ();
return 0;
}

/** 根据torrent 对象创建一个下载对象 **/
download = createDownloadObject (object);
if (!download) {
std::cout <<"创建下载对象失败/n"<<std::endl;
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 <<"开始下载.../n"<<std::endl;
while (1) {
perform();
rak::timer
timeout = next_timeout() + 1000;
static_cast<PollEPoll*>(poll)->poll (timeout.usec()/1000);
curlStack.perform ();
torrent::perform  ();
static_cast<PollEPoll*>(poll)->perform ();
if (1024 > down_rate()->rate()) {
if (1024 > (download->bytes_done() / 1024)) {
std::cout <<"[已下载 "<<download->bytes_done() / 1024<<"KB]"
<<"[down "<<down_rate()->rate()<<"B/s]"<<"[up "<<up_rate()->rate()<<"B/s]"<<std::endl;
} else {
std::cout <<"[已下载 "<<download->bytes_done() / (1024 * 1024)<<"MB]"
<<"[down "<<down_rate()->rate()<<"B/s]"<<"[up "<<up_rate()->rate()<<"B/s]"<<std::endl;
}
} else {
if (1024 > (download->bytes_done() / 1024)) {
std::cout <<"[已下载 "<<download->bytes_done() / 1024<<"kB]"
<<"[down "<<down_rate()->rate()/1024<<"kB/s]"<<"[up "<<up_rate()->rate()/1024<<"kB/s]"<<std::endl;
} else {
std::cout <<"[已下载 "<<download->bytes_done() / (1024 * 1024)<<"MB]"
<<"[down "<<down_rate()->rate()/1024<<"kB/s]"<<"[up "<<up_rate()->rate()/1024<<"kB/s]"<<std::endl;
}
}
}
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<<"程序退出"<<std::endl;
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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: