minor cleanup and refactor (#1069)

This commit is contained in:
Alden Torres 2016-09-06 18:34:03 -04:00 committed by Arvid Norberg
parent 7a0da72813
commit 5e69dc700c
6 changed files with 23 additions and 42 deletions

View File

@ -36,9 +36,9 @@ POSSIBILITY OF SUCH DAMAGE.
#include <vector>
#include <cstdint>
#include "libtorrent/config.hpp"
#include "libtorrent/peer_id.hpp"
#include "libtorrent/address.hpp"
#include <libtorrent/config.hpp>
#include <libtorrent/peer_id.hpp>
#include <libtorrent/address.hpp>
namespace libtorrent { namespace dht
{

View File

@ -33,7 +33,6 @@ POSSIBILITY OF SUCH DAMAGE.
#ifndef RPC_MANAGER_HPP
#define RPC_MANAGER_HPP
#include <unordered_map>
#include <cstdint>
@ -42,11 +41,10 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/aux_/disable_warnings_pop.hpp"
#include <libtorrent/socket.hpp>
#include <libtorrent/time.hpp>
#include <libtorrent/kademlia/node_id.hpp>
#include <libtorrent/kademlia/observer.hpp>
#include "libtorrent/time.hpp"
namespace libtorrent { struct dht_settings; class entry; }
namespace libtorrent { namespace dht
@ -117,12 +115,9 @@ private:
void* allocate_observer();
void free_observer(void* ptr);
std::uint32_t calc_connection_id(udp::endpoint addr);
mutable boost::pool<> m_pool_allocator;
typedef std::unordered_multimap<int, observer_ptr> transactions_t;
transactions_t m_transactions;
std::unordered_multimap<int, observer_ptr> m_transactions;
udp_socket_interface* m_sock;
dht_logger* m_log;
@ -136,5 +131,3 @@ private:
} } // namespace libtorrent::dht
#endif

View File

@ -297,9 +297,6 @@ namespace libtorrent
, io_service& ios
, 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;
virtual ~tracker_connection() {}

View File

@ -940,17 +940,17 @@ namespace libtorrent
#if defined(TORRENT_WINDOWS) || defined(TORRENT_OS2)
#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
#define TORRENT_SEPARATOR "/"
bool const need_sep = lhs[lhs.size()-1] != '/';
bool const need_sep = lhs[lhs.size() - 1] != '/';
#endif
std::string ret;
int target_size = int(lhs.size() + rhs.size() + 2);
ret.resize(target_size);
target_size = std::snprintf(&ret[0], target_size, "%*s%s%*s"
, int(lhs.size()), lhs.data()
, (need_sep?TORRENT_SEPARATOR:"")
, (need_sep ? TORRENT_SEPARATOR : "")
, int(rhs.size()), rhs.data());
ret.resize(target_size);
return ret;

View File

@ -30,9 +30,7 @@ POSSIBILITY OF SUCH DAMAGE.
*/
#include "libtorrent/config.hpp"
#include "libtorrent/socket.hpp"
#include <libtorrent/config.hpp>
#include <libtorrent/io.hpp>
#include <libtorrent/random.hpp>
#include <libtorrent/invariant_check.hpp>
@ -43,7 +41,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include <libtorrent/kademlia/put_data.hpp>
#include <libtorrent/kademlia/refresh.hpp>
#include <libtorrent/kademlia/node.hpp>
#include <libtorrent/kademlia/observer.hpp>
#include <libtorrent/kademlia/dht_observer.hpp>
#include <libtorrent/kademlia/direct_request.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/hasher.hpp>
#include <libtorrent/session_settings.hpp> // for dht_settings
#include <libtorrent/time.hpp>
#include <libtorrent/aux_/time.hpp> // for aux::time_now
#include <type_traits>
#include <functional>
#ifndef TORRENT_DISABLE_LOGGING
#include <cinttypes> // for PRId64 et.al.
#endif
#include <functional>
using namespace std::placeholders;
namespace libtorrent { namespace dht
@ -173,10 +168,9 @@ rpc_manager::~rpc_manager()
TORRENT_ASSERT(!m_destructing);
m_destructing = true;
for (transactions_t::iterator i = m_transactions.begin()
, end(m_transactions.end()); i != end; ++i)
for (auto const& t : m_transactions)
{
i->second->abort();
t.second->abort();
}
}
@ -184,13 +178,13 @@ void* rpc_manager::allocate_observer()
{
m_pool_allocator.set_next_size(10);
void* ret = m_pool_allocator.malloc();
if (ret) ++m_allocated_observers;
if (ret != nullptr) ++m_allocated_observers;
return ret;
}
void rpc_manager::free_observer(void* ptr)
{
if (!ptr) return;
if (ptr == nullptr) return;
--m_allocated_observers;
TORRENT_ASSERT(reinterpret_cast<observer*>(ptr)->m_in_use == false);
m_pool_allocator.free(ptr);
@ -205,10 +199,9 @@ size_t rpc_manager::allocation_size() const
#if TORRENT_USE_INVARIANT_CHECKS
void rpc_manager::check_invariant() const
{
for (transactions_t::const_iterator i = m_transactions.begin()
, end(m_transactions.end()); i != end; ++i)
for (auto const& t : m_transactions)
{
TORRENT_ASSERT(i->second);
TORRENT_ASSERT(t.second);
}
}
#endif
@ -220,8 +213,7 @@ void rpc_manager::unreachable(udp::endpoint const& ep)
, print_endpoint(ep).c_str());
#endif
for (transactions_t::iterator i = m_transactions.begin();
i != m_transactions.end();)
for (auto i = m_transactions.begin(); i != m_transactions.end();)
{
TORRENT_ASSERT(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);
observer_ptr o;
std::pair<transactions_t::iterator, transactions_t::iterator> range = m_transactions.equal_range(tid);
for (transactions_t::iterator i = range.first; i != range.second; ++i)
auto range = m_transactions.equal_range(tid);
for (auto i = range.first; i != range.second; ++i)
{
if (m.addr.address() != i->second->target_addr()) continue;
o = i->second;
@ -366,8 +358,8 @@ time_duration rpc_manager::tick()
{
INVARIANT_CHECK;
static const int short_timeout = 1;
static const int timeout = 15;
constexpr int short_timeout = 1;
constexpr int timeout = 15;
// look for observers that have timed out
@ -379,8 +371,7 @@ time_duration rpc_manager::tick()
time_duration ret = seconds(short_timeout);
time_point now = aux::time_now();
for (transactions_t::iterator i = m_transactions.begin();
i != m_transactions.end();)
for (auto i = m_transactions.begin(); i != m_transactions.end();)
{
observer_ptr o = i->second;

View File

@ -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
TORRENT_TEST(range_lookup_duplicated_files)
{