libktorrent  2.2.0
peer.h
1 /***************************************************************************
2  * Copyright (C) 2005 by Joris Guisson *
3  * joris.guisson@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 #ifndef BTPEER_H
21 #define BTPEER_H
22 
23 #include <QObject>
24 #include <QDateTime>
25 #include <util/timer.h>
26 #include <interfaces/peerinterface.h>
27 #include <util/ptrmap.h>
28 #include <mse/encryptedpacketsocket.h>
29 #include <ktorrent_export.h>
30 #include "peerid.h"
31 #include "peerprotocolextension.h"
32 #include "connectionlimit.h"
33 
34 namespace net
35 {
36  class Address;
37 }
38 
39 namespace bt
40 {
41  class Peer;
42  class Piece;
43  class PacketReader;
44  class PeerDownloader;
45  class PeerUploader;
46  class PeerManager;
47  class BitSet;
48 
49 
58  class KTORRENT_EXPORT Peer : public QObject, public PeerInterface
59  {
60  Q_OBJECT
61  public:
74  Peer(mse::EncryptedPacketSocket::Ptr sock,
75  const PeerID & peer_id,
76  Uint32 num_chunks,
77  Uint32 chunk_size,
78  Uint32 support,
79  bool local,
80  ConnectionLimit::Token::Ptr token,
81  PeerManager* pman);
82 
83  ~Peer() override;
84 
86  Uint32 getID() const {return id;}
87 
89  QString getIPAddresss() const;
90 
92  Uint16 getPort() const;
93 
96 
98  bool isStalled() const;
99 
101  bool isSnubbed() const;
102 
104  Uint32 getUploadRate() const;
105 
107  Uint32 getDownloadRate() const;
108 
110  void update();
111 
113  void pause();
114 
116  void unpause();
117 
119  PeerDownloader* getPeerDownloader() const {return downloader;}
120 
122  PeerUploader* getPeerUploader() const {return uploader;}
123 
125  PeerManager* getPeerManager() {return pman;}
126 
134  Uint32 sendData(const Uint8* data,Uint32 len);
135 
142  Uint32 readData(Uint8* buf,Uint32 len);
143 
145  Uint32 bytesAvailable() const;
146 
151 
155  void kill() override;
156 
158  Uint32 getTimeSinceLastPiece() const;
159 
161  const QTime & getConnectTime() const {return connect_time;}
162 
166  float percentAvailable() const;
167 
169  void setACAScore(double s);
170 
171  bt::Uint32 averageDownloadSpeed() const override;
172 
174  void choke();
175 
180 
184  void emitPex(const QByteArray & data);
185 
187  void setPexEnabled(bool on);
188 
192  void emitMetadataDownloaded(const QByteArray & data);
193 
195  void sendExtProtHandshake(Uint16 port,Uint32 metadata_size,bool partial_seed);
196 
202  void setGroupIDs(Uint32 up_gid,Uint32 down_gid);
203 
205  static void setResolveHostnames(bool on);
206 
208  bool hasWantedChunks(const BitSet & wanted_chunks) const;
209 
213  void sendChoke();
214 
218  void sendUnchoke();
219 
225 
230 
235 
240  void sendRequest(const Request & r);
241 
246  void sendCancel(const Request & r);
247 
248 
253  void sendReject(const Request & r);
254 
259  void sendHave(Uint32 index);
260 
265  void sendAllowedFast(Uint32 index);
266 
275  bool sendChunk(Uint32 index,Uint32 begin,Uint32 len,Chunk * ch);
276 
281  void sendBitSet(const BitSet & bs);
282 
287  void sendPort(Uint16 port);
288 
290  void sendHaveAll();
291 
293  void sendHaveNone();
294 
299  void sendSuggestPiece(Uint32 index);
300 
302  void sendExtProtMsg(Uint8 id,const QByteArray & data);
303 
308 
309  void chunkAllowed(Uint32 chunk) override;
310  void handlePacket(const bt::Uint8* packet, bt::Uint32 size) override;
311 
312  typedef QSharedPointer<Peer> Ptr;
313  typedef QWeakPointer<Peer> WPtr;
314 
315  private Q_SLOTS:
316  void resolved(const QString & hinfo);
317 
318  private:
319  void handleChoke(Uint32 len);
320  void handleUnchoke(Uint32 len);
321  void handleInterested(Uint32 len);
322  void handleNotInterested(Uint32 len);
323  void handleHave(const Uint8* packet,Uint32 len);
324  void handleHaveAll(Uint32 len);
325  void handleHaveNone(Uint32 len);
326  void handleBitField(const Uint8* packet,Uint32 len);
327  void handleRequest(const Uint8* packet,Uint32 len);
328  void handlePiece(const Uint8* packet,Uint32 len);
329  void handleCancel(const Uint8* packet,Uint32 len);
330  void handleReject(const Uint8* packet,Uint32 len);
331  void handlePort(const Uint8* packet,Uint32 len);
332  void handleExtendedPacket(const Uint8* packet,Uint32 size);
333  void handleExtendedHandshake(const Uint8* packet,Uint32 size);
334 
335  Q_SIGNALS:
339  void metadataDownloaded(const QByteArray & data);
340 
341  private:
342  mse::EncryptedPacketSocket::Ptr sock;
343  ConnectionLimit::Token::Ptr token;
344 
345  Timer stalled_timer;
346 
347  Uint32 id;
348 
349  Timer snub_timer;
350  PacketReader* preader;
351  PeerDownloader* downloader;
352  PeerUploader* uploader;
353 
354  QTime connect_time;
355  bool pex_allowed;
356  PeerManager* pman;
358  Uint32 ut_pex_id;
359 
360  Uint64 bytes_downloaded_since_unchoke;
361 
362  static bool resolve_hostname;
363 
364  friend class PeerDownloader;
365  };
366 }
367 
368 #endif
369 
bt::PeerDownloader
Class which downloads pieces from a Peer.
Definition: peerdownloader.h:120
bt::PeerInterface
Interface for a Peer.
Definition: peerinterface.h:58
bt::BitSet
Simple implementation of a BitSet.
Definition: bitset.h:55
bt::Peer::metadataDownloaded
void metadataDownloaded(const QByteArray &data)
bt::Peer::getPeerDownloader
PeerDownloader * getPeerDownloader() const
Get the PeerDownloader.
Definition: peer.h:119
bt::Peer::getTimeSinceLastPiece
Uint32 getTimeSinceLastPiece() const
Get the time in milliseconds since the last time a piece was received.
bt::Peer::getUploadRate
Uint32 getUploadRate() const
Get the upload rate in bytes per sec.
bt::Peer::hasWantedChunks
bool hasWantedChunks(const BitSet &wanted_chunks) const
Check if the peer has wanted chunks.
bt::Peer::sendEvilUnchoke
void sendEvilUnchoke()
net::Address
Definition: address.h:59
bt::Peer::setResolveHostnames
static void setResolveHostnames(bool on)
Enable or disable hostname resolving.
bt::Peer::sendChunk
bool sendChunk(Uint32 index, Uint32 begin, Uint32 len, Chunk *ch)
bt::PtrMap
Map of pointers.
Definition: ptrmap.h:56
bt::Peer::sendCancel
void sendCancel(const Request &r)
bt::Peer::getConnectTime
const QTime & getConnectTime() const
Get the time the peer connection was established.
Definition: peer.h:161
bt::Peer::getIPAddresss
QString getIPAddresss() const
Get the IP address of the Peer.
bt::Peer::choke
void choke()
Choke the peer.
bt::Peer::setPexEnabled
void setPexEnabled(bool on)
Disable or enable pex.
bt::Chunk
Keep track of a piece of the file.
Definition: chunk.h:64
bt::Peer::sendData
Uint32 sendData(const Uint8 *data, Uint32 len)
bt::Peer::emitPortPacket
void emitPortPacket()
bt::Peer::clearPendingPieceUploads
void clearPendingPieceUploads()
bt::PeerManager
Manages all the Peers.
Definition: peermanager.h:67
bt::Peer::kill
void kill() override
bt::Peer::Peer
Peer(mse::EncryptedPacketSocket::Ptr sock, const PeerID &peer_id, Uint32 num_chunks, Uint32 chunk_size, Uint32 support, bool local, ConnectionLimit::Token::Ptr token, PeerManager *pman)
bt::Peer
Manages the connection with a peer.
Definition: peer.h:59
bt::Peer::setGroupIDs
void setGroupIDs(Uint32 up_gid, Uint32 down_gid)
bt::Peer::handlePacket
void handlePacket(const bt::Uint8 *packet, bt::Uint32 size) override
Handle a received packet.
bt::Peer::bytesAvailable
Uint32 bytesAvailable() const
Get the number of bytes available to read.
bt::Timer
Definition: timer.h:53
bt::Peer::sendHave
void sendHave(Uint32 index)
bt::PacketReader
Definition: packetreader.h:67
bt::Peer::sendRequest
void sendRequest(const Request &r)
bt::Peer::isSnubbed
bool isSnubbed() const
Are we being snubbed by the Peer.
bt::Peer::getPort
Uint16 getPort() const
Get the port of the Peer.
bt::Peer::getPeerUploader
PeerUploader * getPeerUploader() const
Get the PeerUploader.
Definition: peer.h:122
bt::Peer::chunkAllowed
void chunkAllowed(Uint32 chunk) override
Peer is allowed to download chunk (used for superseeding)
bt::Peer::sendHaveAll
void sendHaveAll()
Send a have all message.
bt::Peer::sendPort
void sendPort(Uint16 port)
bt::Peer::emitMetadataDownloaded
void emitMetadataDownloaded(const QByteArray &data)
bt::Request
Request of a piece sent to other peers.
Definition: request.h:58
bt::Peer::averageDownloadSpeed
bt::Uint32 averageDownloadSpeed() const override
bt::Peer::sendChoke
void sendChoke()
bt::Peer::sendInterested
void sendInterested()
bt::Peer::unpause
void unpause()
Unpause the peer connection.
bt::Peer::sendSuggestPiece
void sendSuggestPiece(Uint32 index)
bt::Peer::isStalled
bool isStalled() const
See if the peer is stalled.
bt::Peer::closeConnection
void closeConnection()
bt::Peer::getAddress
net::Address getAddress() const
Get the address of the peer.
bt::Peer::getDownloadRate
Uint32 getDownloadRate() const
Get the download rate in bytes per sec.
bt::Peer::percentAvailable
float percentAvailable() const
bt::Peer::sendAllowedFast
void sendAllowedFast(Uint32 index)
bt::Peer::sendHaveNone
void sendHaveNone()
Send a have none message.
bt::Peer::sendBitSet
void sendBitSet(const BitSet &bs)
bt::Peer::update
void update()
Update the up- and down- speed and handle incoming packets.
bt::Peer::sendExtProtMsg
void sendExtProtMsg(Uint8 id, const QByteArray &data)
Send an extended protocol message.
bt::PeerID
Definition: peerid.h:51
bt::Peer::getID
Uint32 getID() const
Get the peer's unique ID.
Definition: peer.h:86
bt::Peer::sendNotInterested
void sendNotInterested()
bt::Peer::getPeerManager
PeerManager * getPeerManager()
Get the PeerManager.
Definition: peer.h:125
bt::Peer::setACAScore
void setACAScore(double s)
Set the ACA score.
bt::Peer::readData
Uint32 readData(Uint8 *buf, Uint32 len)
bt::Peer::emitPex
void emitPex(const QByteArray &data)
bt::Peer::pause
void pause()
Pause the peer connection.
bt::Peer::sendUnchoke
void sendUnchoke()
bt::Peer::sendExtProtHandshake
void sendExtProtHandshake(Uint16 port, Uint32 metadata_size, bool partial_seed)
Send an extended protocol handshake.
bt::Peer::sendReject
void sendReject(const Request &r)
bt::PeerUploader
Uploads pieces to a Peer.
Definition: peeruploader.h:63