PeerSessionResource.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 <vector>
  40. #include "BtConstants.h"
  41. #include "PeerStat.h"
  42. #include "TimerA2.h"
  43. namespace aria2 {
  44. class BitfieldMan;
  45. class BtMessageDispatcher;
  46. class PeerSessionResource {
  47. private:
  48. // localhost is choking this peer
  49. bool _amChoking;
  50. // localhost is interested in this peer
  51. bool _amInterested;
  52. // this peer is choking localhost
  53. bool _peerChoking;
  54. // this peer is interested in localhost
  55. bool _peerInterested;
  56. // choking this peer is requested
  57. bool _chokingRequired;
  58. // this peer is eligible for *optional* unchoking.
  59. bool _optUnchoking;
  60. // this peer is snubbing.
  61. bool _snubbing;
  62. BitfieldMan* _bitfieldMan;
  63. bool _fastExtensionEnabled;
  64. // fast index set which a peer has sent to localhost.
  65. std::vector<size_t> _peerAllowedIndexSet;
  66. // fast index set which localhost has sent to a peer.
  67. std::vector<size_t> _amAllowedIndexSet;
  68. bool _extendedMessagingEnabled;
  69. Extensions _extensions;
  70. bool _dhtEnabled;
  71. PeerStat _peerStat;
  72. Timer _lastDownloadUpdate;
  73. Timer _lastAmUnchoking;
  74. WeakHandle<BtMessageDispatcher> _dispatcher;
  75. public:
  76. PeerSessionResource(size_t pieceLength, uint64_t totalLength);
  77. ~PeerSessionResource();
  78. // localhost is choking this peer
  79. bool amChoking() const
  80. {
  81. return _amChoking;
  82. }
  83. void amChoking(bool b);
  84. // localhost is interested in this peer
  85. bool amInterested() const
  86. {
  87. return _amInterested;
  88. }
  89. void amInterested(bool b);
  90. // this peer is choking localhost
  91. bool peerChoking() const
  92. {
  93. return _peerChoking;
  94. }
  95. void peerChoking(bool b);
  96. // this peer is interested in localhost
  97. bool peerInterested() const
  98. {
  99. return _peerInterested;
  100. }
  101. void peerInterested(bool b);
  102. // this peer should be choked
  103. bool chokingRequired() const
  104. {
  105. return _chokingRequired;
  106. }
  107. void chokingRequired(bool b);
  108. // this peer is eligible for unchoking optionally.
  109. bool optUnchoking() const
  110. {
  111. return _optUnchoking;
  112. }
  113. void optUnchoking(bool b);
  114. bool shouldBeChoking() const;
  115. // this peer is snubbing.
  116. bool snubbing() const
  117. {
  118. return _snubbing;
  119. }
  120. void snubbing(bool b);
  121. bool hasAllPieces() const;
  122. void updateBitfield(size_t index, int operation);
  123. void setBitfield(const unsigned char* bitfield, size_t bitfieldLength);
  124. const unsigned char* getBitfield() const;
  125. size_t getBitfieldLength() const;
  126. bool hasPiece(size_t index) const;
  127. void markSeeder();
  128. bool fastExtensionEnabled() const
  129. {
  130. return _fastExtensionEnabled;
  131. }
  132. void fastExtensionEnabled(bool b);
  133. // fast index set which a peer has sent to localhost.
  134. const std::vector<size_t>& peerAllowedIndexSet() const;
  135. void addPeerAllowedIndex(size_t index);
  136. bool peerAllowedIndexSetContains(size_t index) const;
  137. // fast index set which localhost has sent to a peer.
  138. const std::vector<size_t>& amAllowedIndexSet() const
  139. {
  140. return _amAllowedIndexSet;
  141. }
  142. void addAmAllowedIndex(size_t index);
  143. bool amAllowedIndexSetContains(size_t index) const;
  144. bool extendedMessagingEnabled() const
  145. {
  146. return _extendedMessagingEnabled;
  147. }
  148. void extendedMessagingEnabled(bool b);
  149. uint8_t getExtensionMessageID(const std::string& name) const;
  150. std::string getExtensionName(uint8_t id) const;
  151. void addExtension(const std::string& name, uint8_t id);
  152. bool dhtEnabled() const
  153. {
  154. return _dhtEnabled;
  155. }
  156. void dhtEnabled(bool b);
  157. PeerStat& getPeerStat()
  158. {
  159. return _peerStat;
  160. }
  161. uint64_t uploadLength() const;
  162. void updateUploadLength(size_t bytes);
  163. uint64_t downloadLength() const;
  164. void updateDownloadLength(size_t bytes);
  165. const Timer& getLastDownloadUpdate() const
  166. {
  167. return _lastDownloadUpdate;
  168. }
  169. const Timer& getLastAmUnchoking() const
  170. {
  171. return _lastAmUnchoking;
  172. }
  173. uint64_t getCompletedLength() const;
  174. void setBtMessageDispatcher(const WeakHandle<BtMessageDispatcher>& dpt);
  175. size_t countOutstandingUpload() const;
  176. };
  177. } // namespace aria2
  178. #endif // _D_PEER_SESSION_RESOURCE_H_