summaryrefslogtreecommitdiff
path: root/src/SocketCore.cc
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2009-01-15 15:23:16 +0000
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2009-01-15 15:23:16 +0000
commit1ab8ade5d1a6cbebcd574c48cfd9150058c5dc75 (patch)
tree96e57d1de14db540ab92d51d0c3884ba5da54a07 /src/SocketCore.cc
parent5fbd9977842150c05a4e53ddcecb602f11a1d673 (diff)
2009-01-16 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Added --event-poll option to select the method for polling events. The available methods are "select" and "epoll". "epoll" is available on Linux only. The earlier release has the compile option to choose from these methods. * configure.ac * src/RequestGroupMan.cc * src/OptionHandlerFactory.cc * src/EpollEventPoll.h * src/StreamFileAllocationEntry.cc * src/a2io.h * src/SelectEventPoll.h * src/option_processing.cc * src/prefs.h * src/EpollEventPoll.cc * src/EventPoll.h * src/SocketCore.h * src/Makefile.am * src/main.cc * src/DownloadEngine.h * src/SelectEventPoll.cc * src/DownloadEngine.cc * src/SocketCore.cc * src/DownloadEngineFactory.cc * src/Makefile.in * src/prefs.cc * src/usage_text.h
Diffstat (limited to 'src/SocketCore.cc')
-rw-r--r--src/SocketCore.cc168
1 files changed, 92 insertions, 76 deletions
diff --git a/src/SocketCore.cc b/src/SocketCore.cc
index 9224c82a..29361ec8 100644
--- a/src/SocketCore.cc
+++ b/src/SocketCore.cc
@@ -76,6 +76,12 @@
namespace aria2 {
+#ifdef HAVE_EPOLL
+SocketCore::PollMethod SocketCore::_pollMethod = SocketCore::POLL_METHOD_EPOLL;
+#else // !HAVE_EPOLL
+SocketCore::PollMethod SocketCore::_pollMethod = SocketCore::POLL_METHOD_SELECT;
+#endif // !HAVE_EPOLL
+
#ifdef ENABLE_SSL
SharedHandle<TLSContext> SocketCore::_tlsContext;
@@ -364,50 +370,48 @@ void SocketCore::initEPOLL()
bool SocketCore::isWritable(time_t timeout)
{
-
#ifdef HAVE_EPOLL
-
- if(_epfd == -1) {
- initEPOLL();
- }
- struct epoll_event epEvents[1];
- int r;
- while((r = epoll_wait(_epfd, epEvents, 1, 0)) == -1 && errno == EINTR);
-
- if(r > 0) {
- return epEvents[0].events&(EPOLLOUT|EPOLLHUP|EPOLLERR);
- } else if(r == 0) {
- return false;
- } else {
- throw DlRetryEx(StringFormat(EX_SOCKET_CHECK_WRITABLE, errorMsg()).str());
- }
-
-#else // !HAVE_EPOLL
-
- fd_set fds;
- FD_ZERO(&fds);
- FD_SET(sockfd, &fds);
-
- struct timeval tv;
- tv.tv_sec = timeout;
- tv.tv_usec = 0;
-
- int r = select(sockfd+1, NULL, &fds, NULL, &tv);
- if(r == 1) {
- return true;
- } else if(r == 0) {
- // time out
- return false;
- } else {
- if(SOCKET_ERRNO == A2_EINPROGRESS || SOCKET_ERRNO == EINTR) {
+ if(_pollMethod == SocketCore::POLL_METHOD_EPOLL) {
+ if(_epfd == -1) {
+ initEPOLL();
+ }
+ struct epoll_event epEvents[1];
+ int r;
+ while((r = epoll_wait(_epfd, epEvents, 1, 0)) == -1 && errno == EINTR);
+ if(r > 0) {
+ return epEvents[0].events&(EPOLLOUT|EPOLLHUP|EPOLLERR);
+ } else if(r == 0) {
return false;
} else {
throw DlRetryEx(StringFormat(EX_SOCKET_CHECK_WRITABLE, errorMsg()).str());
}
- }
-
-#endif // !HAVE_EPOLL
-
+ } else
+#endif // HAVE_EPOLL
+ if(_pollMethod == SocketCore::POLL_METHOD_SELECT) {
+ fd_set fds;
+ FD_ZERO(&fds);
+ FD_SET(sockfd, &fds);
+
+ struct timeval tv;
+ tv.tv_sec = timeout;
+ tv.tv_usec = 0;
+
+ int r = select(sockfd+1, NULL, &fds, NULL, &tv);
+ if(r == 1) {
+ return true;
+ } else if(r == 0) {
+ // time out
+ return false;
+ } else {
+ if(SOCKET_ERRNO == A2_EINPROGRESS || SOCKET_ERRNO == EINTR) {
+ return false;
+ } else {
+ throw DlRetryEx(StringFormat(EX_SOCKET_CHECK_WRITABLE, errorMsg()).str());
+ }
+ }
+ } else {
+ abort();
+ }
}
bool SocketCore::isReadable(time_t timeout)
@@ -419,48 +423,48 @@ bool SocketCore::isReadable(time_t timeout)
#endif // HAVE_LIBGNUTLS
#ifdef HAVE_EPOLL
+ if(_pollMethod == SocketCore::POLL_METHOD_EPOLL) {
+ if(_epfd == -1) {
+ initEPOLL();
+ }
+ struct epoll_event epEvents[1];
+ int r;
+ while((r = epoll_wait(_epfd, epEvents, 1, 0)) == -1 && errno == EINTR);
- if(_epfd == -1) {
- initEPOLL();
- }
- struct epoll_event epEvents[1];
- int r;
- while((r = epoll_wait(_epfd, epEvents, 1, 0)) == -1 && errno == EINTR);
-
- if(r > 0) {
- return epEvents[0].events&(EPOLLIN|EPOLLHUP|EPOLLERR);
- } else if(r == 0) {
- return false;
- } else {
- throw DlRetryEx(StringFormat(EX_SOCKET_CHECK_READABLE, errorMsg()).str());
- }
-
-#else // !HAVE_EPOLL
-
- fd_set fds;
- FD_ZERO(&fds);
- FD_SET(sockfd, &fds);
-
- struct timeval tv;
- tv.tv_sec = timeout;
- tv.tv_usec = 0;
-
- int r = select(sockfd+1, &fds, NULL, NULL, &tv);
- if(r == 1) {
- return true;
- } else if(r == 0) {
- // time out
- return false;
- } else {
- if(SOCKET_ERRNO == A2_EINPROGRESS || SOCKET_ERRNO == EINTR) {
+ if(r > 0) {
+ return epEvents[0].events&(EPOLLIN|EPOLLHUP|EPOLLERR);
+ } else if(r == 0) {
return false;
} else {
throw DlRetryEx(StringFormat(EX_SOCKET_CHECK_READABLE, errorMsg()).str());
}
- }
-
-#endif // !HAVE_EPOLL
-
+ } else
+#endif // HAVE_EPOLL
+ if(_pollMethod == SocketCore::POLL_METHOD_SELECT) {
+ fd_set fds;
+ FD_ZERO(&fds);
+ FD_SET(sockfd, &fds);
+
+ struct timeval tv;
+ tv.tv_sec = timeout;
+ tv.tv_usec = 0;
+
+ int r = select(sockfd+1, &fds, NULL, NULL, &tv);
+ if(r == 1) {
+ return true;
+ } else if(r == 0) {
+ // time out
+ return false;
+ } else {
+ if(SOCKET_ERRNO == A2_EINPROGRESS || SOCKET_ERRNO == EINTR) {
+ return false;
+ } else {
+ throw DlRetryEx(StringFormat(EX_SOCKET_CHECK_READABLE, errorMsg()).str());
+ }
+ }
+ } else {
+ abort();
+ }
}
#ifdef HAVE_LIBSSL
@@ -1053,4 +1057,16 @@ bool SocketCore::wantWrite() const
return _wantWrite;
}
+#ifdef HAVE_EPOLL
+void SocketCore::useEpoll()
+{
+ _pollMethod = SocketCore::POLL_METHOD_EPOLL;
+}
+
+void SocketCore::useSelect()
+{
+ _pollMethod = SocketCore::POLL_METHOD_SELECT;
+}
+#endif // HAVE_EPOLL
+
} // namespace aria2