PeerListenCommand.cc 3.9 KB

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