From dd26fd46f5dab6a263e1c7a4dbc050cfc4e20be3 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Mon, 16 Jul 2012 11:15:20 +0200 Subject: [PATCH] Keep track of whether sockets are connected. --- base/socket.cpp | 19 +++++++++++++------ base/socket.h | 1 + base/tcpclient.cpp | 9 +++------ 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/base/socket.cpp b/base/socket.cpp index fb2549935..810ce314b 100644 --- a/base/socket.cpp +++ b/base/socket.cpp @@ -25,9 +25,8 @@ using namespace icinga; * Constructor for the Socket class. */ Socket::Socket(void) -{ - m_FD = INVALID_SOCKET; -} + : m_FD(INVALID_SOCKET), m_Connected(false) +{ } /** * Destructor for the Socket class. @@ -317,8 +316,12 @@ void Socket::ReadThreadProc(void) return; } - if (FD_ISSET(fd, &readfds)) + if (FD_ISSET(fd, &readfds)) { + if (!m_Connected) + m_Connected = true; + HandleReadable(); + } if (FD_ISSET(fd, &exceptfds)) HandleException(); @@ -337,7 +340,7 @@ void Socket::WriteThreadProc(void) FD_ZERO(&writefds); - while (!WantsToWrite()) { + while (!WantsToWrite() && m_Connected) { m_WriteCV.timed_wait(lock, boost::posix_time::seconds(1)); if (GetFD() == INVALID_SOCKET) @@ -365,8 +368,12 @@ void Socket::WriteThreadProc(void) return; } - if (FD_ISSET(fd, &writefds)) + if (FD_ISSET(fd, &writefds)) { + if (!m_Connected) + m_Connected = true; + HandleWritable(); + } } } diff --git a/base/socket.h b/base/socket.h index 5b437f253..e046f5a29 100644 --- a/base/socket.h +++ b/base/socket.h @@ -70,6 +70,7 @@ protected: private: SOCKET m_FD; /**< The socket descriptor. */ + bool m_Connected; thread m_ReadThread; thread m_WriteThread; diff --git a/base/tcpclient.cpp b/base/tcpclient.cpp index fcfdb233d..ce9c89a42 100644 --- a/base/tcpclient.cpp +++ b/base/tcpclient.cpp @@ -27,12 +27,9 @@ using namespace icinga; * @param role The role of the TCP client socket. */ TcpClient::TcpClient(TcpClientRole role) -{ - m_Role = role; - - m_SendQueue = boost::make_shared(); - m_RecvQueue = boost::make_shared(); -} + : m_Role(role), m_SendQueue(boost::make_shared()), + m_RecvQueue(boost::make_shared()) +{ } /** * Retrieves the role of the socket.