PeerSessionResource.h 5.0 KB

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