source: trunk/libtransmission/torrent.h @ 8212

Last change on this file since 8212 was 8212, checked in by charles, 13 years ago

(trunk libT) omit some unnecessary tests on the bitfield checks. these seem small, but bitfields are always the top CPU abuser when I profile...

  • Property svn:keywords set to Date Rev Author Id
File size: 9.8 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 8212 2009-04-11 03:24:36Z 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_torrentFindFromId( tr_session * session,
63                                  int          id );
64
65tr_torrent* tr_torrentFindFromHash( tr_session *    session,
66                                    const uint8_t * hash );
67
68tr_torrent* tr_torrentFindFromHashString( tr_session * session,
69                                          const char * hashString );
70
71tr_torrent* tr_torrentFindFromObfuscatedHash( tr_session    * session,
72                                              const uint8_t * hash );
73
74tr_bool     tr_torrentIsPieceTransferAllowed( const tr_torrent * torrent,
75                                              tr_direction       direction );
76
77
78
79#define tr_block( a, b ) _tr_block( tor, a, b )
80tr_block_index_t _tr_block( const tr_torrent * tor,
81                            tr_piece_index_t   index,
82                            uint32_t           offset );
83
84tr_bool          tr_torrentReqIsValid( const tr_torrent * tor,
85                                       tr_piece_index_t   index,
86                                       uint32_t           offset,
87                                       uint32_t           length );
88
89uint64_t         tr_pieceOffset( const tr_torrent * tor,
90                                 tr_piece_index_t   index,
91                                 uint32_t           offset,
92                                 uint32_t           length );
93
94void             tr_torrentInitFilePriority( tr_torrent       * tor,
95                                             tr_file_index_t    fileIndex,
96                                             tr_priority_t      priority );
97
98int              tr_torrentCountUncheckedPieces( const tr_torrent * );
99
100tr_bool          tr_torrentIsFileChecked( const tr_torrent  * tor,
101                                          tr_file_index_t     file );
102
103void             tr_torrentSetPieceChecked( tr_torrent       * tor,
104                                            tr_piece_index_t   piece,
105                                            tr_bool            isChecked );
106
107void             tr_torrentSetFileChecked( tr_torrent       * tor,
108                                           tr_file_index_t    file,
109                                           tr_bool            isChecked );
110
111void             tr_torrentUncheck( tr_torrent * tor );
112
113int              tr_torrentPromoteTracker( tr_torrent   * tor,
114                                           int            trackerIndex );
115
116time_t*          tr_torrentGetMTimes( const tr_torrent  * tor,
117                                      size_t            * setmeCount );
118
119tr_torrent*      tr_torrentNext( tr_session  * session,
120                                 tr_torrent  * current );
121
122void             tr_torrentCheckSeedRatio( tr_torrent * tor );
123
124
125
126typedef enum
127{
128    TR_VERIFY_NONE,
129    TR_VERIFY_WAIT,
130    TR_VERIFY_NOW
131}
132tr_verify_state;
133
134void             tr_torrentSetVerifyState( tr_torrent      * tor,
135                                           tr_verify_state   state );
136
137struct tr_torrent
138{
139    tr_session *             session;
140    tr_info                  info;
141
142    struct tr_ratecontrol    swarmSpeed;
143
144    int                      magicNumber;
145
146    int                      error;
147    char                     errorString[128];
148
149    uint8_t                  obfuscatedHash[SHA_DIGEST_LENGTH];
150
151    /* If the initiator of the connection receives a handshake in which the
152     * peer_id does not match the expected peerid, then the initiator is
153     * expected to drop the connection. Note that the initiator presumably
154     * received the peer information from the tracker, which includes the
155     * peer_id that was registered by the peer. The peer_id from the tracker
156     * and in the handshake are expected to match.
157     */
158    uint8_t * peer_id;
159
160    /* Where to download */
161    char * downloadDir;
162
163    /* How many bytes we ask for per request */
164    uint32_t                   blockSize;
165    tr_block_index_t           blockCount;
166
167    uint32_t                   lastBlockSize;
168    uint32_t                   lastPieceSize;
169
170    uint32_t                   blockCountInPiece;
171    uint32_t                   blockCountInLastPiece;
172
173    struct tr_completion       completion;
174
175    struct tr_bitfield         checkedPieces;
176    tr_completeness            completeness;
177
178    struct tr_tracker *        tracker;
179    struct tr_publisher_tag *  trackerSubscription;
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    time_t                     addedDate;
189    time_t                     activityDate;
190    time_t                     doneDate;
191    time_t                     startDate;
192    time_t                     anyDate;
193
194    tr_torrent_completeness_func *   completeness_func;
195    void *                     completeness_func_user_data;
196
197    tr_torrent_ratio_limit_hit_func * ratio_limit_hit_func;
198    void *                     ratio_limit_hit_func_user_data;
199
200    tr_bool                    isRunning;
201    tr_bool                    isDeleting;
202    tr_bool                    needsSeedRatioCheck;
203
204    uint16_t                   maxConnectedPeers;
205
206    tr_verify_state            verifyState;
207
208    time_t                     lastStatTime;
209    tr_stat                    stats;
210
211    tr_torrent *               next;
212
213    int                        uniqueId;
214
215    struct tr_bandwidth      * bandwidth;
216
217    struct tr_torrent_peers  * torrentPeers;
218
219    double                     desiredRatio;
220    tr_ratiolimit              ratioLimitMode;
221};
222
223/* get the index of this piece's first block */
224static TR_INLINE tr_block_index_t
225tr_torPieceFirstBlock( const tr_torrent * tor, const tr_piece_index_t piece )
226{
227    return piece * tor->blockCountInPiece;
228}
229
230/* what piece index is this block in? */
231static TR_INLINE tr_piece_index_t
232tr_torBlockPiece( const tr_torrent * tor, const tr_block_index_t block )
233{
234    return block / tor->blockCountInPiece;
235}
236
237/* how many blocks are in this piece? */
238static TR_INLINE uint32_t
239tr_torPieceCountBlocks( const tr_torrent * tor, const tr_piece_index_t piece )
240{
241    return piece == tor->info.pieceCount - 1 ? tor->blockCountInLastPiece
242                                             : tor->blockCountInPiece;
243}
244
245/* how many bytes are in this piece? */
246static TR_INLINE uint32_t
247tr_torPieceCountBytes( const tr_torrent * tor, const tr_piece_index_t piece )
248{
249    return piece == tor->info.pieceCount - 1 ? tor->lastPieceSize
250                                             : tor->info.pieceSize;
251}
252
253/* how many bytes are in this block? */
254static TR_INLINE uint32_t
255tr_torBlockCountBytes( const tr_torrent * tor, const tr_block_index_t block )
256{
257    return block == tor->blockCount - 1 ? tor->lastBlockSize
258                                        : tor->blockSize;
259}
260
261static TR_INLINE void tr_torrentLock( const tr_torrent * tor )
262{
263    tr_globalLock( tor->session );
264}
265
266static TR_INLINE void tr_torrentUnlock( const tr_torrent * tor )
267{
268    tr_globalUnlock( tor->session );
269}
270
271static TR_INLINE tr_bool
272tr_torrentExists( const tr_session * session, const uint8_t *   torrentHash )
273{
274    return tr_torrentFindFromHash( (tr_session*)session, torrentHash ) != NULL;
275}
276
277static TR_INLINE tr_bool
278tr_torrentIsSeed( const tr_torrent * tor )
279{
280    return tor->completeness != TR_LEECH;
281}
282
283static TR_INLINE tr_bool tr_torrentIsPrivate( const tr_torrent * tor )
284{
285    return ( tor != NULL ) && tor->info.isPrivate;
286}
287
288static TR_INLINE tr_bool tr_torrentAllowsPex( const tr_torrent * tor )
289{
290    return ( tor != NULL  ) && tor->session->isPexEnabled && !tr_torrentIsPrivate( tor );
291}
292
293static TR_INLINE tr_bool tr_torrentIsPieceChecked( const tr_torrent  * tor, tr_piece_index_t i )
294{
295    return tr_bitfieldHasFast( &tor->checkedPieces, i );
296}
297
298/***
299****
300***/
301
302enum
303{
304    TORRENT_MAGIC_NUMBER = 95549
305};
306
307static inline tr_bool tr_isTorrent( const tr_torrent * tor )
308{
309    return ( tor != NULL )
310        && ( tor->magicNumber == TORRENT_MAGIC_NUMBER )
311        && ( tr_isSession( tor->session ) );
312}
313
314#endif
Note: See TracBrowser for help on using the repository browser.