BackupIPv4ConnectCommand.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2013 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 "BackupIPv4ConnectCommand.h"
  36. #include "RequestGroup.h"
  37. #include "DownloadEngine.h"
  38. #include "SocketCore.h"
  39. #include "wallclock.h"
  40. #include "RecoverableException.h"
  41. #include "fmt.h"
  42. #include "LogFactory.h"
  43. #include "prefs.h"
  44. #include "Option.h"
  45. namespace aria2 {
  46. BackupConnectInfo::BackupConnectInfo() : cancel(false) {}
  47. BackupIPv4ConnectCommand::BackupIPv4ConnectCommand(
  48. cuid_t cuid, const std::string& ipaddr, uint16_t port,
  49. const std::shared_ptr<BackupConnectInfo>& info, Command* mainCommand,
  50. RequestGroup* requestGroup, DownloadEngine* e)
  51. : Command(cuid),
  52. ipaddr_(ipaddr),
  53. port_(port),
  54. info_(info),
  55. mainCommand_(mainCommand),
  56. requestGroup_(requestGroup),
  57. e_(e),
  58. startTime_(global::wallclock()),
  59. timeoutCheck_(global::wallclock()),
  60. timeout_(requestGroup_->getOption()->getAsInt(PREF_CONNECT_TIMEOUT))
  61. {
  62. requestGroup_->increaseStreamCommand();
  63. requestGroup_->increaseNumCommand();
  64. }
  65. BackupIPv4ConnectCommand::~BackupIPv4ConnectCommand()
  66. {
  67. requestGroup_->decreaseNumCommand();
  68. requestGroup_->decreaseStreamCommand();
  69. if (socket_) {
  70. e_->deleteSocketForWriteCheck(socket_, this);
  71. }
  72. }
  73. bool BackupIPv4ConnectCommand::execute()
  74. {
  75. bool retval = false;
  76. if (requestGroup_->downloadFinished() || requestGroup_->isHaltRequested()) {
  77. retval = true;
  78. }
  79. else if (info_->cancel) {
  80. A2_LOG_INFO(
  81. fmt("CUID#%" PRId64 " - Backup connection canceled", getCuid()));
  82. retval = true;
  83. }
  84. else if (socket_) {
  85. if (writeEventEnabled()) {
  86. try {
  87. std::string error = socket_->getSocketError();
  88. if (error.empty()) {
  89. A2_LOG_INFO(fmt("CUID#%" PRId64 " - Backup connection to %s "
  90. "established",
  91. getCuid(), ipaddr_.c_str()));
  92. info_->ipaddr = ipaddr_;
  93. e_->deleteSocketForWriteCheck(socket_, this);
  94. info_->socket.swap(socket_);
  95. mainCommand_->setStatus(STATUS_ONESHOT_REALTIME);
  96. e_->setNoWait(true);
  97. retval = true;
  98. }
  99. else {
  100. A2_LOG_INFO(fmt("CUID#%" PRId64 " - Backup connection failed: %s",
  101. getCuid(), error.c_str()));
  102. retval = true;
  103. }
  104. }
  105. catch (RecoverableException& e) {
  106. A2_LOG_INFO_EX(
  107. fmt("CUID#%" PRId64 " - Backup connection failed", getCuid()), e);
  108. retval = true;
  109. }
  110. }
  111. }
  112. else if (!socket_) {
  113. // TODO Although we check 300ms initial timeout as described in
  114. // RFC 6555, the interval will be much longer and around 1 second
  115. // due to the refresh interval mechanism in DownloadEngine.
  116. if (startTime_.difference(global::wallclock()) >=
  117. std::chrono::milliseconds(300)) {
  118. socket_ = std::make_shared<SocketCore>();
  119. try {
  120. socket_->establishConnection(ipaddr_, port_);
  121. e_->addSocketForWriteCheck(socket_, this);
  122. timeoutCheck_ = global::wallclock();
  123. }
  124. catch (RecoverableException& e) {
  125. A2_LOG_INFO_EX(
  126. fmt("CUID#%" PRId64 " - Backup connection failed", getCuid()), e);
  127. socket_.reset();
  128. retval = true;
  129. }
  130. }
  131. }
  132. else if (timeoutCheck_.difference(global::wallclock()) >= timeout_) {
  133. A2_LOG_INFO(
  134. fmt("CUID#%" PRId64 " - Backup connection command timeout", getCuid()));
  135. retval = true;
  136. }
  137. if (!retval) {
  138. e_->addCommand(std::unique_ptr<Command>(this));
  139. }
  140. return retval;
  141. }
  142. } // namespace aria2