PeerInteractionCommand.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "PeerInteractionCommand.h"
  23. #include "PeerInitiateConnectionCommand.h"
  24. #include "PeerMessageUtil.h"
  25. #include "DlAbortEx.h"
  26. #include "Util.h"
  27. #include "message.h"
  28. #include "prefs.h"
  29. #include <algorithm>
  30. PeerInteractionCommand::PeerInteractionCommand(int cuid, Peer* peer,
  31. TorrentDownloadEngine* e,
  32. const Socket* s, int sequence)
  33. :PeerAbstractCommand(cuid, peer, e, s), sequence(sequence) {
  34. if(sequence == INITIATOR_SEND_HANDSHAKE) {
  35. setReadCheckSocket(NULL);
  36. setWriteCheckSocket(socket);
  37. setTimeout(e->option->getAsInt(PREF_PEER_CONNECTION_TIMEOUT));
  38. }
  39. peerInteraction = new PeerInteraction(cuid, socket, e->option,
  40. e->torrentMan, this->peer);
  41. peerInteraction->setUploadLimit(e->option->getAsInt(PREF_UPLOAD_LIMIT));
  42. setUploadLimit(e->option->getAsInt(PREF_UPLOAD_LIMIT));
  43. keepAliveCheckPoint.tv_sec = 0;
  44. keepAliveCheckPoint.tv_usec = 0;
  45. chokeCheckPoint.tv_sec = 0;
  46. chokeCheckPoint.tv_usec = 0;
  47. freqCheckPoint.tv_sec = 0;
  48. freqCheckPoint.tv_usec = 0;
  49. chokeUnchokeCount = 0;
  50. haveCount = 0;
  51. keepAliveCount = 0;
  52. e->torrentMan->addActivePeer(this->peer);
  53. }
  54. PeerInteractionCommand::~PeerInteractionCommand() {
  55. delete peerInteraction;
  56. e->torrentMan->unadvertisePiece(cuid);
  57. e->torrentMan->deleteActivePeer(this->peer);
  58. }
  59. bool PeerInteractionCommand::executeInternal() {
  60. if(sequence == INITIATOR_SEND_HANDSHAKE) {
  61. socket->setBlockingMode();
  62. setReadCheckSocket(socket);
  63. setTimeout(e->option->getAsInt(PREF_TIMEOUT));
  64. }
  65. setWriteCheckSocket(NULL);
  66. setUploadLimitCheck(false);
  67. switch(sequence) {
  68. case INITIATOR_SEND_HANDSHAKE:
  69. peerInteraction->sendHandshake();
  70. sequence = INITIATOR_WAIT_HANDSHAKE;
  71. break;
  72. case INITIATOR_WAIT_HANDSHAKE: {
  73. if(peerInteraction->countMessageInQueue() > 0) {
  74. peerInteraction->sendMessages(e->getUploadSpeed());
  75. if(peerInteraction->countMessageInQueue() > 0) {
  76. break;
  77. }
  78. }
  79. HandshakeMessage* handshakeMessage = peerInteraction->receiveHandshake();
  80. if(handshakeMessage == NULL) {
  81. break;
  82. }
  83. peer->setPeerId(handshakeMessage->peerId);
  84. logger->info(MSG_RECEIVE_PEER_MESSAGE, cuid,
  85. peer->ipaddr.c_str(), peer->port,
  86. handshakeMessage->toString().c_str());
  87. delete handshakeMessage;
  88. peerInteraction->sendBitfield();
  89. peerInteraction->sendAllowedFast();
  90. sequence = WIRED;
  91. break;
  92. }
  93. case RECEIVER_WAIT_HANDSHAKE: {
  94. HandshakeMessage* handshakeMessage = peerInteraction->receiveHandshake();
  95. if(handshakeMessage == NULL) {
  96. break;
  97. }
  98. peer->setPeerId(handshakeMessage->peerId);
  99. logger->info(MSG_RECEIVE_PEER_MESSAGE, cuid,
  100. peer->ipaddr.c_str(), peer->port,
  101. handshakeMessage->toString().c_str());
  102. delete handshakeMessage;
  103. peerInteraction->sendHandshake();
  104. peerInteraction->sendBitfield();
  105. peerInteraction->sendAllowedFast();
  106. sequence = WIRED;
  107. break;
  108. }
  109. case WIRED:
  110. peerInteraction->syncPiece();
  111. decideChoking();
  112. receiveMessages();
  113. detectMessageFlooding();
  114. peerInteraction->checkRequestSlot();
  115. peerInteraction->addRequests();
  116. checkHave();
  117. peerInteraction->sendMessages(e->getUploadSpeed());
  118. sendKeepAlive();
  119. break;
  120. }
  121. if(peerInteraction->countMessageInQueue() > 0) {
  122. if(peerInteraction->isSendingMessageInProgress()) {
  123. setWriteCheckSocket(socket);
  124. } else {
  125. setUploadLimitCheck(true);
  126. }
  127. }
  128. e->commands.push_back(this);
  129. return false;
  130. }
  131. void PeerInteractionCommand::detectMessageFlooding() {
  132. struct timeval now;
  133. gettimeofday(&now, NULL);
  134. if(freqCheckPoint.tv_sec == 0 && freqCheckPoint.tv_usec == 0) {
  135. freqCheckPoint = now;
  136. } else {
  137. int elapsed = Util::difftvsec(now, freqCheckPoint);
  138. if(elapsed >= 5) {
  139. if(chokeUnchokeCount*1.0/elapsed >= 0.4
  140. //|| haveCount*1.0/elapsed >= 20.0
  141. || keepAliveCount*1.0/elapsed >= 1.0) {
  142. throw new DlAbortEx("Flooding detected.");
  143. } else {
  144. chokeUnchokeCount = 0;
  145. haveCount = 0;
  146. keepAliveCount = 0;
  147. freqCheckPoint = now;
  148. }
  149. }
  150. }
  151. }
  152. void PeerInteractionCommand::checkLongTimePeerChoking() {
  153. if(e->torrentMan->downloadComplete()) {
  154. return;
  155. }
  156. struct timeval now;
  157. gettimeofday(&now, NULL);
  158. if(chokeCheckPoint.tv_sec == 0 && chokeCheckPoint.tv_usec == 0) {
  159. if(peer->amInterested && peer->peerChoking) {
  160. chokeCheckPoint = now;
  161. }
  162. } else {
  163. if(peer->amInterested && peer->peerChoking) {
  164. if(Util::difftvsec(now, chokeCheckPoint) >= MAX_PEER_CHOKING_INTERVAL) {
  165. throw new DlAbortEx("Too long choking.");
  166. }
  167. } else {
  168. chokeCheckPoint.tv_sec = 0;
  169. chokeCheckPoint.tv_usec = 0;
  170. }
  171. }
  172. }
  173. void PeerInteractionCommand::decideChoking() {
  174. if(peer->shouldBeChoking()) {
  175. if(!peer->amChoking) {
  176. peerInteraction->addMessage(peerInteraction->createChokeMessage());
  177. }
  178. } else {
  179. if(peer->amChoking) {
  180. peerInteraction->addMessage(peerInteraction->createUnchokeMessage());
  181. }
  182. }
  183. }
  184. void PeerInteractionCommand::receiveMessages() {
  185. for(int i = 0; i < 50; i++) {
  186. PeerMessage* message = peerInteraction->receiveMessage();
  187. if(message == NULL) {
  188. return;
  189. }
  190. logger->info(MSG_RECEIVE_PEER_MESSAGE, cuid,
  191. peer->ipaddr.c_str(), peer->port,
  192. message->toString().c_str());
  193. // to detect flooding
  194. switch(message->getId()) {
  195. case KeepAliveMessage::ID:
  196. keepAliveCount++;
  197. break;
  198. case ChokeMessage::ID:
  199. if(!peer->peerChoking) {
  200. chokeUnchokeCount++;
  201. }
  202. break;
  203. case UnchokeMessage::ID:
  204. if(peer->peerChoking) {
  205. chokeUnchokeCount++;
  206. }
  207. break;
  208. case HaveMessage::ID:
  209. haveCount++;
  210. break;
  211. }
  212. try {
  213. message->receivedAction();
  214. delete message;
  215. } catch(Exception* ex) {
  216. delete message;
  217. throw;
  218. }
  219. }
  220. }
  221. // TODO this method removed when PeerBalancerCommand is implemented
  222. bool PeerInteractionCommand::prepareForNextPeer(int wait) {
  223. if(e->torrentMan->isPeerAvailable()) {
  224. Peer* peer = e->torrentMan->getPeer();
  225. int newCuid = e->torrentMan->getNewCuid();
  226. peer->cuid = newCuid;
  227. PeerInitiateConnectionCommand* command = new PeerInitiateConnectionCommand(newCuid, peer, e);
  228. e->commands.push_back(command);
  229. }
  230. return true;
  231. }
  232. bool PeerInteractionCommand::prepareForRetry(int wait) {
  233. e->commands.push_back(this);
  234. return false;
  235. }
  236. void PeerInteractionCommand::onAbort(Exception* ex) {
  237. peerInteraction->abortAllPieces();
  238. PeerAbstractCommand::onAbort(ex);
  239. }
  240. void PeerInteractionCommand::sendKeepAlive() {
  241. if(keepAliveCheckPoint.tv_sec == 0 && keepAliveCheckPoint.tv_usec == 0) {
  242. gettimeofday(&keepAliveCheckPoint, NULL);
  243. } else {
  244. struct timeval now;
  245. gettimeofday(&now, NULL);
  246. if(Util::difftvsec(now, keepAliveCheckPoint) >= 120) {
  247. if(peerInteraction->countMessageInQueue() == 0) {
  248. peerInteraction->addMessage(peerInteraction->createKeepAliveMessage());
  249. peerInteraction->sendMessages(e->getUploadSpeed());
  250. }
  251. keepAliveCheckPoint = now;
  252. }
  253. }
  254. }
  255. void PeerInteractionCommand::checkHave() {
  256. e->torrentMan->unadvertisePiece(cuid);
  257. PieceIndexes indexes = e->torrentMan->getAdvertisedPieceIndexes(cuid);
  258. if(indexes.size() >= 20) {
  259. if(peer->isFastExtensionEnabled()) {
  260. if(e->torrentMan->hasAllPieces()) {
  261. peerInteraction->addMessage(peerInteraction->createHaveAllMessage());
  262. } else {
  263. peerInteraction->addMessage(peerInteraction->createBitfieldMessage());
  264. }
  265. } else {
  266. peerInteraction->addMessage(peerInteraction->createBitfieldMessage());
  267. }
  268. } else {
  269. for(PieceIndexes::iterator itr = indexes.begin(); itr != indexes.end(); itr++) {
  270. peerInteraction->addMessage(peerInteraction->createHaveMessage(*itr));
  271. }
  272. }
  273. }