forked from premiere/premiere-libtorrent
minor cleanup and refactor (#1069)
This commit is contained in:
parent
7a0da72813
commit
5e69dc700c
|
@ -36,9 +36,9 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
#include "libtorrent/config.hpp"
|
#include <libtorrent/config.hpp>
|
||||||
#include "libtorrent/peer_id.hpp"
|
#include <libtorrent/peer_id.hpp>
|
||||||
#include "libtorrent/address.hpp"
|
#include <libtorrent/address.hpp>
|
||||||
|
|
||||||
namespace libtorrent { namespace dht
|
namespace libtorrent { namespace dht
|
||||||
{
|
{
|
||||||
|
|
|
@ -33,7 +33,6 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
#ifndef RPC_MANAGER_HPP
|
#ifndef RPC_MANAGER_HPP
|
||||||
#define RPC_MANAGER_HPP
|
#define RPC_MANAGER_HPP
|
||||||
|
|
||||||
|
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
|
@ -42,11 +41,10 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
||||||
|
|
||||||
#include <libtorrent/socket.hpp>
|
#include <libtorrent/socket.hpp>
|
||||||
|
#include <libtorrent/time.hpp>
|
||||||
#include <libtorrent/kademlia/node_id.hpp>
|
#include <libtorrent/kademlia/node_id.hpp>
|
||||||
#include <libtorrent/kademlia/observer.hpp>
|
#include <libtorrent/kademlia/observer.hpp>
|
||||||
|
|
||||||
#include "libtorrent/time.hpp"
|
|
||||||
|
|
||||||
namespace libtorrent { struct dht_settings; class entry; }
|
namespace libtorrent { struct dht_settings; class entry; }
|
||||||
|
|
||||||
namespace libtorrent { namespace dht
|
namespace libtorrent { namespace dht
|
||||||
|
@ -117,12 +115,9 @@ private:
|
||||||
void* allocate_observer();
|
void* allocate_observer();
|
||||||
void free_observer(void* ptr);
|
void free_observer(void* ptr);
|
||||||
|
|
||||||
std::uint32_t calc_connection_id(udp::endpoint addr);
|
|
||||||
|
|
||||||
mutable boost::pool<> m_pool_allocator;
|
mutable boost::pool<> m_pool_allocator;
|
||||||
|
|
||||||
typedef std::unordered_multimap<int, observer_ptr> transactions_t;
|
std::unordered_multimap<int, observer_ptr> m_transactions;
|
||||||
transactions_t m_transactions;
|
|
||||||
|
|
||||||
udp_socket_interface* m_sock;
|
udp_socket_interface* m_sock;
|
||||||
dht_logger* m_log;
|
dht_logger* m_log;
|
||||||
|
@ -136,5 +131,3 @@ private:
|
||||||
} } // namespace libtorrent::dht
|
} } // namespace libtorrent::dht
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -297,9 +297,6 @@ namespace libtorrent
|
||||||
, io_service& ios
|
, io_service& ios
|
||||||
, std::weak_ptr<request_callback> r);
|
, std::weak_ptr<request_callback> r);
|
||||||
|
|
||||||
void update_transaction_id(std::shared_ptr<udp_tracker_connection> c
|
|
||||||
, std::uint64_t tid);
|
|
||||||
|
|
||||||
std::shared_ptr<request_callback> requester() const;
|
std::shared_ptr<request_callback> requester() const;
|
||||||
virtual ~tracker_connection() {}
|
virtual ~tracker_connection() {}
|
||||||
|
|
||||||
|
|
|
@ -940,17 +940,17 @@ namespace libtorrent
|
||||||
|
|
||||||
#if defined(TORRENT_WINDOWS) || defined(TORRENT_OS2)
|
#if defined(TORRENT_WINDOWS) || defined(TORRENT_OS2)
|
||||||
#define TORRENT_SEPARATOR "\\"
|
#define TORRENT_SEPARATOR "\\"
|
||||||
bool const need_sep = lhs[lhs.size()-1] != '\\' && lhs[lhs.size()-1] != '/';
|
bool const need_sep = lhs[lhs.size() - 1] != '\\' && lhs[lhs.size() - 1] != '/';
|
||||||
#else
|
#else
|
||||||
#define TORRENT_SEPARATOR "/"
|
#define TORRENT_SEPARATOR "/"
|
||||||
bool const need_sep = lhs[lhs.size()-1] != '/';
|
bool const need_sep = lhs[lhs.size() - 1] != '/';
|
||||||
#endif
|
#endif
|
||||||
std::string ret;
|
std::string ret;
|
||||||
int target_size = int(lhs.size() + rhs.size() + 2);
|
int target_size = int(lhs.size() + rhs.size() + 2);
|
||||||
ret.resize(target_size);
|
ret.resize(target_size);
|
||||||
target_size = std::snprintf(&ret[0], target_size, "%*s%s%*s"
|
target_size = std::snprintf(&ret[0], target_size, "%*s%s%*s"
|
||||||
, int(lhs.size()), lhs.data()
|
, int(lhs.size()), lhs.data()
|
||||||
, (need_sep?TORRENT_SEPARATOR:"")
|
, (need_sep ? TORRENT_SEPARATOR : "")
|
||||||
, int(rhs.size()), rhs.data());
|
, int(rhs.size()), rhs.data());
|
||||||
ret.resize(target_size);
|
ret.resize(target_size);
|
||||||
return ret;
|
return ret;
|
||||||
|
|
|
@ -30,9 +30,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "libtorrent/config.hpp"
|
#include <libtorrent/config.hpp>
|
||||||
#include "libtorrent/socket.hpp"
|
|
||||||
|
|
||||||
#include <libtorrent/io.hpp>
|
#include <libtorrent/io.hpp>
|
||||||
#include <libtorrent/random.hpp>
|
#include <libtorrent/random.hpp>
|
||||||
#include <libtorrent/invariant_check.hpp>
|
#include <libtorrent/invariant_check.hpp>
|
||||||
|
@ -43,7 +41,6 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
#include <libtorrent/kademlia/put_data.hpp>
|
#include <libtorrent/kademlia/put_data.hpp>
|
||||||
#include <libtorrent/kademlia/refresh.hpp>
|
#include <libtorrent/kademlia/refresh.hpp>
|
||||||
#include <libtorrent/kademlia/node.hpp>
|
#include <libtorrent/kademlia/node.hpp>
|
||||||
#include <libtorrent/kademlia/observer.hpp>
|
|
||||||
#include <libtorrent/kademlia/dht_observer.hpp>
|
#include <libtorrent/kademlia/dht_observer.hpp>
|
||||||
#include <libtorrent/kademlia/direct_request.hpp>
|
#include <libtorrent/kademlia/direct_request.hpp>
|
||||||
#include <libtorrent/kademlia/get_item.hpp>
|
#include <libtorrent/kademlia/get_item.hpp>
|
||||||
|
@ -51,17 +48,15 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
#include <libtorrent/socket_io.hpp> // for print_endpoint
|
#include <libtorrent/socket_io.hpp> // for print_endpoint
|
||||||
#include <libtorrent/hasher.hpp>
|
#include <libtorrent/hasher.hpp>
|
||||||
#include <libtorrent/session_settings.hpp> // for dht_settings
|
#include <libtorrent/session_settings.hpp> // for dht_settings
|
||||||
#include <libtorrent/time.hpp>
|
|
||||||
#include <libtorrent/aux_/time.hpp> // for aux::time_now
|
#include <libtorrent/aux_/time.hpp> // for aux::time_now
|
||||||
|
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
#include <cinttypes> // for PRId64 et.al.
|
#include <cinttypes> // for PRId64 et.al.
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <functional>
|
|
||||||
|
|
||||||
using namespace std::placeholders;
|
using namespace std::placeholders;
|
||||||
|
|
||||||
namespace libtorrent { namespace dht
|
namespace libtorrent { namespace dht
|
||||||
|
@ -173,10 +168,9 @@ rpc_manager::~rpc_manager()
|
||||||
TORRENT_ASSERT(!m_destructing);
|
TORRENT_ASSERT(!m_destructing);
|
||||||
m_destructing = true;
|
m_destructing = true;
|
||||||
|
|
||||||
for (transactions_t::iterator i = m_transactions.begin()
|
for (auto const& t : m_transactions)
|
||||||
, end(m_transactions.end()); i != end; ++i)
|
|
||||||
{
|
{
|
||||||
i->second->abort();
|
t.second->abort();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -184,13 +178,13 @@ void* rpc_manager::allocate_observer()
|
||||||
{
|
{
|
||||||
m_pool_allocator.set_next_size(10);
|
m_pool_allocator.set_next_size(10);
|
||||||
void* ret = m_pool_allocator.malloc();
|
void* ret = m_pool_allocator.malloc();
|
||||||
if (ret) ++m_allocated_observers;
|
if (ret != nullptr) ++m_allocated_observers;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void rpc_manager::free_observer(void* ptr)
|
void rpc_manager::free_observer(void* ptr)
|
||||||
{
|
{
|
||||||
if (!ptr) return;
|
if (ptr == nullptr) return;
|
||||||
--m_allocated_observers;
|
--m_allocated_observers;
|
||||||
TORRENT_ASSERT(reinterpret_cast<observer*>(ptr)->m_in_use == false);
|
TORRENT_ASSERT(reinterpret_cast<observer*>(ptr)->m_in_use == false);
|
||||||
m_pool_allocator.free(ptr);
|
m_pool_allocator.free(ptr);
|
||||||
|
@ -205,10 +199,9 @@ size_t rpc_manager::allocation_size() const
|
||||||
#if TORRENT_USE_INVARIANT_CHECKS
|
#if TORRENT_USE_INVARIANT_CHECKS
|
||||||
void rpc_manager::check_invariant() const
|
void rpc_manager::check_invariant() const
|
||||||
{
|
{
|
||||||
for (transactions_t::const_iterator i = m_transactions.begin()
|
for (auto const& t : m_transactions)
|
||||||
, end(m_transactions.end()); i != end; ++i)
|
|
||||||
{
|
{
|
||||||
TORRENT_ASSERT(i->second);
|
TORRENT_ASSERT(t.second);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -220,8 +213,7 @@ void rpc_manager::unreachable(udp::endpoint const& ep)
|
||||||
, print_endpoint(ep).c_str());
|
, print_endpoint(ep).c_str());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
for (transactions_t::iterator i = m_transactions.begin();
|
for (auto i = m_transactions.begin(); i != m_transactions.end();)
|
||||||
i != m_transactions.end();)
|
|
||||||
{
|
{
|
||||||
TORRENT_ASSERT(i->second);
|
TORRENT_ASSERT(i->second);
|
||||||
observer_ptr const& o = i->second;
|
observer_ptr const& o = i->second;
|
||||||
|
@ -257,8 +249,8 @@ bool rpc_manager::incoming(msg const& m, node_id* id)
|
||||||
int tid = transaction_id.size() != 2 ? -1 : io::read_uint16(ptr);
|
int tid = transaction_id.size() != 2 ? -1 : io::read_uint16(ptr);
|
||||||
|
|
||||||
observer_ptr o;
|
observer_ptr o;
|
||||||
std::pair<transactions_t::iterator, transactions_t::iterator> range = m_transactions.equal_range(tid);
|
auto range = m_transactions.equal_range(tid);
|
||||||
for (transactions_t::iterator i = range.first; i != range.second; ++i)
|
for (auto i = range.first; i != range.second; ++i)
|
||||||
{
|
{
|
||||||
if (m.addr.address() != i->second->target_addr()) continue;
|
if (m.addr.address() != i->second->target_addr()) continue;
|
||||||
o = i->second;
|
o = i->second;
|
||||||
|
@ -366,8 +358,8 @@ time_duration rpc_manager::tick()
|
||||||
{
|
{
|
||||||
INVARIANT_CHECK;
|
INVARIANT_CHECK;
|
||||||
|
|
||||||
static const int short_timeout = 1;
|
constexpr int short_timeout = 1;
|
||||||
static const int timeout = 15;
|
constexpr int timeout = 15;
|
||||||
|
|
||||||
// look for observers that have timed out
|
// look for observers that have timed out
|
||||||
|
|
||||||
|
@ -379,8 +371,7 @@ time_duration rpc_manager::tick()
|
||||||
time_duration ret = seconds(short_timeout);
|
time_duration ret = seconds(short_timeout);
|
||||||
time_point now = aux::time_now();
|
time_point now = aux::time_now();
|
||||||
|
|
||||||
for (transactions_t::iterator i = m_transactions.begin();
|
for (auto i = m_transactions.begin(); i != m_transactions.end();)
|
||||||
i != m_transactions.end();)
|
|
||||||
{
|
{
|
||||||
observer_ptr o = i->second;
|
observer_ptr o = i->second;
|
||||||
|
|
||||||
|
|
|
@ -135,7 +135,7 @@ TORRENT_TEST(resolve_links)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// this ensure that internally the is a range lookup
|
// this ensure that internally there is a range lookup
|
||||||
// since the zero-hash piece is in the second place
|
// since the zero-hash piece is in the second place
|
||||||
TORRENT_TEST(range_lookup_duplicated_files)
|
TORRENT_TEST(range_lookup_duplicated_files)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue