BackupIPv4ConnectCommand.cc 4.9 KB

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