DefaultBtInteractive.cc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 "DefaultBtInteractive.h"
  36. #include "prefs.h"
  37. #include "message.h"
  38. #include "BtHandshakeMessage.h"
  39. #include "Util.h"
  40. #include "BtKeepAliveMessage.h"
  41. #include "BtChokeMessage.h"
  42. #include "BtUnchokeMessage.h"
  43. #include "BtRequestMessage.h"
  44. #include "BtPieceMessage.h"
  45. #include "DlAbortEx.h"
  46. void DefaultBtInteractive::initiateHandshake() {
  47. BtMessageHandle message = messageFactory->createHandshakeMessage(btContext->getInfoHash(),
  48. btContext->getPeerId());
  49. dispatcher->addMessageToQueue(message);
  50. dispatcher->sendMessages();
  51. }
  52. BtMessageHandle DefaultBtInteractive::receiveHandshake(bool quickReply) {
  53. BtHandshakeMessageHandle message =
  54. btMessageReceiver->receiveHandshake(quickReply);
  55. if(message.isNull()) {
  56. return 0;
  57. }
  58. peer->setPeerId(message->getPeerId());
  59. logger->info(MSG_RECEIVE_PEER_MESSAGE, cuid,
  60. peer->ipaddr.c_str(), peer->port,
  61. message->toString().c_str());
  62. return message;
  63. }
  64. BtMessageHandle DefaultBtInteractive::receiveAndSendHandshake() {
  65. return receiveHandshake(true);
  66. }
  67. void DefaultBtInteractive::doPostHandshakeProcessing() {
  68. // TODO where is the valid place to rest haveCheckTime?
  69. haveCheckPoint.reset();
  70. keepAliveCheckPoint.reset();
  71. floodingCheckPoint.reset();
  72. addBitfieldMessageToQueue();
  73. addAllowedFastMessageToQueue();
  74. sendPendingMessage();
  75. }
  76. void DefaultBtInteractive::addBitfieldMessageToQueue() {
  77. if(peer->isFastExtensionEnabled()) {
  78. if(pieceStorage->allDownloadFinished()) {
  79. dispatcher->addMessageToQueue(messageFactory->createHaveAllMessage());
  80. } else if(pieceStorage->getCompletedLength() > 0) {
  81. dispatcher->addMessageToQueue(messageFactory->createBitfieldMessage());
  82. } else {
  83. dispatcher->addMessageToQueue(messageFactory->createHaveNoneMessage());
  84. }
  85. } else {
  86. if(pieceStorage->getCompletedLength() > 0) {
  87. dispatcher->addMessageToQueue(messageFactory->createBitfieldMessage());
  88. }
  89. }
  90. }
  91. void DefaultBtInteractive::addAllowedFastMessageToQueue() {
  92. if(peer->isFastExtensionEnabled()) {
  93. Integers fastSet = btContext->computeFastSet(peer->ipaddr,
  94. allowedFastSetSize);
  95. for(Integers::const_iterator itr = fastSet.begin();
  96. itr != fastSet.end(); itr++) {
  97. dispatcher->addMessageToQueue(messageFactory->createAllowedFastMessage(*itr));
  98. }
  99. }
  100. }
  101. void DefaultBtInteractive::decideChoking() {
  102. if(peer->shouldBeChoking()) {
  103. if(!peer->amChoking) {
  104. dispatcher->addMessageToQueue(messageFactory->createChokeMessage());
  105. }
  106. } else {
  107. if(peer->amChoking) {
  108. dispatcher->addMessageToQueue(messageFactory->createUnchokeMessage());
  109. }
  110. }
  111. }
  112. void DefaultBtInteractive::checkHave() {
  113. Integers indexes =
  114. pieceStorage->getAdvertisedPieceIndexes(cuid, haveCheckPoint);
  115. haveCheckPoint.reset();
  116. if(indexes.size() >= 20) {
  117. if(peer->isFastExtensionEnabled() && pieceStorage->allDownloadFinished()) {
  118. dispatcher->addMessageToQueue(messageFactory->createHaveAllMessage());
  119. } else {
  120. dispatcher->addMessageToQueue(messageFactory->createBitfieldMessage());
  121. }
  122. } else {
  123. for(Integers::iterator itr = indexes.begin(); itr != indexes.end(); itr++) {
  124. dispatcher->addMessageToQueue(messageFactory->createHaveMessage(*itr));
  125. }
  126. }
  127. }
  128. void DefaultBtInteractive::sendKeepAlive() {
  129. if(keepAliveCheckPoint.elapsed(keepAliveInterval)) {
  130. dispatcher->addMessageToQueue(messageFactory->createKeepAliveMessage());
  131. dispatcher->sendMessages();
  132. keepAliveCheckPoint.reset();
  133. }
  134. }
  135. void DefaultBtInteractive::receiveMessages() {
  136. for(int i = 0; i < 50; i++) {
  137. if(maxDownloadSpeedLimit > 0) {
  138. TransferStat stat = peerStorage->calculateStat();
  139. if(maxDownloadSpeedLimit < stat.downloadSpeed) {
  140. break;
  141. }
  142. }
  143. BtMessageHandle message = btMessageReceiver->receiveMessage();
  144. if(message.isNull()) {
  145. break;
  146. }
  147. logger->info(MSG_RECEIVE_PEER_MESSAGE, cuid,
  148. peer->ipaddr.c_str(), peer->port,
  149. message->toString().c_str());
  150. message->doReceivedAction();
  151. switch(message->getId()) {
  152. case BtKeepAliveMessage::ID:
  153. floodingStat.incKeepAliveCount();
  154. break;
  155. case BtChokeMessage::ID:
  156. if(!peer->peerChoking) {
  157. floodingStat.incChokeUnchokeCount();
  158. }
  159. break;
  160. case BtUnchokeMessage::ID:
  161. if(peer->peerChoking) {
  162. floodingStat.incChokeUnchokeCount();
  163. }
  164. break;
  165. case BtRequestMessage::ID:
  166. case BtPieceMessage::ID:
  167. inactiveCheckPoint.reset();
  168. break;
  169. }
  170. }
  171. }
  172. void DefaultBtInteractive::decideInterest() {
  173. if(pieceStorage->hasMissingPiece(peer)) {
  174. if(!peer->amInterested) {
  175. logger->debug(MSG_PEER_INTERESTED, cuid);
  176. dispatcher->
  177. addMessageToQueue(messageFactory->createInterestedMessage());
  178. }
  179. } else {
  180. if(peer->amInterested) {
  181. logger->debug(MSG_PEER_NOT_INTERESTED, cuid);
  182. dispatcher->
  183. addMessageToQueue(messageFactory->createNotInterestedMessage());
  184. }
  185. }
  186. }
  187. void DefaultBtInteractive::fillPiece(int maxPieceNum) {
  188. if(pieceStorage->hasMissingPiece(peer)) {
  189. if(peer->peerChoking) {
  190. if(peer->isFastExtensionEnabled()) {
  191. while(btRequestFactory->countTargetPiece() < maxPieceNum) {
  192. PieceHandle piece = pieceStorage->getMissingFastPiece(peer);
  193. if(piece.isNull()) {
  194. break;
  195. } else {
  196. btRequestFactory->addTargetPiece(piece);
  197. }
  198. }
  199. }
  200. } else {
  201. while(btRequestFactory->countTargetPiece() < maxPieceNum) {
  202. PieceHandle piece = pieceStorage->getMissingPiece(peer);
  203. if(piece.isNull()) {
  204. break;
  205. } else {
  206. btRequestFactory->addTargetPiece(piece);
  207. }
  208. }
  209. }
  210. }
  211. }
  212. void DefaultBtInteractive::addRequests() {
  213. int32_t MAX_PENDING_REQUEST;
  214. if(peer->getLatency() < 500) {
  215. MAX_PENDING_REQUEST = 24;
  216. } else if(peer->getLatency() < 1500) {
  217. MAX_PENDING_REQUEST = 12;
  218. } else {
  219. MAX_PENDING_REQUEST = 6;
  220. }
  221. int32_t pieceNum;
  222. if(pieceStorage->isEndGame()) {
  223. pieceNum = 1;
  224. } else {
  225. int32_t blocks = DIV_FLOOR(btContext->getPieceLength(), Piece::BLOCK_LENGTH);
  226. pieceNum = DIV_FLOOR(MAX_PENDING_REQUEST, blocks);
  227. }
  228. fillPiece(pieceNum);
  229. int32_t reqNumToCreate =
  230. MAX_PENDING_REQUEST <= dispatcher->countOutstandingRequest() ?
  231. 0 : MAX_PENDING_REQUEST-dispatcher->countOutstandingRequest();
  232. if(reqNumToCreate > 0) {
  233. BtMessages requests;
  234. if(pieceStorage->isEndGame()) {
  235. requests = btRequestFactory->createRequestMessagesOnEndGame(reqNumToCreate);
  236. } else {
  237. requests = btRequestFactory->createRequestMessages(reqNumToCreate);
  238. }
  239. dispatcher->addMessageToQueue(requests);
  240. }
  241. }
  242. void DefaultBtInteractive::cancelAllPiece() {
  243. btRequestFactory->removeAllTargetPiece();
  244. }
  245. void DefaultBtInteractive::sendPendingMessage() {
  246. dispatcher->sendMessages();
  247. }
  248. void DefaultBtInteractive::detectMessageFlooding() {
  249. if(floodingCheckPoint.elapsed(FLOODING_CHECK_INTERVAL)) {
  250. if(floodingStat.getChokeUnchokeCount() >= 2 ||
  251. floodingStat.getKeepAliveCount() >= 2) {
  252. throw new DlAbortEx(EX_FLOODING_DETECTED);
  253. } else {
  254. floodingStat.reset();
  255. }
  256. floodingCheckPoint.reset();
  257. }
  258. }
  259. void DefaultBtInteractive::checkActiveInteraction()
  260. {
  261. int32_t interval = 5*60;
  262. if(inactiveCheckPoint.elapsed(interval)) {
  263. throw new DlAbortEx(EX_DROP_INACTIVE_CONNECTION, interval);
  264. }
  265. }
  266. void DefaultBtInteractive::doInteractionProcessing() {
  267. checkActiveInteraction();
  268. decideChoking();
  269. detectMessageFlooding();
  270. dispatcher->checkRequestSlotAndDoNecessaryThing();
  271. checkHave();
  272. sendKeepAlive();
  273. receiveMessages();
  274. btRequestFactory->removeCompletedPiece();
  275. decideInterest();
  276. if(!pieceStorage->downloadFinished()) {
  277. addRequests();
  278. }
  279. sendPendingMessage();
  280. }