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 | |
---|
24 | typedef 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 | } |
---|
49 | tr_completion; |
---|
50 | |
---|
51 | /** |
---|
52 | *** Life Cycle |
---|
53 | **/ |
---|
54 | |
---|
55 | void tr_cpConstruct( tr_completion *, tr_torrent * ); |
---|
56 | |
---|
57 | void tr_cpBlockInit( tr_completion * cp, const tr_bitfield * blocks ); |
---|
58 | |
---|
59 | static inline void |
---|
60 | tr_cpDestruct( tr_completion * cp ) |
---|
61 | { |
---|
62 | tr_bitfieldDestruct( &cp->blockBitfield ); |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | *** General |
---|
67 | **/ |
---|
68 | |
---|
69 | double tr_cpPercentComplete( const tr_completion * cp ); |
---|
70 | |
---|
71 | double tr_cpPercentDone( const tr_completion * cp ); |
---|
72 | |
---|
73 | tr_completeness tr_cpGetStatus( const tr_completion * ); |
---|
74 | |
---|
75 | uint64_t tr_cpHaveValid( const tr_completion * ); |
---|
76 | |
---|
77 | uint64_t tr_cpSizeWhenDone( const tr_completion * ); |
---|
78 | |
---|
79 | void tr_cpGetAmountDone( const tr_completion * completion, |
---|
80 | float * tab, |
---|
81 | int tabCount ); |
---|
82 | |
---|
83 | |
---|
84 | static inline uint64_t |
---|
85 | tr_cpHaveTotal( const tr_completion * cp ) |
---|
86 | { |
---|
87 | return cp->sizeNow; |
---|
88 | } |
---|
89 | |
---|
90 | static inline uint64_t |
---|
91 | tr_cpLeftUntilComplete( const tr_completion * cp ) |
---|
92 | { |
---|
93 | return tr_torrentInfo(cp->tor)->totalSize - cp->sizeNow; |
---|
94 | } |
---|
95 | |
---|
96 | static inline uint64_t |
---|
97 | tr_cpLeftUntilDone( const tr_completion * cp ) |
---|
98 | { |
---|
99 | return tr_cpSizeWhenDone( cp ) - cp->sizeNow; |
---|
100 | } |
---|
101 | |
---|
102 | static inline bool tr_cpHasAll( const tr_completion * cp ) |
---|
103 | { |
---|
104 | return tr_bitfieldHasAll( &cp->blockBitfield ); |
---|
105 | } |
---|
106 | |
---|
107 | static inline bool tr_cpHasNone( const tr_completion * cp ) |
---|
108 | { |
---|
109 | return tr_bitfieldHasNone( &cp->blockBitfield ); |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | *** Pieces |
---|
114 | **/ |
---|
115 | |
---|
116 | void tr_cpPieceAdd( tr_completion * cp, tr_piece_index_t i ); |
---|
117 | |
---|
118 | void tr_cpPieceRem( tr_completion * cp, tr_piece_index_t i ); |
---|
119 | |
---|
120 | size_t tr_cpMissingBlocksInPiece( const tr_completion *, tr_piece_index_t ); |
---|
121 | |
---|
122 | size_t tr_cpMissingBytesInPiece ( const tr_completion *, tr_piece_index_t ); |
---|
123 | |
---|
124 | static inline bool |
---|
125 | tr_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 | |
---|
134 | void tr_cpBlockAdd( tr_completion * cp, tr_block_index_t i ); |
---|
135 | |
---|
136 | static inline bool |
---|
137 | tr_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 | |
---|
146 | bool tr_cpFileIsComplete( const tr_completion * cp, tr_file_index_t ); |
---|
147 | |
---|
148 | void* tr_cpCreatePieceBitfield( const tr_completion * cp, size_t * byte_count ); |
---|
149 | |
---|
150 | static inline void |
---|
151 | tr_cpInvalidateDND( tr_completion * cp ) |
---|
152 | { |
---|
153 | cp->sizeWhenDoneIsDirty = true; |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | #endif |
---|