From 2cc63e31aaa43073d4d81fcb5e271b7710cc2a42 Mon Sep 17 00:00:00 2001 From: crs Date: Thu, 30 May 2002 16:11:59 +0000 Subject: [PATCH] fixed bug in closing down a socket. --- net/CTCPSocket.cpp | 30 ++++++++++++++++++++++++++---- net/CTCPSocket.h | 1 + 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/net/CTCPSocket.cpp b/net/CTCPSocket.cpp index 189a5e3e..989a9668 100644 --- a/net/CTCPSocket.cpp +++ b/net/CTCPSocket.cpp @@ -140,13 +140,27 @@ void CTCPSocket::ioThread(void*) { try { ioService(); + ioCleanup(); + } + catch (...) { + ioCleanup(); + throw; + } +} + +void CTCPSocket::ioCleanup() +{ + try { m_input->close(); + } + catch (...) { + // ignore + } + try { m_output->close(); } catch (...) { - m_input->close(); - m_output->close(); - throw; + // ignore } } @@ -162,6 +176,9 @@ void CTCPSocket::ioService() // choose events to poll for CLock lock(m_mutex); pfds[0].events = 0; + if (m_connected == 0) { + return; + } if ((m_connected & kRead) != 0) { // still open for reading pfds[0].events |= CNetwork::kPOLLIN; @@ -174,6 +191,11 @@ void CTCPSocket::ioService() // check for status CThread::testCancel(); + if (pfds[0].events == 0) { + CThread::sleep(0.05); + CThread::testCancel(); + continue; + } const int status = CNetwork::poll(pfds, 1, 50); CThread::testCancel(); @@ -197,7 +219,7 @@ void CTCPSocket::ioService() else if (n == 0) { // stream hungup m_input->hangup(); - return; + m_connected &= ~kRead; } } diff --git a/net/CTCPSocket.h b/net/CTCPSocket.h index 70fe9770..3e7913b2 100644 --- a/net/CTCPSocket.h +++ b/net/CTCPSocket.h @@ -32,6 +32,7 @@ public: private: void init(); void ioThread(void*); + void ioCleanup(); void ioService(); void closeInput(void*); void closeOutput(void*);