Peer.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 "Peer.h"
  36. #include <cstring>
  37. #include <cassert>
  38. #include "util.h"
  39. #include "a2functional.h"
  40. #include "PeerSessionResource.h"
  41. #include "BtMessageDispatcher.h"
  42. #include "wallclock.h"
  43. namespace aria2 {
  44. Peer::Peer(std::string ipaddr, uint16_t port, bool incoming)
  45. : ipaddr_(std::move(ipaddr)),
  46. port_(port),
  47. origPort_(port),
  48. cuid_(0),
  49. firstContactTime_(global::wallclock()),
  50. dropStartTime_(Timer::zero()),
  51. seeder_(false),
  52. incoming_(incoming),
  53. localPeer_(false),
  54. disconnectedGracefully_(false)
  55. {
  56. memset(peerId_, 0, PEER_ID_LENGTH);
  57. }
  58. Peer::~Peer() { releaseSessionResource(); }
  59. void Peer::usedBy(cuid_t cuid) { cuid_ = cuid; }
  60. void Peer::allocateSessionResource(int32_t pieceLength, int64_t totalLength)
  61. {
  62. res_ = make_unique<PeerSessionResource>(pieceLength, totalLength);
  63. res_->getNetStat().downloadStart();
  64. updateSeeder();
  65. }
  66. void Peer::reconfigureSessionResource(int32_t pieceLength, int64_t totalLength)
  67. {
  68. assert(res_);
  69. res_->reconfigure(pieceLength, totalLength);
  70. }
  71. void Peer::releaseSessionResource() { res_.reset(); }
  72. void Peer::setPeerId(const unsigned char* peerId)
  73. {
  74. memcpy(peerId_, peerId, PEER_ID_LENGTH);
  75. }
  76. bool Peer::amChoking() const
  77. {
  78. assert(res_);
  79. return res_->amChoking();
  80. }
  81. void Peer::amChoking(bool b) const
  82. {
  83. assert(res_);
  84. res_->amChoking(b);
  85. }
  86. // localhost is interested in this peer
  87. bool Peer::amInterested() const
  88. {
  89. assert(res_);
  90. return res_->amInterested();
  91. }
  92. void Peer::amInterested(bool b) const
  93. {
  94. assert(res_);
  95. res_->amInterested(b);
  96. }
  97. // this peer is choking localhost
  98. bool Peer::peerChoking() const
  99. {
  100. assert(res_);
  101. return res_->peerChoking();
  102. }
  103. void Peer::peerChoking(bool b) const
  104. {
  105. assert(res_);
  106. res_->peerChoking(b);
  107. }
  108. // this peer is interested in localhost
  109. bool Peer::peerInterested() const
  110. {
  111. assert(res_);
  112. return res_->peerInterested();
  113. }
  114. void Peer::peerInterested(bool b)
  115. {
  116. assert(res_);
  117. res_->peerInterested(b);
  118. }
  119. // this peer should be choked
  120. bool Peer::chokingRequired() const
  121. {
  122. assert(res_);
  123. return res_->chokingRequired();
  124. }
  125. void Peer::chokingRequired(bool b)
  126. {
  127. assert(res_);
  128. res_->chokingRequired(b);
  129. }
  130. // this peer is eligible for unchoking optionally.
  131. bool Peer::optUnchoking() const
  132. {
  133. assert(res_);
  134. return res_->optUnchoking();
  135. }
  136. void Peer::optUnchoking(bool b)
  137. {
  138. assert(res_);
  139. res_->optUnchoking(b);
  140. }
  141. // this peer is snubbing.
  142. bool Peer::snubbing() const
  143. {
  144. assert(res_);
  145. return res_->snubbing();
  146. }
  147. void Peer::snubbing(bool b)
  148. {
  149. assert(res_);
  150. res_->snubbing(b);
  151. }
  152. void Peer::updateUploadSpeed(int32_t bytes)
  153. {
  154. assert(res_);
  155. res_->updateUploadSpeed(bytes);
  156. }
  157. void Peer::updateUploadLength(int32_t bytes)
  158. {
  159. assert(res_);
  160. res_->updateUploadLength(bytes);
  161. }
  162. void Peer::updateDownload(int32_t bytes)
  163. {
  164. assert(res_);
  165. res_->updateDownload(bytes);
  166. }
  167. void Peer::updateSeeder()
  168. {
  169. assert(res_);
  170. seeder_ = res_->hasAllPieces();
  171. }
  172. void Peer::updateBitfield(size_t index, int operation)
  173. {
  174. assert(res_);
  175. res_->updateBitfield(index, operation);
  176. updateSeeder();
  177. }
  178. int Peer::calculateUploadSpeed()
  179. {
  180. assert(res_);
  181. return res_->getNetStat().calculateUploadSpeed();
  182. }
  183. int Peer::calculateDownloadSpeed()
  184. {
  185. assert(res_);
  186. return res_->getNetStat().calculateDownloadSpeed();
  187. }
  188. int64_t Peer::getSessionUploadLength() const
  189. {
  190. assert(res_);
  191. return res_->uploadLength();
  192. }
  193. int64_t Peer::getSessionDownloadLength() const
  194. {
  195. assert(res_);
  196. return res_->downloadLength();
  197. }
  198. void Peer::setBitfield(const unsigned char* bitfield, size_t bitfieldLength)
  199. {
  200. assert(res_);
  201. res_->setBitfield(bitfield, bitfieldLength);
  202. updateSeeder();
  203. }
  204. const unsigned char* Peer::getBitfield() const
  205. {
  206. assert(res_);
  207. return res_->getBitfield();
  208. }
  209. size_t Peer::getBitfieldLength() const
  210. {
  211. assert(res_);
  212. return res_->getBitfieldLength();
  213. }
  214. bool Peer::shouldBeChoking() const
  215. {
  216. assert(res_);
  217. return res_->shouldBeChoking();
  218. }
  219. bool Peer::hasPiece(size_t index) const
  220. {
  221. assert(res_);
  222. return res_->hasPiece(index);
  223. }
  224. void Peer::setFastExtensionEnabled(bool enabled)
  225. {
  226. assert(res_);
  227. return res_->fastExtensionEnabled(enabled);
  228. }
  229. bool Peer::isFastExtensionEnabled() const
  230. {
  231. assert(res_);
  232. return res_->fastExtensionEnabled();
  233. }
  234. size_t Peer::countPeerAllowedIndexSet() const
  235. {
  236. assert(res_);
  237. return res_->peerAllowedIndexSet().size();
  238. }
  239. const std::set<size_t>& Peer::getPeerAllowedIndexSet() const
  240. {
  241. assert(res_);
  242. return res_->peerAllowedIndexSet();
  243. }
  244. bool Peer::isInPeerAllowedIndexSet(size_t index) const
  245. {
  246. assert(res_);
  247. return res_->peerAllowedIndexSetContains(index);
  248. }
  249. void Peer::addPeerAllowedIndex(size_t index)
  250. {
  251. assert(res_);
  252. res_->addPeerAllowedIndex(index);
  253. }
  254. bool Peer::isInAmAllowedIndexSet(size_t index) const
  255. {
  256. assert(res_);
  257. return res_->amAllowedIndexSetContains(index);
  258. }
  259. void Peer::addAmAllowedIndex(size_t index)
  260. {
  261. assert(res_);
  262. res_->addAmAllowedIndex(index);
  263. }
  264. void Peer::setAllBitfield()
  265. {
  266. assert(res_);
  267. res_->markSeeder();
  268. updateSeeder();
  269. }
  270. void Peer::startDrop() { dropStartTime_ = global::wallclock(); }
  271. uint8_t Peer::getExtensionMessageID(int key) const
  272. {
  273. assert(res_);
  274. return res_->getExtensionMessageID(key);
  275. }
  276. const char* Peer::getExtensionName(uint8_t id) const
  277. {
  278. assert(res_);
  279. return res_->getExtensionName(id);
  280. }
  281. void Peer::setExtension(int key, uint8_t id)
  282. {
  283. assert(res_);
  284. res_->addExtension(key, id);
  285. }
  286. void Peer::setExtendedMessagingEnabled(bool enabled)
  287. {
  288. assert(res_);
  289. res_->extendedMessagingEnabled(enabled);
  290. }
  291. bool Peer::isExtendedMessagingEnabled() const
  292. {
  293. assert(res_);
  294. return res_->extendedMessagingEnabled();
  295. }
  296. void Peer::setDHTEnabled(bool enabled)
  297. {
  298. assert(res_);
  299. res_->dhtEnabled(enabled);
  300. }
  301. bool Peer::isDHTEnabled() const
  302. {
  303. assert(res_);
  304. return res_->dhtEnabled();
  305. }
  306. const Timer& Peer::getLastDownloadUpdate() const
  307. {
  308. assert(res_);
  309. return res_->getLastDownloadUpdate();
  310. }
  311. const Timer& Peer::getLastAmUnchoking() const
  312. {
  313. assert(res_);
  314. return res_->getLastAmUnchoking();
  315. }
  316. int64_t Peer::getCompletedLength() const
  317. {
  318. assert(res_);
  319. return res_->getCompletedLength();
  320. }
  321. void Peer::setIncomingPeer(bool incoming) { incoming_ = incoming; }
  322. void Peer::setFirstContactTime(const Timer& time) { firstContactTime_ = time; }
  323. void Peer::setBtMessageDispatcher(BtMessageDispatcher* dpt)
  324. {
  325. assert(res_);
  326. res_->setBtMessageDispatcher(dpt);
  327. }
  328. size_t Peer::countOutstandingUpload() const
  329. {
  330. assert(res_);
  331. return res_->countOutstandingUpload();
  332. }
  333. } // namespace aria2