基于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); }
相关文章推荐
- 简单的基于libcurl和c++11 thread多线程的多线程下载程序
- Java 基于socket thread 简单聊天程序
- 基于linux socket 简单学生信息管理系统server程序
- 基于visual c++之windows核心编程代码分析 自动下载更新程序
- 数据采集:完美下载淘宝Ip数据库 简单的程序节省60元人民币而不必购买数据库
- 基于UDP协议的简单基本视频传输程序的编写
- C++基于TCP/IP简单的客户端、服务器通信程序实例
- 基于UDP协议的简单聊天程序
- 基于PHP实现简单的随机抽奖小程序
- Ruby程序中发送基于HTTP协议的请求的简单示例
- 一个简单的下载程序VB.NET示例
- 简单的爬取并下载图片的程序
- 抛弃框架:基于MVC模式的CRUD和Servlet(最简单的JavaWeb程序)
- 半夜听到黄家驹的声音, 忽醒,一身冷汗,哦,原来是计算机忘了关,还在播放Beyond演唱会.于是做了个较简单的自动关机程序(代码下载)
- 半夜听到黄家驹的声音, 忽醒,一身冷汗,哦,原来是计算机忘了关,还在播放Beyond演唱会.于是做了个较简单的自动关机程序(代码下载)
- 基于TCP/IP的简单的聊天程序
- U-Boot使用loadb下载程序(附测试代码) --基于Linux下Kermit工具
- 模拟一个简单的基于tcp的远程关机程序
- 树莓派用Python写几个简单程序3_UART_BT-HM10
- C# 一个简单分词程序的思路和代码(六) 源代码 ,测试程序,词库下载地址