source: trunk/libtransmission/torrent.h @ 7584

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

(trunk libT) avoid some unnecessary memory fragmentation... for composited objects that have a tr_ratecontrol, contain the it directly rather than a pointer to one allocated elsewhere on the heap.

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