PortEventPoll.cc 10 KB

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