2008-06-07 21:26:41 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2008-2014 Mnemosyne LLC
|
2008-06-07 21:26:41 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2008-06-07 21:26:41 +00:00
|
|
|
*
|
|
|
|
*/
|
2008-11-24 20:17:36 +00:00
|
|
|
|
2017-11-14 20:21:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2008-11-24 20:17:36 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
2017-04-19 12:04:45 +00:00
|
|
|
#error only libtransmission should #include this header.
|
2008-11-24 20:17:36 +00:00
|
|
|
#endif
|
2008-06-07 21:26:41 +00:00
|
|
|
|
|
|
|
#include "transmission.h"
|
2013-02-03 22:40:00 +00:00
|
|
|
#include "bitfield.h"
|
|
|
|
#include "history.h"
|
|
|
|
#include "quark.h"
|
2011-02-17 05:14:53 +00:00
|
|
|
|
2013-02-03 23:29:34 +00:00
|
|
|
/**
|
|
|
|
* @addtogroup peers Peers
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2021-10-07 13:33:55 +00:00
|
|
|
class tr_peer;
|
2021-10-10 00:13:40 +00:00
|
|
|
class tr_swarm;
|
2021-10-07 13:33:55 +00:00
|
|
|
struct peer_atom;
|
2013-02-04 16:23:33 +00:00
|
|
|
|
2021-10-07 13:33:55 +00:00
|
|
|
/* This is the maximum size of a block request.
|
|
|
|
most bittorrent clients will reject requests
|
|
|
|
larger than this size. */
|
|
|
|
auto inline constexpr MAX_BLOCK_SIZE = 1024 * 16;
|
2009-11-08 23:20:00 +00:00
|
|
|
|
2008-06-07 21:26:41 +00:00
|
|
|
/**
|
|
|
|
*** Peer Publish / Subscribe
|
|
|
|
**/
|
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
enum PeerEventType
|
2008-06-07 21:26:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
TR_PEER_CLIENT_GOT_BLOCK,
|
|
|
|
TR_PEER_CLIENT_GOT_CHOKE,
|
|
|
|
TR_PEER_CLIENT_GOT_PIECE_DATA,
|
|
|
|
TR_PEER_CLIENT_GOT_ALLOWED_FAST,
|
|
|
|
TR_PEER_CLIENT_GOT_SUGGEST,
|
|
|
|
TR_PEER_CLIENT_GOT_PORT,
|
|
|
|
TR_PEER_CLIENT_GOT_REJ,
|
|
|
|
TR_PEER_CLIENT_GOT_BITFIELD,
|
|
|
|
TR_PEER_CLIENT_GOT_HAVE,
|
|
|
|
TR_PEER_CLIENT_GOT_HAVE_ALL,
|
|
|
|
TR_PEER_CLIENT_GOT_HAVE_NONE,
|
|
|
|
TR_PEER_PEER_GOT_PIECE_DATA,
|
|
|
|
TR_PEER_ERROR
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2008-06-07 21:26:41 +00:00
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
struct tr_peer_event
|
2008-06-07 21:26:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
PeerEventType eventType;
|
|
|
|
|
|
|
|
uint32_t pieceIndex; /* for GOT_BLOCK, GOT_HAVE, CANCEL, ALLOWED, SUGGEST */
|
2021-10-11 17:29:14 +00:00
|
|
|
Bitfield* bitfield; /* for GOT_BITFIELD */
|
2017-04-19 12:04:45 +00:00
|
|
|
uint32_t offset; /* for GOT_BLOCK */
|
|
|
|
uint32_t length; /* for GOT_BLOCK + GOT_PIECE_DATA */
|
|
|
|
int err; /* errno for GOT_ERROR */
|
|
|
|
tr_port port; /* for GOT_PORT */
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2008-06-07 21:26:41 +00:00
|
|
|
|
2021-10-07 13:33:55 +00:00
|
|
|
using tr_peer_callback = void (*)(tr_peer* peer, tr_peer_event const* event, void* client_data);
|
2013-02-04 16:23:33 +00:00
|
|
|
|
2013-02-03 22:40:00 +00:00
|
|
|
/**
|
|
|
|
* State information about a connected peer.
|
|
|
|
*
|
|
|
|
* @see struct peer_atom
|
2013-02-04 16:23:33 +00:00
|
|
|
* @see tr_peerMsgs
|
2013-02-03 22:40:00 +00:00
|
|
|
*/
|
2021-10-07 13:33:55 +00:00
|
|
|
class tr_peer
|
2013-02-03 22:40:00 +00:00
|
|
|
{
|
2021-10-07 13:33:55 +00:00
|
|
|
public:
|
|
|
|
tr_peer(tr_torrent const* tor, peer_atom* atom = nullptr);
|
|
|
|
virtual ~tr_peer();
|
|
|
|
|
|
|
|
virtual bool is_transferring_pieces(uint64_t now, tr_direction direction, unsigned int* setme_Bps) const = 0;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* whether or not we should free this peer soon.
|
|
|
|
NOTE: private to peer-mgr.c */
|
2021-10-07 13:33:55 +00:00
|
|
|
bool doPurge = false;
|
2013-02-03 22:40:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* number of bad pieces they've contributed to */
|
2021-10-07 13:33:55 +00:00
|
|
|
uint8_t strikes = 0;
|
2013-02-03 22:40:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* how many requests the peer has made that we haven't responded to yet */
|
2021-10-07 13:33:55 +00:00
|
|
|
int pendingReqsToClient = 0;
|
2013-02-03 22:40:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* how many requests we've made and are currently awaiting a response for */
|
2021-10-07 13:33:55 +00:00
|
|
|
int pendingReqsToPeer = 0;
|
|
|
|
|
|
|
|
tr_session* const session;
|
2013-02-03 22:40:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Hook to private peer-mgr information */
|
2021-10-07 13:33:55 +00:00
|
|
|
peer_atom* const atom;
|
2013-02-03 22:40:00 +00:00
|
|
|
|
2021-10-07 13:33:55 +00:00
|
|
|
tr_swarm* const swarm;
|
2013-02-04 16:23:33 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/** how complete the peer's copy of the torrent is. [0.0...1.0] */
|
2021-10-07 13:33:55 +00:00
|
|
|
float progress = 0.0f;
|
2013-02-03 22:40:00 +00:00
|
|
|
|
2021-10-11 17:29:14 +00:00
|
|
|
Bitfield blame;
|
|
|
|
Bitfield have;
|
2013-02-03 22:40:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* the client name.
|
|
|
|
For BitTorrent peers, this is the app name derived from the `v' string in LTEP's handshake dictionary */
|
2021-10-07 13:33:55 +00:00
|
|
|
tr_quark client = TR_KEY_NONE;
|
2013-02-03 22:40:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_recentHistory blocksSentToClient;
|
|
|
|
tr_recentHistory blocksSentToPeer;
|
2013-02-03 22:40:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_recentHistory cancelsSentToClient;
|
|
|
|
tr_recentHistory cancelsSentToPeer;
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2010-06-19 14:33:10 +00:00
|
|
|
|
2011-02-24 14:35:45 +00:00
|
|
|
/** Update the tr_peer.progress field based on the 'have' bitset. */
|
2021-10-07 13:33:55 +00:00
|
|
|
void tr_peerUpdateProgress(tr_torrent* tor, tr_peer*);
|
2011-02-24 14:35:45 +00:00
|
|
|
|
2021-10-07 13:33:55 +00:00
|
|
|
bool tr_peerIsSeed(tr_peer const* peer);
|
2013-07-08 16:41:12 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
struct tr_swarm_stats
|
2013-07-08 16:41:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int activePeerCount[2];
|
|
|
|
int activeWebseedCount;
|
|
|
|
int peerCount;
|
|
|
|
int peerFromCount[TR_PEER_FROM__MAX];
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2013-07-08 16:41:12 +00:00
|
|
|
|
2021-10-10 00:13:40 +00:00
|
|
|
void tr_swarmGetStats(tr_swarm const* swarm, tr_swarm_stats* setme);
|
2013-07-08 16:41:12 +00:00
|
|
|
|
2021-10-10 00:13:40 +00:00
|
|
|
void tr_swarmIncrementActivePeers(tr_swarm* swarm, tr_direction direction, bool is_active);
|
2013-07-08 16:41:12 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
#undef EMSGSIZE
|
|
|
|
#define EMSGSIZE WSAEMSGSIZE
|
2008-10-10 00:38:37 +00:00
|
|
|
#endif
|
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/** @} */
|