source: trunk/libtransmission/torrent.h @ 8878

Last change on this file since 8878 was 8878, checked in by charles, 12 years ago

(trunk libT) #2137: .resume file gets saved too frequently

  • Property svn:keywords set to Date Rev Author Id
File size: 10.5 KB
Line 
1/*
2 * This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com>
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: torrent.h 8878 2009-08-07 05:29:37Z charles $
11 */
12
13#ifndef __TRANSMISSION__
14 #error only libtransmission should #include this header.
15#endif
16
17#ifndef TR_TORRENT_H
18#define TR_TORRENT_H 1
19
20#include "completion.h" /* tr_completion */
21#include "ratecontrol.h" /* tr_ratecontrol */
22#include "session.h" /* tr_globalLock(), tr_globalUnlock() */
23#include "utils.h" /* tr_bitfield */
24
25struct tr_bandwidth;
26struct tr_ratecontrol;
27struct tr_torrent_peers;
28
29/**
30***  Package-visible ctor API
31**/
32
33void        tr_ctorSetSave( tr_ctor * ctor,
34                            tr_bool   saveMetadataInOurTorrentsDir );
35
36int         tr_ctorGetSave( const tr_ctor * ctor );
37
38void        tr_ctorInitTorrentPriorities( const tr_ctor * ctor, tr_torrent * tor );
39
40void        tr_ctorInitTorrentWanted( const tr_ctor * ctor, tr_torrent * tor );
41
42/**
43***
44**/
45
46/* just like tr_torrentSetFileDLs but doesn't trigger a fastresume save */
47void        tr_torrentInitFileDLs( tr_torrent *      tor,
48                                   tr_file_index_t * files,
49                                   tr_file_index_t   fileCount,
50                                   tr_bool           do_download );
51
52void        tr_torrentRecheckCompleteness( tr_torrent * );
53
54void        tr_torrentResetTransferStats( tr_torrent * );
55
56void        tr_torrentSetHasPiece( tr_torrent *     tor,
57                                   tr_piece_index_t pieceIndex,
58                                   tr_bool          has );
59
60void        tr_torrentChangeMyPort( tr_torrent * session );
61
62tr_torrent* tr_torrentFindFromHash( tr_session *    session,
63                                    const uint8_t * hash );
64
65tr_torrent* tr_torrentFindFromHashString( tr_session * session,
66                                          const char * hashString );
67
68tr_torrent* tr_torrentFindFromObfuscatedHash( tr_session    * session,
69                                              const uint8_t * hash );
70
71tr_bool     tr_torrentIsPieceTransferAllowed( const tr_torrent * torrent,
72                                              tr_direction       direction );
73
74
75
76#define tr_block( a, b ) _tr_block( tor, a, b )
77tr_block_index_t _tr_block( const tr_torrent * tor,
78                            tr_piece_index_t   index,
79                            uint32_t           offset );
80
81tr_bool          tr_torrentReqIsValid( const tr_torrent * tor,
82                                       tr_piece_index_t   index,
83                                       uint32_t           offset,
84                                       uint32_t           length );
85
86uint64_t         tr_pieceOffset( const tr_torrent * tor,
87                                 tr_piece_index_t   index,
88                                 uint32_t           offset,
89                                 uint32_t           length );
90
91void             tr_torrentInitFilePriority( tr_torrent       * tor,
92                                             tr_file_index_t    fileIndex,
93                                             tr_priority_t      priority );
94
95int              tr_torrentCountUncheckedPieces( const tr_torrent * );
96
97tr_bool          tr_torrentIsFileChecked( const tr_torrent  * tor,
98                                          tr_file_index_t     file );
99
100void             tr_torrentSetPieceChecked( tr_torrent       * tor,
101                                            tr_piece_index_t   piece,
102                                            tr_bool            isChecked );
103
104void             tr_torrentSetFileChecked( tr_torrent       * tor,
105                                           tr_file_index_t    file,
106                                           tr_bool            isChecked );
107
108void             tr_torrentUncheck( tr_torrent * tor );
109
110int              tr_torrentPromoteTracker( tr_torrent   * tor,
111                                           int            trackerIndex );
112
113time_t*          tr_torrentGetMTimes( const tr_torrent  * tor,
114                                      size_t            * setmeCount );
115
116tr_torrent*      tr_torrentNext( tr_session  * session,
117                                 tr_torrent  * current );
118
119void             tr_torrentCheckSeedRatio( tr_torrent * tor );
120
121
122
123typedef enum
124{
125    TR_VERIFY_NONE,
126    TR_VERIFY_WAIT,
127    TR_VERIFY_NOW
128}
129tr_verify_state;
130
131void             tr_torrentSetVerifyState( tr_torrent      * tor,
132                                           tr_verify_state   state );
133
134struct tr_torrent
135{
136    tr_session *             session;
137    tr_info                  info;
138
139    struct tr_ratecontrol    swarmSpeed;
140
141    int                      magicNumber;
142
143    tr_stat_errtype          error;
144    char                     errorString[128];
145
146    uint8_t                  obfuscatedHash[SHA_DIGEST_LENGTH];
147
148    /* If the initiator of the connection receives a handshake in which the
149     * peer_id does not match the expected peerid, then the initiator is
150     * expected to drop the connection. Note that the initiator presumably
151     * received the peer information from the tracker, which includes the
152     * peer_id that was registered by the peer. The peer_id from the tracker
153     * and in the handshake are expected to match.
154     */
155    uint8_t * peer_id;
156
157    /* Where to download */
158    char * downloadDir;
159
160    /* How many bytes we ask for per request */
161    uint32_t                   blockSize;
162    tr_block_index_t           blockCount;
163
164    uint32_t                   lastBlockSize;
165    uint32_t                   lastPieceSize;
166
167    uint32_t                   blockCountInPiece;
168    uint32_t                   blockCountInLastPiece;
169
170    struct tr_completion       completion;
171
172    struct tr_bitfield         checkedPieces;
173    tr_completeness            completeness;
174
175    struct tr_tracker *        tracker;
176    struct tr_publisher_tag *  trackerSubscription;
177
178    time_t                     dhtAnnounceAt;
179    tr_bool                    dhtAnnounceInProgress;
180
181    uint64_t                   downloadedCur;
182    uint64_t                   downloadedPrev;
183    uint64_t                   uploadedCur;
184    uint64_t                   uploadedPrev;
185    uint64_t                   corruptCur;
186    uint64_t                   corruptPrev;
187
188    uint64_t                   etaSpeedCalculatedAt;
189    double                     etaSpeed;
190
191    time_t                     addedDate;
192    time_t                     activityDate;
193    time_t                     doneDate;
194    time_t                     startDate;
195    time_t                     anyDate;
196
197    tr_torrent_completeness_func *   completeness_func;
198    void *                     completeness_func_user_data;
199
200    tr_torrent_ratio_limit_hit_func * ratio_limit_hit_func;
201    void *                     ratio_limit_hit_func_user_data;
202
203    tr_bool                    isRunning;
204    tr_bool                    isDeleting;
205    tr_bool                    needsSeedRatioCheck;
206    tr_bool                    startAfterVerify;
207    tr_bool                    isDirty;
208
209    uint16_t                   maxConnectedPeers;
210
211    tr_verify_state            verifyState;
212
213    time_t                     lastStatTime;
214    tr_stat                    stats;
215
216    tr_torrent *               next;
217
218    int                        uniqueId;
219
220    struct tr_bandwidth      * bandwidth;
221
222    struct tr_torrent_peers  * torrentPeers;
223
224    double                     desiredRatio;
225    tr_ratiolimit              ratioLimitMode;
226};
227
228/* get the index of this piece's first block */
229static TR_INLINE tr_block_index_t
230tr_torPieceFirstBlock( const tr_torrent * tor, const tr_piece_index_t piece )
231{
232    return piece * tor->blockCountInPiece;
233}
234
235/* what piece index is this block in? */
236static TR_INLINE tr_piece_index_t
237tr_torBlockPiece( const tr_torrent * tor, const tr_block_index_t block )
238{
239    return block / tor->blockCountInPiece;
240}
241
242/* how many blocks are in this piece? */
243static TR_INLINE uint32_t
244tr_torPieceCountBlocks( const tr_torrent * tor, const tr_piece_index_t piece )
245{
246    return piece == tor->info.pieceCount - 1 ? tor->blockCountInLastPiece
247                                             : tor->blockCountInPiece;
248}
249
250/* how many bytes are in this piece? */
251static TR_INLINE uint32_t
252tr_torPieceCountBytes( const tr_torrent * tor, const tr_piece_index_t piece )
253{
254    return piece == tor->info.pieceCount - 1 ? tor->lastPieceSize
255                                             : tor->info.pieceSize;
256}
257
258/* how many bytes are in this block? */
259static TR_INLINE uint32_t
260tr_torBlockCountBytes( const tr_torrent * tor, const tr_block_index_t block )
261{
262    return block == tor->blockCount - 1 ? tor->lastBlockSize
263                                        : tor->blockSize;
264}
265
266static TR_INLINE void tr_torrentLock( const tr_torrent * tor )
267{
268    tr_globalLock( tor->session );
269}
270
271static TR_INLINE void tr_torrentUnlock( const tr_torrent * tor )
272{
273    tr_globalUnlock( tor->session );
274}
275
276static TR_INLINE tr_bool
277tr_torrentExists( const tr_session * session, const uint8_t *   torrentHash )
278{
279    return tr_torrentFindFromHash( (tr_session*)session, torrentHash ) != NULL;
280}
281
282static TR_INLINE tr_bool
283tr_torrentIsSeed( const tr_torrent * tor )
284{
285    return tor->completeness != TR_LEECH;
286}
287
288static TR_INLINE tr_bool tr_torrentIsPrivate( const tr_torrent * tor )
289{
290    return ( tor != NULL ) && tor->info.isPrivate;
291}
292
293static TR_INLINE tr_bool tr_torrentAllowsPex( const tr_torrent * tor )
294{
295    return ( tor != NULL )
296        && ( tor->session->isPexEnabled )
297        && ( !tr_torrentIsPrivate( tor ) );
298}
299
300static TR_INLINE tr_bool tr_torrentAllowsDHT( const tr_torrent * tor )
301{
302    return ( tor != NULL )
303        && ( tr_sessionAllowsDHT( tor->session ) )
304        && ( !tr_torrentIsPrivate( tor ) );
305}
306
307static TR_INLINE tr_bool tr_torrentIsPieceChecked( const tr_torrent  * tor,
308                                                   tr_piece_index_t    i )
309{
310    return tr_bitfieldHasFast( &tor->checkedPieces, i );
311}
312
313/***
314****
315***/
316
317enum
318{
319    TORRENT_MAGIC_NUMBER = 95549
320};
321
322static inline tr_bool tr_isTorrent( const tr_torrent * tor )
323{
324    return ( tor != NULL )
325        && ( tor->magicNumber == TORRENT_MAGIC_NUMBER )
326        && ( tr_isSession( tor->session ) );
327}
328
329/* set a flag indicating that the torrent's .resume file
330 * needs to be saved when the torrent is closed */
331static inline void tr_torrentSetDirty( tr_torrent * tor )
332{
333    assert( tr_isTorrent( tor ) );
334
335    tor->isDirty = TRUE;
336}
337
338#endif
Note: See TracBrowser for help on using the repository browser.