AbstractCommand.h 7.9 KB

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