From ce8c65f8f9f3cf05cb6f7d686366c3227a546e4e Mon Sep 17 00:00:00 2001 From: walker0643 <> Date: Mon, 29 Jan 2018 12:57:17 -0500 Subject: [PATCH] move deletion of accepted client socket pointers to ClientListener where it belongs. previously SecureListenSocket would delete the client socket but TCPListenSocket would not. PacketStreamFilter would then attempt to delete the socket regardless of what type it was. this would cause an access violation when SecureListenSocket attempted to delete the socket again. fixes #3 --- src/lib/net/SecureListenSocket.cpp | 11 ----------- src/lib/net/SecureListenSocket.h | 6 ------ src/lib/server/ClientListener.cpp | 22 +++++++++++++++++++++- src/lib/server/ClientListener.h | 4 ++++ 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/lib/net/SecureListenSocket.cpp b/src/lib/net/SecureListenSocket.cpp index 31eafa62..1e279077 100644 --- a/src/lib/net/SecureListenSocket.cpp +++ b/src/lib/net/SecureListenSocket.cpp @@ -37,15 +37,6 @@ SecureListenSocket::SecureListenSocket( { } -SecureListenSocket::~SecureListenSocket() -{ - SecureSocketSet::iterator it; - for (it = m_secureSocketSet.begin(); it != m_secureSocketSet.end(); it++) { - delete *it; - } - m_secureSocketSet.clear(); -} - IDataSocket* SecureListenSocket::accept() { @@ -74,8 +65,6 @@ SecureListenSocket::accept() socket->secureAccept(); - m_secureSocketSet.insert(socket); - return dynamic_cast(socket); } catch (XArchNetwork&) { diff --git a/src/lib/net/SecureListenSocket.h b/src/lib/net/SecureListenSocket.h index 3e178453..2bef73df 100644 --- a/src/lib/net/SecureListenSocket.h +++ b/src/lib/net/SecureListenSocket.h @@ -28,14 +28,8 @@ class SecureListenSocket : public TCPListenSocket{ public: SecureListenSocket(IEventQueue* events, SocketMultiplexer* socketMultiplexer); - ~SecureListenSocket(); // IListenSocket overrides virtual IDataSocket* accept(); - -private: - typedef std::set SecureSocketSet; - - SecureSocketSet m_secureSocketSet; }; diff --git a/src/lib/server/ClientListener.cpp b/src/lib/server/ClientListener.cpp index c12a77ac..334e730c 100644 --- a/src/lib/server/ClientListener.cpp +++ b/src/lib/server/ClientListener.cpp @@ -96,6 +96,7 @@ ClientListener::~ClientListener() m_events->removeHandler(m_events->forIListenSocket().connecting(), m_listen); cleanupListenSocket(); + cleanupClientSockets(); delete m_socketFactory; } @@ -128,6 +129,8 @@ ClientListener::handleClientConnecting(const Event&, void*) return; } + m_clientSockets.insert(socket); + m_events->adoptHandler(m_events->forClientListener().accepted(), socket->getEventTarget(), new TMethodEventJob(this, @@ -149,7 +152,7 @@ ClientListener::handleClientAccepted(const Event&, void* vsocket) IDataSocket* socket = static_cast(vsocket); // filter socket messages, including a packetizing filter - barrier::IStream* stream = new PacketStreamFilter(m_events, socket, true); + barrier::IStream* stream = new PacketStreamFilter(m_events, socket, false); assert(m_server != NULL); // create proxy for unknown client @@ -221,7 +224,14 @@ ClientListener::handleClientDisconnected(const Event&, void* vclient) m_waitingClients.erase(i); m_events->removeHandler(m_events->forClientProxy().disconnected(), client); + + // pull out the socket before deleting the client so + // we know which socket we no longer need + IDataSocket* socket = static_cast(client->getStream()); delete client; + m_clientSockets.erase(socket); + delete socket; + break; } } @@ -232,3 +242,13 @@ ClientListener::cleanupListenSocket() { delete m_listen; } + +void +ClientListener::cleanupClientSockets() +{ + ClientSockets::iterator it; + for (it = m_clientSockets.begin(); it != m_clientSockets.end(); it++) { + delete *it; + } + m_clientSockets.clear(); +} diff --git a/src/lib/server/ClientListener.h b/src/lib/server/ClientListener.h index cd54e475..b02cbb18 100644 --- a/src/lib/server/ClientListener.h +++ b/src/lib/server/ClientListener.h @@ -31,6 +31,7 @@ class IListenSocket; class ISocketFactory; class Server; class IEventQueue; +class IDataSocket; class ClientListener { public: @@ -72,10 +73,12 @@ private: void handleClientDisconnected(const Event&, void*); void cleanupListenSocket(); + void cleanupClientSockets(); private: typedef std::set NewClients; typedef std::deque WaitingClients; + typedef std::set ClientSockets; IListenSocket* m_listen; ISocketFactory* m_socketFactory; @@ -84,4 +87,5 @@ private: Server* m_server; IEventQueue* m_events; bool m_useSecureNetwork; + ClientSockets m_clientSockets; };