SelectEventPoll.cc 10 KB

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