PeerSessionResource.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 <string>
  41. #include <deque>
  42. namespace aria2 {
  43. class BitfieldMan;
  44. class PeerSessionResource {
  45. private:
  46. // localhost is choking this peer
  47. bool _amChoking;
  48. // localhost is interested in this peer
  49. bool _amInterested;
  50. // this peer is choking localhost
  51. bool _peerChoking;
  52. // this peer is interested in localhost
  53. bool _peerInterested;
  54. // choking this peer is requested
  55. bool _chokingRequired;
  56. // this peer is eligible for *optional* unchoking.
  57. bool _optUnchoking;
  58. // this peer is snubbing.
  59. bool _snubbing;
  60. BitfieldMan* _bitfieldMan;
  61. bool _fastExtensionEnabled;
  62. // fast index set which a peer has sent to localhost.
  63. std::deque<int32_t> _peerAllowedIndexSet;
  64. // fast index set which localhost has sent to a peer.
  65. std::deque<int32_t> _amAllowedIndexSet;
  66. bool _extendedMessagingEnabled;
  67. Extensions _extensions;
  68. bool _dhtEnabled;
  69. PeerStat _peerStat;
  70. int32_t _latency;
  71. int64_t _uploadLength;
  72. int64_t _downloadLength;
  73. template<typename T>
  74. bool indexIncluded(const std::deque<T>& c, T index) const;
  75. public:
  76. PeerSessionResource(int32_t pieceLength, int64_t totalLength);
  77. ~PeerSessionResource();
  78. // localhost is choking this peer
  79. bool amChoking() const;
  80. void amChoking(bool b);
  81. // localhost is interested in this peer
  82. bool amInterested() const;
  83. void amInterested(bool b);
  84. // this peer is choking localhost
  85. bool peerChoking() const;
  86. void peerChoking(bool b);
  87. // this peer is interested in localhost
  88. bool peerInterested() const;
  89. void peerInterested(bool b);
  90. // this peer should be choked
  91. bool chokingRequired() const;
  92. void chokingRequired(bool b);
  93. // this peer is eligible for unchoking optionally.
  94. bool optUnchoking() const;
  95. void optUnchoking(bool b);
  96. bool shouldBeChoking() const;
  97. // this peer is snubbing.
  98. bool snubbing() const;
  99. void snubbing(bool b);
  100. bool hasAllPieces() const;
  101. void updateBitfield(int32_t index, int32_t operation);
  102. void setBitfield(const unsigned char* bitfield, size_t bitfieldLength);
  103. const unsigned char* getBitfield() const;
  104. size_t getBitfieldLength() const;
  105. bool hasPiece(int32_t index) const;
  106. void markSeeder();
  107. bool fastExtensionEnabled() const;
  108. void fastExtensionEnabled(bool b);
  109. // fast index set which a peer has sent to localhost.
  110. const std::deque<int32_t>& peerAllowedIndexSet() const;
  111. void addPeerAllowedIndex(int32_t index);
  112. bool peerAllowedIndexSetContains(int32_t index) const;
  113. // fast index set which localhost has sent to a peer.
  114. const std::deque<int32_t>& amAllowedIndexSet() const;
  115. void addAmAllowedIndex(int32_t index);
  116. bool amAllowedIndexSetContains(int32_t index) const;
  117. bool extendedMessagingEnabled() const;
  118. void extendedMessagingEnabled(bool b);
  119. uint8_t getExtensionMessageID(const std::string& name) const;
  120. std::string getExtensionName(uint8_t id) const;
  121. void addExtension(const std::string& name, uint8_t id);
  122. bool dhtEnabled() const;
  123. void dhtEnabled(bool b);
  124. PeerStat& getPeerStat();
  125. int32_t latency() const;
  126. void updateLatency(int32_t l);
  127. int64_t uploadLength() const;
  128. void updateUploadLength(int32_t bytes);
  129. int64_t downloadLength() const;
  130. void updateDownloadLength(int32_t bytes);
  131. };
  132. } // namespace aria2
  133. #endif // _D_PEER_SESSION_RESOURCE_H_