Hello Libtorrent : 확장 토렌트 플러그인
4658 단어 libtorrentcppbittorrentp2p
경고 알림 메시지를 사용하기 위해 피어의 IP 주소를 얻는 방법을 설명했습니다.
그러나 libtorrent 확장을 사용하면 그렇게 할 수 있습니다.
이번에는 완전한 코드
#include <iostream>
#include <libtorrent/session.hpp>
#include <libtorrent/alert_types.hpp>
#include <libtorrent/alert.hpp>
#include <libtorrent/magnet_uri.hpp>
#include <libtorrent/error_code.hpp>
// ---
struct test_plugin_torrent : lt::torrent_plugin
{
void on_add_peer(lt::tcp::endpoint const &endpoint, lt::peer_source_flags_t flag1, lt::add_peer_flags_t flag2) override
{
std::cout << "[on_add_peer]" << endpoint.address().to_string() << ":" << endpoint.port() << std::endl;
}
void on_state(lt::torrent_status::state_t st)
{
std::cout << "[on_state]" << st << std::endl;
}
};
struct plugin_creator
{
std::shared_ptr<lt::torrent_plugin>
operator()(lt::torrent_handle const &, lt::client_data_t)
{
return std::make_shared<test_plugin_torrent>();
}
};
// ----
std::string my_listen_interfaces = ""; // "0.0.0.0:6881,[::]:6881" "{E4F0B674-0DFC-48BB-98A5-2AA730BDB6D6}:7777"
std::string target_torrentfile_path = "a.torrent";
int main(int argc, char *argv[])
try
{
//
// createsession
lt::settings_pack session_params;
session_params.set_int(lt::settings_pack::alert_mask, lt::alert_category::all);
if (my_listen_interfaces.length() != 0)
{
session_params.set_str(lt::settings_pack::listen_interfaces, my_listen_interfaces);
}
lt::session session(session_params);
//
// add torrent
lt::add_torrent_params torrent_params;
torrent_params.save_path = ".data"; // save in this dir
torrent_params.ti = std::make_shared<lt::torrent_info>(target_torrentfile_path);
plugin_creator creator;
// ----
torrent_params.extensions.push_back(creator);
// ----
lt::torrent_handle h = session.add_torrent(std::move(torrent_params));
session.add_torrent(torrent_params);
while (true)
{
std::vector<lt::alert *> alerts;
session.pop_alerts(&alerts);
lt::state_update_alert *st;
for (lt::alert *a : alerts)
{
std::cout << "[" << a->type() << "](" << a->what() << ") " << a->message() << std::endl;
switch (a->type())
{
case lt::torrent_finished_alert::alert_type:
goto END;
case lt::torrent_error_alert::alert_type:
goto END;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
END:
return 0;
}
catch (std::exception &e)
{
std::cerr << "Error: " << e.what() << std::endl;
}
토렌트 플러그인
Torrent 플러그인을 사용하여. 더 자세한 정보를 얻을 수 있습니다.
torrent_plugin으로 모든 이벤트를 연결할 수 있습니다.
#include <iostream>
#include <libtorrent/session.hpp>
#include <libtorrent/alert_types.hpp>
#include <libtorrent/alert.hpp>
#include <libtorrent/magnet_uri.hpp>
#include <libtorrent/error_code.hpp>
// ---
struct test_plugin_torrent : lt::torrent_plugin
{
void on_add_peer(lt::tcp::endpoint const &endpoint, lt::peer_source_flags_t flag1, lt::add_peer_flags_t flag2) override
{
std::cout << "[on_add_peer]" << endpoint.address().to_string() << ":" << endpoint.port() << std::endl;
}
void on_state(lt::torrent_status::state_t st)
{
std::cout << "[on_state]" << st << std::endl;
}
};
struct plugin_creator
{
std::shared_ptr<lt::torrent_plugin>
operator()(lt::torrent_handle const &, lt::client_data_t)
{
return std::make_shared<test_plugin_torrent>();
}
};
// ----
std::string my_listen_interfaces = ""; // "0.0.0.0:6881,[::]:6881" "{E4F0B674-0DFC-48BB-98A5-2AA730BDB6D6}:7777"
std::string target_torrentfile_path = "a.torrent";
int main(int argc, char *argv[])
try
{
//
// createsession
lt::settings_pack session_params;
session_params.set_int(lt::settings_pack::alert_mask, lt::alert_category::all);
if (my_listen_interfaces.length() != 0)
{
session_params.set_str(lt::settings_pack::listen_interfaces, my_listen_interfaces);
}
lt::session session(session_params);
//
// add torrent
lt::add_torrent_params torrent_params;
torrent_params.save_path = ".data"; // save in this dir
torrent_params.ti = std::make_shared<lt::torrent_info>(target_torrentfile_path);
plugin_creator creator;
// ----
torrent_params.extensions.push_back(creator);
// ----
lt::torrent_handle h = session.add_torrent(std::move(torrent_params));
session.add_torrent(torrent_params);
while (true)
{
std::vector<lt::alert *> alerts;
session.pop_alerts(&alerts);
lt::state_update_alert *st;
for (lt::alert *a : alerts)
{
std::cout << "[" << a->type() << "](" << a->what() << ") " << a->message() << std::endl;
switch (a->type())
{
case lt::torrent_finished_alert::alert_type:
goto END;
case lt::torrent_error_alert::alert_type:
goto END;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
END:
return 0;
}
catch (std::exception &e)
{
std::cerr << "Error: " << e.what() << std::endl;
}
Torrent 플러그인을 사용하여. 더 자세한 정보를 얻을 수 있습니다.
torrent_plugin으로 모든 이벤트를 연결할 수 있습니다.
피어 주소 가져오기
이 샘플 코드에서는 on_add_peer 이벤트를 연결했습니다.
on_add_peer 이벤트 정보에서 피어 주소를 얻을 수 있습니다.
torrent_param 개체에 플러그인 개체를 등록하여 연결할 수 있습니다.
torrent_params.extensions.push_back(creator);
struct test_plugin_torrent : lt::torrent_plugin
{
void on_add_peer(lt::tcp::endpoint const &endpoint, lt::peer_source_flags_t flag1, lt::add_peer_flags_t flag2) override
{
std::cout << "[on_add_peer]" << endpoint.address().to_string() << ":" << endpoint.port() << std::endl;
}
};
그리고 거기에 도착
[on_add_peer]xx.xx.xx.x:7881
[on_add_peer]yy.y.y.y.y:37151
[on_add_peer]zz.zz.z.z.z:6881
[on_add_peer]cc.cc.cc.20:6881
[on_add_peer]84.aaa.ddd.152:58515
...
`
끝.
원천
https://github.com/kyorohiro/hello_libtorrent
앱/main_extension_torrent_plugin.cpp
Reference
이 문제에 관하여(Hello Libtorrent : 확장 토렌트 플러그인), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://dev.to/kyorohiro/hello-libtorrent-extension-torrent-plugin-11oo
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
torrent_params.extensions.push_back(creator);
struct test_plugin_torrent : lt::torrent_plugin
{
void on_add_peer(lt::tcp::endpoint const &endpoint, lt::peer_source_flags_t flag1, lt::add_peer_flags_t flag2) override
{
std::cout << "[on_add_peer]" << endpoint.address().to_string() << ":" << endpoint.port() << std::endl;
}
};
[on_add_peer]xx.xx.xx.x:7881
[on_add_peer]yy.y.y.y.y:37151
[on_add_peer]zz.zz.z.z.z:6881
[on_add_peer]cc.cc.cc.20:6881
[on_add_peer]84.aaa.ddd.152:58515
...
https://github.com/kyorohiro/hello_libtorrent
앱/main_extension_torrent_plugin.cpp
Reference
이 문제에 관하여(Hello Libtorrent : 확장 토렌트 플러그인), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/kyorohiro/hello-libtorrent-extension-torrent-plugin-11oo텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)