DHTInteractionCommand.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 "DHTInteractionCommand.h"
  36. #include <array>
  37. #include "DownloadEngine.h"
  38. #include "RecoverableException.h"
  39. #include "DHTMessageDispatcher.h"
  40. #include "DHTMessageReceiver.h"
  41. #include "DHTTaskQueue.h"
  42. #include "DHTMessage.h"
  43. #include "SocketCore.h"
  44. #include "message.h"
  45. #include "RequestGroupMan.h"
  46. #include "Logger.h"
  47. #include "LogFactory.h"
  48. #include "DHTMessageCallback.h"
  49. #include "DHTNode.h"
  50. #include "DHTConnection.h"
  51. #include "UDPTrackerClient.h"
  52. #include "UDPTrackerRequest.h"
  53. #include "fmt.h"
  54. #include "wallclock.h"
  55. #include "TrackerWatcherCommand.h"
  56. namespace aria2 {
  57. // TODO This name of this command is misleading, because now it also
  58. // handles UDP trackers as well as DHT.
  59. DHTInteractionCommand::DHTInteractionCommand(cuid_t cuid, DownloadEngine* e)
  60. : Command{cuid},
  61. e_{e},
  62. dispatcher_{nullptr},
  63. receiver_{nullptr},
  64. taskQueue_{nullptr}
  65. {
  66. setStatusRealtime();
  67. }
  68. DHTInteractionCommand::~DHTInteractionCommand()
  69. {
  70. disableReadCheckSocket(readCheckSocket_);
  71. }
  72. void DHTInteractionCommand::setReadCheckSocket(
  73. const std::shared_ptr<SocketCore>& socket)
  74. {
  75. readCheckSocket_ = socket;
  76. if (socket) {
  77. e_->addSocketForReadCheck(socket, this);
  78. }
  79. }
  80. void DHTInteractionCommand::disableReadCheckSocket(
  81. const std::shared_ptr<SocketCore>& socket)
  82. {
  83. if (socket) {
  84. e_->deleteSocketForReadCheck(socket, this);
  85. }
  86. }
  87. bool DHTInteractionCommand::execute()
  88. {
  89. // We need to keep this command alive while TrackerWatcherCommand
  90. // needs this.
  91. if (e_->getRequestGroupMan()->downloadFinished() ||
  92. (e_->isHaltRequested() && udpTrackerClient_->getNumWatchers() == 0)) {
  93. A2_LOG_DEBUG("DHTInteractionCommand exiting");
  94. return true;
  95. }
  96. else if (e_->isForceHaltRequested()) {
  97. udpTrackerClient_->failAll();
  98. A2_LOG_DEBUG("DHTInteractionCommand exiting");
  99. return true;
  100. }
  101. taskQueue_->executeTask();
  102. std::string remoteAddr;
  103. uint16_t remotePort;
  104. std::array<unsigned char, 64_k> data;
  105. try {
  106. while (1) {
  107. ssize_t length = connection_->receiveMessage(data.data(), data.size(),
  108. remoteAddr, remotePort);
  109. if (length <= 0) {
  110. break;
  111. }
  112. if (data[0] == 'd') {
  113. // udp tracker response does not start with 'd', so assume
  114. // this message belongs to DHT. nothrow.
  115. receiver_->receiveMessage(remoteAddr, remotePort, data.data(), length);
  116. }
  117. else {
  118. // this may be udp tracker response. nothrow.
  119. std::shared_ptr<UDPTrackerRequest> req;
  120. if (udpTrackerClient_->receiveReply(req, data.data(), length,
  121. remoteAddr, remotePort,
  122. global::wallclock()) == 0) {
  123. if (req->action == UDPT_ACT_ANNOUNCE) {
  124. auto c = static_cast<TrackerWatcherCommand*>(req->user_data);
  125. if (c) {
  126. c->setStatus(Command::STATUS_ONESHOT_REALTIME);
  127. e_->setNoWait(true);
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }
  134. catch (RecoverableException& e) {
  135. A2_LOG_INFO_EX("Exception thrown while receiving UDP message.", e);
  136. }
  137. receiver_->handleTimeout();
  138. udpTrackerClient_->handleTimeout(global::wallclock());
  139. dispatcher_->sendMessages();
  140. while (!udpTrackerClient_->getPendingRequests().empty()) {
  141. // no throw
  142. ssize_t length = udpTrackerClient_->createRequest(
  143. data.data(), data.size(), remoteAddr, remotePort, global::wallclock());
  144. if (length == -1) {
  145. break;
  146. }
  147. try {
  148. // throw
  149. connection_->sendMessage(data.data(), length, remoteAddr, remotePort);
  150. udpTrackerClient_->requestSent(global::wallclock());
  151. }
  152. catch (RecoverableException& e) {
  153. A2_LOG_INFO_EX("Exception thrown while sending UDP tracker request.", e);
  154. udpTrackerClient_->requestFail(UDPT_ERR_NETWORK);
  155. }
  156. }
  157. e_->addRoutineCommand(std::unique_ptr<Command>(this));
  158. return false;
  159. }
  160. void DHTInteractionCommand::setMessageDispatcher(
  161. DHTMessageDispatcher* dispatcher)
  162. {
  163. dispatcher_ = dispatcher;
  164. }
  165. void DHTInteractionCommand::setMessageReceiver(DHTMessageReceiver* receiver)
  166. {
  167. receiver_ = receiver;
  168. }
  169. void DHTInteractionCommand::setTaskQueue(DHTTaskQueue* taskQueue)
  170. {
  171. taskQueue_ = taskQueue;
  172. }
  173. void DHTInteractionCommand::setConnection(
  174. std::unique_ptr<DHTConnection> connection)
  175. {
  176. connection_ = std::move(connection);
  177. }
  178. void DHTInteractionCommand::setUDPTrackerClient(
  179. const std::shared_ptr<UDPTrackerClient>& udpTrackerClient)
  180. {
  181. udpTrackerClient_ = udpTrackerClient;
  182. }
  183. } // namespace aria2