AbstractCommand.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. #ifndef D_ABSTRACT_COMMAND_H
  36. #define D_ABSTRACT_COMMAND_H
  37. #include "Command.h"
  38. #include <vector>
  39. #include <string>
  40. #include <memory>
  41. #include "TimerA2.h"
  42. namespace aria2 {
  43. class FileEntry;
  44. class RequestGroup;
  45. class CheckIntegrityEntry;
  46. class DownloadContext;
  47. class SegmentMan;
  48. class PieceStorage;
  49. class Request;
  50. class DownloadEngine;
  51. class Segment;
  52. class SocketCore;
  53. class Option;
  54. class SocketRecvBuffer;
  55. #ifdef ENABLE_ASYNC_DNS
  56. class AsyncNameResolver;
  57. class AsyncNameResolverMan;
  58. #endif // ENABLE_ASYNC_DNS
  59. class AbstractCommand : public Command {
  60. private:
  61. std::shared_ptr<Request> req_;
  62. std::shared_ptr<FileEntry> fileEntry_;
  63. std::shared_ptr<SocketCore> socket_;
  64. std::shared_ptr<SocketRecvBuffer> socketRecvBuffer_;
  65. std::shared_ptr<SocketCore> readCheckTarget_;
  66. std::shared_ptr<SocketCore> writeCheckTarget_;
  67. #ifdef ENABLE_ASYNC_DNS
  68. std::unique_ptr<AsyncNameResolverMan> asyncNameResolverMan_;
  69. #endif // ENABLE_ASYNC_DNS
  70. RequestGroup* requestGroup_;
  71. DownloadEngine* e_;
  72. std::vector<std::shared_ptr<Segment>> segments_;
  73. Timer checkPoint_;
  74. Timer serverStatTimer_;
  75. std::chrono::seconds timeout_;
  76. bool checkSocketIsReadable_;
  77. bool checkSocketIsWritable_;
  78. bool incNumConnection_;
  79. int32_t calculateMinSplitSize() const;
  80. void useFasterRequest(const std::shared_ptr<Request>& fasterRequest);
  81. bool shouldProcess() const;
  82. public:
  83. RequestGroup* getRequestGroup() const { return requestGroup_; }
  84. const std::shared_ptr<Request>& getRequest() const { return req_; }
  85. void setRequest(const std::shared_ptr<Request>& request);
  86. // Resets request_. This method is more efficient than
  87. // setRequest(std::shared_ptr<Request>());
  88. void resetRequest();
  89. const std::shared_ptr<FileEntry>& getFileEntry() const { return fileEntry_; }
  90. void setFileEntry(const std::shared_ptr<FileEntry>& fileEntry);
  91. DownloadEngine* getDownloadEngine() const { return e_; }
  92. const std::shared_ptr<SocketCore>& getSocket() const { return socket_; }
  93. std::shared_ptr<SocketCore>& getSocket() { return socket_; }
  94. void setSocket(const std::shared_ptr<SocketCore>& s);
  95. void createSocket();
  96. const std::shared_ptr<SocketRecvBuffer>& getSocketRecvBuffer() const
  97. {
  98. return socketRecvBuffer_;
  99. }
  100. const std::vector<std::shared_ptr<Segment>>& getSegments() const
  101. {
  102. return segments_;
  103. }
  104. // Resolves hostname. The resolved addresses are stored in addrs
  105. // and first element is returned. If resolve is not finished,
  106. // return empty string. In this case, call this function with same
  107. // arguments until resolved address is returned. Exception is
  108. // thrown on error. port is used for retrieving cached addresses.
  109. std::string resolveHostname(std::vector<std::string>& addrs,
  110. const std::string& hostname, uint16_t port);
  111. void tryReserved();
  112. void setReadCheckSocket(const std::shared_ptr<SocketCore>& socket);
  113. void setWriteCheckSocket(const std::shared_ptr<SocketCore>& socket);
  114. void disableReadCheckSocket();
  115. void disableWriteCheckSocket();
  116. /**
  117. * If pred == true, calls setReadCheckSocket(socket). Otherwise, calls
  118. * disableReadCheckSocket().
  119. */
  120. void setReadCheckSocketIf(const std::shared_ptr<SocketCore>& socket,
  121. bool pred);
  122. /**
  123. * If pred == true, calls setWriteCheckSocket(socket). Otherwise, calls
  124. * disableWriteCheckSocket().
  125. */
  126. void setWriteCheckSocketIf(const std::shared_ptr<SocketCore>& socket,
  127. bool pred);
  128. // Swaps socket_ with socket. This disables current read and write
  129. // check.
  130. void swapSocket(std::shared_ptr<SocketCore>& socket);
  131. std::chrono::seconds getTimeout() const { return timeout_; }
  132. void setTimeout(std::chrono::seconds timeout)
  133. {
  134. timeout_ = std::move(timeout);
  135. }
  136. void prepareForNextAction(std::unique_ptr<CheckIntegrityEntry> checkEntry);
  137. // Check if socket is connected. If socket is not connected and
  138. // there are other addresses to try, command is created using
  139. // InitiateConnectionCommandFactory and it is pushed to
  140. // DownloadEngine and returns false. If no addresses left, DlRetryEx
  141. // exception is thrown.
  142. bool checkIfConnectionEstablished(const std::shared_ptr<SocketCore>& socket,
  143. const std::string& connectedHostname,
  144. const std::string& connectedAddr,
  145. uint16_t connectedPort);
  146. /*
  147. * Returns true if proxy for the procol indicated by Request::getProtocol()
  148. * is defined. Otherwise, returns false.
  149. */
  150. bool isProxyDefined() const;
  151. /*
  152. * Creates Request object for proxy URI and returns it.
  153. * If no valid proxy is defined, then returns std::shared_ptr<Request>().
  154. */
  155. std::shared_ptr<Request> createProxyRequest() const;
  156. // Returns proxy method for given protocol. Either V_GET or V_TUNNEL
  157. // is returned. For HTTPS, always returns V_TUNNEL.
  158. const std::string& resolveProxyMethod(const std::string& protocol) const;
  159. const std::shared_ptr<Option>& getOption() const;
  160. const std::shared_ptr<DownloadContext>& getDownloadContext() const;
  161. const std::shared_ptr<SegmentMan>& getSegmentMan() const;
  162. const std::shared_ptr<PieceStorage>& getPieceStorage() const;
  163. Timer& getCheckPoint() { return checkPoint_; }
  164. void checkSocketRecvBuffer();
  165. void addCommandSelf();
  166. protected:
  167. virtual bool prepareForRetry(time_t wait);
  168. virtual void onAbort();
  169. virtual bool executeInternal() = 0;
  170. // Returns true if the derived class wants to execute
  171. // executeInternal() unconditionally
  172. virtual bool noCheck() const { return false; }
  173. public:
  174. AbstractCommand(
  175. cuid_t cuid, const std::shared_ptr<Request>& req,
  176. const std::shared_ptr<FileEntry>& fileEntry, RequestGroup* requestGroup,
  177. DownloadEngine* e, const std::shared_ptr<SocketCore>& s = nullptr,
  178. const std::shared_ptr<SocketRecvBuffer>& socketRecvBuffer = nullptr,
  179. bool incNumConnection = true);
  180. virtual ~AbstractCommand();
  181. virtual bool execute() CXX11_OVERRIDE;
  182. };
  183. // Returns proxy URI for given protocol. If no proxy URI is defined,
  184. // then returns an empty string.
  185. std::string getProxyUri(const std::string& protocol, const Option* option);
  186. } // namespace aria2
  187. #endif // D_ABSTRACT_COMMAND_H