PeerListenCommand.cc 2.9 KB

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