DownloadEngine.h 8.4 KB

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