source: trunk/libtransmission/torrent.h @ 7888

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

(trunk) #1787: add support for seeding ratio limiting in libtransmission

  • Property svn:keywords set to Date Rev Author Id
File size: 9.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 7888 2009-02-13 18:23:56Z 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
38/**
39***
40**/
41
42/* just like tr_torrentSetFileDLs but doesn't trigger a fastresume save */
43void        tr_torrentInitFileDLs( tr_torrent *      tor,
44                                   tr_file_index_t * files,
45                                   tr_file_index_t   fileCount,
46                                   tr_bool           do_download );
47
48void        tr_torrentRecheckCompleteness( tr_torrent * );
49
50void        tr_torrentResetTransferStats( tr_torrent * );
51
52void        tr_torrentSetHasPiece( tr_torrent *     tor,
53                                   tr_piece_index_t pieceIndex,
54                                   tr_bool          has );
55
56void        tr_torrentChangeMyPort( tr_torrent * session );
57
58tr_torrent* tr_torrentFindFromId( tr_session * session,
59                                  int          id );
60
61tr_torrent* tr_torrentFindFromHash( tr_session *    session,
62                                    const uint8_t * hash );
63
64tr_torrent* tr_torrentFindFromHashString( tr_session * session,
65                                          const char * hashString );
66
67tr_torrent* tr_torrentFindFromObfuscatedHash( tr_session    * session,
68                                              const uint8_t * hash );
69
70tr_bool     tr_torrentIsPieceTransferAllowed( const tr_torrent * torrent,
71                                              tr_direction       direction );
72
73tr_bool     tr_torrentGetSeedRatio( const tr_torrent * tor, double * ratio );
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
96
97int              tr_torrentCountUncheckedPieces( const tr_torrent * );
98
99tr_bool          tr_torrentIsFileChecked( const tr_torrent  * tor,
100                                          tr_file_index_t     file );
101
102void             tr_torrentSetPieceChecked( tr_torrent       * tor,
103                                            tr_piece_index_t   piece,
104                                            tr_bool            isChecked );
105
106void             tr_torrentSetFileChecked( tr_torrent       * tor,
107                                           tr_file_index_t    file,
108                                           tr_bool            isChecked );
109
110void             tr_torrentUncheck( tr_torrent * tor );
111
112int              tr_torrentPromoteTracker( tr_torrent   * tor,
113                                           int            trackerIndex );
114
115time_t*          tr_torrentGetMTimes( const tr_torrent  * tor,
116                                      size_t            * setmeCount );
117
118tr_torrent*      tr_torrentNext( tr_session  * session,
119                                 tr_torrent  * current );
120
121
122typedef enum
123{
124    TR_VERIFY_NONE,
125    TR_VERIFY_WAIT,
126    TR_VERIFY_NOW
127}
128tr_verify_state;
129
130struct tr_torrent
131{
132    tr_session *             session;
133    tr_info                  info;
134
135    tr_speedlimit            speedLimitMode[2];
136
137    struct tr_ratecontrol    swarmSpeed;
138
139    int                      magicNumber;
140
141    int                      error;
142    char                     errorString[128];
143
144    uint8_t                  obfuscatedHash[SHA_DIGEST_LENGTH];
145
146    /* If the initiator of the connection receives a handshake in which the
147     * peer_id does not match the expected peerid, then the initiator is
148     * expected to drop the connection. Note that the initiator presumably
149     * received the peer information from the tracker, which includes the
150     * peer_id that was registered by the peer. The peer_id from the tracker
151     * and in the handshake are expected to match.
152     */
153    uint8_t * peer_id;
154
155    /* Where to download */
156    char * downloadDir;
157
158    /* How many bytes we ask for per request */
159    uint32_t                   blockSize;
160    tr_block_index_t           blockCount;
161
162    uint32_t                   lastBlockSize;
163    uint32_t                   lastPieceSize;
164
165    uint32_t                   blockCountInPiece;
166    uint32_t                   blockCountInLastPiece;
167
168    struct tr_completion       completion;
169
170    struct tr_bitfield         checkedPieces;
171    tr_completeness            completeness;
172
173    struct tr_tracker *        tracker;
174    struct tr_publisher_tag *  trackerSubscription;
175
176    uint64_t                   downloadedCur;
177    uint64_t                   downloadedPrev;
178    uint64_t                   uploadedCur;
179    uint64_t                   uploadedPrev;
180    uint64_t                   corruptCur;
181    uint64_t                   corruptPrev;
182
183    time_t                     addedDate;
184    time_t                     activityDate;
185    time_t                     doneDate;
186    time_t                     startDate;
187
188    tr_torrent_completeness_func *   completeness_func;
189    void *                     completeness_func_user_data;
190
191    tr_torrent_ratio_limit_hit_func * ratio_limit_hit_func;
192    void *                     ratio_limit_hit_func_user_data;
193
194    tr_bool                    isRunning;
195    tr_bool                    isDeleting;
196
197    uint16_t                   maxConnectedPeers;
198
199    tr_verify_state            verifyState;
200
201    time_t                     lastStatTime;
202    tr_stat                    stats;
203
204    tr_torrent *               next;
205
206    int                        uniqueId;
207
208    struct tr_bandwidth      * bandwidth;
209
210    struct tr_torrent_peers  * torrentPeers;
211
212    double                     desiredRatio;
213    tr_ratiolimit              ratioLimitMode;
214};
215
216/* get the index of this piece's first block */
217static TR_INLINE tr_block_index_t
218tr_torPieceFirstBlock( const tr_torrent * tor, const tr_piece_index_t piece )
219{
220    return piece * tor->blockCountInPiece;
221}
222
223/* what piece index is this block in? */
224static TR_INLINE tr_piece_index_t
225tr_torBlockPiece( const tr_torrent * tor, const tr_block_index_t block )
226{
227    return block / tor->blockCountInPiece;
228}
229
230/* how many blocks are in this piece? */
231static TR_INLINE uint32_t
232tr_torPieceCountBlocks( const tr_torrent * tor, const tr_piece_index_t piece )
233{
234    return piece == tor->info.pieceCount - 1 ? tor->blockCountInLastPiece
235                                             : tor->blockCountInPiece;
236}
237
238/* how many bytes are in this piece? */
239static TR_INLINE uint32_t
240tr_torPieceCountBytes( const tr_torrent * tor, const tr_piece_index_t piece )
241{
242    return piece == tor->info.pieceCount - 1 ? tor->lastPieceSize
243                                             : tor->info.pieceSize;
244}
245
246/* how many bytes are in this block? */
247static TR_INLINE uint32_t
248tr_torBlockCountBytes( const tr_torrent * tor, const tr_block_index_t block )
249{
250    return block == tor->blockCount - 1 ? tor->lastBlockSize
251                                        : tor->blockSize;
252}
253
254static TR_INLINE void tr_torrentLock( const tr_torrent * tor )
255{
256    tr_globalLock( tor->session );
257}
258
259static TR_INLINE void tr_torrentUnlock( const tr_torrent * tor )
260{
261    tr_globalUnlock( tor->session );
262}
263
264static TR_INLINE tr_bool
265tr_torrentExists( const tr_session * session, const uint8_t *   torrentHash )
266{
267    return tr_torrentFindFromHash( (tr_session*)session, torrentHash ) != NULL;
268}
269
270static TR_INLINE tr_bool
271tr_torrentIsSeed( const tr_torrent * tor )
272{
273    return tor->completeness != TR_LEECH;
274}
275
276static TR_INLINE tr_bool tr_torrentIsPrivate( const tr_torrent * tor )
277{
278    return ( tor != NULL ) && tor->info.isPrivate;
279}
280
281static TR_INLINE tr_bool tr_torrentAllowsPex( const tr_torrent * tor )
282{
283    return ( tor != NULL  ) && tor->session->isPexEnabled && !tr_torrentIsPrivate( tor );
284}
285
286static TR_INLINE tr_bool tr_torrentIsPieceChecked( const tr_torrent  * tor, tr_piece_index_t i )
287{
288    return tr_bitfieldHas( &tor->checkedPieces, i );
289}
290
291/***
292****
293***/
294
295enum
296{
297    TORRENT_MAGIC_NUMBER = 95549
298};
299
300static inline tr_bool tr_isTorrent( const tr_torrent * tor )
301{
302    return ( tor != NULL )
303        && ( tor->magicNumber == TORRENT_MAGIC_NUMBER )
304        && ( tr_isSession( tor->session ) );
305}
306
307#endif
Note: See TracBrowser for help on using the repository browser.