PeerListenCommand.cc 3.1 KB

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