From 89ddfdd7e7a563b7ae9cf430e9d70a53d93596a8 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Thu, 29 Mar 2012 13:15:54 +0200 Subject: [PATCH] Set FIONBIO flag for sockets Make m_FD member variable private. --- base/socket.cpp | 5 +++++ base/socket.h | 3 ++- base/tcpserver.cpp | 2 +- base/tcpsocket.cpp | 9 +++++++-- base/unix.h | 2 ++ 5 files changed, 17 insertions(+), 4 deletions(-) diff --git a/base/socket.cpp b/base/socket.cpp index a903b58d7..210ab9840 100644 --- a/base/socket.cpp +++ b/base/socket.cpp @@ -30,6 +30,11 @@ void Socket::Stop(void) void Socket::SetFD(SOCKET fd) { + unsigned long lTrue = 1; + + if (fd != INVALID_SOCKET) + ioctlsocket(fd, FIONBIO, &lTrue); + m_FD = fd; } diff --git a/base/socket.h b/base/socket.h index 3bf14ebbf..375092282 100644 --- a/base/socket.h +++ b/base/socket.h @@ -5,9 +5,10 @@ namespace icinga { class Socket : public Object { -protected: +private: SOCKET m_FD; +protected: Socket(void); void Close(bool from_dtor); diff --git a/base/tcpserver.cpp b/base/tcpserver.cpp index 9df1d49fc..778463576 100644 --- a/base/tcpserver.cpp +++ b/base/tcpserver.cpp @@ -27,7 +27,7 @@ void TCPServer::Start(void) void TCPServer::Listen(void) { - listen(m_FD, SOMAXCONN); + listen(GetFD(), SOMAXCONN); Start(); } diff --git a/base/tcpsocket.cpp b/base/tcpsocket.cpp index 96c54827a..09a85a03b 100644 --- a/base/tcpsocket.cpp +++ b/base/tcpsocket.cpp @@ -4,9 +4,14 @@ using namespace icinga; void TCPSocket::MakeSocket(void) { - assert(m_FD == INVALID_SOCKET); + assert(GetFD() == INVALID_SOCKET); - m_FD = socket(AF_INET, SOCK_STREAM, 0); + int fd = socket(AF_INET, SOCK_STREAM, 0); + + if (fd == INVALID_SOCKET) + throw exception(/*"socket() failed."*/); + + SetFD(fd); } void TCPSocket::Bind(unsigned short port) diff --git a/base/unix.h b/base/unix.h index 6bdeb892e..20f795289 100644 --- a/base/unix.h +++ b/base/unix.h @@ -22,4 +22,6 @@ inline void closesocket(int fd) close(fd); } +#define ioctlsocket ioctl + #endif /* I2_UNIX_H */ \ No newline at end of file