source: trunk/libtransmission/torrent.h @ 8693

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

(trunk libT) avoid an extra per-torrent malloc/free by aggregating its tr_bandwidth into its struct

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