PeerSessionResource.h 5.7 KB

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