source: trunk/libtransmission/torrent.h @ 7578

Last change on this file since 7578 was 7578, checked in by charles, 12 years ago

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