source: trunk/libtransmission/torrent.h @ 7591

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

(trunk libT) inline a few more torrent methods

  • Property svn:keywords set to Date Rev Author Id
File size: 9.6 KB
Line 
1/******************************************************************************
2 * $Id: torrent.h 7591 2009-01-03 00:25:27Z charles $
3 *
4 * Copyright (c) 2005-2008 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#ifndef __TRANSMISSION__
26#error only libtransmission should #include this header.
27#endif
28
29#ifndef TR_TORRENT_H
30#define TR_TORRENT_H 1
31
32#include "completion.h" /* tr_completion */
33#include "ratecontrol.h" /* tr_ratecontrol */
34#include "session.h" /* tr_globalLock(), tr_globalUnlock() */
35#include "utils.h" /* tr_bitfield */
36
37struct tr_bandwidth;
38struct tr_ratecontrol;
39
40/**
41***  Package-visible ctor API
42**/
43
44void        tr_ctorSetSave( tr_ctor * ctor,
45                            tr_bool   saveMetadataInOurTorrentsDir );
46
47int         tr_ctorGetSave( const tr_ctor * ctor );
48
49/**
50***
51**/
52
53/* just like tr_torrentSetFileDLs but doesn't trigger a fastresume save */
54void        tr_torrentInitFileDLs( tr_torrent *      tor,
55                                   tr_file_index_t * files,
56                                   tr_file_index_t   fileCount,
57                                   tr_bool           do_download );
58
59void        tr_torrentRecheckCompleteness( tr_torrent * );
60
61void        tr_torrentResetTransferStats( tr_torrent * );
62
63void        tr_torrentSetHasPiece( tr_torrent *     tor,
64                                   tr_piece_index_t pieceIndex,
65                                   tr_bool          has );
66
67void        tr_torrentChangeMyPort( tr_torrent * session );
68
69tr_torrent* tr_torrentFindFromId( tr_session * session,
70                                  int          id );
71
72tr_torrent* tr_torrentFindFromHash( tr_session *    session,
73                                    const uint8_t * hash );
74
75tr_torrent* tr_torrentFindFromHashString( tr_session * session,
76                                          const char * hashString );
77
78tr_torrent* tr_torrentFindFromObfuscatedHash( tr_session    * session,
79                                              const uint8_t * hash );
80
81tr_bool     tr_torrentIsPieceTransferAllowed( const tr_torrent * torrent,
82                                              tr_direction       direction );
83
84
85#define tr_block( a, b ) _tr_block( tor, a, b )
86tr_block_index_t _tr_block( const tr_torrent * tor,
87                            tr_piece_index_t   index,
88                            uint32_t           offset );
89
90tr_bool          tr_torrentReqIsValid( const tr_torrent * tor,
91                                       tr_piece_index_t   index,
92                                       uint32_t           offset,
93                                       uint32_t           length );
94
95uint64_t         tr_pieceOffset( const tr_torrent * tor,
96                                 tr_piece_index_t   index,
97                                 uint32_t           offset,
98                                 uint32_t           length );
99
100void             tr_torrentInitFilePriority( tr_torrent       * tor,
101                                             tr_file_index_t    fileIndex,
102                                             tr_priority_t      priority );
103
104
105int              tr_torrentCountUncheckedPieces( const tr_torrent * );
106
107tr_bool          tr_torrentIsFileChecked( const tr_torrent  * tor,
108                                          tr_file_index_t     file );
109
110void             tr_torrentSetPieceChecked( tr_torrent       * tor,
111                                            tr_piece_index_t   piece,
112                                            tr_bool            isChecked );
113
114void             tr_torrentSetFileChecked( tr_torrent       * tor,
115                                           tr_file_index_t    file,
116                                           tr_bool            isChecked );
117
118void             tr_torrentUncheck( tr_torrent * tor );
119
120int              tr_torrentPromoteTracker( tr_torrent   * tor,
121                                           int            trackerIndex );
122
123time_t*          tr_torrentGetMTimes( const tr_torrent  * tor,
124                                      size_t            * setmeCount );
125
126typedef enum
127{
128    TR_VERIFY_NONE,
129    TR_VERIFY_WAIT,
130    TR_VERIFY_NOW
131}
132tr_verify_state;
133
134struct tr_torrent
135{
136    tr_session *             session;
137    tr_info                  info;
138
139    tr_speedlimit            speedLimitMode[2];
140
141    struct tr_ratecontrol    swarmSpeed;
142
143    int                      error;
144    char                     errorString[128];
145
146    uint8_t                  obfuscatedHash[SHA_DIGEST_LENGTH];
147
148    /* If the initiator of the connection receives a handshake in which the
149     * peer_id does not match the expected peerid, then the initiator is
150     * expected to drop the connection. Note that the initiator presumably
151     * received the peer information from the tracker, which includes the
152     * peer_id that was registered by the peer. The peer_id from the tracker
153     * and in the handshake are expected to match.
154     */
155    uint8_t * peer_id;
156
157    /* Where to download */
158    char * downloadDir;
159
160    /* How many bytes we ask for per request */
161    uint32_t                   blockSize;
162    tr_block_index_t           blockCount;
163
164    uint32_t                   lastBlockSize;
165    uint32_t                   lastPieceSize;
166
167    uint32_t                   blockCountInPiece;
168    uint32_t                   blockCountInLastPiece;
169
170    struct tr_completion       completion;
171
172    struct tr_bitfield         checkedPieces;
173    tr_completeness            completeness;
174
175    struct tr_tracker *        tracker;
176    struct tr_publisher_tag *  trackerSubscription;
177
178    uint64_t                   downloadedCur;
179    uint64_t                   downloadedPrev;
180    uint64_t                   uploadedCur;
181    uint64_t                   uploadedPrev;
182    uint64_t                   corruptCur;
183    uint64_t                   corruptPrev;
184
185    time_t                     addedDate;
186    time_t                     activityDate;
187    time_t                     doneDate;
188    time_t                     startDate;
189
190    tr_torrent_completeness_func *   completeness_func;
191    void *                     completeness_func_user_data;
192
193    tr_bool                    isRunning;
194    tr_bool                    isDeleting;
195
196    uint16_t                   maxConnectedPeers;
197
198    tr_verify_state            verifyState;
199
200    time_t                     lastStatTime;
201    tr_stat                    stats;
202
203    tr_torrent *               next;
204
205    int                        uniqueId;
206
207    struct tr_bandwidth      * bandwidth;
208};
209
210/* get the index of this piece's first block */
211static inline tr_block_index_t
212tr_torPieceFirstBlock( const tr_torrent * tor, const tr_piece_index_t piece )
213{
214    return piece * tor->blockCountInPiece;
215}
216
217/* what piece index is this block in? */
218static inline tr_piece_index_t
219tr_torBlockPiece( const tr_torrent * tor, const tr_block_index_t block )
220{
221    return block / tor->blockCountInPiece;
222}
223
224/* how many blocks are in this piece? */
225static inline uint32_t
226tr_torPieceCountBlocks( const tr_torrent * tor, const tr_piece_index_t piece )
227{
228    return piece == tor->info.pieceCount - 1 ? tor->blockCountInLastPiece
229                                             : tor->blockCountInPiece;
230}
231
232/* how many bytes are in this piece? */
233static inline uint32_t
234tr_torPieceCountBytes( const tr_torrent * tor, const tr_piece_index_t piece )
235{
236    return piece == tor->info.pieceCount - 1 ? tor->lastPieceSize
237                                             : tor->info.pieceSize;
238}
239
240/* how many bytes are in this block? */
241static inline uint32_t
242tr_torBlockCountBytes( const tr_torrent * tor, const tr_block_index_t block )
243{
244    return block == tor->blockCount - 1 ? tor->lastBlockSize
245                                        : tor->blockSize;
246}
247
248static inline void tr_torrentLock( const tr_torrent * tor )
249{
250    tr_globalLock( tor->session );
251}
252
253static inline void tr_torrentUnlock( const tr_torrent * tor )
254{
255    tr_globalUnlock( tor->session );
256}
257
258static inline tr_bool
259tr_torrentExists( const tr_session * session, const uint8_t *   torrentHash )
260{
261    return tr_torrentFindFromHash( (tr_session*)session, torrentHash ) != NULL;
262}
263
264static inline tr_bool
265tr_torrentIsSeed( const tr_torrent * tor )
266{
267    return tor->completeness != TR_LEECH;
268}
269
270static inline tr_bool tr_torrentIsPrivate( const tr_torrent * tor )
271{
272    return ( tor != NULL ) && tor->info.isPrivate;
273}
274
275static inline tr_bool tr_torrentAllowsPex( const tr_torrent * tor )
276{
277    return ( tor != NULL  ) && tor->session->isPexEnabled && !tr_torrentIsPrivate( tor );
278}
279
280static inline tr_bool tr_torrentIsPieceChecked( const tr_torrent  * tor, tr_piece_index_t i )
281{
282    return tr_bitfieldHas( &tor->checkedPieces, i );
283}
284
285#endif
Note: See TracBrowser for help on using the repository browser.