Hello Libtorrent : 확장 토렌트 플러그인

이 섹션에서는 피어의 IP 주소를 얻는 방법을 설명합니다.

경고 알림 메시지를 사용하기 위해 피어의 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으로 모든 이벤트를 연결할 수 있습니다.
  • 가상 무효 on_piece_pass(piece_index_t) {}
  • 가상 무효 on_piece_failed(piece_index_t) {}
  • 가상 부울 on_pause() { 반환 거짓; }
  • 가상 부울 on_resume() { false를 반환합니다. }
  • 가상 무효 on_files_checked() {}
  • 가상 무효 on_state(torrent_status::state_t) {}
  • 가상 무효 on_add_peer(tcp::endpoint const&....

  • 피어 주소 가져오기



    이 샘플 코드에서는 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

    좋은 웹페이지 즐겨찾기