source: trunk/libtransmission/completion.h @ 12262

Last change on this file since 12262 was 12262, checked in by jordan, 12 years ago

(trunk libT) handle situations where we don't know the bitfield's upper bound in advance. This comes up sometimes with magnet links.

  • Property svn:keywords set to Date Rev Author Id
File size: 3.7 KB
Line 
1/*
2 * This file Copyright (C) Mnemosyne LLC
3 *
4 * This file is licensed by the GPL version 2. Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id: completion.h 12262 2011-03-30 04:14:57Z jordan $
11 */
12
13#ifndef __TRANSMISSION__
14 #error only libtransmission should #include this header.
15#endif
16
17#ifndef TR_COMPLETION_H
18#define TR_COMPLETION_H
19
20#include "transmission.h"
21#include "bitfield.h"
22#include "utils.h" /* tr_getRatio() */
23
24typedef struct tr_completion
25{
26    tr_torrent * tor;
27
28    tr_bitfield blockBitfield;
29
30    /* number of bytes we'll have when done downloading. [0..info.totalSize]
31       DON'T access this directly; it's a lazy field.
32       use tr_cpSizeWhenDone() instead! */
33    uint64_t sizeWhenDoneLazy;
34
35    /* whether or not sizeWhenDone needs to be recalculated */
36    bool sizeWhenDoneIsDirty;
37
38    /* number of bytes we'll have when done downloading. [0..info.totalSize]
39       DON'T access this directly; it's a lazy field.
40       use tr_cpHaveValid() instead! */
41    uint64_t haveValidLazy;
42
43    /* whether or not haveValidLazy needs to be recalculated */
44    bool haveValidIsDirty;
45
46    /* number of bytes we want or have now. [0..sizeWhenDone] */
47    uint64_t sizeNow;
48}
49tr_completion;
50
51/**
52*** Life Cycle
53**/
54
55void  tr_cpConstruct( tr_completion *, tr_torrent * );
56
57void  tr_cpBlockInit( tr_completion * cp, const tr_bitfield * blocks );
58
59static inline void
60tr_cpDestruct( tr_completion * cp )
61{
62    tr_bitfieldDestruct( &cp->blockBitfield );
63}
64
65/**
66*** General
67**/
68
69double            tr_cpPercentComplete( const tr_completion * cp );
70
71double            tr_cpPercentDone( const tr_completion * cp );
72
73tr_completeness   tr_cpGetStatus( const tr_completion * );
74
75uint64_t          tr_cpHaveValid( const tr_completion * );
76
77uint64_t          tr_cpSizeWhenDone( const tr_completion * );
78
79void              tr_cpGetAmountDone( const   tr_completion * completion,
80                                      float                 * tab,
81                                      int                     tabCount );
82
83
84static inline uint64_t
85tr_cpHaveTotal( const tr_completion * cp )
86{
87    return cp->sizeNow;
88}
89
90static inline uint64_t
91tr_cpLeftUntilComplete( const tr_completion * cp )
92{
93    return tr_torrentInfo(cp->tor)->totalSize - cp->sizeNow;
94}
95
96static inline uint64_t
97tr_cpLeftUntilDone( const tr_completion * cp )
98{
99    return tr_cpSizeWhenDone( cp ) - cp->sizeNow;
100}
101
102static inline bool tr_cpHasAll( const tr_completion * cp )
103{
104    return tr_bitfieldHasAll( &cp->blockBitfield );
105}
106
107static inline bool tr_cpHasNone( const tr_completion * cp )
108{
109    return tr_bitfieldHasNone( &cp->blockBitfield );
110}
111
112/**
113***  Pieces
114**/
115
116void    tr_cpPieceAdd( tr_completion * cp, tr_piece_index_t i );
117
118void    tr_cpPieceRem( tr_completion * cp, tr_piece_index_t i );
119
120size_t  tr_cpMissingBlocksInPiece( const tr_completion *, tr_piece_index_t );
121
122size_t  tr_cpMissingBytesInPiece ( const tr_completion *, tr_piece_index_t );
123
124static inline bool
125tr_cpPieceIsComplete( const tr_completion * cp, tr_piece_index_t i )
126{
127    return tr_cpMissingBlocksInPiece( cp, i ) == 0;
128}
129
130/**
131***  Blocks
132**/
133
134void  tr_cpBlockAdd( tr_completion * cp, tr_block_index_t i );
135
136static inline bool
137tr_cpBlockIsComplete( const tr_completion * cp, tr_block_index_t i )
138{
139    return tr_bitfieldHas( &cp->blockBitfield, i );
140}
141
142/***
143****  Misc
144***/
145
146bool  tr_cpFileIsComplete( const tr_completion * cp, tr_file_index_t );
147
148void* tr_cpCreatePieceBitfield( const tr_completion * cp, size_t * byte_count );
149
150static inline void
151tr_cpInvalidateDND( tr_completion * cp )
152{
153    cp->sizeWhenDoneIsDirty = true;
154}
155
156
157#endif
Note: See TracBrowser for help on using the repository browser.