PeerSessionResource.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. #include "PeerSessionResource.h"
  36. #include <cassert>
  37. #include <algorithm>
  38. #include "BitfieldMan.h"
  39. #include "A2STR.h"
  40. #include "BtMessageDispatcher.h"
  41. #include "wallclock.h"
  42. #include "a2functional.h"
  43. namespace aria2 {
  44. PeerSessionResource::PeerSessionResource(int32_t pieceLength,
  45. int64_t totalLength)
  46. : bitfieldMan_(make_unique<BitfieldMan>(pieceLength, totalLength)),
  47. lastDownloadUpdate_(Timer::zero()),
  48. lastAmUnchoking_(Timer::zero()),
  49. dispatcher_(nullptr),
  50. amChoking_(true),
  51. amInterested_(false),
  52. peerChoking_(true),
  53. peerInterested_(false),
  54. chokingRequired_(true),
  55. optUnchoking_(false),
  56. snubbing_(false),
  57. fastExtensionEnabled_(false),
  58. extendedMessagingEnabled_(false),
  59. dhtEnabled_(false)
  60. {
  61. }
  62. PeerSessionResource::~PeerSessionResource() {}
  63. void PeerSessionResource::amChoking(bool b)
  64. {
  65. amChoking_ = b;
  66. if (!b) {
  67. lastAmUnchoking_ = global::wallclock();
  68. }
  69. }
  70. void PeerSessionResource::amInterested(bool b) { amInterested_ = b; }
  71. void PeerSessionResource::peerChoking(bool b) { peerChoking_ = b; }
  72. void PeerSessionResource::peerInterested(bool b) { peerInterested_ = b; }
  73. void PeerSessionResource::chokingRequired(bool b) { chokingRequired_ = b; }
  74. void PeerSessionResource::optUnchoking(bool b) { optUnchoking_ = b; }
  75. bool PeerSessionResource::shouldBeChoking() const
  76. {
  77. if (optUnchoking_) {
  78. return false;
  79. }
  80. return chokingRequired_;
  81. }
  82. void PeerSessionResource::snubbing(bool b)
  83. {
  84. snubbing_ = b;
  85. if (snubbing_) {
  86. chokingRequired(true);
  87. optUnchoking(false);
  88. }
  89. }
  90. bool PeerSessionResource::hasAllPieces() const
  91. {
  92. return bitfieldMan_->isAllBitSet();
  93. }
  94. void PeerSessionResource::updateBitfield(size_t index, int operation)
  95. {
  96. if (operation == 1) {
  97. bitfieldMan_->setBit(index);
  98. }
  99. else if (operation == 0) {
  100. bitfieldMan_->unsetBit(index);
  101. }
  102. }
  103. void PeerSessionResource::setBitfield(const unsigned char* bitfield,
  104. size_t bitfieldLength)
  105. {
  106. bitfieldMan_->setBitfield(bitfield, bitfieldLength);
  107. }
  108. const unsigned char* PeerSessionResource::getBitfield() const
  109. {
  110. return bitfieldMan_->getBitfield();
  111. }
  112. size_t PeerSessionResource::getBitfieldLength() const
  113. {
  114. return bitfieldMan_->getBitfieldLength();
  115. }
  116. bool PeerSessionResource::hasPiece(size_t index) const
  117. {
  118. return bitfieldMan_->isBitSet(index);
  119. }
  120. void PeerSessionResource::markSeeder() { bitfieldMan_->setAllBit(); }
  121. void PeerSessionResource::fastExtensionEnabled(bool b)
  122. {
  123. fastExtensionEnabled_ = b;
  124. }
  125. const std::set<size_t>& PeerSessionResource::peerAllowedIndexSet() const
  126. {
  127. return peerAllowedIndexSet_;
  128. }
  129. void PeerSessionResource::addPeerAllowedIndex(size_t index)
  130. {
  131. peerAllowedIndexSet_.insert(index);
  132. }
  133. bool PeerSessionResource::peerAllowedIndexSetContains(size_t index) const
  134. {
  135. return peerAllowedIndexSet_.count(index) == 1;
  136. }
  137. void PeerSessionResource::addAmAllowedIndex(size_t index)
  138. {
  139. amAllowedIndexSet_.insert(index);
  140. }
  141. bool PeerSessionResource::amAllowedIndexSetContains(size_t index) const
  142. {
  143. return amAllowedIndexSet_.count(index) == 1;
  144. }
  145. void PeerSessionResource::extendedMessagingEnabled(bool b)
  146. {
  147. extendedMessagingEnabled_ = b;
  148. }
  149. uint8_t PeerSessionResource::getExtensionMessageID(int key) const
  150. {
  151. return extreg_.getExtensionMessageID(key);
  152. }
  153. const char* PeerSessionResource::getExtensionName(uint8_t id) const
  154. {
  155. return extreg_.getExtensionName(id);
  156. }
  157. void PeerSessionResource::addExtension(int key, uint8_t id)
  158. {
  159. extreg_.setExtensionMessageID(key, id);
  160. }
  161. void PeerSessionResource::dhtEnabled(bool b) { dhtEnabled_ = b; }
  162. int64_t PeerSessionResource::uploadLength() const
  163. {
  164. return netStat_.getSessionUploadLength();
  165. }
  166. void PeerSessionResource::updateUploadSpeed(int32_t bytes)
  167. {
  168. netStat_.updateUploadSpeed(bytes);
  169. }
  170. void PeerSessionResource::updateUploadLength(int32_t bytes)
  171. {
  172. netStat_.updateUploadLength(bytes);
  173. }
  174. int64_t PeerSessionResource::downloadLength() const
  175. {
  176. return netStat_.getSessionDownloadLength();
  177. }
  178. void PeerSessionResource::updateDownload(int32_t bytes)
  179. {
  180. netStat_.updateDownload(bytes);
  181. lastDownloadUpdate_ = global::wallclock();
  182. }
  183. int64_t PeerSessionResource::getCompletedLength() const
  184. {
  185. return bitfieldMan_->getCompletedLength();
  186. }
  187. void PeerSessionResource::setBtMessageDispatcher(BtMessageDispatcher* dpt)
  188. {
  189. dispatcher_ = dpt;
  190. }
  191. size_t PeerSessionResource::countOutstandingUpload() const
  192. {
  193. assert(dispatcher_);
  194. return dispatcher_->countOutstandingUpload();
  195. }
  196. void PeerSessionResource::reconfigure(int32_t pieceLength, int64_t totalLenth)
  197. {
  198. bitfieldMan_ = make_unique<BitfieldMan>(pieceLength, totalLenth);
  199. }
  200. } // namespace aria2