PeerListenCommand.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 "PeerListenCommand.h"
  23. #include "PeerInteractionCommand.h"
  24. PeerListenCommand::PeerListenCommand(int cuid, TorrentDownloadEngine* e)
  25. :Command(cuid), e(e) {}
  26. PeerListenCommand::~PeerListenCommand() {}
  27. int PeerListenCommand::bindPort(int portRangeStart, int portRangeEnd) {
  28. if(portRangeStart > portRangeEnd) {
  29. return -1;
  30. }
  31. for(int port = portRangeStart; port <= portRangeEnd; port++) {
  32. try {
  33. socket->beginListen(port);
  34. logger->info("CUID#%d - using port %d for accepting new connections",
  35. cuid, port);
  36. return port;
  37. } catch(Exception* ex) {
  38. logger->error("CUID#%d - an error occurred while binding port=%d",
  39. ex, cuid, port);
  40. socket->closeConnection();
  41. delete ex;
  42. }
  43. }
  44. return -1;
  45. }
  46. bool PeerListenCommand::execute() {
  47. if(e->torrentMan->isHalt()) {
  48. return true;
  49. }
  50. for(int i = 0; i < 3 && socket->isReadable(0); i++) {
  51. SocketHandle peerSocket;
  52. try {
  53. peerSocket = socket->acceptConnection();
  54. pair<string, int> peerInfo;
  55. peerSocket->getPeerInfo(peerInfo);
  56. pair<string, int> localInfo;
  57. peerSocket->getAddrInfo(localInfo);
  58. if(peerInfo.first != localInfo.first &&
  59. e->torrentMan->connections < MAX_PEERS) {
  60. PeerHandle peer = PeerHandle(new Peer(peerInfo.first, peerInfo.second,
  61. e->torrentMan->pieceLength,
  62. e->torrentMan->getTotalLength()));
  63. if(e->torrentMan->addPeer(peer)) {
  64. int newCuid = e->torrentMan->getNewCuid();
  65. peer->cuid = newCuid;
  66. PeerInteractionCommand* command =
  67. new PeerInteractionCommand(newCuid, peer, e, peerSocket,
  68. PeerInteractionCommand::RECEIVER_WAIT_HANDSHAKE);
  69. e->commands.push_back(command);
  70. logger->debug("CUID#%d - incoming connection, adding new command CUID#%d", cuid, newCuid);
  71. }
  72. }
  73. } catch(Exception* ex) {
  74. logger->error("CUID#%d - error in accepting connection", ex, cuid);
  75. delete ex;
  76. }
  77. }
  78. e->commands.push_back(this);
  79. return false;
  80. }