req->down = tier->byteCounts[TR_ANN_DOWN];
req->corrupt = tier->byteCounts[TR_ANN_CORRUPT];
req->leftUntilComplete = tr_torrentHasMetadata (tor)
- ? tor->info.totalSize - tr_cpHaveTotal (&tor->completion)
+ ? tor->info.totalSize - tr_torrentHaveTotal (tor)
: ~ (uint64_t)0;
req->event = event;
req->numwant = event == TR_ANNOUNCE_EVENT_STOPPED ? 0 : NUMWANT;
req->key = announcer->key;
- req->partial_seed = tr_cpGetStatus (&tor->completion) == TR_PARTIAL_SEED;
+ req->partial_seed = tr_torrentGetCompleteness (tor) == TR_PARTIAL_SEED;
tier_build_log_name (tier, req->log_name, sizeof (req->log_name));
return req;
}
{
const struct cache_block * b = blocks[i-1];
info->last_block_time = b->time;
- info->is_piece_done = tr_cpPieceIsComplete (&b->tor->completion, b->piece);
+ info->is_piece_done = tr_torrentPieceIsComplete (b->tor, b->piece);
info->is_multi_piece = b->piece != blocks[pos]->piece ? true : false;
info->len = i - pos;
info->pos = pos;
typedef struct tr_completion
{
- tr_torrent * tor;
+ tr_torrent * tor;
- tr_bitfield blockBitfield;
+ tr_bitfield blockBitfield;
- /* number of bytes we'll have when done downloading. [0..info.totalSize]
- DON'T access this directly; it's a lazy field.
- use tr_cpSizeWhenDone () instead! */
- uint64_t sizeWhenDoneLazy;
+ /* number of bytes we'll have when done downloading. [0..info.totalSize]
+ DON'T access this directly; it's a lazy field.
+ use tr_cpSizeWhenDone () instead! */
+ uint64_t sizeWhenDoneLazy;
- /* whether or not sizeWhenDone needs to be recalculated */
- bool sizeWhenDoneIsDirty;
+ /* whether or not sizeWhenDone needs to be recalculated */
+ bool sizeWhenDoneIsDirty;
- /* number of bytes we'll have when done downloading. [0..info.totalSize]
- DON'T access this directly; it's a lazy field.
- use tr_cpHaveValid () instead! */
- uint64_t haveValidLazy;
+ /* number of bytes we'll have when done downloading. [0..info.totalSize]
+ DON'T access this directly; it's a lazy field.
+ use tr_cpHaveValid () instead! */
+ uint64_t haveValidLazy;
- /* whether or not haveValidLazy needs to be recalculated */
- bool haveValidIsDirty;
+ /* whether or not haveValidLazy needs to be recalculated */
+ bool haveValidIsDirty;
- /* number of bytes we want or have now. [0..sizeWhenDone] */
- uint64_t sizeNow;
+ /* number of bytes we want or have now. [0..sizeWhenDone] */
+ uint64_t sizeNow;
}
tr_completion;
static inline void
tr_cpDestruct (tr_completion * cp)
{
- tr_bitfieldDestruct (&cp->blockBitfield);
+ tr_bitfieldDestruct (&cp->blockBitfield);
}
/**
static inline uint64_t
tr_cpHaveTotal (const tr_completion * cp)
{
- return cp->sizeNow;
+ return cp->sizeNow;
}
static inline bool tr_cpHasAll (const tr_completion * cp)
{
- return tr_torrentHasMetadata (cp->tor)
- && tr_bitfieldHasAll (&cp->blockBitfield);
+ return tr_torrentHasMetadata (cp->tor)
+ && tr_bitfieldHasAll (&cp->blockBitfield);
}
static inline bool tr_cpHasNone (const tr_completion * cp)
{
- return !tr_torrentHasMetadata (cp->tor)
- || tr_bitfieldHasNone (&cp->blockBitfield);
+ return !tr_torrentHasMetadata (cp->tor)
+ || tr_bitfieldHasNone (&cp->blockBitfield);
}
/**
static inline bool
tr_cpPieceIsComplete (const tr_completion * cp, tr_piece_index_t i)
{
- return tr_cpMissingBlocksInPiece (cp, i) == 0;
+ return tr_cpMissingBlocksInPiece (cp, i) == 0;
}
/**
static inline bool
tr_cpBlockIsComplete (const tr_completion * cp, tr_block_index_t i)
{
- return tr_bitfieldHas (&cp->blockBitfield, i);
+ return tr_bitfieldHas (&cp->blockBitfield, i);
}
/***
static inline void
tr_cpInvalidateDND (tr_completion * cp)
{
- cp->sizeWhenDoneIsDirty = true;
+ cp->sizeWhenDoneIsDirty = true;
}
/* we consider ourselves to be in endgame if the number of bytes
we've got requested is >= the number of bytes left to download */
return (s->requestCount * s->tor->blockSize)
- >= tr_cpLeftUntilDone (&s->tor->completion);
+ >= tr_torrentGetLeftUntilDone (s->tor);
}
static void
const uint16_t * rep = weightReplication;
/* primary key: weight */
- missing = tr_cpMissingBlocksInPiece (&tor->completion, a->index);
+ missing = tr_torrentMissingBlocksInPiece (tor, a->index);
pending = a->requestCount;
ia = missing > pending ? missing - pending : (tor->blockCountInPiece + pending);
- missing = tr_cpMissingBlocksInPiece (&tor->completion, b->index);
+ missing = tr_torrentMissingBlocksInPiece (tor, b->index);
pending = b->requestCount;
ib = missing > pending ? missing - pending : (tor->blockCountInPiece + pending);
if (ia < ib) return -1;
pool = tr_new (tr_piece_index_t, inf->pieceCount);
for (i=0; i<inf->pieceCount; ++i)
if (!inf->pieces[i].dnd)
- if (!tr_cpPieceIsComplete (&tor->completion, i))
+ if (!tr_torrentPieceIsComplete (tor, i))
pool[poolCount++] = i;
pieceCount = poolCount;
pieces = tr_new0 (struct weighted_piece, pieceCount);
tr_peer ** peers;
/* don't request blocks we've already got */
- if (tr_cpBlockIsComplete (&tor->completion, b))
+ if (tr_torrentBlockIsComplete (tor, b))
continue;
/* always add peer if this block has no peers yet */
return;
/* don't ask for it if we've already got it */
- if (tr_cpPieceIsComplete (t->tor->completion, pieceIndex))
+ if (tr_torrentPieceIsComplete (t->tor, pieceIndex))
return;
/* don't ask for it if they don't have it */
for (b=first; b<=last; ++b)
{
- if (!tr_cpBlockIsComplete (tor->completion, b))
+ if (tr_torrentBlockIsComplete (tor, b))
{
const uint32_t offset = getBlockOffsetInPiece (tor, b);
const uint32_t length = tr_torBlockCountBytes (tor, b);
const int peerCount = tr_ptrArraySize (&tor->swarm->peers);
const tr_peer ** peers = (const tr_peer**) tr_ptrArrayBase (&tor->swarm->peers);
const float interval = tor->info.pieceCount / (float)tabCount;
- const bool isSeed = tr_cpGetStatus (&tor->completion) == TR_SEED;
+ const bool isSeed = tr_torrentGetCompleteness (tor) == TR_SEED;
for (i=0; i<tabCount; ++i)
{
const int piece = i * interval;
- if (isSeed || tr_cpPieceIsComplete (&tor->completion, piece))
+ if (isSeed || tr_torrentPieceIsComplete (tor, piece))
{
tab[i] = -1;
}
const tr_peer ** peers = (const tr_peer**) tr_ptrArrayBase (&s->peers);
for (i=0; i<n; ++i)
if (peers[i]->atom && atomIsSeed (peers[i]->atom))
- return tr_cpLeftUntilDone (&tor->completion);
+ return tr_torrentGetLeftUntilDone (tor);
}
if (!s->pieceReplication || !s->pieceReplicationSize)
desiredAvailable = 0;
for (i=0, n=MIN (tor->info.pieceCount, s->pieceReplicationSize); i<n; ++i)
if (!tor->info.pieces[i].dnd && (s->pieceReplication[i] > 0))
- desiredAvailable += tr_cpMissingBytesInPiece (&tor->completion, i);
+ desiredAvailable += tr_torrentMissingBytesInPiece (tor, i);
assert (desiredAvailable <= tor->info.totalSize);
return desiredAvailable;
/* build a bitfield of interesting pieces... */
piece_is_interesting = tr_new (bool, n);
for (i=0; i<n; i++)
- piece_is_interesting[i] = !tor->info.pieces[i].dnd && !tr_cpPieceIsComplete (&tor->completion, i);
+ piece_is_interesting[i] = !tor->info.pieces[i].dnd && !tr_torrentPieceIsComplete (tor, i);
/* decide WHICH peers to be interested in (based on their cancel-to-block ratio) */
for (i=0; i<peerCount; ++i)
{
const bool fext = tr_peerIoSupportsFEXT (msgs->io);
const int reqIsValid = requestIsValid (msgs, req);
- const int clientHasPiece = reqIsValid && tr_cpPieceIsComplete (&msgs->torrent->completion, req->index);
+ const int clientHasPiece = reqIsValid && tr_torrentPieceIsComplete (msgs->torrent, req->index);
const int peerIsChoked = msgs->peer_is_choked;
int allow = false;
dbgmsg (msgs, "we didn't ask for this message...");
return 0;
}
- if (tr_cpPieceIsComplete (&msgs->torrent->completion, req->index)) {
+ if (tr_torrentPieceIsComplete (msgs->torrent, req->index)) {
dbgmsg (msgs, "we did ask for this message, but the piece is already complete...");
return 0;
}
--msgs->prefetchCount;
if (requestIsValid (msgs, &req)
- && tr_cpPieceIsComplete (&msgs->torrent->completion, req.index))
+ && tr_torrentPieceIsComplete (msgs->torrent, req.index))
{
int err;
const uint32_t msglen = 4 + 1 + 4 + 4 + req.length;
assert (tr_torrentHasMetadata (msgs->torrent));
- bytes = tr_cpCreatePieceBitfield (&msgs->torrent->completion, &byte_count);
+ bytes = tr_torrentCreatePieceBitfield (msgs->torrent, &byte_count);
evbuffer_add_uint32 (out, sizeof (uint8_t) + byte_count);
evbuffer_add_uint8 (out, BT_BITFIELD);
evbuffer_add (out, bytes, byte_count);
{
const bool fext = tr_peerIoSupportsFEXT (msgs->io);
- if (fext && tr_cpHasAll (&msgs->torrent->completion))
+ if (fext && tr_torrentHasAll (msgs->torrent))
{
protocolSendHaveAll (msgs);
}
- else if (fext && tr_cpHasNone (&msgs->torrent->completion))
+ else if (fext && tr_torrentHasNone (msgs->torrent))
{
protocolSendHaveNone (msgs);
}
- else if (!tr_cpHasNone (&msgs->torrent->completion))
+ else if (!tr_torrentHasNone (msgs->torrent))
{
sendBitfield (msgs);
}
if (tr_torrentHasMetadata (tor))
{
size_t byte_count = 0;
- void * bytes = tr_cpCreatePieceBitfield (&tor->completion, &byte_count);
+ void * bytes = tr_torrentCreatePieceBitfield (tor, &byte_count);
char * str = tr_base64_encode (bytes, byte_count, NULL);
tr_variantDictAddStr (d, key, str!=NULL ? str : "");
tr_free (str);
static bool
setLocalErrorIfFilesDisappeared (tr_torrent * tor)
{
- const bool disappeared = (tr_cpHaveTotal (&tor->completion) > 0) && !hasAnyLocalData (tor);
+ const bool disappeared = (tr_torrentHaveTotal (tor) > 0) && !hasAnyLocalData (tor);
if (disappeared)
{
s->metadataPercentComplete = tr_torrentGetMetadataPercent (tor);
s->percentDone = tr_cpPercentDone (&tor->completion);
- s->leftUntilDone = tr_cpLeftUntilDone (&tor->completion);
+ s->leftUntilDone = tr_torrentGetLeftUntilDone (tor);
s->sizeWhenDone = tr_cpSizeWhenDone (&tor->completion);
s->recheckProgress = s->activity == TR_STATUS_CHECK ? getVerifyProgress (tor) : 0;
s->activityDate = tor->activityDate;
s->downloadedEver = tor->downloadedCur + tor->downloadedPrev;
s->uploadedEver = tor->uploadedCur + tor->uploadedPrev;
s->haveValid = tr_cpHaveValid (&tor->completion);
- s->haveUnchecked = tr_cpHaveTotal (&tor->completion) - s->haveValid;
+ s->haveUnchecked = tr_torrentHaveTotal (tor) - s->haveValid;
s->desiredAvailable = tr_peerMgrGetDesiredAvailable (tor);
s->ratio = tr_getRatio (s->uploadedEver,
if (first == last)
{
- if (tr_cpBlockIsComplete (&tor->completion, first))
+ if (tr_torrentBlockIsComplete (tor, first))
total = f->length;
}
else
{
/* the first block */
- if (tr_cpBlockIsComplete (&tor->completion, first))
+ if (tr_torrentBlockIsComplete (tor, first))
total += tor->blockSize - (f->offset % tor->blockSize);
/* the middle blocks */
}
/* the last block */
- if (tr_cpBlockIsComplete (&tor->completion, last))
+ if (tr_torrentBlockIsComplete (tor, last))
total += (f->offset + f->length) - ((uint64_t)tor->blockSize * last);
}
}
void
tr_torrentGotBlock (tr_torrent * tor, tr_block_index_t block)
{
- const bool block_is_new = !tr_cpBlockIsComplete (&tor->completion, block);
+ const bool block_is_new = !tr_torrentBlockIsComplete (tor, block);
assert (tr_isTorrent (tor));
assert (tr_amInEventThread (tor->session));
tr_torrentSetDirty (tor);
p = tr_torBlockPiece (tor, block);
- if (tr_cpPieceIsComplete (&tor->completion, p))
+ if (tr_torrentPieceIsComplete (tor, p))
{
tr_logAddTorDbg (tor, "[LAZY] checking just-completed piece %zu", (size_t)p);
return tr_torrentFindFromHash ((tr_session*)session, torrentHash) != NULL;
}
+static inline tr_completeness
+tr_torrentGetCompleteness (const tr_torrent * tor)
+{
+ return tor->completeness;
+}
+
static inline bool
tr_torrentIsSeed (const tr_torrent * tor)
{
- return tor->completeness != TR_LEECH;
+ return tr_torrentGetCompleteness(tor) != TR_LEECH;
}
static inline bool tr_torrentIsPrivate (const tr_torrent * tor)
const unsigned char * tr_torrentGetPeerId (tr_torrent * tor);
+static inline uint64_t
+tr_torrentGetLeftUntilDone (const tr_torrent * tor)
+{
+ return tr_cpLeftUntilDone (&tor->completion);
+}
+
+static inline bool
+tr_torrentHasAll (const tr_torrent * tor)
+{
+ return tr_cpHasAll (&tor->completion);
+}
+
+static inline bool
+tr_torrentHasNone (const tr_torrent * tor)
+{
+ return tr_cpHasNone (&tor->completion);
+}
+
+static inline bool
+tr_torrentPieceIsComplete (const tr_torrent * tor, tr_piece_index_t i)
+{
+ return tr_cpPieceIsComplete (&tor->completion, i);
+}
+
+static inline bool
+tr_torrentBlockIsComplete (const tr_torrent * tor, tr_block_index_t i)
+{
+ return tr_cpBlockIsComplete (&tor->completion, i);
+}
+
+static inline size_t
+tr_torrentMissingBlocksInPiece (const tr_torrent * tor, tr_piece_index_t i)
+{
+ return tr_cpMissingBlocksInPiece (&tor->completion, i);
+}
+
+static inline size_t
+tr_torrentMissingBytesInPiece (const tr_torrent * tor, tr_piece_index_t i)
+{
+ return tr_cpMissingBytesInPiece (&tor->completion, i);
+}
+
+static inline void *
+tr_torrentCreatePieceBitfield (const tr_torrent * tor, size_t * byte_count)
+{
+ return tr_cpCreatePieceBitfield (&tor->completion, byte_count);
+}
+
+static inline uint64_t
+tr_torrentHaveTotal (const tr_torrent * tor)
+{
+ return tr_cpHaveTotal (&tor->completion);
+}
+
static inline bool
tr_torrentIsQueued (const tr_torrent * tor)
{
- return tor->isQueued;
+ return tor->isQueued;
}
static inline tr_direction
tr_torrentGetQueueDirection (const tr_torrent * tor)
{
- return tr_torrentIsSeed (tor) ? TR_UP : TR_DOWN;
+ return tr_torrentIsSeed (tor) ? TR_UP : TR_DOWN;
}
#endif
/* if we're starting a new piece... */
if (piecePos == 0)
- hadPiece = tr_cpPieceIsComplete (&tor->completion, pieceIndex);
+ hadPiece = tr_torrentPieceIsComplete (tor, pieceIndex);
/* if we're starting a new file... */
if (!filePos && (fd<0) && (fileIndex!=prevFileIndex))