source: trunk/libtransmission/peer-mgr.h @ 12248

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

(trunk libT) break the mac build and introduce new crashes.

This is partially to address #4145 "Downloads stuck at 100%" by refactoring the bitset, bitfield, and tr_completion; however, the ripple effect is larger than usual so things may get worse in the short term before getting better.

livings124: to fix the mac build, remove bitset.[ch] from xcode

  • Property svn:keywords set to Date Rev Author Id
File size: 7.5 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: peer-mgr.h 12248 2011-03-28 16:31:05Z jordan $
11 */
12
13#ifndef __TRANSMISSION__
14#error only libtransmission should #include this header.
15#endif
16
17#ifndef TR_PEER_MGR_H
18#define TR_PEER_MGR_H
19
20#include <inttypes.h> /* uint16_t */
21
22#ifdef WIN32
23 #include <winsock2.h> /* struct in_addr */
24#endif
25
26#include "bitfield.h"
27#include "history.h"
28#include "net.h" /* tr_address */
29#include "peer-common.h" /* struct peer_request */
30
31/**
32 * @addtogroup peers Peers
33 * @{
34 */
35
36struct UTPSocket;
37struct tr_peer_stat;
38struct tr_torrent;
39typedef struct tr_peerMgr tr_peerMgr;
40
41/* added_f's bitwise-or'ed flags */
42enum
43{
44    /* true if the peer supports encryption */
45    ADDED_F_ENCRYPTION_FLAG = 1,
46
47    /* true if the peer is a seed or partial seed */
48    ADDED_F_SEED_FLAG = 2,
49
50    /* true if the peer supports uTP */
51    ADDED_F_UTP_FLAGS = 4,
52
53    /* true if the peer has holepunch support */
54    ADDED_F_HOLEPUNCH = 8,
55
56    /* true if the peer telling us about this peer
57     * initiated the connection (implying that it is connectible) */
58    ADDED_F_CONNECTABLE = 16
59};
60
61typedef struct tr_pex
62{
63    tr_address addr;
64    tr_port    port; /* this field is in network byte order */
65    uint8_t    flags;
66}
67tr_pex;
68
69
70struct tr_peerIo;
71struct tr_peermsgs;
72
73enum
74{
75    ENCRYPTION_PREFERENCE_UNKNOWN,
76    ENCRYPTION_PREFERENCE_YES,
77    ENCRYPTION_PREFERENCE_NO
78};
79
80/* opaque forward declaration */
81struct peer_atom;
82
83/**
84 * State information about a connected peer.
85 *
86 * @see struct peer_atom
87 * @see tr_peermsgs
88 */
89typedef struct tr_peer
90{
91    bool                     peerIsChoked;
92    bool                     peerIsInterested;
93    bool                     clientIsChoked;
94    bool                     clientIsInterested;
95    bool                     doPurge;
96
97    /* number of bad pieces they've contributed to */
98    uint8_t                  strikes;
99
100    uint8_t                  encryption_preference;
101    tr_port                  dht_port;
102
103    /* how many requests the peer has made that we haven't responded to yet */
104    int                      pendingReqsToClient;
105
106    /* how many requests we've made and are currently awaiting a response for */
107    int                      pendingReqsToPeer;
108
109    struct tr_peerIo       * io;
110    struct peer_atom       * atom;
111
112    struct tr_bitfield       blame;
113    struct tr_bitfield       have;
114
115    /** how complete the peer's copy of the torrent is. [0.0...1.0] */
116    float                    progress;
117
118    /* the client name from the `v' string in LTEP's handshake dictionary */
119    char                   * client;
120
121    time_t                   chokeChangedAt;
122
123    tr_recentHistory         blocksSentToClient;
124    tr_recentHistory         blocksSentToPeer;
125
126    tr_recentHistory         cancelsSentToClient;
127    tr_recentHistory         cancelsSentToPeer;
128
129    struct tr_peermsgs     * msgs;
130}
131tr_peer;
132
133void tr_peerConstruct( struct tr_peer * peer );
134
135void tr_peerDestruct( tr_torrent * tor, struct tr_peer * peer );
136
137
138static inline bool
139tr_isPex( const tr_pex * pex )
140{
141    return pex && tr_address_is_valid( &pex->addr );
142}
143
144const tr_address * tr_peerAddress( const tr_peer * );
145
146int tr_pexCompare( const void * a, const void * b );
147
148tr_peerMgr* tr_peerMgrNew( tr_session * );
149
150void tr_peerMgrFree( tr_peerMgr * manager );
151
152bool tr_peerMgrPeerIsSeed( const tr_torrent * tor,
153                              const tr_address * addr );
154
155void tr_peerMgrSetUtpSupported( tr_torrent       * tor,
156                                const tr_address * addr );
157
158void tr_peerMgrSetUtpFailed( tr_torrent *tor,
159                             const tr_address *addr,
160                             bool failed );
161
162void tr_peerMgrGetNextRequests( tr_torrent          * torrent,
163                                tr_peer             * peer,
164                                int                   numwant,
165                                tr_block_index_t    * setme,
166                                int                 * numgot );
167
168bool tr_peerMgrDidPeerRequest( const tr_torrent  * torrent,
169                               const tr_peer     * peer,
170                               tr_block_index_t    block );
171
172void tr_peerMgrRebuildRequests( tr_torrent * torrent );
173
174void tr_peerMgrAddIncoming( tr_peerMgr  * manager,
175                            tr_address  * addr,
176                            tr_port       port,
177                            int           socket,
178                            struct UTPSocket *utp_socket );
179
180tr_pex * tr_peerMgrCompactToPex( const void    * compact,
181                                 size_t          compactLen,
182                                 const uint8_t * added_f,
183                                 size_t          added_f_len,
184                                 size_t        * setme_pex_count );
185
186tr_pex * tr_peerMgrCompact6ToPex( const void    * compact,
187                                  size_t          compactLen,
188                                  const uint8_t * added_f,
189                                  size_t          added_f_len,
190                                  size_t        * pexCount );
191
192tr_pex * tr_peerMgrArrayToPex( const void * array,
193                               size_t       arrayLen,
194                               size_t      * setme_pex_count );
195
196/**
197 * @param seedProbability [0..100] for likelihood that the peer is a seed; -1 for unknown
198 */
199void tr_peerMgrAddPex( tr_torrent     * tor,
200                       uint8_t          from,
201                       const tr_pex   * pex,
202                       int8_t           seedProbability );
203
204void tr_peerMgrMarkAllAsSeeds( tr_torrent * tor );
205
206enum
207{
208    TR_PEERS_CONNECTED,
209    TR_PEERS_ALL
210};
211
212int  tr_peerMgrGetPeers( tr_torrent      * tor,
213                         tr_pex         ** setme_pex,
214                         uint8_t           address_type,
215                         uint8_t           peer_list_mode,
216                         int               max_peer_count );
217
218void tr_peerMgrStartTorrent( tr_torrent * tor );
219
220void tr_peerMgrStopTorrent( tr_torrent * tor );
221
222void tr_peerMgrAddTorrent( tr_peerMgr         * manager,
223                           struct tr_torrent  * tor );
224
225void tr_peerMgrRemoveTorrent( tr_torrent * tor );
226
227void tr_peerMgrTorrentAvailability( const tr_torrent * tor,
228                                    int8_t           * tab,
229                                    unsigned int       tabCount );
230
231uint64_t tr_peerMgrGetDesiredAvailable( const tr_torrent * tor );
232
233void tr_peerMgrOnTorrentGotMetainfo( tr_torrent * tor );
234
235void tr_peerMgrOnBlocklistChanged( tr_peerMgr * manager );
236
237void tr_peerMgrTorrentStats( tr_torrent * tor,
238                             int * setmePeersConnected,
239                             int * setmeWebseedsSendingToUs,
240                             int * setmePeersSendingToUs,
241                             int * setmePeersGettingFromUs,
242                             int * setmePeersFrom ); /* TR_PEER_FROM__MAX */
243
244struct tr_peer_stat* tr_peerMgrPeerStats( const tr_torrent * tor,
245                                          int              * setmeCount );
246
247double* tr_peerMgrWebSpeeds_KBps( const tr_torrent * tor );
248
249
250int tr_peerGetPieceSpeed_Bps( const tr_peer    * peer,
251                              uint64_t           now,
252                              tr_direction       direction );
253
254void tr_peerMgrClearInterest( tr_torrent * tor );
255
256/* @} */
257
258#endif
Note: See TracBrowser for help on using the repository browser.