DownloadEngine.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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_DOWNLOAD_ENGINE_H_
  36. #define _D_DOWNLOAD_ENGINE_H_
  37. #include "common.h"
  38. #ifdef HAVE_EPOLL_CREATE
  39. # include <sys/epoll.h>
  40. #endif // HAVE_EPOLL_CREATE
  41. #include <string>
  42. #include <deque>
  43. #include <map>
  44. #include "SharedHandle.h"
  45. #include "Command.h"
  46. #include "a2netcompat.h"
  47. #include "TimeA2.h"
  48. #include "a2io.h"
  49. #ifdef ENABLE_ASYNC_DNS
  50. # include "AsyncNameResolver.h"
  51. #endif // ENABLE_ASYNC_DNS
  52. #include "CUIDCounter.h"
  53. namespace aria2 {
  54. class Logger;
  55. class Option;
  56. class RequestGroupMan;
  57. class FileAllocationMan;
  58. class StatCalc;
  59. class CheckIntegrityMan;
  60. class SocketCore;
  61. class CookieStorage;
  62. class BtRegistry;
  63. class DNSCache;
  64. class CommandEvent
  65. {
  66. private:
  67. Command* _command;
  68. int _events;
  69. public:
  70. CommandEvent(Command* command, int events);
  71. Command* getCommand() const;
  72. int getEvents() const;
  73. void addEvents(int events);
  74. void removeEvents(int events);
  75. bool eventsEmpty() const;
  76. void processEvents(int events);
  77. bool operator==(const CommandEvent& event) const;
  78. };
  79. #if defined HAVE_EPOLL && defined ENABLE_ASYNC_DNS
  80. class ADNSEvent {
  81. private:
  82. SharedHandle<AsyncNameResolver> _resolver;
  83. Command* _command;
  84. sock_t _socket;
  85. int _events;
  86. public:
  87. ADNSEvent(const SharedHandle<AsyncNameResolver>& resolver, Command* command,
  88. sock_t socket, int events);
  89. void processEvents(int events);
  90. bool operator==(const ADNSEvent& event) const;
  91. int getEvents() const;
  92. };
  93. #endif // HAVE_EPOLL && ENABLE_ASYNC_DNS
  94. class SocketEntry {
  95. private:
  96. sock_t _socket;
  97. std::deque<CommandEvent> _commandEvents;
  98. #if defined HAVE_EPOLL && defined ENABLE_ASYNC_DNS
  99. std::deque<ADNSEvent> _adnsEvents;
  100. #endif // HAVE_EPOLL && ENABLE_ASYNC_DNS
  101. #ifdef HAVE_EPOLL
  102. struct epoll_event _epEvent;
  103. #endif // HAVE_EPOLL
  104. public:
  105. #ifdef HAVE_EPOLL
  106. enum EventType {
  107. EVENT_READ = EPOLLIN,
  108. EVENT_WRITE = EPOLLOUT,
  109. EVENT_ERROR = EPOLLERR,
  110. EVENT_HUP = EPOLLHUP,
  111. };
  112. #else // !HAVE_EPOLL
  113. enum EventType {
  114. EVENT_READ = 1,
  115. EVENT_WRITE = 1 << 1,
  116. EVENT_ERROR = 1 << 2,
  117. EVENT_HUP = 1 << 3,
  118. };
  119. #endif // !HAVE_EPOLL
  120. SocketEntry(sock_t socket);
  121. bool operator==(const SocketEntry& entry) const;
  122. bool operator<(const SocketEntry& entry) const;
  123. void addCommandEvent(Command* command, int events);
  124. void removeCommandEvent(Command* command, int events);
  125. #if defined HAVE_EPOLL && defined ENABLE_ASYNC_DNS
  126. void addADNSEvent(const SharedHandle<AsyncNameResolver>& resolver,
  127. Command* command, int events);
  128. void removeADNSEvent(const SharedHandle<AsyncNameResolver>& resolver,
  129. Command* command);
  130. #endif // HAVE_EPOLL && ENABLE_ASYNC_DNS
  131. #ifdef HAVE_EPOLL
  132. struct epoll_event& getEpEvent();
  133. #else // !HAVE_EPOLL
  134. int getEvents();
  135. #endif // !HAVE_EPOLL
  136. sock_t getSocket() const;
  137. bool eventEmpty() const;
  138. void processEvents(int events);
  139. };
  140. #ifdef ENABLE_ASYNC_DNS
  141. class DownloadEngine;
  142. class AsyncNameResolverEntry {
  143. private:
  144. SharedHandle<AsyncNameResolver> _nameResolver;
  145. Command* _command;
  146. #ifdef HAVE_EPOLL
  147. // HAVE_EPOLL assumes c-ares
  148. size_t _socketsSize;
  149. sock_t _sockets[ARES_GETSOCK_MAXNUM];
  150. #endif // HAVE_EPOLL
  151. public:
  152. AsyncNameResolverEntry(const SharedHandle<AsyncNameResolver>& nameResolver,
  153. Command* command);
  154. bool operator==(const AsyncNameResolverEntry& entry);
  155. #ifdef HAVE_EPOLL
  156. void addSocketEvents(DownloadEngine* e);
  157. void removeSocketEvents(DownloadEngine* e);
  158. #else // !HAVE_EPOLL
  159. int getFds(fd_set* rfdsPtr, fd_set* wfdsPtr);
  160. void process(fd_set* rfdsPtr, fd_set* wfdsPtr);
  161. #endif // !HAVE_EPOLL
  162. };
  163. #endif // ENABLE_ASYNC_DNS
  164. class DownloadEngine {
  165. private:
  166. void waitData();
  167. std::deque<SharedHandle<SocketEntry> > socketEntries;
  168. #ifdef ENABLE_ASYNC_DNS
  169. std::deque<SharedHandle<AsyncNameResolverEntry> > nameResolverEntries;
  170. #endif // ENABLE_ASYNC_DNS
  171. #ifdef HAVE_EPOLL
  172. int _epfd;
  173. struct epoll_event* _epEvents;
  174. static const size_t EPOLL_EVENTS_MAX = 1024;
  175. #else // !HAVE_EPOLL
  176. // If epoll is not available, then use select system call.
  177. fd_set rfdset;
  178. fd_set wfdset;
  179. int fdmax;
  180. #endif // !HAVE_EPOLL
  181. Logger* logger;
  182. SharedHandle<StatCalc> _statCalc;
  183. bool _haltRequested;
  184. class SocketPoolEntry {
  185. private:
  186. SharedHandle<SocketCore> _socket;
  187. std::map<std::string, std::string> _options;
  188. time_t _timeout;
  189. Time _registeredTime;
  190. public:
  191. SocketPoolEntry(const SharedHandle<SocketCore>& socket,
  192. const std::map<std::string, std::string>& option,
  193. time_t timeout);
  194. ~SocketPoolEntry();
  195. bool isTimeout() const;
  196. SharedHandle<SocketCore> getSocket() const;
  197. const std::map<std::string, std::string>& getOptions() const;
  198. };
  199. // key = IP address:port, value = SocketPoolEntry
  200. std::multimap<std::string, SocketPoolEntry> _socketPool;
  201. Time _lastSocketPoolScan;
  202. bool _noWait;
  203. std::deque<Command*> _routineCommands;
  204. SharedHandle<CookieStorage> _cookieStorage;
  205. SharedHandle<BtRegistry> _btRegistry;
  206. CUIDCounter _cuidCounter;
  207. SharedHandle<DNSCache> _dnsCache;
  208. void shortSleep() const;
  209. /**
  210. * Delegates to StatCalc
  211. */
  212. void calculateStatistics();
  213. void onEndOfRun();
  214. void afterEachIteration();
  215. void poolSocket(const std::string& ipaddr,
  216. uint16_t port,
  217. const SocketPoolEntry& entry);
  218. std::multimap<std::string, SocketPoolEntry>::iterator
  219. findSocketPoolEntry(const std::string& ipaddr, uint16_t port);
  220. public:
  221. std::deque<Command*> commands;
  222. SharedHandle<RequestGroupMan> _requestGroupMan;
  223. SharedHandle<FileAllocationMan> _fileAllocationMan;
  224. SharedHandle<CheckIntegrityMan> _checkIntegrityMan;
  225. const Option* option;
  226. DownloadEngine();
  227. virtual ~DownloadEngine();
  228. void run();
  229. void cleanQueue();
  230. #ifndef HAVE_EPOLL
  231. void updateFdSet();
  232. #endif // !HAVE_EPOLL
  233. bool addSocketForReadCheck(const SharedHandle<SocketCore>& socket,
  234. Command* command);
  235. bool deleteSocketForReadCheck(const SharedHandle<SocketCore>& socket,
  236. Command* command);
  237. bool addSocketForWriteCheck(const SharedHandle<SocketCore>& socket,
  238. Command* command);
  239. bool deleteSocketForWriteCheck(const SharedHandle<SocketCore>& socket,
  240. Command* command);
  241. bool addSocketEvents(sock_t socket, Command* command, int events
  242. #if defined HAVE_EPOLL && defined ENABLE_ASYNC_DNS
  243. ,const SharedHandle<AsyncNameResolver>& rs =
  244. SharedHandle<AsyncNameResolver>()
  245. #endif // HAVE_EPOLL && ENABLE_ASYNC_DNS
  246. );
  247. bool deleteSocketEvents(sock_t socket, Command* command, int events
  248. #if defined HAVE_EPOLL && defined ENABLE_ASYNC_DNS
  249. ,const SharedHandle<AsyncNameResolver>& rs =
  250. SharedHandle<AsyncNameResolver>()
  251. #endif // HAVE_EPOLL && ENABLE_ASYNC_DNS
  252. );
  253. #ifdef ENABLE_ASYNC_DNS
  254. bool addNameResolverCheck(const SharedHandle<AsyncNameResolver>& resolver,
  255. Command* command);
  256. bool deleteNameResolverCheck(const SharedHandle<AsyncNameResolver>& resolver,
  257. Command* command);
  258. #endif // ENABLE_ASYNC_DNS
  259. void addCommand(const Commands& commands);
  260. void fillCommand();
  261. void setStatCalc(const SharedHandle<StatCalc>& statCalc);
  262. bool isHaltRequested() const
  263. {
  264. return _haltRequested;
  265. }
  266. void requestHalt();
  267. void setNoWait(bool b);
  268. void addRoutineCommand(Command* command);
  269. void poolSocket(const std::string& ipaddr, uint16_t port,
  270. const SharedHandle<SocketCore>& sock,
  271. const std::map<std::string, std::string>& options,
  272. time_t timeout = 15);
  273. void poolSocket(const std::string& ipaddr, uint16_t port,
  274. const SharedHandle<SocketCore>& sock,
  275. time_t timeout = 15);
  276. SharedHandle<SocketCore> popPooledSocket(const std::string& ipaddr,
  277. uint16_t port);
  278. SharedHandle<SocketCore> popPooledSocket
  279. (std::map<std::string, std::string>& options,
  280. const std::string& ipaddr,
  281. uint16_t port);
  282. SharedHandle<SocketCore>
  283. popPooledSocket(const std::deque<std::string>& ipaddrs, uint16_t port);
  284. SharedHandle<SocketCore>
  285. popPooledSocket
  286. (std::map<std::string, std::string>& options,
  287. const std::deque<std::string>& ipaddrs,
  288. uint16_t port);
  289. SharedHandle<CookieStorage> getCookieStorage() const;
  290. SharedHandle<BtRegistry> getBtRegistry() const;
  291. CUID newCUID();
  292. const std::string& findCachedIPAddress(const std::string& hostname) const;
  293. void cacheIPAddress(const std::string& hostname, const std::string& ipaddr);
  294. };
  295. typedef SharedHandle<DownloadEngine> DownloadEngineHandle;
  296. } // namespace aria2
  297. #endif // _D_DOWNLOAD_ENGINE_H_