Peer.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #ifndef D_PEER_H
  36. #define D_PEER_H
  37. #include "common.h"
  38. #include <cassert>
  39. #include <string>
  40. #include <set>
  41. #include <algorithm>
  42. #include "TimerA2.h"
  43. #include "BtConstants.h"
  44. #include "PeerStat.h"
  45. #include "a2functional.h"
  46. #include "Command.h"
  47. namespace aria2 {
  48. class PeerSessionResource;
  49. class BtMessageDispatcher;
  50. class Peer {
  51. private:
  52. std::string ipaddr_;
  53. // TCP port of the other end of communication. If incoming_ is
  54. // true, then this port is not a port the peer is listening to and
  55. // we cannot connect to it.
  56. uint16_t port_;
  57. // This is the port number passed in the constructor arguments. This
  58. // is used to distinguish peer identity.
  59. uint16_t origPort_;
  60. cuid_t cuid_;
  61. unsigned char peerId_[PEER_ID_LENGTH];
  62. Timer firstContactTime_;
  63. Timer dropStartTime_;
  64. bool seeder_;
  65. std::unique_ptr<PeerSessionResource> res_;
  66. // If true, port is assumed not to be a listening port.
  67. bool incoming_;
  68. // If true, this peer is from local network.
  69. bool localPeer_;
  70. // If true, this peer is disconnected gracefully.
  71. bool disconnectedGracefully_;
  72. // Before calling updateSeeder(), make sure that
  73. // allocateSessionResource() is called and res_ is created.
  74. // Otherwise assertion fails.
  75. void updateSeeder();
  76. public:
  77. Peer(std::string ipaddr, uint16_t port, bool incoming = false);
  78. ~Peer();
  79. const std::string& getIPAddress() const { return ipaddr_; }
  80. uint16_t getPort() const { return port_; }
  81. void setPort(uint16_t port) { port_ = port; }
  82. uint16_t getOrigPort() const { return origPort_; }
  83. void usedBy(cuid_t cuid);
  84. cuid_t usedBy() const { return cuid_; }
  85. bool unused() const { return cuid_ == 0; }
  86. // Returns true iff res_ != 0.
  87. bool isActive() const { return res_.get() != nullptr; }
  88. void setPeerId(const unsigned char* peerId);
  89. const unsigned char* getPeerId() const { return peerId_; }
  90. bool isSeeder() const { return seeder_; }
  91. void startDrop();
  92. void allocateSessionResource(int32_t pieceLength, int64_t totalLength);
  93. void reconfigureSessionResource(int32_t pieceLength, int64_t totalLength);
  94. void releaseSessionResource();
  95. const Timer& getFirstContactTime() const { return firstContactTime_; }
  96. void setFirstContactTime(const Timer& time);
  97. const Timer& getDropStartTime() const { return dropStartTime_; }
  98. // Before calling following member functions, make sure that
  99. // allocateSessionResource() is called and res_ is created.
  100. // Otherwise assertion fails.
  101. // localhost is choking this peer
  102. bool amChoking() const;
  103. void amChoking(bool b) const;
  104. // localhost is interested in this peer
  105. bool amInterested() const;
  106. void amInterested(bool b) const;
  107. // this peer is choking localhost
  108. bool peerChoking() const;
  109. void peerChoking(bool b) const;
  110. // this peer is interested in localhost
  111. bool peerInterested() const;
  112. void peerInterested(bool b);
  113. // this peer should be choked
  114. bool chokingRequired() const;
  115. void chokingRequired(bool b);
  116. // this peer is eligible for unchoking optionally.
  117. bool optUnchoking() const;
  118. void optUnchoking(bool b);
  119. // this peer is snubbing.
  120. bool snubbing() const;
  121. void snubbing(bool b);
  122. void updateUploadSpeed(int32_t bytes);
  123. void updateUploadLength(int32_t bytes);
  124. void updateDownload(int32_t bytes);
  125. /**
  126. * Returns the transfer rate from localhost to remote host.
  127. */
  128. int calculateUploadSpeed();
  129. /**
  130. * Returns the transfer rate from remote host to localhost.
  131. */
  132. int calculateDownloadSpeed();
  133. /**
  134. * Returns the number of bytes uploaded to the remote host.
  135. */
  136. int64_t getSessionUploadLength() const;
  137. /**
  138. * Returns the number of bytes downloaded from the remote host.
  139. */
  140. int64_t getSessionDownloadLength() const;
  141. void setBitfield(const unsigned char* bitfield, size_t bitfieldLength);
  142. const unsigned char* getBitfield() const;
  143. size_t getBitfieldLength() const;
  144. void setAllBitfield();
  145. /**
  146. * operation = 1: set index-th bit to 1
  147. * operation = 0: set index-th bit to 0
  148. */
  149. void updateBitfield(size_t index, int operation);
  150. void setFastExtensionEnabled(bool enabled);
  151. bool isFastExtensionEnabled() const;
  152. void addPeerAllowedIndex(size_t index);
  153. bool isInPeerAllowedIndexSet(size_t index) const;
  154. size_t countPeerAllowedIndexSet() const;
  155. const std::set<size_t>& getPeerAllowedIndexSet() const;
  156. void addAmAllowedIndex(size_t index);
  157. bool isInAmAllowedIndexSet(size_t index) const;
  158. void setExtendedMessagingEnabled(bool enabled);
  159. bool isExtendedMessagingEnabled() const;
  160. void setDHTEnabled(bool enabled);
  161. bool isDHTEnabled() const;
  162. bool shouldBeChoking() const;
  163. bool hasPiece(size_t index) const;
  164. uint8_t getExtensionMessageID(int key) const;
  165. const char* getExtensionName(uint8_t id) const;
  166. void setExtension(int key, uint8_t id);
  167. const Timer& getLastDownloadUpdate() const;
  168. const Timer& getLastAmUnchoking() const;
  169. int64_t getCompletedLength() const;
  170. bool isIncomingPeer() const { return incoming_; }
  171. void setIncomingPeer(bool incoming);
  172. bool isLocalPeer() const { return localPeer_; }
  173. void setLocalPeer(bool flag) { localPeer_ = flag; }
  174. bool isDisconnectedGracefully() const { return disconnectedGracefully_; }
  175. void setDisconnectedGracefully(bool f) { disconnectedGracefully_ = f; }
  176. void setBtMessageDispatcher(BtMessageDispatcher* dpt);
  177. size_t countOutstandingUpload() const;
  178. };
  179. template <typename InputIterator>
  180. size_t countSeeder(InputIterator first, InputIterator last)
  181. {
  182. size_t res = 0;
  183. for (; first != last; ++first) {
  184. if ((*first)->isActive() && (*first)->isSeeder()) {
  185. ++res;
  186. }
  187. }
  188. return res;
  189. }
  190. } // namespace aria2
  191. #endif // D_PEER_H