DownloadEngine.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. int _socket;
  80. int _events;
  81. public:
  82. ADNSEvent(const SharedHandle<AsyncNameResolver>& resolver, Command* command,
  83. int 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. int _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(int 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. int 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. int _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. static const size_t EPOLL_EVENTS_MAX = 1024;
  169. #else // !HAVE_EPOLL
  170. // If epoll is not available, then use select system call.
  171. fd_set rfdset;
  172. fd_set wfdset;
  173. int fdmax;
  174. #endif // !HAVE_EPOLL
  175. Logger* logger;
  176. SharedHandle<StatCalc> _statCalc;
  177. bool _haltRequested;
  178. class SocketPoolEntry {
  179. private:
  180. SharedHandle<SocketCore> _socket;
  181. time_t _timeout;
  182. Time _registeredTime;
  183. public:
  184. SocketPoolEntry(const SharedHandle<SocketCore>& socket,
  185. time_t timeout);
  186. ~SocketPoolEntry();
  187. bool isTimeout() const;
  188. SharedHandle<SocketCore> getSocket() const;
  189. };
  190. // key = IP address:port, value = SocketPoolEntry
  191. std::multimap<std::string, SocketPoolEntry> _socketPool;
  192. void shortSleep() const;
  193. /**
  194. * Delegates to StatCalc
  195. */
  196. void calculateStatistics();
  197. void onEndOfRun();
  198. void afterEachIteration();
  199. private:
  200. bool _noWait;
  201. std::deque<Command*> _routineCommands;
  202. public:
  203. std::deque<Command*> commands;
  204. SharedHandle<RequestGroupMan> _requestGroupMan;
  205. SharedHandle<FileAllocationMan> _fileAllocationMan;
  206. SharedHandle<CheckIntegrityMan> _checkIntegrityMan;
  207. const Option* option;
  208. DownloadEngine();
  209. virtual ~DownloadEngine();
  210. void run();
  211. void cleanQueue();
  212. #ifndef HAVE_EPOLL
  213. void updateFdSet();
  214. #endif // !HAVE_EPOLL
  215. bool addSocketForReadCheck(const SharedHandle<SocketCore>& socket,
  216. Command* command);
  217. bool deleteSocketForReadCheck(const SharedHandle<SocketCore>& socket,
  218. Command* command);
  219. bool addSocketForWriteCheck(const SharedHandle<SocketCore>& socket,
  220. Command* command);
  221. bool deleteSocketForWriteCheck(const SharedHandle<SocketCore>& socket,
  222. Command* command);
  223. bool addSocketEvents(int socket, Command* command, int events
  224. #if defined HAVE_EPOLL && defined ENABLE_ASYNC_DNS
  225. ,const SharedHandle<AsyncNameResolver>& rs =
  226. SharedHandle<AsyncNameResolver>()
  227. #endif // HAVE_EPOLL && ENABLE_ASYNC_DNS
  228. );
  229. bool deleteSocketEvents(int socket, Command* command, int events
  230. #if defined HAVE_EPOLL && defined ENABLE_ASYNC_DNS
  231. ,const SharedHandle<AsyncNameResolver>& rs =
  232. SharedHandle<AsyncNameResolver>()
  233. #endif // HAVE_EPOLL && ENABLE_ASYNC_DNS
  234. );
  235. #ifdef ENABLE_ASYNC_DNS
  236. bool addNameResolverCheck(const SharedHandle<AsyncNameResolver>& resolver,
  237. Command* command);
  238. bool deleteNameResolverCheck(const SharedHandle<AsyncNameResolver>& resolver,
  239. Command* command);
  240. #endif // ENABLE_ASYNC_DNS
  241. void addCommand(const Commands& commands);
  242. void fillCommand();
  243. void setStatCalc(const SharedHandle<StatCalc>& statCalc);
  244. bool isHaltRequested() const
  245. {
  246. return _haltRequested;
  247. }
  248. void requestHalt();
  249. void setNoWait(bool b);
  250. void addRoutineCommand(Command* command);
  251. void poolSocket(const std::string& ipaddr, uint16_t port,
  252. const SharedHandle<SocketCore>& sock, time_t timeout = 15);
  253. SharedHandle<SocketCore> popPooledSocket(const std::string& ipaddr,
  254. uint16_t port);
  255. SharedHandle<SocketCore>
  256. popPooledSocket(const std::deque<std::string>& ipaddrs, uint16_t port);
  257. };
  258. typedef SharedHandle<DownloadEngine> DownloadEngineHandle;
  259. } // namespace aria2
  260. #endif // _D_DOWNLOAD_ENGINE_H_