From eab70b04665a263f2cb87e6d65218af70f1c224a Mon Sep 17 00:00:00 2001 From: Miguel Freitas Date: Sat, 12 Oct 2013 16:45:06 -0300 Subject: [PATCH] force dht_announce on every torrent after adding dht nodes --- src/twister.cpp | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/twister.cpp b/src/twister.cpp index f11efd31..af4211c8 100644 --- a/src/twister.cpp +++ b/src/twister.cpp @@ -185,6 +185,7 @@ void ThreadMaintainDHTNodes() while(1) { MilliSleep(15000); + bool nodesAdded = false; if( ses ) { LOCK(cs_vNodes); vector vAddr = addrman.GetAddr(); @@ -193,30 +194,34 @@ void ThreadMaintainDHTNodes() if( (!ss.dht_nodes && totalNodesCandidates) || ss.dht_nodes < totalNodesCandidates / 2 ) { printf("ThreadMaintainDHTNodes: too few dht_nodes, trying to add some...\n"); - BOOST_FOREACH(CNode* pnode, vNodes) { - - // if !fInbound we created this connection so ip is reachable - if( !pnode->fInbound ) { - std::string addr = pnode->addr.ToStringIP(); - int port = pnode->addr.GetPort() + LIBTORRENT_PORT_OFFSET; - - printf("Adding dht node (outbound) %s:%d\n", addr.c_str(), port); - ses->add_dht_node(std::pair(addr, port)); - } else if( !ss.dht_nodes /* last resort! */ ) { - // can't use port number of inbound connectio, try standard port - std::string addr = pnode->addr.ToStringIP(); - int port = Params().GetDefaultPort() + LIBTORRENT_PORT_OFFSET; - - printf("Adding dht node (inbound) %s:%d\n", addr.c_str(), port); - ses->add_dht_node(std::pair(addr, port)); - } - } BOOST_FOREACH(const CAddress &a, vAddr) { std::string addr = a.ToStringIP(); int port = a.GetPort() + LIBTORRENT_PORT_OFFSET; printf("Adding dht node (addrman) %s:%d\n", addr.c_str(), port); ses->add_dht_node(std::pair(addr, port)); + nodesAdded = true; } + BOOST_FOREACH(CNode* pnode, vNodes) { + // if !fInbound we created this connection so ip is reachable. + // we can't use port number of inbound connection, so try standard port. + // only use inbound as last resort (if dht_nodes empty) + if( !pnode->fInbound || !ss.dht_nodes ) { + std::string addr = pnode->addr.ToStringIP(); + int port = (!pnode->fInbound) ? pnode->addr.GetPort() : Params().GetDefaultPort(); + port += LIBTORRENT_PORT_OFFSET; + + printf("Adding dht node (%sbound) %s:%d\n", (!pnode->fInbound) ? "out" : "in", + addr.c_str(), port); + ses->add_dht_node(std::pair(addr, port)); + nodesAdded = true; + } + } + } + } + if( nodesAdded ) { + LOCK(cs_twister); + BOOST_FOREACH(const PAIRTYPE(std::string, torrent_handle)& item, m_userTorrent) { + item.second.force_dht_announce(); } } }