SelectEventPoll.cc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. memcpy(&efds, &wfdset_, sizeof(fd_set));
  162. #endif // __MINGW32__
  163. #ifdef ENABLE_ASYNC_DNS
  164. for (auto& i : nameResolverEntries_) {
  165. auto& entry = i.second;
  166. int fd = entry.getFds(&rfds, &wfds);
  167. // TODO force error if fd == 0
  168. if (fdmax_ < fd) {
  169. fdmax_ = fd;
  170. }
  171. }
  172. #endif // ENABLE_ASYNC_DNS
  173. int retval;
  174. do {
  175. struct timeval ttv = tv;
  176. #ifdef __MINGW32__
  177. // winsock will report non-blocking connect() errors in efds,
  178. // unlike posix, which will mark such sockets as writable.
  179. retval = select(fdmax_ + 1, &rfds, &wfds, &efds, &ttv);
  180. #else // !__MINGW32__
  181. retval = select(fdmax_ + 1, &rfds, &wfds, nullptr, &ttv);
  182. #endif // !__MINGW32__
  183. } while (retval == -1 && errno == EINTR);
  184. if (retval > 0) {
  185. for (auto& i : socketEntries_) {
  186. auto& e = i.second;
  187. int events = 0;
  188. if (FD_ISSET(e.getSocket(), &rfds)) {
  189. events |= EventPoll::EVENT_READ;
  190. }
  191. if (FD_ISSET(e.getSocket(), &wfds)) {
  192. events |= EventPoll::EVENT_WRITE;
  193. }
  194. #ifdef __MINGW32__
  195. if (FD_ISSET(e.getSocket(), &efds)) {
  196. events |= EventPoll::EVENT_ERROR;
  197. }
  198. #endif // __MINGW32__
  199. e.processEvents(events);
  200. }
  201. }
  202. else if (retval == -1) {
  203. int errNum = errno;
  204. A2_LOG_INFO(fmt("select error: %s, fdmax: %d",
  205. util::safeStrerror(errNum).c_str(), fdmax_));
  206. }
  207. #ifdef ENABLE_ASYNC_DNS
  208. for (auto& i : nameResolverEntries_) {
  209. i.second.process(&rfds, &wfds);
  210. }
  211. #endif // ENABLE_ASYNC_DNS
  212. }
  213. #ifdef __MINGW32__
  214. namespace {
  215. void checkFdCountMingw(const fd_set& fdset)
  216. {
  217. if (fdset.fd_count >= FD_SETSIZE) {
  218. A2_LOG_WARN("The number of file descriptor exceeded FD_SETSIZE. "
  219. "Download may slow down or fail.");
  220. }
  221. }
  222. } // namespace
  223. #endif // __MINGW32__
  224. void SelectEventPoll::updateFdSet()
  225. {
  226. FD_ZERO(&rfdset_);
  227. FD_ZERO(&wfdset_);
  228. #ifdef __MINGW32__
  229. FD_SET(dummySocket_, &rfdset_);
  230. FD_SET(dummySocket_, &wfdset_);
  231. fdmax_ = dummySocket_;
  232. #else // !__MINGW32__
  233. fdmax_ = 0;
  234. #endif // !__MINGW32__
  235. for (auto& i : socketEntries_) {
  236. auto& e = i.second;
  237. sock_t fd = e.getSocket();
  238. #ifndef __MINGW32__
  239. if (fd < 0 || FD_SETSIZE <= fd) {
  240. A2_LOG_WARN("Detected file descriptor >= FD_SETSIZE or < 0. "
  241. "Download may slow down or fail.");
  242. continue;
  243. }
  244. #endif // !__MINGW32__
  245. int events = e.getEvents();
  246. if (events & EventPoll::EVENT_READ) {
  247. #ifdef __MINGW32__
  248. checkFdCountMingw(rfdset_);
  249. #endif // __MINGW32__
  250. FD_SET(fd, &rfdset_);
  251. }
  252. if (events & EventPoll::EVENT_WRITE) {
  253. #ifdef __MINGW32__
  254. checkFdCountMingw(wfdset_);
  255. #endif // __MINGW32__
  256. FD_SET(fd, &wfdset_);
  257. }
  258. if (fdmax_ < fd) {
  259. fdmax_ = fd;
  260. }
  261. }
  262. }
  263. bool SelectEventPoll::addEvents(sock_t socket, Command* command,
  264. EventPoll::EventType events)
  265. {
  266. auto i = socketEntries_.lower_bound(socket);
  267. if (i != std::end(socketEntries_) && (*i).first == socket) {
  268. (*i).second.addCommandEvent(command, events);
  269. }
  270. else {
  271. i = socketEntries_.insert(i, std::make_pair(socket, SocketEntry(socket)));
  272. (*i).second.addCommandEvent(command, events);
  273. }
  274. updateFdSet();
  275. return true;
  276. }
  277. bool SelectEventPoll::deleteEvents(sock_t socket, Command* command,
  278. EventPoll::EventType events)
  279. {
  280. auto i = socketEntries_.find(socket);
  281. if (i == std::end(socketEntries_)) {
  282. A2_LOG_DEBUG(fmt("Socket %d is not found in SocketEntries.", socket));
  283. return false;
  284. }
  285. auto& socketEntry = (*i).second;
  286. socketEntry.removeCommandEvent(command, events);
  287. if (socketEntry.eventEmpty()) {
  288. socketEntries_.erase(i);
  289. }
  290. updateFdSet();
  291. return true;
  292. }
  293. #ifdef ENABLE_ASYNC_DNS
  294. bool SelectEventPoll::addNameResolver(
  295. const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
  296. {
  297. auto key = std::make_pair(resolver.get(), command);
  298. auto itr = nameResolverEntries_.lower_bound(key);
  299. if (itr != std::end(nameResolverEntries_) && (*itr).first == key) {
  300. return false;
  301. }
  302. nameResolverEntries_.insert(
  303. itr, std::make_pair(key, AsyncNameResolverEntry(resolver, command)));
  304. return true;
  305. }
  306. bool SelectEventPoll::deleteNameResolver(
  307. const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
  308. {
  309. auto key = std::make_pair(resolver.get(), command);
  310. return nameResolverEntries_.erase(key) == 1;
  311. }
  312. #endif // ENABLE_ASYNC_DNS
  313. } // namespace aria2