PeerListenCommand.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 "PeerListenCommand.h"
  36. #include "DownloadEngine.h"
  37. #include "Peer.h"
  38. #include "RequestGroupMan.h"
  39. #include "RecoverableException.h"
  40. #include "CUIDCounter.h"
  41. #include "message.h"
  42. #include "ReceiverMSEHandshakeCommand.h"
  43. #include "Logger.h"
  44. #include "Socket.h"
  45. #include <utility>
  46. namespace aria2 {
  47. int32_t PeerListenCommand::__numInstance = 0;
  48. PeerListenCommand* PeerListenCommand::__instance = 0;
  49. PeerListenCommand::PeerListenCommand(int32_t cuid, DownloadEngine* e):
  50. Command(cuid),
  51. e(e),
  52. _lowestSpeedLimit(20*1024)
  53. {
  54. ++__numInstance;
  55. }
  56. PeerListenCommand::~PeerListenCommand()
  57. {
  58. --__numInstance;
  59. }
  60. int32_t PeerListenCommand::bindPort(IntSequence& seq)
  61. {
  62. while(seq.hasNext()) {
  63. int32_t port = seq.next();
  64. try {
  65. socket->bind(port);
  66. socket->beginListen();
  67. socket->setNonBlockingMode();
  68. logger->info(MSG_LISTENING_PORT,
  69. cuid, port);
  70. return port;
  71. } catch(RecoverableException* ex) {
  72. logger->error(MSG_BIND_FAILURE,
  73. ex, cuid, port);
  74. socket->closeConnection();
  75. delete ex;
  76. }
  77. }
  78. return -1;
  79. }
  80. bool PeerListenCommand::execute() {
  81. if(e->isHaltRequested() || e->_requestGroupMan->downloadFinished()) {
  82. return true;
  83. }
  84. for(int32_t i = 0; i < 3 && socket->isReadable(0); i++) {
  85. SocketHandle peerSocket;
  86. try {
  87. peerSocket = socket->acceptConnection();
  88. std::pair<std::string, int32_t> peerInfo;
  89. peerSocket->getPeerInfo(peerInfo);
  90. std::pair<std::string, int32_t> localInfo;
  91. peerSocket->getAddrInfo(localInfo);
  92. if(peerInfo.first == localInfo.first) {
  93. continue;
  94. }
  95. // Since peerSocket may be in non-blocking mode, make it blocking mode
  96. // here.
  97. peerSocket->setBlockingMode();
  98. PeerHandle peer = new Peer(peerInfo.first, 0);
  99. int32_t cuid = CUIDCounterSingletonHolder::instance()->newID();
  100. Command* command =
  101. new ReceiverMSEHandshakeCommand(cuid, peer, e, peerSocket);
  102. e->commands.push_back(command);
  103. logger->debug("Accepted the connection from %s:%u.",
  104. peer->ipaddr.c_str(),
  105. peer->port);
  106. logger->debug("Added CUID#%d to receive BitTorrent/MSE handshake.", cuid);
  107. } catch(RecoverableException* ex) {
  108. logger->debug(MSG_ACCEPT_FAILURE, ex, cuid);
  109. delete ex;
  110. }
  111. }
  112. e->commands.push_back(this);
  113. return false;
  114. }
  115. PeerListenCommand* PeerListenCommand::getInstance(DownloadEngine* e)
  116. {
  117. if(__numInstance == 0) {
  118. __instance = new PeerListenCommand(CUIDCounterSingletonHolder::instance()->newID(), e);
  119. }
  120. return __instance;
  121. }
  122. } // namespace aria2