PollEventPoll.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 "PollEventPoll.h"
  36. #include <cstring>
  37. #include <algorithm>
  38. #include <numeric>
  39. #include "Command.h"
  40. #include "LogFactory.h"
  41. #include "Logger.h"
  42. #include "a2functional.h"
  43. #include "fmt.h"
  44. #include "util.h"
  45. namespace aria2 {
  46. PollEventPoll::KSocketEntry::KSocketEntry(sock_t s)
  47. : SocketEntry<KCommandEvent, KADNSEvent>(s)
  48. {
  49. }
  50. int accumulateEvent(int events, const PollEventPoll::KEvent& event)
  51. {
  52. return events | event.getEvents();
  53. }
  54. struct pollfd PollEventPoll::KSocketEntry::getEvents()
  55. {
  56. struct pollfd pollEvent;
  57. pollEvent.fd = socket_;
  58. #ifdef ENABLE_ASYNC_DNS
  59. pollEvent.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. pollEvent.events = std::accumulate(commandEvents_.begin(),
  66. commandEvents_.end(), 0, accumulateEvent);
  67. #endif // !ENABLE_ASYNC_DNS
  68. pollEvent.revents = 0;
  69. return pollEvent;
  70. }
  71. PollEventPoll::PollEventPoll()
  72. : pollfdCapacity_(1024),
  73. pollfdNum_(0),
  74. pollfds_(make_unique<struct pollfd[]>(pollfdCapacity_))
  75. {
  76. }
  77. PollEventPoll::~PollEventPoll() = default;
  78. void PollEventPoll::poll(const struct timeval& tv)
  79. {
  80. // timeout is millisec
  81. int timeout = tv.tv_sec * 1000 + tv.tv_usec / 1000;
  82. int res;
  83. while ((res = ::poll(pollfds_.get(), pollfdNum_, timeout)) == -1 &&
  84. errno == EINTR)
  85. ;
  86. if (res > 0) {
  87. for (auto first = pollfds_.get(), last = pollfds_.get() + pollfdNum_;
  88. first != last; ++first) {
  89. if (first->revents) {
  90. auto itr = socketEntries_.find(first->fd);
  91. if (itr == std::end(socketEntries_)) {
  92. A2_LOG_DEBUG(
  93. fmt("Socket %d is not found in SocketEntries.", first->fd));
  94. }
  95. else {
  96. (*itr).second.processEvents(first->revents);
  97. }
  98. }
  99. }
  100. }
  101. else if (res == -1) {
  102. int errNum = errno;
  103. A2_LOG_INFO(fmt("poll error: %s", util::safeStrerror(errNum).c_str()));
  104. }
  105. #ifdef ENABLE_ASYNC_DNS
  106. // It turns out that we have to call ares_process_fd before ares's
  107. // own timeout and ares may create new sockets or closes socket in
  108. // their API. So we call ares_process_fd for all ares_channel and
  109. // re-register their sockets.
  110. for (auto& r : nameResolverEntries_) {
  111. auto& ent = r.second;
  112. ent.processTimeout();
  113. ent.removeSocketEvents(this);
  114. ent.addSocketEvents(this);
  115. }
  116. #endif // ENABLE_ASYNC_DNS
  117. // TODO timeout of name resolver is determined in Command(AbstractCommand,
  118. // DHTEntryPoint...Command)
  119. }
  120. int PollEventPoll::translateEvents(EventPoll::EventType events)
  121. {
  122. int newEvents = 0;
  123. if (EventPoll::EVENT_READ & events) {
  124. newEvents |= IEV_READ;
  125. }
  126. if (EventPoll::EVENT_WRITE & events) {
  127. newEvents |= IEV_WRITE;
  128. }
  129. if (EventPoll::EVENT_ERROR & events) {
  130. newEvents |= IEV_ERROR;
  131. }
  132. if (EventPoll::EVENT_HUP & events) {
  133. newEvents |= IEV_HUP;
  134. }
  135. return newEvents;
  136. }
  137. bool PollEventPoll::addEvents(sock_t socket, const PollEventPoll::KEvent& event)
  138. {
  139. auto i = socketEntries_.lower_bound(socket);
  140. if (i != std::end(socketEntries_) && (*i).first == socket) {
  141. auto& socketEntry = (*i).second;
  142. event.addSelf(&socketEntry);
  143. for (auto first = pollfds_.get(), last = pollfds_.get() + pollfdNum_;
  144. first != last; ++first) {
  145. if (first->fd == socket) {
  146. *first = socketEntry.getEvents();
  147. break;
  148. }
  149. }
  150. }
  151. else {
  152. i = socketEntries_.insert(i, std::make_pair(socket, KSocketEntry(socket)));
  153. auto& socketEntry = (*i).second;
  154. event.addSelf(&socketEntry);
  155. if (pollfdCapacity_ == pollfdNum_) {
  156. pollfdCapacity_ *= 2;
  157. auto newPollfds = make_unique<struct pollfd[]>(pollfdCapacity_);
  158. memcpy(newPollfds.get(), pollfds_.get(),
  159. pollfdNum_ * sizeof(struct pollfd));
  160. pollfds_ = std::move(newPollfds);
  161. }
  162. pollfds_[pollfdNum_] = socketEntry.getEvents();
  163. ++pollfdNum_;
  164. }
  165. return true;
  166. }
  167. bool PollEventPoll::addEvents(sock_t socket, Command* command,
  168. EventPoll::EventType events)
  169. {
  170. int pollEvents = translateEvents(events);
  171. return addEvents(socket, KCommandEvent(command, pollEvents));
  172. }
  173. #ifdef ENABLE_ASYNC_DNS
  174. bool PollEventPoll::addEvents(sock_t socket, Command* command, int events,
  175. const std::shared_ptr<AsyncNameResolver>& rs)
  176. {
  177. return addEvents(socket, KADNSEvent(rs, command, socket, events));
  178. }
  179. #endif // ENABLE_ASYNC_DNS
  180. bool PollEventPoll::deleteEvents(sock_t socket,
  181. const PollEventPoll::KEvent& event)
  182. {
  183. auto i = socketEntries_.find(socket);
  184. if (i == std::end(socketEntries_)) {
  185. A2_LOG_DEBUG(fmt("Socket %d is not found in SocketEntries.", socket));
  186. return false;
  187. }
  188. auto& socketEntry = (*i).second;
  189. event.removeSelf(&socketEntry);
  190. for (auto first = pollfds_.get(), last = pollfds_.get() + pollfdNum_;
  191. first != last; ++first) {
  192. if (first->fd == socket) {
  193. if (socketEntry.eventEmpty()) {
  194. if (pollfdNum_ >= 2) {
  195. *first = *(last - 1);
  196. }
  197. --pollfdNum_;
  198. socketEntries_.erase(i);
  199. }
  200. else {
  201. *first = socketEntry.getEvents();
  202. }
  203. break;
  204. }
  205. }
  206. return true;
  207. }
  208. #ifdef ENABLE_ASYNC_DNS
  209. bool PollEventPoll::deleteEvents(sock_t socket, Command* command,
  210. const std::shared_ptr<AsyncNameResolver>& rs)
  211. {
  212. return deleteEvents(socket, KADNSEvent(rs, command, socket, 0));
  213. }
  214. #endif // ENABLE_ASYNC_DNS
  215. bool PollEventPoll::deleteEvents(sock_t socket, Command* command,
  216. EventPoll::EventType events)
  217. {
  218. int pollEvents = translateEvents(events);
  219. return deleteEvents(socket, KCommandEvent(command, pollEvents));
  220. }
  221. #ifdef ENABLE_ASYNC_DNS
  222. bool PollEventPoll::addNameResolver(
  223. const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
  224. {
  225. auto key = std::make_pair(resolver.get(), command);
  226. auto itr = nameResolverEntries_.lower_bound(key);
  227. if (itr != std::end(nameResolverEntries_) && (*itr).first == key) {
  228. return false;
  229. }
  230. itr = nameResolverEntries_.insert(
  231. itr, std::make_pair(key, KAsyncNameResolverEntry(resolver, command)));
  232. (*itr).second.addSocketEvents(this);
  233. return true;
  234. }
  235. bool PollEventPoll::deleteNameResolver(
  236. const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
  237. {
  238. auto key = std::make_pair(resolver.get(), command);
  239. auto itr = nameResolverEntries_.find(key);
  240. if (itr == std::end(nameResolverEntries_)) {
  241. return false;
  242. }
  243. (*itr).second.removeSocketEvents(this);
  244. nameResolverEntries_.erase(itr);
  245. return true;
  246. }
  247. #endif // ENABLE_ASYNC_DNS
  248. } // namespace aria2