PortEventPoll.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2010 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 "PortEventPoll.h"
  36. #include <cstring>
  37. #include <algorithm>
  38. #include <numeric>
  39. #include "Command.h"
  40. #include "LogFactory.h"
  41. #include "Logger.h"
  42. namespace aria2 {
  43. PortEventPoll::KSocketEntry::KSocketEntry(sock_t s):
  44. SocketEntry<KCommandEvent, KADNSEvent>(s) {}
  45. int accumulateEvent(int events, const PortEventPoll::KEvent& event)
  46. {
  47. return events|event.getEvents();
  48. }
  49. PortEventPoll::A2PortEvent PortEventPoll::KSocketEntry::getEvents()
  50. {
  51. A2PortEvent portEvent;
  52. portEvent.socketEntry = this;
  53. #ifdef ENABLE_ASYNC_DNS
  54. portEvent.events =
  55. std::accumulate(_adnsEvents.begin(),
  56. _adnsEvents.end(),
  57. std::accumulate(_commandEvents.begin(),
  58. _commandEvents.end(), 0, accumulateEvent),
  59. accumulateEvent);
  60. #else // !ENABLE_ASYNC_DNS
  61. portEvent.events =
  62. std::accumulate(_commandEvents.begin(), _commandEvents.end(), 0,
  63. accumulateEvent);
  64. #endif // !ENABLE_ASYNC_DNS
  65. return portEvent;
  66. }
  67. PortEventPoll::PortEventPoll():
  68. _portEventsSize(PORT_EVENTS_SIZE),
  69. _portEvents(new port_event_t[_portEventsSize]),
  70. _logger(LogFactory::getInstance())
  71. {
  72. _port = port_create();
  73. }
  74. PortEventPoll::~PortEventPoll()
  75. {
  76. if(_port != -1) {
  77. int r;
  78. while((r = close(_port)) == -1 && errno == EINTR);
  79. if(r == -1) {
  80. _logger->error("Error occurred while closing port %d: %s",
  81. _port, strerror(errno));
  82. }
  83. }
  84. delete [] _portEvents;
  85. }
  86. bool PortEventPoll::good() const
  87. {
  88. return _port != -1;
  89. }
  90. void PortEventPoll::poll(const struct timeval& tv)
  91. {
  92. struct timespec timeout = { tv.tv_sec, tv.tv_usec*1000 };
  93. int res;
  94. uint_t nget = 1;
  95. // If port_getn was interrupted by signal, it can consume events but
  96. // not updat nget!. For this very annoying bug, we have to check
  97. // actually event is filled or not.
  98. _portEvents[0].portev_user = (void*)-1;
  99. res = port_getn(_port, _portEvents, _portEventsSize, &nget, &timeout);
  100. if(res == 0 ||
  101. (res == -1 && (errno == ETIME || errno == EINTR) &&
  102. _portEvents[0].portev_user != (void*)-1)) {
  103. if(_logger->debug()) {
  104. _logger->debug("nget=%u", nget);
  105. }
  106. for(uint_t i = 0; i < nget; ++i) {
  107. const port_event_t& pev = _portEvents[i];
  108. KSocketEntry* p = reinterpret_cast<KSocketEntry*>(pev.portev_user);
  109. p->processEvents(pev.portev_events);
  110. int r = port_associate(_port, PORT_SOURCE_FD, pev.portev_object,
  111. p->getEvents().events, p);
  112. if(r == -1) {
  113. _logger->error("port_associate failed for file descriptor %d: cause %s",
  114. pev.portev_object, strerror(errno));
  115. }
  116. }
  117. }
  118. #ifdef ENABLE_ASYNC_DNS
  119. // It turns out that we have to call ares_process_fd before ares's
  120. // own timeout and ares may create new sockets or closes socket in
  121. // their API. So we call ares_process_fd for all ares_channel and
  122. // re-register their sockets.
  123. for(std::deque<SharedHandle<KAsyncNameResolverEntry> >::iterator i =
  124. _nameResolverEntries.begin(), eoi = _nameResolverEntries.end();
  125. i != eoi; ++i) {
  126. (*i)->processTimeout();
  127. (*i)->removeSocketEvents(this);
  128. (*i)->addSocketEvents(this);
  129. }
  130. #endif // ENABLE_ASYNC_DNS
  131. // TODO timeout of name resolver is determined in Command(AbstractCommand,
  132. // DHTEntryPoint...Command)
  133. }
  134. static int translateEvents(EventPoll::EventType events)
  135. {
  136. int newEvents = 0;
  137. if(EventPoll::EVENT_READ&events) {
  138. newEvents |= PortEventPoll::IEV_READ;
  139. }
  140. if(EventPoll::EVENT_WRITE&events) {
  141. newEvents |= PortEventPoll::IEV_WRITE;
  142. }
  143. if(EventPoll::EVENT_ERROR&events) {
  144. newEvents |= PortEventPoll::IEV_ERROR;
  145. }
  146. if(EventPoll::EVENT_HUP&events) {
  147. newEvents |= PortEventPoll::IEV_HUP;
  148. }
  149. return newEvents;
  150. }
  151. bool PortEventPoll::addEvents(sock_t socket,
  152. const PortEventPoll::KEvent& event)
  153. {
  154. SharedHandle<KSocketEntry> socketEntry(new KSocketEntry(socket));
  155. std::deque<SharedHandle<KSocketEntry> >::iterator i =
  156. std::lower_bound(_socketEntries.begin(), _socketEntries.end(), socketEntry);
  157. int r = 0;
  158. if(i != _socketEntries.end() && (*i) == socketEntry) {
  159. event.addSelf(*i);
  160. A2PortEvent pv = (*i)->getEvents();
  161. r = port_associate(_port, PORT_SOURCE_FD, (*i)->getSocket(),
  162. pv.events, pv.socketEntry);
  163. } else {
  164. _socketEntries.insert(i, socketEntry);
  165. if(_socketEntries.size() > _portEventsSize) {
  166. _portEventsSize *= 2;
  167. delete [] _portEvents;
  168. _portEvents = new port_event_t[_portEventsSize];
  169. }
  170. event.addSelf(socketEntry);
  171. A2PortEvent pv = socketEntry->getEvents();
  172. r = port_associate(_port, PORT_SOURCE_FD, socketEntry->getSocket(),
  173. pv.events, pv.socketEntry);
  174. }
  175. if(r == -1) {
  176. if(_logger->debug()) {
  177. _logger->debug("Failed to add socket event %d:%s",
  178. socket, strerror(errno));
  179. }
  180. return false;
  181. } else {
  182. return true;
  183. }
  184. }
  185. bool PortEventPoll::addEvents(sock_t socket, Command* command,
  186. EventPoll::EventType events)
  187. {
  188. int portEvents = translateEvents(events);
  189. return addEvents(socket, KCommandEvent(command, portEvents));
  190. }
  191. #ifdef ENABLE_ASYNC_DNS
  192. bool PortEventPoll::addEvents(sock_t socket, Command* command, int events,
  193. const SharedHandle<AsyncNameResolver>& rs)
  194. {
  195. return addEvents(socket, KADNSEvent(rs, command, socket, events));
  196. }
  197. #endif // ENABLE_ASYNC_DNS
  198. bool PortEventPoll::deleteEvents(sock_t socket,
  199. const PortEventPoll::KEvent& event)
  200. {
  201. SharedHandle<KSocketEntry> socketEntry(new KSocketEntry(socket));
  202. std::deque<SharedHandle<KSocketEntry> >::iterator i =
  203. std::lower_bound(_socketEntries.begin(), _socketEntries.end(), socketEntry);
  204. if(i != _socketEntries.end() && (*i) == socketEntry) {
  205. event.removeSelf(*i);
  206. int r = 0;
  207. if((*i)->eventEmpty()) {
  208. r = port_dissociate(_port, PORT_SOURCE_FD, (*i)->getSocket());
  209. _socketEntries.erase(i);
  210. } else {
  211. A2PortEvent pv = (*i)->getEvents();
  212. r = port_associate(_port, PORT_SOURCE_FD, (*i)->getSocket(),
  213. pv.events, pv.socketEntry);
  214. }
  215. if(r == -1) {
  216. if(_logger->debug()) {
  217. _logger->debug("Failed to delete socket event:%s", strerror(errno));
  218. }
  219. return false;
  220. } else {
  221. return true;
  222. }
  223. } else {
  224. if(_logger->debug()) {
  225. _logger->debug("Socket %d is not found in SocketEntries.", socket);
  226. }
  227. return false;
  228. }
  229. }
  230. #ifdef ENABLE_ASYNC_DNS
  231. bool PortEventPoll::deleteEvents(sock_t socket, Command* command,
  232. const SharedHandle<AsyncNameResolver>& rs)
  233. {
  234. return deleteEvents(socket, KADNSEvent(rs, command, socket, 0));
  235. }
  236. #endif // ENABLE_ASYNC_DNS
  237. bool PortEventPoll::deleteEvents(sock_t socket, Command* command,
  238. EventPoll::EventType events)
  239. {
  240. int portEvents = translateEvents(events);
  241. return deleteEvents(socket, KCommandEvent(command, portEvents));
  242. }
  243. #ifdef ENABLE_ASYNC_DNS
  244. bool PortEventPoll::addNameResolver
  245. (const SharedHandle<AsyncNameResolver>& resolver, Command* command)
  246. {
  247. SharedHandle<KAsyncNameResolverEntry> entry
  248. (new KAsyncNameResolverEntry(resolver, command));
  249. std::deque<SharedHandle<KAsyncNameResolverEntry> >::iterator itr =
  250. std::find(_nameResolverEntries.begin(), _nameResolverEntries.end(), entry);
  251. if(itr == _nameResolverEntries.end()) {
  252. _nameResolverEntries.push_back(entry);
  253. entry->addSocketEvents(this);
  254. return true;
  255. } else {
  256. return false;
  257. }
  258. }
  259. bool PortEventPoll::deleteNameResolver
  260. (const SharedHandle<AsyncNameResolver>& resolver, Command* command)
  261. {
  262. SharedHandle<KAsyncNameResolverEntry> entry
  263. (new KAsyncNameResolverEntry(resolver, command));
  264. std::deque<SharedHandle<KAsyncNameResolverEntry> >::iterator itr =
  265. std::find(_nameResolverEntries.begin(), _nameResolverEntries.end(), entry);
  266. if(itr == _nameResolverEntries.end()) {
  267. return false;
  268. } else {
  269. (*itr)->removeSocketEvents(this);
  270. _nameResolverEntries.erase(itr);
  271. return true;
  272. }
  273. }
  274. #endif // ENABLE_ASYNC_DNS
  275. } // namespace aria2