PortEventPoll.cc 9.6 KB

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