SelectEventPoll.cc 10 KB

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