diff options
author | Benoit Foucher <benoit@zeroc.com> | 2007-05-29 13:25:28 +0000 |
---|---|---|
committer | Benoit Foucher <benoit@zeroc.com> | 2007-05-29 13:25:28 +0000 |
commit | e245463bb9b97d557a728316b7f4041e56c45997 (patch) | |
tree | cd7a563758f5f58646018c315f8d9dc3b1fb8932 /cppe/src | |
parent | Set SO_REUSEADDR for server sockets. Bug 2079. (diff) | |
download | ice-e245463bb9b97d557a728316b7f4041e56c45997.tar.bz2 ice-e245463bb9b97d557a728316b7f4041e56c45997.tar.xz ice-e245463bb9b97d557a728316b7f4041e56c45997.zip |
Bug 2079
Diffstat (limited to 'cppe/src')
-rw-r--r-- | cppe/src/IceE/Network.cpp | 22 | ||||
-rw-r--r-- | cppe/src/IceE/Network.h | 1 | ||||
-rw-r--r-- | cppe/src/TcpTransport/Acceptor.cpp | 15 |
3 files changed, 28 insertions, 10 deletions
diff --git a/cppe/src/IceE/Network.cpp b/cppe/src/IceE/Network.cpp index 5338648cf1a..d1b9702333a 100644 --- a/cppe/src/IceE/Network.cpp +++ b/cppe/src/IceE/Network.cpp @@ -450,19 +450,21 @@ IceInternal::getRecvBufferSize(SOCKET fd) } void -IceInternal::doBind(SOCKET fd, struct sockaddr_in& addr) +IceInternal::setReuseAddress(SOCKET fd, bool reuse) { -#ifndef _WIN32 - int flag = 1; + int flag = reuse ? 1 : 0; if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&flag, int(sizeof(int))) == SOCKET_ERROR) { - closeSocketNoThrow(fd); - SocketException ex(__FILE__, __LINE__); - ex.error = getSocketErrno(); - throw ex; + closeSocketNoThrow(fd); + SocketException ex(__FILE__, __LINE__); + ex.error = getSocketErrno(); + throw ex; } -#endif +} +void +IceInternal::doBind(SOCKET fd, struct sockaddr_in& addr) +{ if(bind(fd, reinterpret_cast<struct sockaddr*>(&addr), int(sizeof(addr))) == SOCKET_ERROR) { closeSocketNoThrow(fd); @@ -1109,11 +1111,11 @@ IceInternal::setTcpBufSize(SOCKET fd, const Ice::PropertiesPtr& properties, cons assert(fd != INVALID_SOCKET); // - // By default, on Windows we use a 128KB buffer size. On Unix + // By default, on Windows we use a 64KB buffer size. On Unix // platforms, we use the system defaults. // #ifdef _WIN32 - const int dfltBufSize = 128 * 1024; + const int dfltBufSize = 64 * 1024; #else const int dfltBufSize = 0; #endif diff --git a/cppe/src/IceE/Network.h b/cppe/src/IceE/Network.h index d7f95cb634d..2274bee007c 100644 --- a/cppe/src/IceE/Network.h +++ b/cppe/src/IceE/Network.h @@ -97,6 +97,7 @@ void setSendBufferSize(SOCKET, int); int getSendBufferSize(SOCKET); void setRecvBufferSize(SOCKET, int); int getRecvBufferSize(SOCKET); +void setReuseAddress(SOCKET, bool); void doBind(SOCKET, struct sockaddr_in&); void doListen(SOCKET, int); diff --git a/cppe/src/TcpTransport/Acceptor.cpp b/cppe/src/TcpTransport/Acceptor.cpp index 72e53c7ace9..b32928219ec 100644 --- a/cppe/src/TcpTransport/Acceptor.cpp +++ b/cppe/src/TcpTransport/Acceptor.cpp @@ -116,6 +116,21 @@ IceInternal::Acceptor::Acceptor(const InstancePtr& instance, const string& host, _fd = createSocket(); getAddress(host, port, _addr); setTcpBufSize(_fd, _instance->initializationData().properties, _logger); +#ifndef _WIN32 + // + // Enable SO_REUSEADDR on Unix platforms to allow re-using the + // socket even if it's in the TIME_WAIT state. On Windows, + // this doesn't appear to be necessary and enabling + // SO_REUSEADDR would actually not be a good thing since it + // allows a second process to bind to an address even it's + // already bound by another process. + // + // TODO: using SO_EXCLUSIVEADDRUSE on Windows would probably + // be better but it's only supported by recent Windows + // versions (XP SP2, Windows Server 2003). + // + setReuseAddress(_fd, true); +#endif if(_traceLevels->network >= 2) { Trace out(_logger, _traceLevels->networkCat); |