libktorrent  2.2.0
utpex.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 BTUTPEX_H
21 #define BTUTPEX_H
22 
23 #include <map>
24 #include <ktorrent_export.h>
25 #include <net/address.h>
26 #include <util/constants.h>
27 #include <peer/peermanager.h>
28 #include "peerprotocolextension.h"
29 
30 namespace bt
31 {
32  class Peer;
33  class BEncoder;
34 
40  class KTORRENT_EXPORT UTPex : public PeerProtocolExtension, public PeerManager::PeerVisitor
41  {
42  public:
43  UTPex(Peer* peer,Uint32 id);
44  ~UTPex() override;
45 
51  void handlePacket(const Uint8* packet,Uint32 size) override;
52 
54  bool needsUpdate() const override;
55 
57  void update() override;
58 
60  void changeID(Uint32 nid) {id = nid;}
61 
63  static void setEnabled(bool on) {pex_enabled = on;}
64 
66  static bool isEnabled() {return pex_enabled;}
67  private:
68  void encode(BEncoder & enc,const std::map<Uint32,net::Address> & ps);
69  void encodeFlags(BEncoder & enc,const std::map<Uint32,Uint8> & flags);
70  void visit(const bt::Peer::Ptr p) override;
71 
72  private:
73  std::map<Uint32,net::Address> peers;
74  TimeStamp last_updated;
75  static bool pex_enabled;
76 
77  std::map<Uint32,net::Address> added;
78  std::map<Uint32,Uint8> flags;
79  std::map<Uint32,net::Address> npeers;
80  };
81 
82 }
83 
84 #endif
bt::BEncoder
Helper class to b-encode stuff.
Definition: bencoder.h:92
bt::UTPex::isEnabled
static bool isEnabled()
Is PEX enabled globally.
Definition: utpex.h:66
bt::Peer
Manages the connection with a peer.
Definition: peer.h:59
bt::UTPex::needsUpdate
bool needsUpdate() const override
Do we need to update PEX (should happen every minute)
bt::UTPex::setEnabled
static void setEnabled(bool on)
Globally disable or enabled PEX.
Definition: utpex.h:63
bt::PeerProtocolExtension
Definition: peerprotocolextension.h:38
bt::UTPex
Definition: utpex.h:41
bt::UTPex::changeID
void changeID(Uint32 nid)
Change the ID used in the extended packets.
Definition: utpex.h:60
bt::PeerManager::PeerVisitor
Definition: peermanager.h:218
bt::UTPex::handlePacket
void handlePacket(const Uint8 *packet, Uint32 size) override
bt::UTPex::update
void update() override
Send a new PEX packet to the Peer.