source: trunk/libtransmission/torrent.h @ 7707

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

(trunk libT) Remove tr_torrentNext() from the public libT API (wereHamster)

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