DownloadEngine.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "Command.h"
  38. #include "Socket.h"
  39. #include "SegmentMan.h"
  40. #include "common.h"
  41. #include "Logger.h"
  42. #include "Option.h"
  43. #ifdef ENABLE_ASYNC_DNS
  44. # include "NameResolver.h"
  45. #endif // ENABLE_ASYNC_DNS
  46. typedef deque<SocketHandle> Sockets;
  47. typedef deque<Command*> Commands;
  48. class SocketEntry {
  49. public:
  50. enum TYPE {
  51. TYPE_RD,
  52. TYPE_WR,
  53. };
  54. SocketHandle socket;
  55. Command* command;
  56. TYPE type;
  57. public:
  58. SocketEntry(const SocketHandle& socket,
  59. Command* command,
  60. TYPE type):
  61. socket(socket), command(command), type(type) {}
  62. ~SocketEntry() {}
  63. bool operator==(const SocketEntry& entry) {
  64. return socket == entry.socket &&
  65. command == entry.command &&
  66. type == entry.type;
  67. }
  68. };
  69. typedef deque<SocketEntry> SocketEntries;
  70. #ifdef ENABLE_ASYNC_DNS
  71. class NameResolverEntry {
  72. public:
  73. NameResolverHandle nameResolver;
  74. Command* command;
  75. public:
  76. NameResolverEntry(const NameResolverHandle& nameResolver,
  77. Command* command):
  78. nameResolver(nameResolver), command(command) {}
  79. ~NameResolverEntry() {}
  80. bool operator==(const NameResolverEntry& entry) {
  81. return nameResolver == entry.nameResolver &&
  82. command == entry.command;
  83. }
  84. };
  85. typedef deque<NameResolverEntry> NameResolverEntries;
  86. #endif // ENABLE_ASYNC_DNS
  87. class DownloadEngine {
  88. private:
  89. void waitData(Commands& activeCommands);
  90. SocketEntries socketEntries;
  91. #ifdef ENABLE_ASYNC_DNS
  92. NameResolverEntries nameResolverEntries;
  93. #endif // ENABLE_ASYNC_DNS
  94. fd_set rfdset;
  95. fd_set wfdset;
  96. int fdmax;
  97. void shortSleep() const;
  98. bool addSocket(const SocketEntry& socketEntry);
  99. bool deleteSocket(const SocketEntry& socketEntry);
  100. protected:
  101. const Logger* logger;
  102. virtual void initStatistics() = 0;
  103. virtual void calculateStatistics() = 0;
  104. virtual void onEndOfRun() = 0;
  105. virtual void afterEachIteration() {}
  106. public:
  107. bool noWait;
  108. Commands commands;
  109. SegmentMan* segmentMan;
  110. const Option* option;
  111. DownloadEngine();
  112. virtual ~DownloadEngine();
  113. void run();
  114. void cleanQueue();
  115. void updateFdSet();
  116. bool addSocketForReadCheck(const SocketHandle& socket,
  117. Command* command);
  118. bool deleteSocketForReadCheck(const SocketHandle& socket,
  119. Command* command);
  120. bool addSocketForWriteCheck(const SocketHandle& socket,
  121. Command* command);
  122. bool deleteSocketForWriteCheck(const SocketHandle& socket,
  123. Command* command);
  124. #ifdef ENABLE_ASYNC_DNS
  125. bool addNameResolverCheck(const NameResolverHandle& resolver,
  126. Command* command);
  127. bool deleteNameResolverCheck(const NameResolverHandle& resolver,
  128. Command* command);
  129. #endif // ENABLE_ASYNC_DNS
  130. };
  131. #endif // _D_DOWNLOAD_ENGINE_H_