PortEventPoll.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 = close(port_);
  81. int errNum = errno;
  82. if(r == -1) {
  83. A2_LOG_ERROR(fmt("Error occurred while closing port %d: %s",
  84. port_,
  85. util::safeStrerror(errNum).c_str()));
  86. }
  87. }
  88. delete [] portEvents_;
  89. }
  90. bool PortEventPoll::good() const
  91. {
  92. return port_ != -1;
  93. }
  94. void PortEventPoll::poll(const struct timeval& tv)
  95. {
  96. struct timespec timeout = { tv.tv_sec, tv.tv_usec*1000 };
  97. int res;
  98. uint_t nget = 1;
  99. // If port_getn was interrupted by signal, it can consume events but
  100. // not updat nget!. For this very annoying bug, we have to check
  101. // actually event is filled or not.
  102. portEvents_[0].portev_user = (void*)-1;
  103. res = port_getn(port_, portEvents_, portEventsSize_, &nget, &timeout);
  104. if(res == 0 ||
  105. (res == -1 && (errno == ETIME || errno == EINTR) &&
  106. portEvents_[0].portev_user != (void*)-1)) {
  107. A2_LOG_DEBUG(fmt("nget=%u", nget));
  108. for(uint_t i = 0; i < nget; ++i) {
  109. const port_event_t& pev = portEvents_[i];
  110. KSocketEntry* p = reinterpret_cast<KSocketEntry*>(pev.portev_user);
  111. p->processEvents(pev.portev_events);
  112. int r = port_associate(port_, PORT_SOURCE_FD, pev.portev_object,
  113. p->getEvents().events, p);
  114. int errNum = errno;
  115. if(r == -1) {
  116. A2_LOG_INFO(fmt("port_associate failed for file descriptor %d:"
  117. " cause %s",
  118. pev.portev_object,
  119. util::safeStrerror(errNum).c_str()));
  120. }
  121. }
  122. } else if(res == -1) {
  123. int errNum = errno;
  124. A2_LOG_INFO(fmt("port_getn error: %s", util::safeStrerror(errNum).c_str()));
  125. }
  126. #ifdef ENABLE_ASYNC_DNS
  127. // It turns out that we have to call ares_process_fd before ares's
  128. // own timeout and ares may create new sockets or closes socket in
  129. // their API. So we call ares_process_fd for all ares_channel and
  130. // re-register their sockets.
  131. for(KAsyncNameResolverEntrySet::iterator i = nameResolverEntries_.begin(),
  132. eoi = nameResolverEntries_.end(); i != eoi; ++i) {
  133. (*i)->processTimeout();
  134. (*i)->removeSocketEvents(this);
  135. (*i)->addSocketEvents(this);
  136. }
  137. #endif // ENABLE_ASYNC_DNS
  138. // TODO timeout of name resolver is determined in Command(AbstractCommand,
  139. // DHTEntryPoint...Command)
  140. }
  141. namespace {
  142. int translateEvents(EventPoll::EventType events)
  143. {
  144. int newEvents = 0;
  145. if(EventPoll::EVENT_READ&events) {
  146. newEvents |= PortEventPoll::IEV_READ;
  147. }
  148. if(EventPoll::EVENT_WRITE&events) {
  149. newEvents |= PortEventPoll::IEV_WRITE;
  150. }
  151. if(EventPoll::EVENT_ERROR&events) {
  152. newEvents |= PortEventPoll::IEV_ERROR;
  153. }
  154. if(EventPoll::EVENT_HUP&events) {
  155. newEvents |= PortEventPoll::IEV_HUP;
  156. }
  157. return newEvents;
  158. }
  159. } // namespace
  160. bool PortEventPoll::addEvents(sock_t socket,
  161. const PortEventPoll::KEvent& event)
  162. {
  163. std::shared_ptr<KSocketEntry> socketEntry(new KSocketEntry(socket));
  164. KSocketEntrySet::iterator i = socketEntries_.lower_bound(socketEntry);
  165. int r = 0;
  166. int errNum = 0;
  167. if(i != socketEntries_.end() && *(*i) == *socketEntry) {
  168. event.addSelf((*i).get());
  169. A2PortEvent pv = (*i)->getEvents();
  170. r = port_associate(port_, PORT_SOURCE_FD, (*i)->getSocket(),
  171. pv.events, pv.socketEntry);
  172. errNum = r;
  173. } else {
  174. socketEntries_.insert(i, socketEntry);
  175. if(socketEntries_.size() > portEventsSize_) {
  176. portEventsSize_ *= 2;
  177. delete [] portEvents_;
  178. portEvents_ = new port_event_t[portEventsSize_];
  179. }
  180. event.addSelf(socketEntry.get());
  181. A2PortEvent pv = socketEntry->getEvents();
  182. r = port_associate(port_, PORT_SOURCE_FD, socketEntry->getSocket(),
  183. pv.events, pv.socketEntry);
  184. errNum = r;
  185. }
  186. if(r == -1) {
  187. A2_LOG_DEBUG(fmt("Failed to add socket event %d:%s",
  188. socket, util::safeStrerror(errNum).c_str()));
  189. return false;
  190. } else {
  191. return true;
  192. }
  193. }
  194. bool PortEventPoll::addEvents(sock_t socket, Command* command,
  195. EventPoll::EventType events)
  196. {
  197. int portEvents = translateEvents(events);
  198. return addEvents(socket, KCommandEvent(command, portEvents));
  199. }
  200. #ifdef ENABLE_ASYNC_DNS
  201. bool PortEventPoll::addEvents(sock_t socket, Command* command, int events,
  202. const std::shared_ptr<AsyncNameResolver>& rs)
  203. {
  204. return addEvents(socket, KADNSEvent(rs, command, socket, events));
  205. }
  206. #endif // ENABLE_ASYNC_DNS
  207. bool PortEventPoll::deleteEvents(sock_t socket,
  208. const PortEventPoll::KEvent& event)
  209. {
  210. std::shared_ptr<KSocketEntry> socketEntry(new KSocketEntry(socket));
  211. KSocketEntrySet::iterator i = socketEntries_.find(socketEntry);
  212. if(i == socketEntries_.end()) {
  213. A2_LOG_DEBUG(fmt("Socket %d is not found in SocketEntries.", socket));
  214. return false;
  215. } else {
  216. event.removeSelf((*i).get());
  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. }
  237. }
  238. #ifdef ENABLE_ASYNC_DNS
  239. bool PortEventPoll::deleteEvents(sock_t socket, Command* command,
  240. const std::shared_ptr<AsyncNameResolver>& rs)
  241. {
  242. return deleteEvents(socket, KADNSEvent(rs, command, socket, 0));
  243. }
  244. #endif // ENABLE_ASYNC_DNS
  245. bool PortEventPoll::deleteEvents(sock_t socket, Command* command,
  246. EventPoll::EventType events)
  247. {
  248. int portEvents = translateEvents(events);
  249. return deleteEvents(socket, KCommandEvent(command, portEvents));
  250. }
  251. #ifdef ENABLE_ASYNC_DNS
  252. bool PortEventPoll::addNameResolver
  253. (const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
  254. {
  255. std::shared_ptr<KAsyncNameResolverEntry> entry
  256. (new KAsyncNameResolverEntry(resolver, command));
  257. KAsyncNameResolverEntrySet::iterator itr = nameResolverEntries_.find(entry);
  258. if(itr == nameResolverEntries_.end()) {
  259. nameResolverEntries_.insert(entry);
  260. entry->addSocketEvents(this);
  261. return true;
  262. } else {
  263. return false;
  264. }
  265. }
  266. bool PortEventPoll::deleteNameResolver
  267. (const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
  268. {
  269. std::shared_ptr<KAsyncNameResolverEntry> entry
  270. (new KAsyncNameResolverEntry(resolver, command));
  271. KAsyncNameResolverEntrySet::iterator itr = nameResolverEntries_.find(entry);
  272. if(itr == nameResolverEntries_.end()) {
  273. return false;
  274. } else {
  275. (*itr)->removeSocketEvents(this);
  276. nameResolverEntries_.erase(itr);
  277. return true;
  278. }
  279. }
  280. #endif // ENABLE_ASYNC_DNS
  281. } // namespace aria2