本文整理汇总了C++中libtorrent::session::listen_on方法的典型用法代码示例。如果您正苦于以下问题:C++ session::listen_on方法的具体用法?C++ session::listen_on怎么用?C++ session::listen_on使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类libtorrent::session
的用法示例。
在下文中一共展示了session::listen_on方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
//-----------------------------------------------------------------------------
JNIEXPORT jboolean JNICALL Java_com_softwarrior_libtorrent_LibTorrent_SetSession
(JNIEnv *env, jobject obj, jint ListenPort, jint UploadLimit, jint DownloadLimit)
{
jboolean result = JNI_FALSE;
try{
gSession.set_alert_mask(libtorrent::alert::all_categories
& ~(libtorrent::alert::dht_notification
+ libtorrent::alert::progress_notification
+ libtorrent::alert::debug_notification
+ libtorrent::alert::stats_notification));
int listenPort = 54321;
if(ListenPort > 0)
listenPort = ListenPort;
gSession.listen_on(std::make_pair(listenPort, listenPort+10));
int uploadLimit = UploadLimit;
if(uploadLimit > 0){
gSession.set_upload_rate_limit(uploadLimit * 1000);
}
else{
gSession.set_upload_rate_limit(0);
}
int downloadLimit = DownloadLimit;
if(downloadLimit > 0){
gSession.set_download_rate_limit(downloadLimit * 1000);
}
else{
gSession.set_download_rate_limit(0);
}
libtorrent::session_settings sets = gSession.settings();
sets.announce_to_all_trackers = true;
sets.announce_to_all_tiers = true;
sets.prefer_udp_trackers = false;
sets.max_peerlist_size = 0;
gSession.set_settings(sets);
LOG_INFO("ListenPort: %d\n", listenPort);
LOG_INFO("DownloadLimit: %d\n", downloadLimit);
LOG_INFO("UploadLimit: %d\n", uploadLimit);
gSessionState=true;
}catch(...){
LOG_ERR("Exception: failed to set session");
gSessionState=false;
}
if(!gSessionState) LOG_ERR("LibTorrent.SetSession SessionState==false");
gSessionState==true ? result=JNI_TRUE : result=JNI_FALSE;
return result;
}
示例2: main
int main(int argc, char* argv[])
{
if (argc == 1) {
printhelp(argv[0]);
}
int tmp;
char torrentfile[255];
int usrpid = 0;
int mypid;
char pidfilename[255];
char ip[16];
int delay = 10;
strcpy(ip, "0.0.0.0");
strcpy(pidfilename, argv[0]);
strcat(pidfilename, ".pid");
while((tmp=getopt(argc,argv,"ht:p:f:b:d:"))!=-1) {
switch(tmp) {
case 'h':
printhelp(argv[0]);
break;
case 't':
strcpy(torrentfile, optarg);
break;
case 'p':
usrpid = atoi(optarg);
break;
case 'f':
strcpy(pidfilename, optarg);
break;
case 'b':
strcpy(ip, optarg);
break;
case 'd':
delay = atoi(optarg);
break;
default:
printhelp(argv[0]);
break;
}
}
if (usrpid == 0) {
printhelp(argv[0]);
}
mypid = ::getpid();
std::signal(SIGINT, exit_signalHandler);
std::signal(SIGTERM, exit_signalHandler);
using namespace libtorrent;
// set peer_id to nodename
char hostname[HOST_NAME_MAX];
gethostname(hostname, HOST_NAME_MAX);
char buff[21];
snprintf(buff, sizeof(buff), "%20s", hostname);
peer_id my_peer_id = sha1_hash(buff);
s.set_peer_id(my_peer_id);
error_code ec;
// set up torrent session
s.listen_on(std::make_pair(6881, 6889), ec, ip);
if (ec)
{
fprintf(stderr, "failed to open listen socket: %s\n", ec.message().c_str());
return 1;
}
// create torrent object
add_torrent_params p;
p.save_path = "./";
p.ti = new torrent_info(torrentfile, ec);
if (ec)
{
fprintf(stderr, "%s\n", ec.message().c_str());
return 1;
}
// start downloading torrent
torrent_handle torrent = s.add_torrent(p, ec);
if (ec)
{
fprintf(stderr, "%s\n", ec.message().c_str());
return 1;
}
// create pidfile
std::ofstream pidfile;
pidfile.open(pidfilename);
pidfile << mypid;
pidfile << "\n";
pidfile.close();
std::vector<torrent_status> vts;
s.get_torrent_status(&vts, &yes, 0);
torrent_status& st = vts[0];
boost::int64_t remains = st.total_wanted - st.total_wanted_done;
fprintf(stdout, "Remains: %i\n", remains);
//fprintf(stdout, "Torrent: %i\n", torrent);
while( remains > 0 && run ) {
//.........这里部分代码省略.........