summaryrefslogtreecommitdiff
path: root/cppe/src
diff options
context:
space:
mode:
authorBenoit Foucher <benoit@zeroc.com>2007-04-11 14:20:23 +0000
committerBenoit Foucher <benoit@zeroc.com>2007-04-11 14:20:23 +0000
commita5d04099cacfdbe7fffdfa0e3393c612e90ae3bd (patch)
treeffc7d33c8db9a5c2b7bbf83064b7238755da1d4e /cppe/src
parentFixed define for Linux on ARM (diff)
downloadice-a5d04099cacfdbe7fffdfa0e3393c612e90ae3bd.tar.bz2
ice-a5d04099cacfdbe7fffdfa0e3393c612e90ae3bd.tar.xz
ice-a5d04099cacfdbe7fffdfa0e3393c612e90ae3bd.zip
Added back getifaddrs for Mac OS X Use poll() instead of select() on Unix
platforms
Diffstat (limited to 'cppe/src')
-rw-r--r--cppe/src/IceE/Network.cpp74
-rw-r--r--cppe/src/IceE/Network.h4
-rw-r--r--cppe/src/IceE/Transceiver.h4
-rw-r--r--cppe/src/TcpTransport/Acceptor.cpp2
-rw-r--r--cppe/src/TcpTransport/Connector.cpp2
-rw-r--r--cppe/src/TcpTransport/Transceiver.cpp40
6 files changed, 63 insertions, 63 deletions
diff --git a/cppe/src/IceE/Network.cpp b/cppe/src/IceE/Network.cpp
index 9d0a0da363d..949f8b8b297 100644
--- a/cppe/src/IceE/Network.cpp
+++ b/cppe/src/IceE/Network.cpp
@@ -14,6 +14,8 @@
#if defined(_WIN32)
# include <winsock2.h>
+#elif defined(__APPLE__) || defined(__FreeBSD__)
+# include <ifaddrs.h>
#else
# include <sys/ioctl.h>
# include <net/if.h>
@@ -342,7 +344,7 @@ IceInternal::setBlock(SOCKET fd, bool block)
}
}
-#ifndef ICEE_USE_SELECT_FOR_TIMEOUTS
+#ifndef ICEE_USE_SELECT_OR_POLL_FOR_TIMEOUTS
void
IceInternal::setTimeout(SOCKET fd, bool recv, int timeout)
{
@@ -547,30 +549,11 @@ repeatConnect:
assert(nevents.lNetworkEvents & FD_CONNECT);
val = nevents.iErrorCode[FD_CONNECT_BIT];
#else
- repeatSelect:
-
- int ret;
- fd_set wFdSet;
- FD_ZERO(&wFdSet);
- FD_SET(fd, &wFdSet);
- //
- // Note that although we use a different mechanism for
- // WIN32, winsock notifies about connection failures
- // through the exception filedescriptors
- //
- if(timeout >= 0)
- {
- struct timeval tv;
- tv.tv_sec = timeout / 1000;
- tv.tv_usec = (timeout - tv.tv_sec * 1000) * 1000;
- ret = ::select(fd + 1, 0, &wFdSet, 0, &tv);
- }
- else
- {
-
- ret = ::select(fd + 1, 0, &wFdSet, 0, 0);
- }
-
+ repeatPoll:
+ struct pollfd pollFd[1];
+ pollFd[0].fd = fd;
+ pollFd[0].events = POLLOUT;
+ int ret = ::poll(pollFd, 1, timeout);
if(ret == 0)
{
closeSocketNoThrow(fd);
@@ -580,7 +563,7 @@ repeatConnect:
{
if(interrupted())
{
- goto repeatSelect;
+ goto repeatPoll;
}
SocketException ex(__FILE__, __LINE__);
@@ -588,12 +571,12 @@ repeatConnect:
throw ex;
}
- //
- // Strange windows bug: The following call to Sleep() is
- // necessary, otherwise no error is reported through
- // getsockopt.
- //
- //Sleep(0);
+ //
+ // Strange windows bug: The following call to Sleep() is
+ // necessary, otherwise no error is reported through
+ // getsockopt.
+ //
+ //Sleep(0);
socklen_t len = static_cast<socklen_t>(sizeof(int));
if(getsockopt(fd, SOL_SOCKET, SO_ERROR, reinterpret_cast<char*>(&val), &len) == SOCKET_ERROR)
{
@@ -944,6 +927,31 @@ IceInternal::getLocalHosts()
{
result.push_back(inetAddrToString(addrs[i].sin_addr));
}
+#elif defined(__APPLE__) || defined(__FreeBSD__)
+ struct ifaddrs* ifap;
+ if(::getifaddrs(&ifap) == SOCKET_ERROR)
+ {
+ SocketException ex(__FILE__, __LINE__);
+ ex.error = getSocketErrno();
+ throw ex;
+ }
+
+ struct ifaddrs* curr = ifap;
+ while(curr != 0)
+ {
+ if(curr->ifa_addr && curr->ifa_addr->sa_family == AF_INET)
+ {
+ struct sockaddr_in* addr = reinterpret_cast<struct sockaddr_in*>(curr->ifa_addr);
+ if(addr->sin_addr.s_addr != 0)
+ {
+ result.push_back(inetAddrToString((*addr).sin_addr));
+ }
+ }
+
+ curr = curr->ifa_next;
+ }
+
+ ::freeifaddrs(ifap);
#else
SOCKET fd = createSocket();
@@ -967,10 +975,10 @@ IceInternal::getLocalHosts()
int bufsize = numaddrs * sizeof(struct ifreq);
ifc.ifc_len = bufsize;
ifc.ifc_buf = (char*)malloc(bufsize);
-
int rs = ioctl(fd, cmd, &ifc);
if(rs == SOCKET_ERROR)
{
+ free(ifc.ifc_buf);
closeSocketNoThrow(fd);
SocketException ex(__FILE__, __LINE__);
ex.error = getSocketErrno();
diff --git a/cppe/src/IceE/Network.h b/cppe/src/IceE/Network.h
index fe7559bc8d7..d8c1233648c 100644
--- a/cppe/src/IceE/Network.h
+++ b/cppe/src/IceE/Network.h
@@ -27,7 +27,7 @@ typedef int ssize_t;
# if defined(__hpux)
# include <sys/time.h>
# else
-# include <sys/select.h>
+# include <sys/poll.h>
# endif
# include <netinet/in.h>
@@ -86,7 +86,7 @@ void shutdownSocketWrite(SOCKET);
void shutdownSocketReadWrite(SOCKET);
void setBlock(SOCKET, bool);
-#ifndef ICEE_USE_SELECT_FOR_TIMEOUTS
+#ifndef ICEE_USE_SELECT_OR_POLL_FOR_TIMEOUTS
void setTimeout(SOCKET, bool, int);
#endif
void setTcpNoDelay(SOCKET);
diff --git a/cppe/src/IceE/Transceiver.h b/cppe/src/IceE/Transceiver.h
index 18c384a25e9..f8d13747819 100644
--- a/cppe/src/IceE/Transceiver.h
+++ b/cppe/src/IceE/Transceiver.h
@@ -63,7 +63,7 @@ private:
friend class Connector;
friend class Acceptor;
-#ifdef ICEE_USE_SELECT_FOR_TIMEOUTS
+#ifdef ICEE_USE_SELECT_OR_POLL_FOR_TIMEOUTS
void doSelect(bool, int);
#endif
@@ -74,7 +74,7 @@ private:
int _readTimeout;
int _writeTimeout;
-#ifdef ICEE_USE_SELECT_FOR_TIMEOUTS
+#ifdef ICEE_USE_SELECT_OR_POLL_FOR_TIMEOUTS
#ifdef _WIN32
WSAEVENT _event;
WSAEVENT _readEvent;
diff --git a/cppe/src/TcpTransport/Acceptor.cpp b/cppe/src/TcpTransport/Acceptor.cpp
index c1824b2a2b4..1631789effb 100644
--- a/cppe/src/TcpTransport/Acceptor.cpp
+++ b/cppe/src/TcpTransport/Acceptor.cpp
@@ -65,7 +65,7 @@ TransceiverPtr
IceInternal::Acceptor::accept()
{
SOCKET fd = doAccept(_fd);
-#ifndef ICEE_USE_SELECT_FOR_TIMEOUTS
+#ifndef ICEE_USE_SELECT_OR_POLL_FOR_TIMEOUTS
setBlock(fd, true);
#endif
diff --git a/cppe/src/TcpTransport/Connector.cpp b/cppe/src/TcpTransport/Connector.cpp
index 3a5525bc417..c2c110f4dd3 100644
--- a/cppe/src/TcpTransport/Connector.cpp
+++ b/cppe/src/TcpTransport/Connector.cpp
@@ -33,7 +33,7 @@ Connector::connect(int timeout)
SOCKET fd = createSocket();
setBlock(fd, false);
doConnect(fd, _addr, timeout);
-#ifndef ICEE_USE_SELECT_FOR_TIMEOUTS
+#ifndef ICEE_USE_SELECT_OR_POLL_FOR_TIMEOUTS
setBlock(fd, true);
#endif
diff --git a/cppe/src/TcpTransport/Transceiver.cpp b/cppe/src/TcpTransport/Transceiver.cpp
index 16c8e07fd6c..9ee819d8057 100644
--- a/cppe/src/TcpTransport/Transceiver.cpp
+++ b/cppe/src/TcpTransport/Transceiver.cpp
@@ -26,12 +26,12 @@ void
IceInternal::Transceiver::setTimeouts(int readTimeout, int writeTimeout)
{
_readTimeout = readTimeout;
-#ifndef ICEE_USE_SELECT_FOR_TIMEOUTS
+#ifndef ICEE_USE_SELECT_OR_POLL_FOR_TIMEOUTS
setTimeout(_fd, true, _readTimeout);
#endif
_writeTimeout = writeTimeout;
-#ifndef ICEE_USE_SELECT_FOR_TIMEOUTS
+#ifndef ICEE_USE_SELECT_OR_POLL_FOR_TIMEOUTS
setTimeout(_fd, false, _writeTimeout);
#endif
}
@@ -52,7 +52,7 @@ IceInternal::Transceiver::close()
out << "closing tcp connection\n" << toString();
}
-#ifdef ICEE_USE_SELECT_FOR_TIMEOUTS
+#ifdef ICEE_USE_SELECT_OR_POLL_FOR_TIMEOUTS
#ifdef _WIN32
assert(_event != 0);
WSACloseEvent(_event);
@@ -119,7 +119,7 @@ IceInternal::Transceiver::writeWithTimeout(Buffer& buf, int timeout)
}
#endif
-#ifndef ICEE_USE_SELECT_FOR_TIMEOUTS
+#ifndef ICEE_USE_SELECT_OR_POLL_FOR_TIMEOUTS
if(timeout > 0 && timeout != _writeTimeout)
{
setTimeout(_fd, false, timeout);
@@ -154,7 +154,7 @@ IceInternal::Transceiver::writeWithTimeout(Buffer& buf, int timeout)
goto repeatSend;
}
-#ifndef ICEE_USE_SELECT_FOR_TIMEOUTS
+#ifndef ICEE_USE_SELECT_OR_POLL_FOR_TIMEOUTS
if(timedout())
{
throw TimeoutException(__FILE__, __LINE__);
@@ -194,7 +194,7 @@ IceInternal::Transceiver::writeWithTimeout(Buffer& buf, int timeout)
packetSize = static_cast<Buffer::Container::difference_type>(buf.b.end() - buf.i);
}
}
-#ifndef ICEE_USE_SELECT_FOR_TIMEOUTS
+#ifndef ICEE_USE_SELECT_OR_POLL_FOR_TIMEOUTS
}
catch(const Ice::LocalException&)
{
@@ -234,7 +234,7 @@ IceInternal::Transceiver::readWithTimeout(Buffer& buf, int timeout)
Buffer::Container::difference_type packetSize =
static_cast<Buffer::Container::difference_type>(buf.b.end() - buf.i);
-#ifndef ICEE_USE_SELECT_FOR_TIMEOUTS
+#ifndef ICEE_USE_SELECT_OR_POLL_FOR_TIMEOUTS
if(timeout > 0 && timeout != _readTimeout)
{
setTimeout(_fd, true, timeout);
@@ -279,7 +279,7 @@ IceInternal::Transceiver::readWithTimeout(Buffer& buf, int timeout)
goto repeatRead;
}
-#ifndef ICEE_USE_SELECT_FOR_TIMEOUTS
+#ifndef ICEE_USE_SELECT_OR_POLL_FOR_TIMEOUTS
if(timedout())
{
throw TimeoutException(__FILE__, __LINE__);
@@ -327,7 +327,7 @@ IceInternal::Transceiver::readWithTimeout(Buffer& buf, int timeout)
packetSize = static_cast<Buffer::Container::difference_type>(buf.b.end() - buf.i);
}
}
-#ifndef ICEE_USE_SELECT_FOR_TIMEOUTS
+#ifndef ICEE_USE_SELECT_OR_POLL_FOR_TIMEOUTS
}
catch(const Ice::LocalException&)
{
@@ -382,7 +382,7 @@ IceInternal::Transceiver::Transceiver(const InstancePtr& instance, SOCKET fd) :
, _isPeerLocal(isPeerLocal(fd))
#endif
{
-#ifdef ICEE_USE_SELECT_FOR_TIMEOUTS
+#ifdef ICEE_USE_SELECT_OR_POLL_FOR_TIMEOUTS
#ifdef _WIN32
_event = WSACreateEvent();
_readEvent = WSACreateEvent();
@@ -435,7 +435,7 @@ IceInternal::Transceiver::Transceiver(const InstancePtr& instance, SOCKET fd) :
IceInternal::Transceiver::~Transceiver()
{
assert(_fd == INVALID_SOCKET);
-#ifdef ICEE_USE_SELECT_FOR_TIMEOUTS
+#ifdef ICEE_USE_SELECT_OR_POLL_FOR_TIMEOUTS
#ifdef _WIN32
assert(_event == 0);
assert(_readEvent == 0);
@@ -444,7 +444,7 @@ IceInternal::Transceiver::~Transceiver()
#endif
}
-#ifdef ICEE_USE_SELECT_FOR_TIMEOUTS
+#ifdef ICEE_USE_SELECT_OR_POLL_FOR_TIMEOUTS
void
IceInternal::Transceiver::doSelect(bool read, int timeout)
{
@@ -574,18 +574,10 @@ IceInternal::Transceiver::doSelect(bool read, int timeout)
FD_SET(_fd, &_wFdSet);
}
- if(timeout >= 0)
- {
- struct timeval tv;
- tv.tv_sec = timeout / 1000;
- tv.tv_usec = (timeout - tv.tv_sec * 1000) * 1000;
- rs = ::select(_fd + 1, read ? &_rFdSet : 0, read ? 0 : &_wFdSet, 0, &tv);
- }
- else
- {
- rs = ::select(_fd + 1, read ? &_rFdSet : 0, read ? 0 : &_wFdSet, 0, 0);
- }
-
+ struct pollfd pollFd[1];
+ pollFd[0].fd = _fd;
+ pollFd[0].events = read ? POLLIN : POLLOUT;
+ rs = ::poll(pollFd, 1, timeout);
if(rs == SOCKET_ERROR)
{
if(interrupted())