source: trunk/libtransmission/session.h @ 12182

Last change on this file since 12182 was 12182, checked in by jordan, 11 years ago

(trunk libT) move tr_lpdAnnounceMore() out of the announcer module

We can stop local peer discovery immediately during shutdown, but need to leave the announcer running for the event=stopped messages. So it doesn't make sense to keep them on the same periodic timer.

  • Property svn:keywords set to Date Rev Author Id
File size: 9.6 KB
Line 
1/*
2 * This file Copyright (C) Mnemosyne LLC
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: session.h 12182 2011-03-17 12:45:29Z jordan $
11 */
12
13#ifndef __TRANSMISSION__
14 #error only libtransmission should #include this header.
15#endif
16
17#ifndef TR_INTERNAL_H
18#define TR_INTERNAL_H 1
19
20#define TR_NAME "Transmission"
21
22#ifndef UNUSED
23 #ifdef __GNUC__
24  #define UNUSED __attribute__ ( ( unused ) )
25 #else
26  #define UNUSED
27 #endif
28#endif
29
30#include "bencode.h"
31#include "bitfield.h"
32#include "utils.h"
33
34typedef enum { TR_NET_OK, TR_NET_ERROR, TR_NET_WAIT } tr_tristate_t;
35
36enum
37{
38  PEER_ID_LEN = 20
39};
40
41void tr_peerIdInit( uint8_t * setme );
42
43struct event_base;
44struct evdns_base;
45
46struct tr_address;
47struct tr_announcer;
48struct tr_announcer_udp;
49struct tr_bandwidth;
50struct tr_bindsockets;
51struct tr_cache;
52struct tr_fdInfo;
53
54typedef void ( tr_web_config_func )( tr_session * session, void * curl_pointer, const char * url );
55
56struct tr_turtle_info
57{
58    /* TR_UP and TR_DOWN speed limits */
59    int speedLimit_Bps[2];
60
61    /* is turtle mode on right now? */
62    tr_bool isEnabled;
63
64    /* does turtle mode turn itself on and off at given times? */
65    tr_bool isClockEnabled;
66
67    /* when clock mode is on, minutes after midnight to turn on turtle mode */
68    int beginMinute;
69
70    /* when clock mode is on, minutes after midnight to turn off turtle mode */
71    int endMinute;
72
73    /* only use clock mode on these days of the week */
74    tr_sched_day days;
75
76    /* called when isEnabled changes */
77    tr_altSpeedFunc * callback;
78
79    /* the callback's user_data argument */
80    void * callbackUserData;
81
82    /* the callback's changedByUser argument.
83     * indicates whether the change came from the user or from the clock. */
84    tr_bool changedByUser;
85
86    /* bitfield of all the minutes in a week.
87     * Each bit's value indicates whether the scheduler wants turtle
88     * limits on or off at that given minute in the week. */
89    tr_bitfield minutes;
90};
91
92/** @brief handle to an active libtransmission session */
93struct tr_session
94{
95    tr_bool                      isPortRandom;
96    tr_bool                      isPexEnabled;
97    tr_bool                      isDHTEnabled;
98    tr_bool                      isUTPEnabled;
99    tr_bool                      isLPDEnabled;
100    tr_bool                      isBlocklistEnabled;
101    tr_bool                      isPrefetchEnabled;
102    tr_bool                      isTorrentDoneScriptEnabled;
103    tr_bool                      isClosed;
104    tr_bool                      useLazyBitfield;
105    tr_bool                      isIncompleteFileNamingEnabled;
106    tr_bool                      isRatioLimited;
107    tr_bool                      isIdleLimited;
108    tr_bool                      isIncompleteDirEnabled;
109    tr_bool                      pauseAddedTorrent;
110    tr_bool                      deleteSourceTorrent;
111
112    tr_benc                      removedTorrents;
113
114    int                          umask;
115
116    int                          speedLimit_Bps[2];
117    tr_bool                      speedLimitEnabled[2];
118
119    struct tr_turtle_info        turtle;
120
121    struct tr_fdInfo           * fdInfo;
122
123    int                          magicNumber;
124
125    tr_encryption_mode           encryptionMode;
126
127    tr_preallocation_mode        preallocationMode;
128
129    struct event_base          * event_base;
130    struct evdns_base          * evdns_base;
131    struct tr_event_handle     * events;
132
133    uint16_t                     peerLimitPerTorrent;
134
135    int                          uploadSlotsPerTorrent;
136
137    /* The UDP sockets used for the DHT and uTP. */
138    tr_port                      udp_port;
139    int                          udp_socket;
140    int                          udp6_socket;
141    unsigned char *              udp6_bound;
142    struct event                 *udp_event;
143    struct event                 *udp6_event;
144
145    time_t                       lpdUpkeepAt;
146
147    /* The open port on the local machine for incoming peer requests */
148    tr_port                      private_peer_port;
149
150    /**
151     * The open port on the public device for incoming peer requests.
152     * This is usually the same as private_peer_port but can differ
153     * if the public device is a router and it decides to use a different
154     * port than the one requested by Transmission.
155     */
156    tr_port                      public_peer_port;
157
158    tr_port                      randomPortLow;
159    tr_port                      randomPortHigh;
160
161    int                          peerSocketTOS;
162    char *                       peer_congestion_algorithm;
163
164    int                          torrentCount;
165    tr_torrent *                 torrentList;
166
167    char *                       torrentDoneScript;
168
169    char *                       tag;
170    char *                       configDir;
171    char *                       downloadDir;
172    char *                       resumeDir;
173    char *                       torrentDir;
174    char *                       incompleteDir;
175
176    char *                       blocklist_url;
177
178    struct tr_list *             blocklists;
179    struct tr_peerMgr *          peerMgr;
180    struct tr_shared *           shared;
181
182    struct tr_cache *            cache;
183
184    struct tr_lock *             lock;
185
186    struct tr_web *              web;
187
188    struct tr_rpc_server *       rpcServer;
189    tr_rpc_func                  rpc_func;
190    void *                       rpc_func_user_data;
191
192    struct tr_stats_handle     * sessionStats;
193
194    struct tr_announcer        * announcer;
195    struct tr_announcer_udp    * announcer_udp;
196
197    tr_benc                    * metainfoLookup;
198
199    struct event               * nowTimer;
200    struct event               * saveTimer;
201
202    /* monitors the "global pool" speeds */
203    struct tr_bandwidth        * bandwidth;
204
205    double                       desiredRatio;
206
207    uint16_t                     idleLimitMinutes;
208
209    struct tr_bindinfo         * public_ipv4;
210    struct tr_bindinfo         * public_ipv6;
211
212    /* a page-aligned buffer for use by the libtransmission thread.
213     * @see SESSION_BUFFER_SIZE */
214    void * buffer;
215
216    tr_bool bufferInUse;
217
218    tr_web_config_func          * curl_easy_config_func;
219
220    uint8_t peer_id[PEER_ID_LEN+1];
221};
222
223static inline tr_port
224tr_sessionGetPublicPeerPort( const tr_session * session )
225{
226    return session->public_peer_port;
227}
228
229static inline const uint8_t*
230tr_getPeerId( tr_session * session )
231{
232    return session->peer_id;
233}
234
235tr_bool      tr_sessionAllowsDHT( const tr_session * session );
236
237tr_bool      tr_sessionAllowsLPD( const tr_session * session );
238
239const char * tr_sessionFindTorrentFile( const tr_session * session,
240                                        const char *       hashString );
241
242void         tr_sessionSetTorrentFile( tr_session * session,
243                                       const char * hashString,
244                                       const char * filename );
245
246tr_bool      tr_sessionIsAddressBlocked( const tr_session        * session,
247                                         const struct tr_address * addr );
248
249void         tr_sessionLock( tr_session * );
250
251void         tr_sessionUnlock( tr_session * );
252
253tr_bool      tr_sessionIsLocked( const tr_session * );
254
255const struct tr_address*  tr_sessionGetPublicAddress( const tr_session  * session,
256                                                      int                 tr_af_type,
257                                                      tr_bool           * is_default_value );
258
259
260struct tr_bindsockets * tr_sessionGetBindSockets( tr_session * );
261
262int tr_sessionCountTorrents( const tr_session * session );
263
264enum
265{
266    SESSION_MAGIC_NUMBER = 3845,
267
268    /* @see tr_session.buffer */
269    SESSION_BUFFER_SIZE = (16*1024)
270};
271
272void* tr_sessionGetBuffer( tr_session * session );
273
274void tr_sessionReleaseBuffer( tr_session * session );
275
276static inline tr_bool tr_isSession( const tr_session * session )
277{
278    return ( session != NULL ) && ( session->magicNumber == SESSION_MAGIC_NUMBER );
279}
280
281static inline tr_bool tr_isPreallocationMode( tr_preallocation_mode m  )
282{
283    return ( m == TR_PREALLOCATE_NONE )
284        || ( m == TR_PREALLOCATE_SPARSE )
285        || ( m == TR_PREALLOCATE_FULL );
286}
287
288static inline tr_bool tr_isEncryptionMode( tr_encryption_mode m )
289{
290    return ( m == TR_CLEAR_PREFERRED )
291        || ( m == TR_ENCRYPTION_PREFERRED )
292        || ( m == TR_ENCRYPTION_REQUIRED );
293}
294
295static inline tr_bool tr_isPriority( tr_priority_t p )
296{
297    return ( p == TR_PRI_LOW )
298        || ( p == TR_PRI_NORMAL )
299        || ( p == TR_PRI_HIGH );
300}
301
302/***
303****
304***/
305
306static inline unsigned int toSpeedBytes ( unsigned int KBps ) { return KBps * tr_speed_K; }
307static inline double       toSpeedKBps  ( unsigned int Bps )  { return Bps / (double)tr_speed_K; }
308
309static inline uint64_t toMemBytes ( unsigned int MB ) { uint64_t B = tr_mem_K * tr_mem_K; B *= MB; return B; }
310static inline int      toMemMB    ( uint64_t B )      { return B / ( tr_mem_K * tr_mem_K ); }
311
312/**
313**/
314
315int  tr_sessionGetSpeedLimit_Bps( const tr_session *, tr_direction );
316int  tr_sessionGetAltSpeed_Bps  ( const tr_session *, tr_direction );
317int  tr_sessionGetRawSpeed_Bps  ( const tr_session *, tr_direction );
318int  tr_sessionGetPieceSpeed_Bps( const tr_session *, tr_direction );
319
320void tr_sessionSetSpeedLimit_Bps( tr_session *, tr_direction, int Bps );
321void tr_sessionSetAltSpeed_Bps  ( tr_session *, tr_direction, int Bps );
322
323tr_bool  tr_sessionGetActiveSpeedLimit_Bps( const tr_session  * session,
324                                            tr_direction        dir,
325                                            int               * setme );
326
327#endif
Note: See TracBrowser for help on using the repository browser.