PeerListenCommand.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. logger->info(MSG_LISTENING_PORT,
  68. cuid, port);
  69. return port;
  70. } catch(RecoverableException* ex) {
  71. logger->error(MSG_BIND_FAILURE,
  72. ex, cuid, port);
  73. socket->closeConnection();
  74. delete ex;
  75. }
  76. }
  77. return -1;
  78. }
  79. bool PeerListenCommand::execute() {
  80. if(e->isHaltRequested() || e->_requestGroupMan->countRequestGroup() == 0) {
  81. return true;
  82. }
  83. for(int32_t i = 0; i < 3 && socket->isReadable(0); i++) {
  84. SocketHandle peerSocket;
  85. try {
  86. peerSocket = socket->acceptConnection();
  87. std::pair<std::string, int32_t> peerInfo;
  88. peerSocket->getPeerInfo(peerInfo);
  89. std::pair<std::string, int32_t> localInfo;
  90. peerSocket->getAddrInfo(localInfo);
  91. if(peerInfo.first == localInfo.first) {
  92. continue;
  93. }
  94. // Since peerSocket may be in non-blocking mode, make it blocking mode
  95. // here.
  96. peerSocket->setBlockingMode();
  97. PeerHandle peer = new Peer(peerInfo.first, 0);
  98. int32_t cuid = CUIDCounterSingletonHolder::instance()->newID();
  99. Command* command =
  100. new ReceiverMSEHandshakeCommand(cuid, peer, e, peerSocket);
  101. e->commands.push_back(command);
  102. logger->debug("Accepted the connection from %s:%u.",
  103. peer->ipaddr.c_str(),
  104. peer->port);
  105. logger->debug("Added CUID#%d to receive BitTorrent/MSE handshake.", cuid);
  106. } catch(RecoverableException* ex) {
  107. logger->debug(MSG_ACCEPT_FAILURE, ex, cuid);
  108. delete ex;
  109. }
  110. }
  111. e->commands.push_back(this);
  112. return false;
  113. }
  114. PeerListenCommand* PeerListenCommand::getInstance(DownloadEngine* e)
  115. {
  116. if(__numInstance == 0) {
  117. __instance = new PeerListenCommand(CUIDCounterSingletonHolder::instance()->newID(), e);
  118. }
  119. return __instance;
  120. }
  121. } // namespace aria2