source: trunk/libtransmission/torrent.h @ 7710

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

(trunk libT) peerMgr: operate directly on tr_torrent and not peerMgr+torrentHash

  • Property svn:keywords set to Date Rev Author Id
File size: 8.9 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 7710 2009-01-13 21:00:05Z 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
73
74#define tr_block( a, b ) _tr_block( tor, a, b )
75tr_block_index_t _tr_block( const tr_torrent * tor,
76                            tr_piece_index_t   index,
77                            uint32_t           offset );
78
79tr_bool          tr_torrentReqIsValid( const tr_torrent * tor,
80                                       tr_piece_index_t   index,
81                                       uint32_t           offset,
82                                       uint32_t           length );
83
84uint64_t         tr_pieceOffset( const tr_torrent * tor,
85                                 tr_piece_index_t   index,
86                                 uint32_t           offset,
87                                 uint32_t           length );
88
89void             tr_torrentInitFilePriority( tr_torrent       * tor,
90                                             tr_file_index_t    fileIndex,
91                                             tr_priority_t      priority );
92
93
94int              tr_torrentCountUncheckedPieces( const tr_torrent * );
95
96tr_bool          tr_torrentIsFileChecked( const tr_torrent  * tor,
97                                          tr_file_index_t     file );
98
99void             tr_torrentSetPieceChecked( tr_torrent       * tor,
100                                            tr_piece_index_t   piece,
101                                            tr_bool            isChecked );
102
103void             tr_torrentSetFileChecked( tr_torrent       * tor,
104                                           tr_file_index_t    file,
105                                           tr_bool            isChecked );
106
107void             tr_torrentUncheck( tr_torrent * tor );
108
109int              tr_torrentPromoteTracker( tr_torrent   * tor,
110                                           int            trackerIndex );
111
112time_t*          tr_torrentGetMTimes( const tr_torrent  * tor,
113                                      size_t            * setmeCount );
114
115tr_torrent*      tr_torrentNext( tr_session  * session,
116                                 tr_torrent  * current );
117
118
119typedef enum
120{
121    TR_VERIFY_NONE,
122    TR_VERIFY_WAIT,
123    TR_VERIFY_NOW
124}
125tr_verify_state;
126
127struct tr_torrent
128{
129    tr_session *             session;
130    tr_info                  info;
131
132    tr_speedlimit            speedLimitMode[2];
133
134    struct tr_ratecontrol    swarmSpeed;
135
136    int                      error;
137    char                     errorString[128];
138
139    uint8_t                  obfuscatedHash[SHA_DIGEST_LENGTH];
140
141    /* If the initiator of the connection receives a handshake in which the
142     * peer_id does not match the expected peerid, then the initiator is
143     * expected to drop the connection. Note that the initiator presumably
144     * received the peer information from the tracker, which includes the
145     * peer_id that was registered by the peer. The peer_id from the tracker
146     * and in the handshake are expected to match.
147     */
148    uint8_t * peer_id;
149
150    /* Where to download */
151    char * downloadDir;
152
153    /* How many bytes we ask for per request */
154    uint32_t                   blockSize;
155    tr_block_index_t           blockCount;
156
157    uint32_t                   lastBlockSize;
158    uint32_t                   lastPieceSize;
159
160    uint32_t                   blockCountInPiece;
161    uint32_t                   blockCountInLastPiece;
162
163    struct tr_completion       completion;
164
165    struct tr_bitfield         checkedPieces;
166    tr_completeness            completeness;
167
168    struct tr_tracker *        tracker;
169    struct tr_publisher_tag *  trackerSubscription;
170
171    uint64_t                   downloadedCur;
172    uint64_t                   downloadedPrev;
173    uint64_t                   uploadedCur;
174    uint64_t                   uploadedPrev;
175    uint64_t                   corruptCur;
176    uint64_t                   corruptPrev;
177
178    time_t                     addedDate;
179    time_t                     activityDate;
180    time_t                     doneDate;
181    time_t                     startDate;
182
183    tr_torrent_completeness_func *   completeness_func;
184    void *                     completeness_func_user_data;
185
186    tr_bool                    isRunning;
187    tr_bool                    isDeleting;
188
189    uint16_t                   maxConnectedPeers;
190
191    tr_verify_state            verifyState;
192
193    time_t                     lastStatTime;
194    tr_stat                    stats;
195
196    tr_torrent *               next;
197
198    int                        uniqueId;
199
200    struct tr_bandwidth      * bandwidth;
201
202    struct tr_torrent_peers  * torrentPeers;
203};
204
205/* get the index of this piece's first block */
206static TR_INLINE tr_block_index_t
207tr_torPieceFirstBlock( const tr_torrent * tor, const tr_piece_index_t piece )
208{
209    return piece * tor->blockCountInPiece;
210}
211
212/* what piece index is this block in? */
213static TR_INLINE tr_piece_index_t
214tr_torBlockPiece( const tr_torrent * tor, const tr_block_index_t block )
215{
216    return block / tor->blockCountInPiece;
217}
218
219/* how many blocks are in this piece? */
220static TR_INLINE uint32_t
221tr_torPieceCountBlocks( const tr_torrent * tor, const tr_piece_index_t piece )
222{
223    return piece == tor->info.pieceCount - 1 ? tor->blockCountInLastPiece
224                                             : tor->blockCountInPiece;
225}
226
227/* how many bytes are in this piece? */
228static TR_INLINE uint32_t
229tr_torPieceCountBytes( const tr_torrent * tor, const tr_piece_index_t piece )
230{
231    return piece == tor->info.pieceCount - 1 ? tor->lastPieceSize
232                                             : tor->info.pieceSize;
233}
234
235/* how many bytes are in this block? */
236static TR_INLINE uint32_t
237tr_torBlockCountBytes( const tr_torrent * tor, const tr_block_index_t block )
238{
239    return block == tor->blockCount - 1 ? tor->lastBlockSize
240                                        : tor->blockSize;
241}
242
243static TR_INLINE void tr_torrentLock( const tr_torrent * tor )
244{
245    tr_globalLock( tor->session );
246}
247
248static TR_INLINE void tr_torrentUnlock( const tr_torrent * tor )
249{
250    tr_globalUnlock( tor->session );
251}
252
253static TR_INLINE tr_bool
254tr_torrentExists( const tr_session * session, const uint8_t *   torrentHash )
255{
256    return tr_torrentFindFromHash( (tr_session*)session, torrentHash ) != NULL;
257}
258
259static TR_INLINE tr_bool
260tr_torrentIsSeed( const tr_torrent * tor )
261{
262    return tor->completeness != TR_LEECH;
263}
264
265static TR_INLINE tr_bool tr_torrentIsPrivate( const tr_torrent * tor )
266{
267    return ( tor != NULL ) && tor->info.isPrivate;
268}
269
270static TR_INLINE tr_bool tr_torrentAllowsPex( const tr_torrent * tor )
271{
272    return ( tor != NULL  ) && tor->session->isPexEnabled && !tr_torrentIsPrivate( tor );
273}
274
275static TR_INLINE tr_bool tr_torrentIsPieceChecked( const tr_torrent  * tor, tr_piece_index_t i )
276{
277    return tr_bitfieldHas( &tor->checkedPieces, i );
278}
279
280#endif
Note: See TracBrowser for help on using the repository browser.