summaryrefslogtreecommitdiff
path: root/src/SocketCore.cc
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2010-04-21 14:31:44 +0000
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2010-04-21 14:31:44 +0000
commit9cf05b71154ed8f999344856ee07b2549abc6716 (patch)
treed5617736f9aff9d3e3f09aeb96d524ddac34e14e /src/SocketCore.cc
parentc1047561b666cf3625424a4afe5225dc22be2678 (diff)
2010-04-21 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Added opensolaris port_associate() support. * src/DownloadEngineFactory.cc * src/Makefile.am * src/OptionHandlerFactory.cc * src/PortEventPoll.cc * src/PortEventPoll.h * src/SocketCore.cc * src/SocketCore.h * src/configure.ac * src/main.cc * src/prefs.cc * src/prefs.h
Diffstat (limited to 'src/SocketCore.cc')
-rw-r--r--src/SocketCore.cc196
1 files changed, 128 insertions, 68 deletions
diff --git a/src/SocketCore.cc b/src/SocketCore.cc
index 58668cae..299c3fac 100644
--- a/src/SocketCore.cc
+++ b/src/SocketCore.cc
@@ -38,6 +38,9 @@
#ifdef HAVE_IFADDRS_H
# include <ifaddrs.h>
#endif // HAVE_IFADDRS_H
+#ifdef HAVE_PORT_H
+# include <port.h>
+#endif // HAVE_PORT_H
#include <cerrno>
#include <cstring>
@@ -148,12 +151,12 @@ SocketCore::SocketCore(sock_t sockfd, int sockType):_sockType(sockType), sockfd(
void SocketCore::init()
{
-
#ifdef HAVE_EPOLL
-
_epfd = -1;
-
#endif // HAVE_EPOLL
+#ifdef HAVE_PORT_ASSOCIATE
+ _portfd = -1;
+#endif // HAVE_PORT_ASSOCIATE
blocking = true;
secure = 0;
@@ -175,15 +178,16 @@ void SocketCore::init()
SocketCore::~SocketCore() {
closeConnection();
-
#ifdef HAVE_EPOLL
-
if(_epfd != -1) {
CLOSE(_epfd);
}
-
#endif // HAVE_EPOLL
-
+#ifdef HAVE_PORT_ASSOCIATE
+ if(_portfd != -1) {
+ CLOSE(_portfd);
+ }
+#endif // HAVE_PORT_ASSOCIATE
#ifdef HAVE_LIBGNUTLS
delete [] peekBuf;
#endif // HAVE_LIBGNUTLS
@@ -587,6 +591,19 @@ void SocketCore::initEPOLL()
#endif // HAVE_EPOLL
+#ifdef HAVE_PORT_ASSOCIATE
+void SocketCore::initPort()
+{
+ if((_portfd = port_create()) == -1) {
+ throw DL_RETRY_EX(StringFormat("port_create failed:%s", errorMsg()).str());
+ }
+ if(port_associate(_portfd, PORT_SOURCE_FD, sockfd, POLLIN|POLLOUT, 0) == -1) {
+ throw DL_RETRY_EX
+ (StringFormat("port_associate failed:%s", errorMsg()).str());
+ }
+}
+#endif // HAVE_PORT_ASSOCIATE
+
bool SocketCore::isWritable(time_t timeout)
{
#ifdef HAVE_EPOLL
@@ -607,49 +624,67 @@ bool SocketCore::isWritable(time_t timeout)
}
} else
#endif // HAVE_EPOLL
-#ifdef HAVE_POLL
- if(_pollMethod == SocketCore::POLL_METHOD_POLL) {
- struct pollfd p;
- p.fd = sockfd;
- p.events = POLLOUT;
- int r;
- while((r = poll(&p, 1, timeout*1000)) == -1 && errno == EINTR);
- if(r > 0) {
- return p.revents&(POLLOUT|POLLHUP|POLLERR);
- } else if(r == 0) {
+#ifdef HAVE_PORT_ASSOCIATE
+ if(_pollMethod == SocketCore::POLL_METHOD_PORT) {
+ if(_portfd == -1) {
+ initPort();
+ }
+ struct timespec ts = { timeout, 0 };
+ port_event_t portEvent;
+ int r = port_get(_portfd, &portEvent, &ts);
+ if(r == 0) {
+ return portEvent.portev_events&(POLLOUT|POLLHUP|POLLERR);
+ } else if(r == -1 && (errno == ETIME || errno == EINTR)) {
return false;
} else {
throw DL_RETRY_EX
(StringFormat(EX_SOCKET_CHECK_WRITABLE, errorMsg()).str());
}
} else
-#endif // HAVE_POLL
- 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;
+#endif // HAVE_PORT_ASSOCIATE
+#ifdef HAVE_POLL
+ if(_pollMethod == SocketCore::POLL_METHOD_POLL) {
+ struct pollfd p;
+ p.fd = sockfd;
+ p.events = POLLOUT;
+ int r;
+ while((r = poll(&p, 1, timeout*1000)) == -1 && errno == EINTR);
+ if(r > 0) {
+ return p.revents&(POLLOUT|POLLHUP|POLLERR);
} else if(r == 0) {
- // time out
return false;
} else {
- if(SOCKET_ERRNO == A2_EINPROGRESS || SOCKET_ERRNO == A2_EINTR) {
+ throw DL_RETRY_EX
+ (StringFormat(EX_SOCKET_CHECK_WRITABLE, errorMsg()).str());
+ }
+ } else
+#endif // HAVE_POLL
+ 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 {
- throw DL_RETRY_EX
- (StringFormat(EX_SOCKET_CHECK_WRITABLE, errorMsg()).str());
+ if(SOCKET_ERRNO == A2_EINPROGRESS || SOCKET_ERRNO == A2_EINTR) {
+ return false;
+ } else {
+ throw DL_RETRY_EX
+ (StringFormat(EX_SOCKET_CHECK_WRITABLE, errorMsg()).str());
+ }
}
+ } else {
+ abort();
}
- } else {
- abort();
- }
}
bool SocketCore::isReadable(time_t timeout)
@@ -679,49 +714,67 @@ bool SocketCore::isReadable(time_t timeout)
}
} else
#endif // HAVE_EPOLL
-#ifdef HAVE_POLL
- if(_pollMethod == SocketCore::POLL_METHOD_POLL) {
- struct pollfd p;
- p.fd = sockfd;
- p.events = POLLIN;
- int r;
- while((r = poll(&p, 1, timeout*1000)) == -1 && errno == EINTR);
- if(r > 0) {
- return p.revents&(POLLIN|POLLHUP|POLLERR);
- } else if(r == 0) {
+#ifdef HAVE_PORT_ASSOCIATE
+ if(_pollMethod == SocketCore::POLL_METHOD_PORT) {
+ if(_portfd == -1) {
+ initPort();
+ }
+ struct timespec ts = { timeout, 0 };
+ port_event_t portEvent;
+ int r = port_get(_portfd, &portEvent, &ts);
+ if(r == 0) {
+ return portEvent.portev_events&(POLLIN|POLLHUP|POLLERR);
+ } else if(r == -1 && (errno == ETIME || errno == EINTR)) {
return false;
} else {
throw DL_RETRY_EX
(StringFormat(EX_SOCKET_CHECK_READABLE, errorMsg()).str());
}
- } else
-#endif // HAVE_POLL
- 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
+#endif // HAVE_PORT_ASSOCIATE
+#ifdef HAVE_POLL
+ if(_pollMethod == SocketCore::POLL_METHOD_POLL) {
+ struct pollfd p;
+ p.fd = sockfd;
+ p.events = POLLIN;
+ int r;
+ while((r = poll(&p, 1, timeout*1000)) == -1 && errno == EINTR);
+ if(r > 0) {
+ return p.revents&(POLLIN|POLLHUP|POLLERR);
} else if(r == 0) {
- // time out
return false;
} else {
- if(SOCKET_ERRNO == A2_EINPROGRESS || SOCKET_ERRNO == A2_EINTR) {
+ throw DL_RETRY_EX
+ (StringFormat(EX_SOCKET_CHECK_READABLE, errorMsg()).str());
+ }
+ } else
+#endif // HAVE_POLL
+ 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 {
- throw DL_RETRY_EX
- (StringFormat(EX_SOCKET_CHECK_READABLE, errorMsg()).str());
+ if(SOCKET_ERRNO == A2_EINPROGRESS || SOCKET_ERRNO == A2_EINTR) {
+ return false;
+ } else {
+ throw DL_RETRY_EX
+ (StringFormat(EX_SOCKET_CHECK_READABLE, errorMsg()).str());
+ }
}
+ } else {
+ abort();
}
- } else {
- abort();
- }
}
#ifdef HAVE_LIBSSL
@@ -1274,6 +1327,13 @@ void SocketCore::useEpoll()
}
#endif // HAVE_EPOLL
+#ifdef HAVE_PORT_ASSOCIATE
+void SocketCore::usePort()
+{
+ _pollMethod = SocketCore::POLL_METHOD_PORT;
+}
+#endif // HAVE_PORT_ASSOCIATE
+
#ifdef HAVE_POLL
void SocketCore::usePoll()
{