refactor: make tr_swarm a class (#1915)

This commit is contained in:
Charles Kerr 2021-10-09 19:13:40 -05:00 committed by GitHub
parent c287b82c00
commit 0783e9691d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 45 deletions

View File

@ -23,7 +23,7 @@
*/
class tr_peer;
struct tr_swarm;
class tr_swarm;
struct peer_atom;
/* This is the maximum size of a block request.
@ -134,9 +134,9 @@ struct tr_swarm_stats
int peerFromCount[TR_PEER_FROM__MAX];
};
void tr_swarmGetStats(struct tr_swarm const* swarm, tr_swarm_stats* setme);
void tr_swarmGetStats(tr_swarm const* swarm, tr_swarm_stats* setme);
void tr_swarmIncrementActivePeers(struct tr_swarm* swarm, tr_direction direction, bool is_active);
void tr_swarmIncrementActivePeers(tr_swarm* swarm, tr_direction direction, bool is_active);
/***
****

View File

@ -163,50 +163,58 @@ enum piece_sort_state
};
/** @brief Opaque, per-torrent data structure for peer connection information */
struct tr_swarm
class tr_swarm
{
tr_swarm_stats stats;
public:
tr_swarm(tr_peerMgr* manager_in, tr_torrent* tor_in)
: manager{ manager_in }
, tor{ tor_in }
{
}
tr_ptrArray outgoingHandshakes; /* tr_handshake */
tr_ptrArray pool; /* struct peer_atom */
tr_ptrArray peers; /* tr_peerMsgs */
tr_ptrArray webseeds; /* tr_webseed */
public:
tr_swarm_stats stats = {};
tr_torrent* tor;
struct tr_peerMgr* manager;
tr_ptrArray outgoingHandshakes = {}; /* tr_handshake */
tr_ptrArray pool = {}; /* struct peer_atom */
tr_ptrArray peers = {}; /* tr_peerMsgs */
tr_ptrArray webseeds = {}; /* tr_webseed */
tr_peerMsgs* optimistic; /* the optimistic peer, or nullptr if none */
int optimisticUnchokeTimeScaler;
tr_peerMgr* const manager;
tr_torrent* const tor;
bool poolIsAllSeeds;
bool poolIsAllSeedsDirty; /* true if poolIsAllSeeds needs to be recomputed */
bool isRunning;
bool needsCompletenessCheck;
tr_peerMsgs* optimistic = nullptr; /* the optimistic peer, or nullptr if none */
int optimisticUnchokeTimeScaler = 0;
struct block_request* requests;
int requestCount;
int requestAlloc;
bool poolIsAllSeeds = false;
bool poolIsAllSeedsDirty = true; /* true if poolIsAllSeeds needs to be recomputed */
bool isRunning = false;
bool needsCompletenessCheck = true;
struct weighted_piece* pieces;
int pieceCount;
enum piece_sort_state pieceSortState;
struct block_request* requests = nullptr;
int requestCount = 0;
int requestAlloc = 0;
struct weighted_piece* pieces = nullptr;
int pieceCount = 0;
enum piece_sort_state pieceSortState = PIECES_UNSORTED;
/* An array of pieceCount items stating how many peers have each piece.
This is used to help us for downloading pieces "rarest first."
This may be nullptr if we don't have metainfo yet, or if we're not
downloading and don't care about rarity */
uint16_t* pieceReplication;
size_t pieceReplicationSize;
uint16_t* pieceReplication = nullptr;
size_t pieceReplicationSize = 0;
int interestedCount;
int maxPeers;
time_t lastCancel;
int interestedCount = 0;
int maxPeers = 0;
time_t lastCancel = 0;
/* Before the endgame this should be 0. In endgame, is contains the average
* number of pending requests per peer. Only peers which have more pending
* requests are considered 'fast' are allowed to request a block that's
* already been requested from another (slower?) peer. */
int endgame;
int endgame = 0;
};
struct tr_peerMgr
@ -438,7 +446,8 @@ static void swarmFree(void* vs)
tr_free(s->requests);
tr_free(s->pieces);
tr_free(s);
delete s;
}
static void peerCallbackFunc(tr_peer*, tr_peer_event const*, void*);
@ -462,19 +471,11 @@ static void rebuildWebseedArray(tr_swarm* s, tr_torrent* tor)
static tr_swarm* swarmNew(tr_peerMgr* manager, tr_torrent* tor)
{
tr_swarm* s;
auto* swarm = new tr_swarm{ manager, tor };
s = tr_new0(tr_swarm, 1);
s->manager = manager;
s->tor = tor;
s->pool = {};
s->peers = {};
s->webseeds = {};
s->outgoingHandshakes = {};
rebuildWebseedArray(swarm, tor);
rebuildWebseedArray(s, tor);
return s;
return swarm;
}
static void ensureMgrTimersExist(struct tr_peerMgr* m);
@ -4131,7 +4132,7 @@ static bool checkBestScoresComeFirst(struct peer_candidate const* candidates, in
#endif /* TR_ENABLE_ASSERTS */
static bool calculateAllSeeds(struct tr_swarm* swarm)
static bool calculateAllSeeds(tr_swarm* swarm)
{
int nAtoms = 0;
struct peer_atom** atoms = (struct peer_atom**)tr_ptrArrayPeek(&swarm->pool, &nAtoms);
@ -4147,7 +4148,7 @@ static bool calculateAllSeeds(struct tr_swarm* swarm)
return true;
}
static bool swarmIsAllSeeds(struct tr_swarm* swarm)
static bool swarmIsAllSeeds(tr_swarm* swarm)
{
if (swarm->poolIsAllSeedsDirty)
{

View File

@ -29,12 +29,12 @@
*/
class tr_peerMsgs;
class tr_swarm;
struct UTPSocket;
struct peer_atom;
struct tr_peerIo;
struct tr_peerMgr;
struct tr_peer_stat;
struct tr_swarm;
struct tr_torrent;
/* added_f's bitwise-or'ed flags */

View File

@ -22,6 +22,7 @@
#include "tr-macros.h"
#include "utils.h" /* TR_GNUC_PRINTF */
class tr_swarm;
struct tr_torrent_tiers;
struct tr_magnet_info;
@ -261,7 +262,7 @@ struct tr_torrent
// TODO: change tr_bandwidth* to owning pointer to the bandwidth, or remove * and own the value
struct tr_bandwidth* bandwidth;
struct tr_swarm* swarm;
tr_swarm* swarm;
float desiredRatio;
tr_ratiolimit ratioLimitMode;