PeerSessionResource.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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_SESSION_RESOURCE_H
  36. #define D_PEER_SESSION_RESOURCE_H
  37. #include "common.h"
  38. #include <string>
  39. #include <set>
  40. #include <memory>
  41. #include "BtConstants.h"
  42. #include "NetStat.h"
  43. #include "TimerA2.h"
  44. #include "ExtensionMessageRegistry.h"
  45. namespace aria2 {
  46. class BitfieldMan;
  47. class BtMessageDispatcher;
  48. class PeerSessionResource {
  49. private:
  50. std::unique_ptr<BitfieldMan> bitfieldMan_;
  51. // fast index set which a peer has sent to localhost.
  52. std::set<size_t> peerAllowedIndexSet_;
  53. // fast index set which localhost has sent to a peer.
  54. std::set<size_t> amAllowedIndexSet_;
  55. ExtensionMessageRegistry extreg_;
  56. NetStat netStat_;
  57. Timer lastDownloadUpdate_;
  58. Timer lastAmUnchoking_;
  59. BtMessageDispatcher* dispatcher_;
  60. // localhost is choking this peer
  61. bool amChoking_;
  62. // localhost is interested in this peer
  63. bool amInterested_;
  64. // this peer is choking localhost
  65. bool peerChoking_;
  66. // this peer is interested in localhost
  67. bool peerInterested_;
  68. // choking this peer is requested
  69. bool chokingRequired_;
  70. // this peer is eligible for *optional* unchoking.
  71. bool optUnchoking_;
  72. // this peer is snubbing.
  73. bool snubbing_;
  74. bool fastExtensionEnabled_;
  75. bool extendedMessagingEnabled_;
  76. bool dhtEnabled_;
  77. public:
  78. PeerSessionResource(int32_t pieceLength, int64_t totalLength);
  79. ~PeerSessionResource();
  80. // localhost is choking this peer
  81. bool amChoking() const { return amChoking_; }
  82. void amChoking(bool b);
  83. // localhost is interested in this peer
  84. bool amInterested() const { return amInterested_; }
  85. void amInterested(bool b);
  86. // this peer is choking localhost
  87. bool peerChoking() const { return peerChoking_; }
  88. void peerChoking(bool b);
  89. // this peer is interested in localhost
  90. bool peerInterested() const { return peerInterested_; }
  91. void peerInterested(bool b);
  92. // this peer should be choked
  93. bool chokingRequired() const { return chokingRequired_; }
  94. void chokingRequired(bool b);
  95. // this peer is eligible for unchoking optionally.
  96. bool optUnchoking() const { return optUnchoking_; }
  97. void optUnchoking(bool b);
  98. bool shouldBeChoking() const;
  99. // this peer is snubbing.
  100. bool snubbing() const { return snubbing_; }
  101. void snubbing(bool b);
  102. bool hasAllPieces() const;
  103. void updateBitfield(size_t index, int operation);
  104. void setBitfield(const unsigned char* bitfield, size_t bitfieldLength);
  105. const unsigned char* getBitfield() const;
  106. size_t getBitfieldLength() const;
  107. void reconfigure(int32_t pieceLength, int64_t totalLength);
  108. bool hasPiece(size_t index) const;
  109. void markSeeder();
  110. bool fastExtensionEnabled() const { return fastExtensionEnabled_; }
  111. void fastExtensionEnabled(bool b);
  112. // fast index set which a peer has sent to localhost.
  113. const std::set<size_t>& peerAllowedIndexSet() const;
  114. void addPeerAllowedIndex(size_t index);
  115. bool peerAllowedIndexSetContains(size_t index) const;
  116. // fast index set which localhost has sent to a peer.
  117. const std::set<size_t>& amAllowedIndexSet() const
  118. {
  119. return amAllowedIndexSet_;
  120. }
  121. void addAmAllowedIndex(size_t index);
  122. bool amAllowedIndexSetContains(size_t index) const;
  123. bool extendedMessagingEnabled() const { return extendedMessagingEnabled_; }
  124. void extendedMessagingEnabled(bool b);
  125. uint8_t getExtensionMessageID(int key) const;
  126. const char* getExtensionName(uint8_t id) const;
  127. void addExtension(int key, uint8_t id);
  128. bool dhtEnabled() const { return dhtEnabled_; }
  129. void dhtEnabled(bool b);
  130. NetStat& getNetStat() { return netStat_; }
  131. int64_t uploadLength() const;
  132. void updateUploadSpeed(int32_t bytes);
  133. void updateUploadLength(int32_t bytes);
  134. int64_t downloadLength() const;
  135. void updateDownload(int32_t bytes);
  136. const Timer& getLastDownloadUpdate() const { return lastDownloadUpdate_; }
  137. const Timer& getLastAmUnchoking() const { return lastAmUnchoking_; }
  138. int64_t getCompletedLength() const;
  139. void setBtMessageDispatcher(BtMessageDispatcher* dpt);
  140. size_t countOutstandingUpload() const;
  141. };
  142. } // namespace aria2
  143. #endif // D_PEER_SESSION_RESOURCE_H