Skip to content

Commit

Permalink
Added thread-safe tracker state.
Browse files Browse the repository at this point in the history
  • Loading branch information
rakshasa authored Jan 30, 2025
1 parent 94a11ba commit a588c73
Show file tree
Hide file tree
Showing 22 changed files with 550 additions and 704 deletions.
2 changes: 0 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,5 @@ AC_CONFIG_FILES([
src/torrent/Makefile
test/Makefile
])
#test/torrent/net/Makefile
#test/net/Makefile

AC_OUTPUT
54 changes: 8 additions & 46 deletions src/dht/dht_transaction.cc
Original file line number Diff line number Diff line change
@@ -1,41 +1,7 @@
// libTorrent - BitTorrent library
// Copyright (C) 2005-2011, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <[email protected]>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY

#include "config.h"

#include <cassert>

#include "torrent/exceptions.h"
#include "torrent/object_stream.h"
#include "tracker/tracker_dht.h"
Expand All @@ -61,13 +27,10 @@ DhtSearch::DhtSearch(const HashString& target, const DhtBucket& contacts)

DhtSearch::~DhtSearch() {
// Make sure transactions were destructed first. Since it is the destruction
// of a transaction that triggers this destructor, that should always be the
// of a transaction that triggers this destructor, that should always be the
// case.
if (m_pending)
throw internal_error("DhtSearch::~DhtSearch called with pending transactions.");

if (m_concurrency != 3)
throw internal_error("DhtSearch::~DhtSearch with invalid concurrency limit.");
assert(!m_pending && "DhtSearch::~DhtSearch called with pending transactions.");
assert(m_concurrency == 3 && "DhtSearch::~DhtSearch called with invalid concurrency limit.");

for (accessor itr = begin(); itr != end(); ++itr)
delete itr.node();
Expand Down Expand Up @@ -208,12 +171,11 @@ DhtSearch::node_status(const_accessor& n, bool success) {
}

DhtAnnounce::~DhtAnnounce() {
if (!complete())
throw internal_error("DhtAnnounce::~DhtAnnounce called while announce not complete.");
assert(complete() && "DhtAnnounce::~DhtAnnounce called while announce not complete.");

const char* failure = NULL;

if (m_tracker->get_state() != TrackerDht::state_announcing) {
if (m_tracker->get_dht_state() != TrackerDht::state_announcing) {
if (!m_contacted)
failure = "No DHT nodes available for peer search.";
else
Expand Down Expand Up @@ -244,7 +206,7 @@ DhtAnnounce::start_announce() {

m_contacted = m_pending = size();
m_replied = 0;
m_tracker->set_state(TrackerDht::state_announcing);
m_tracker->set_dht_state(TrackerDht::state_announcing);

for (const_accessor itr(begin()); itr != end(); ++itr)
set_node_active(itr, true);
Expand Down
2 changes: 1 addition & 1 deletion src/manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Manager {
ClientList* client_list() { return m_clientList; }
ConnectionManager* connection_manager() { return m_connectionManager; }
DhtManager* dht_manager() { return m_dhtManager; }

Poll* poll() { return m_main_thread_main.poll(); }

thread_main* main_thread_main() { return &m_main_thread_main; }
Expand Down
16 changes: 13 additions & 3 deletions src/torrent/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ libtorrent_torrent_la_SOURCES = \
peer/connection_list.h \
peer/peer.cc \
peer/peer.h \
peer/peer_info.cc \
peer/peer_info.h \
peer/peer_info.cc \
peer/peer_info.h \
peer/peer_list.cc \
peer/peer_list.h \
\
tracker/tracker_manager.cc \
tracker/tracker_manager.h \
tracker/tracker_state.h \
\
utils/directory_events.cc \
utils/directory_events.h \
Expand Down Expand Up @@ -179,7 +183,7 @@ libtorrent_torrent_peer_include_HEADERS = \
peer/client_list.h \
peer/connection_list.h \
peer/peer.h \
peer/peer_info.h \
peer/peer_info.h \
peer/peer_list.h

libtorrent_torrent_utils_includedir = $(includedir)/torrent/utils
Expand All @@ -196,6 +200,12 @@ libtorrent_torrent_utils_include_HEADERS = \
utils/thread_interrupt.h \
utils/uri_parser.h

libtorrent_torrent_tracker_includedir = $(includedir)/torrent/tracker
libtorrent_torrent_tracker_include_HEADERS = \
tracker/tracker_manager.h \
tracker/tracker_state.h


libtorrent_torrent_includedir = $(includedir)/torrent
libtorrent_torrent_include_HEADERS = \
bitfield.h \
Expand Down
43 changes: 0 additions & 43 deletions src/torrent/data/Makefile.am

This file was deleted.

100 changes: 49 additions & 51 deletions src/torrent/tracker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

#include <algorithm>

#include "exceptions.h"
#include "torrent/exceptions.h"
#include "torrent/tracker.h"
#include "torrent/tracker_list.h"
#include "globals.h"
#include "tracker.h"
#include "tracker_list.h"

namespace torrent {

Expand All @@ -19,32 +19,13 @@ constexpr int Tracker::max_normal_interval;
Tracker::Tracker(TrackerList* parent, const std::string& url, int flags) :
m_flags(flags),
m_parent(parent),
m_group(0),
m_url(url),

m_normal_interval(0),
m_min_interval(0),

m_latest_event(EVENT_NONE),
m_latest_new_peers(0),
m_latest_sum_peers(0),

m_success_time_last(0),
m_success_counter(0),

m_failed_time_last(0),
m_failed_counter(0),

m_scrape_time_last(0),
m_scrape_counter(0),

m_scrape_complete(0),
m_scrape_incomplete(0),
m_scrape_downloaded(0),

m_request_time_last(torrent::cachedTime.seconds()),
m_request_counter(0)
m_request_time_last(torrent::cachedTime.seconds())
{
// TODO: Not needed when EVENT_NONE is default.
auto tracker_state = TrackerState{};
tracker_state.m_latest_event = EVENT_NONE;
m_state.store(tracker_state);
}

void
Expand All @@ -70,25 +51,6 @@ Tracker::disable() {
m_parent->slot_tracker_disabled()(this);
}

uint32_t
Tracker::success_time_next() const {
if (m_success_counter == 0)
return 0;

return m_success_time_last + std::max(m_normal_interval, (uint32_t)min_normal_interval);
}

uint32_t
Tracker::failed_time_next() const {
if (m_failed_counter == 0)
return 0;

if (m_min_interval > min_min_interval)
return m_failed_time_last + m_min_interval;

return m_failed_time_last + std::min(5 << std::min(m_failed_counter - 1, (uint32_t)6), min_min_interval-1);
}

std::string
Tracker::scrape_url_from(std::string url) {
size_t delim_slash = url.rfind('/');
Expand All @@ -114,14 +76,50 @@ Tracker::inc_request_counter() {
throw internal_error("Tracker request had more than 10 requests in 10 seconds.");
}

void
Tracker::clear_intervals() {
auto state = m_state.load();

state.m_normal_interval = 0;
state.m_min_interval = 0;

m_state.store(state);
}

void
Tracker::clear_stats() {
m_latest_new_peers = 0;
m_latest_sum_peers = 0;
auto state = m_state.load();

state.m_latest_new_peers = 0;
state.m_latest_sum_peers = 0;
state.m_success_counter = 0;
state.m_failed_counter = 0;
state.m_scrape_counter = 0;

m_state.store(state);
}

void
Tracker::set_latest_event(int v) {
auto state = m_state.load();

state.m_latest_event = v;

m_state.store(state);
}

void
Tracker::update_tracker_id(const std::string& id) {
if (id.empty())
return;

if (tracker_id() == id)
return;

auto new_id = std::array<char,64>{};
std::copy_n(id.begin(), std::min(id.size(), size_t(63)), new_id.begin());

m_success_counter = 0;
m_failed_counter = 0;
m_scrape_counter = 0;
m_tracker_id.store(new_id);
}

}
Loading

0 comments on commit a588c73

Please sign in to comment.