From 187904d1e81d162f4dee3c73376bc9e46eef7bec Mon Sep 17 00:00:00 2001 From: Miguel Freitas Date: Thu, 1 Aug 2013 20:05:00 -0300 Subject: [PATCH] create new twister.cpp with libtorrent initialization --- src/init.cpp | 79 ++-------------------------------------------- src/twister.cpp | 83 +++++++++++++++++++++++++++++++++++++++++++++++++ src/twister.h | 17 ++++++++++ twister-qt.pro | 2 ++ 4 files changed, 104 insertions(+), 77 deletions(-) create mode 100644 src/twister.cpp create mode 100644 src/twister.h diff --git a/src/init.cpp b/src/init.cpp index 924df580..8bfa4655 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -14,6 +14,7 @@ #include "util.h" #include "ui_interface.h" #include "checkpoints.h" +#include "twister.h" #include #include @@ -32,8 +33,6 @@ using namespace boost; CWallet* pwalletMain; CClientUIInterface uiInterface; -void startSessionTorrent(boost::thread_group& threadGroup); - #ifdef WIN32 // Win32 LevelDB doesn't use filedescriptors, and the ones used for // accessing block files, don't count towards to fd_set size limit @@ -949,83 +948,9 @@ bool AppInit2(boost::thread_group& threadGroup) // Run a thread to flush wallet periodically threadGroup.create_thread(boost::bind(&ThreadFlushWalletDB, boost::ref(pwalletMain->strWalletFile))); + // Start libtorrent + dht startSessionTorrent(threadGroup); return !fRequestShutdown; } -// ===================== LIBTORRENT & DHT =========================== - -#include "libtorrent/config.hpp" -#include "libtorrent/entry.hpp" -#include "libtorrent/bencode.hpp" -#include "libtorrent/session.hpp" - -#define TORRENT_DISABLE_GEO_IP -#include "libtorrent/aux_/session_impl.hpp" - -using namespace libtorrent; -static session *ses; - -void ThreadWaitExtIP() -{ - RenameThread("wait-extip"); - - std::string ipStr; - - // wait up to 5 seconds for bitcoin to get the external IP - for( int i = 0; i < 10; i++ ) { - const CNetAddr paddrPeer("8.8.8.8"); - CAddress addr( GetLocalAddress(&paddrPeer) ); - if( addr != CAddress() ) { - ipStr = addr.ToStringIP(); - break; - } - MilliSleep(500); - } - - error_code ec; - int listen_port = GetListenPort() + 1000; - std::string bind_to_interface = ""; - - printf("Creating new libtorrent session ext_ip=%s port=%d\n", ipStr.c_str(), listen_port); - - ses = new session(fingerprint("LT", LIBTORRENT_VERSION_MAJOR, LIBTORRENT_VERSION_MINOR, 0, 0) - , session::add_default_plugins - , alert::all_categories - & ~(alert::dht_notification - + alert::progress_notification - + alert::debug_notification - + alert::stats_notification) - , ipStr.size() ? ipStr.c_str() : NULL ); - - /* - std::vector in; - if (load_file(".ses_state", in, ec) == 0) - { - lazy_entry e; - if (lazy_bdecode(&in[0], &in[0] + in.size(), e, ec) == 0) - ses.load_state(e); - } - */ - - ses->listen_on(std::make_pair(listen_port, listen_port) - , ec, bind_to_interface.c_str()); - if (ec) - { - fprintf(stderr, "failed to listen%s%s on ports %d-%d: %s\n" - , bind_to_interface.empty() ? "" : " on ", bind_to_interface.c_str() - , listen_port, listen_port+1, ec.message().c_str()); - } - - ses->start_dht(); - printf("libtorrent + dht started\n"); -} - -void startSessionTorrent(boost::thread_group& threadGroup) -{ - printf("startSessionTorrent (waiting for external IP)\n"); - - threadGroup.create_thread(boost::bind(&ThreadWaitExtIP)); -} - diff --git a/src/twister.cpp b/src/twister.cpp new file mode 100644 index 00000000..a9112977 --- /dev/null +++ b/src/twister.cpp @@ -0,0 +1,83 @@ +#include "twister.h" + +#include "main.h" + +twister::twister() +{ +} + +// ===================== LIBTORRENT & DHT =========================== + +#include "libtorrent/config.hpp" +#include "libtorrent/entry.hpp" +#include "libtorrent/bencode.hpp" +#include "libtorrent/session.hpp" + +#define TORRENT_DISABLE_GEO_IP +#include "libtorrent/aux_/session_impl.hpp" + +using namespace libtorrent; +static session *ses; + +void ThreadWaitExtIP() +{ + RenameThread("wait-extip"); + + std::string ipStr; + + // wait up to 5 seconds for bitcoin to get the external IP + for( int i = 0; i < 10; i++ ) { + const CNetAddr paddrPeer("8.8.8.8"); + CAddress addr( GetLocalAddress(&paddrPeer) ); + if( addr.IsValid() ) { + ipStr = addr.ToStringIP(); + break; + } + MilliSleep(500); + } + + error_code ec; + int listen_port = GetListenPort() + LIBTORRENT_PORT_OFFSET; + std::string bind_to_interface = ""; + + printf("Creating new libtorrent session ext_ip=%s port=%d\n", ipStr.c_str(), listen_port); + + ses = new session(fingerprint("LT", LIBTORRENT_VERSION_MAJOR, LIBTORRENT_VERSION_MINOR, 0, 0) + , session::add_default_plugins + , alert::all_categories + & ~(alert::dht_notification + + alert::progress_notification + + alert::debug_notification + + alert::stats_notification) + , ipStr.size() ? ipStr.c_str() : NULL ); + + /* + std::vector in; + if (load_file(".ses_state", in, ec) == 0) + { + lazy_entry e; + if (lazy_bdecode(&in[0], &in[0] + in.size(), e, ec) == 0) + ses.load_state(e); + } + */ + + ses->listen_on(std::make_pair(listen_port, listen_port) + , ec, bind_to_interface.c_str()); + if (ec) + { + fprintf(stderr, "failed to listen%s%s on ports %d-%d: %s\n" + , bind_to_interface.empty() ? "" : " on ", bind_to_interface.c_str() + , listen_port, listen_port+1, ec.message().c_str()); + } + + ses->start_dht(); + printf("libtorrent + dht started\n"); +} + +void startSessionTorrent(boost::thread_group& threadGroup) +{ + printf("startSessionTorrent (waiting for external IP)\n"); + + threadGroup.create_thread(boost::bind(&ThreadWaitExtIP)); +} + diff --git a/src/twister.h b/src/twister.h new file mode 100644 index 00000000..9c13f447 --- /dev/null +++ b/src/twister.h @@ -0,0 +1,17 @@ +#ifndef TWISTER_H +#define TWISTER_H + +#include + +#define LIBTORRENT_PORT_OFFSET 1000 + +class twister +{ +public: + twister(); +}; + +void startSessionTorrent(boost::thread_group& threadGroup); + + +#endif // TWISTER_H diff --git a/twister-qt.pro b/twister-qt.pro index ea282078..8b7fb43f 100644 --- a/twister-qt.pro +++ b/twister-qt.pro @@ -187,6 +187,7 @@ HEADERS += \ src/leveldb.h \ src/threadsafety.h \ src/limitedmap.h \ + src/twister.h # src/qt/bitcoingui.h # src/qt/transactiontablemodel.h \ @@ -259,6 +260,7 @@ SOURCES += \ #src/qt/bitcoin.cpp \ src/noui.cpp \ src/leveldb.cpp \ src/txdb.cpp \ + src/twister.cpp # src/qt/guiutil.cpp \ # src/qt/bitcoingui.cpp \