source: trunk/libtransmission/torrent.h @ 8123

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

(trunk rpc) add the ability to set file priorities and download flags when adding a new torrent via rpc

  • Property svn:keywords set to Date Rev Author Id
File size: 9.7 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 8123 2009-04-02 20:43:42Z 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
203    uint16_t                   maxConnectedPeers;
204
205    tr_verify_state            verifyState;
206
207    time_t                     lastStatTime;
208    tr_stat                    stats;
209
210    tr_torrent *               next;
211
212    int                        uniqueId;
213
214    struct tr_bandwidth      * bandwidth;
215
216    struct tr_torrent_peers  * torrentPeers;
217
218    double                     desiredRatio;
219    tr_ratiolimit              ratioLimitMode;
220};
221
222/* get the index of this piece's first block */
223static TR_INLINE tr_block_index_t
224tr_torPieceFirstBlock( const tr_torrent * tor, const tr_piece_index_t piece )
225{
226    return piece * tor->blockCountInPiece;
227}
228
229/* what piece index is this block in? */
230static TR_INLINE tr_piece_index_t
231tr_torBlockPiece( const tr_torrent * tor, const tr_block_index_t block )
232{
233    return block / tor->blockCountInPiece;
234}
235
236/* how many blocks are in this piece? */
237static TR_INLINE uint32_t
238tr_torPieceCountBlocks( const tr_torrent * tor, const tr_piece_index_t piece )
239{
240    return piece == tor->info.pieceCount - 1 ? tor->blockCountInLastPiece
241                                             : tor->blockCountInPiece;
242}
243
244/* how many bytes are in this piece? */
245static TR_INLINE uint32_t
246tr_torPieceCountBytes( const tr_torrent * tor, const tr_piece_index_t piece )
247{
248    return piece == tor->info.pieceCount - 1 ? tor->lastPieceSize
249                                             : tor->info.pieceSize;
250}
251
252/* how many bytes are in this block? */
253static TR_INLINE uint32_t
254tr_torBlockCountBytes( const tr_torrent * tor, const tr_block_index_t block )
255{
256    return block == tor->blockCount - 1 ? tor->lastBlockSize
257                                        : tor->blockSize;
258}
259
260static TR_INLINE void tr_torrentLock( const tr_torrent * tor )
261{
262    tr_globalLock( tor->session );
263}
264
265static TR_INLINE void tr_torrentUnlock( const tr_torrent * tor )
266{
267    tr_globalUnlock( tor->session );
268}
269
270static TR_INLINE tr_bool
271tr_torrentExists( const tr_session * session, const uint8_t *   torrentHash )
272{
273    return tr_torrentFindFromHash( (tr_session*)session, torrentHash ) != NULL;
274}
275
276static TR_INLINE tr_bool
277tr_torrentIsSeed( const tr_torrent * tor )
278{
279    return tor->completeness != TR_LEECH;
280}
281
282static TR_INLINE tr_bool tr_torrentIsPrivate( const tr_torrent * tor )
283{
284    return ( tor != NULL ) && tor->info.isPrivate;
285}
286
287static TR_INLINE tr_bool tr_torrentAllowsPex( const tr_torrent * tor )
288{
289    return ( tor != NULL  ) && tor->session->isPexEnabled && !tr_torrentIsPrivate( tor );
290}
291
292static TR_INLINE tr_bool tr_torrentIsPieceChecked( const tr_torrent  * tor, tr_piece_index_t i )
293{
294    return tr_bitfieldHas( &tor->checkedPieces, i );
295}
296
297/***
298****
299***/
300
301enum
302{
303    TORRENT_MAGIC_NUMBER = 95549
304};
305
306static inline tr_bool tr_isTorrent( const tr_torrent * tor )
307{
308    return ( tor != NULL )
309        && ( tor->magicNumber == TORRENT_MAGIC_NUMBER )
310        && ( tr_isSession( tor->session ) );
311}
312
313#endif
Note: See TracBrowser for help on using the repository browser.