SelectEventPoll.cc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2009 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. #include "SelectEventPoll.h"
  36. #ifdef __MINGW32__
  37. #include <cassert>
  38. #endif // __MINGW32__
  39. #include <cstring>
  40. #include <algorithm>
  41. #include <numeric>
  42. #include "Command.h"
  43. #include "LogFactory.h"
  44. #include "Logger.h"
  45. #include "a2functional.h"
  46. #include "fmt.h"
  47. #include "util.h"
  48. namespace aria2 {
  49. SelectEventPoll::CommandEvent::CommandEvent(Command* command, int events)
  50. : command_(command), events_(events)
  51. {
  52. }
  53. void SelectEventPoll::CommandEvent::processEvents(int events)
  54. {
  55. if ((events_ & events) ||
  56. ((EventPoll::EVENT_ERROR | EventPoll::EVENT_HUP) & events)) {
  57. command_->setStatusActive();
  58. }
  59. if (EventPoll::EVENT_READ & events) {
  60. command_->readEventReceived();
  61. }
  62. if (EventPoll::EVENT_WRITE & events) {
  63. command_->writeEventReceived();
  64. }
  65. if (EventPoll::EVENT_ERROR & events) {
  66. command_->errorEventReceived();
  67. }
  68. if (EventPoll::EVENT_HUP & events) {
  69. command_->hupEventReceived();
  70. }
  71. }
  72. SelectEventPoll::SocketEntry::SocketEntry(sock_t socket) : socket_(socket) {}
  73. void SelectEventPoll::SocketEntry::addCommandEvent(Command* command, int events)
  74. {
  75. CommandEvent cev(command, events);
  76. auto i = std::find(commandEvents_.begin(), commandEvents_.end(), cev);
  77. if (i == commandEvents_.end()) {
  78. commandEvents_.push_back(cev);
  79. }
  80. else {
  81. (*i).addEvents(events);
  82. }
  83. }
  84. void SelectEventPoll::SocketEntry::removeCommandEvent(Command* command,
  85. int events)
  86. {
  87. CommandEvent cev(command, events);
  88. auto i = std::find(commandEvents_.begin(), commandEvents_.end(), cev);
  89. if (i == commandEvents_.end()) {
  90. // not found
  91. }
  92. else {
  93. (*i).removeEvents(events);
  94. if ((*i).eventsEmpty()) {
  95. commandEvents_.erase(i);
  96. }
  97. }
  98. }
  99. void SelectEventPoll::SocketEntry::processEvents(int events)
  100. {
  101. using namespace std::placeholders;
  102. std::for_each(commandEvents_.begin(), commandEvents_.end(),
  103. std::bind(&CommandEvent::processEvents, _1, events));
  104. }
  105. int accumulateEvent(int events, const SelectEventPoll::CommandEvent& event)
  106. {
  107. return events | event.getEvents();
  108. }
  109. int SelectEventPoll::SocketEntry::getEvents()
  110. {
  111. return std::accumulate(commandEvents_.begin(), commandEvents_.end(), 0,
  112. accumulateEvent);
  113. }
  114. #ifdef ENABLE_ASYNC_DNS
  115. SelectEventPoll::AsyncNameResolverEntry::AsyncNameResolverEntry(
  116. const std::shared_ptr<AsyncNameResolver>& nameResolver, Command* command)
  117. : nameResolver_(nameResolver), command_(command)
  118. {
  119. }
  120. int SelectEventPoll::AsyncNameResolverEntry::getFds(fd_set* rfdsPtr,
  121. fd_set* wfdsPtr)
  122. {
  123. return nameResolver_->getFds(rfdsPtr, wfdsPtr);
  124. }
  125. void SelectEventPoll::AsyncNameResolverEntry::process(fd_set* rfdsPtr,
  126. fd_set* wfdsPtr)
  127. {
  128. nameResolver_->process(rfdsPtr, wfdsPtr);
  129. switch (nameResolver_->getStatus()) {
  130. case AsyncNameResolver::STATUS_SUCCESS:
  131. case AsyncNameResolver::STATUS_ERROR:
  132. command_->setStatusActive();
  133. break;
  134. default:
  135. break;
  136. }
  137. }
  138. #endif // ENABLE_ASYNC_DNS
  139. SelectEventPoll::SelectEventPoll()
  140. {
  141. #ifdef __MINGW32__
  142. dummySocket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  143. assert(dummySocket_ != (sock_t)-1);
  144. #endif // __MINGW32__
  145. updateFdSet();
  146. }
  147. SelectEventPoll::~SelectEventPoll()
  148. {
  149. #ifdef __MINGW32__
  150. ::closesocket(dummySocket_);
  151. #endif // __MINGW32__
  152. }
  153. void SelectEventPoll::poll(const struct timeval& tv)
  154. {
  155. fd_set rfds;
  156. fd_set wfds;
  157. memcpy(&rfds, &rfdset_, sizeof(fd_set));
  158. memcpy(&wfds, &wfdset_, sizeof(fd_set));
  159. #ifdef __MINGW32__
  160. fd_set efds;
  161. FD_ZERO(&efds);
  162. FD_SET(dummySocket_, &efds);
  163. #endif // __MINGW32__
  164. #ifdef ENABLE_ASYNC_DNS
  165. for (auto& i : nameResolverEntries_) {
  166. auto& entry = i.second;
  167. int fd = entry.getFds(&rfds, &wfds);
  168. // TODO force error if fd == 0
  169. if (fdmax_ < fd) {
  170. fdmax_ = fd;
  171. }
  172. }
  173. #endif // ENABLE_ASYNC_DNS
  174. int retval;
  175. do {
  176. struct timeval ttv = tv;
  177. #ifdef __MINGW32__
  178. retval = select(fdmax_ + 1, &rfds, &wfds, &efds, &ttv);
  179. #else // !__MINGW32__
  180. retval = select(fdmax_ + 1, &rfds, &wfds, nullptr, &ttv);
  181. #endif // !__MINGW32__
  182. } while (retval == -1 && errno == EINTR);
  183. if (retval > 0) {
  184. for (auto& i : socketEntries_) {
  185. auto& e = i.second;
  186. int events = 0;
  187. if (FD_ISSET(e.getSocket(), &rfds)) {
  188. events |= EventPoll::EVENT_READ;
  189. }
  190. if (FD_ISSET(e.getSocket(), &wfds)) {
  191. events |= EventPoll::EVENT_WRITE;
  192. }
  193. e.processEvents(events);
  194. }
  195. }
  196. else if (retval == -1) {
  197. int errNum = errno;
  198. A2_LOG_INFO(fmt("select error: %s", util::safeStrerror(errNum).c_str()));
  199. }
  200. #ifdef ENABLE_ASYNC_DNS
  201. for (auto& i : nameResolverEntries_) {
  202. i.second.process(&rfds, &wfds);
  203. }
  204. #endif // ENABLE_ASYNC_DNS
  205. }
  206. #ifdef __MINGW32__
  207. namespace {
  208. void checkFdCountMingw(const fd_set& fdset)
  209. {
  210. if (fdset.fd_count >= FD_SETSIZE) {
  211. A2_LOG_WARN("The number of file descriptor exceeded FD_SETSIZE. "
  212. "Download may slow down or fail.");
  213. }
  214. }
  215. } // namespace
  216. #endif // __MINGW32__
  217. void SelectEventPoll::updateFdSet()
  218. {
  219. #ifdef __MINGW32__
  220. fdmax_ = dummySocket_;
  221. #else // !__MINGW32__
  222. fdmax_ = 0;
  223. #endif // !__MINGW32__
  224. FD_ZERO(&rfdset_);
  225. FD_ZERO(&wfdset_);
  226. for (auto& i : socketEntries_) {
  227. auto& e = i.second;
  228. sock_t fd = e.getSocket();
  229. #ifndef __MINGW32__
  230. if (fd < 0 || FD_SETSIZE <= fd) {
  231. A2_LOG_WARN("Detected file descriptor >= FD_SETSIZE or < 0. "
  232. "Download may slow down or fail.");
  233. continue;
  234. }
  235. #endif // !__MINGW32__
  236. int events = e.getEvents();
  237. if (events & EventPoll::EVENT_READ) {
  238. #ifdef __MINGW32__
  239. checkFdCountMingw(rfdset_);
  240. #endif // __MINGW32__
  241. FD_SET(fd, &rfdset_);
  242. }
  243. if (events & EventPoll::EVENT_WRITE) {
  244. #ifdef __MINGW32__
  245. checkFdCountMingw(wfdset_);
  246. #endif // __MINGW32__
  247. FD_SET(fd, &wfdset_);
  248. }
  249. if (fdmax_ < fd) {
  250. fdmax_ = fd;
  251. }
  252. }
  253. }
  254. bool SelectEventPoll::addEvents(sock_t socket, Command* command,
  255. EventPoll::EventType events)
  256. {
  257. auto i = socketEntries_.lower_bound(socket);
  258. if (i != std::end(socketEntries_) && (*i).first == socket) {
  259. (*i).second.addCommandEvent(command, events);
  260. }
  261. else {
  262. i = socketEntries_.insert(i, std::make_pair(socket, SocketEntry(socket)));
  263. (*i).second.addCommandEvent(command, events);
  264. }
  265. updateFdSet();
  266. return true;
  267. }
  268. bool SelectEventPoll::deleteEvents(sock_t socket, Command* command,
  269. EventPoll::EventType events)
  270. {
  271. auto i = socketEntries_.find(socket);
  272. if (i == std::end(socketEntries_)) {
  273. A2_LOG_DEBUG(fmt("Socket %d is not found in SocketEntries.", socket));
  274. return false;
  275. }
  276. auto& socketEntry = (*i).second;
  277. socketEntry.removeCommandEvent(command, events);
  278. if (socketEntry.eventEmpty()) {
  279. socketEntries_.erase(i);
  280. }
  281. updateFdSet();
  282. return true;
  283. }
  284. #ifdef ENABLE_ASYNC_DNS
  285. bool SelectEventPoll::addNameResolver(
  286. const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
  287. {
  288. auto key = std::make_pair(resolver.get(), command);
  289. auto itr = nameResolverEntries_.lower_bound(key);
  290. if (itr != std::end(nameResolverEntries_) && (*itr).first == key) {
  291. return false;
  292. }
  293. nameResolverEntries_.insert(
  294. itr, std::make_pair(key, AsyncNameResolverEntry(resolver, command)));
  295. return true;
  296. }
  297. bool SelectEventPoll::deleteNameResolver(
  298. const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
  299. {
  300. auto key = std::make_pair(resolver.get(), command);
  301. return nameResolverEntries_.erase(key) == 1;
  302. }
  303. #endif // ENABLE_ASYNC_DNS
  304. } // namespace aria2