From efc7311893b0fb25129eb2b715992ba2ac43d65c Mon Sep 17 00:00:00 2001 From: dec05eba <0xdec05eba@gmail.com> Date: Sun, 14 Oct 2018 06:05:09 +0200 Subject: Removed user from subscriptions on disconnect --- src/DirectConnection.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'src/DirectConnection.cpp') diff --git a/src/DirectConnection.cpp b/src/DirectConnection.cpp index 71c7f1f..083f557 100644 --- a/src/DirectConnection.cpp +++ b/src/DirectConnection.cpp @@ -29,7 +29,8 @@ namespace sibs DirectConnections::DirectConnections(u16 _port) : port(_port == 0 ? (u16)generateRandomNumber(2000, 32000) : _port), - alive(true) + alive(true), + removeDisconnectedPeerCallback(nullptr) { UDT::startup(); eid = UDT::epoll_create(); @@ -185,6 +186,11 @@ namespace sibs sendDataCallbackFunc(PubSubResult::OK, ""); }).detach(); } + + void DirectConnections::onRemoveDisconnectedPeer(PubSubOnRemoveDisconnectedPeerCallback callbackFunc) + { + removeDisconnectedPeerCallback = callbackFunc; + } bool DirectConnections::removePeer(int peerSocket) { @@ -193,6 +199,8 @@ namespace sibs auto peerIt = peers.find(peerSocket); if(peerIt != peers.end()) { + if(removeDisconnectedPeerCallback) + removeDisconnectedPeerCallback(peerIt->second); UDT::epoll_remove_usock(eid, peerSocket); peers.erase(peerIt); wasRemoved = true; @@ -206,10 +214,16 @@ namespace sibs peersMutex.lock(); for(std::unordered_map>::iterator it = peers.begin(); it != peers.end(); ) { - UDTSTATUS peerSocketStatus = UDT::getsockstate(it->first); + int socket = it->first; + UDTSTATUS peerSocketStatus = UDT::getsockstate(socket); if(peerSocketStatus == UDTSTATUS::BROKEN || peerSocketStatus == UDTSTATUS::CLOSING || peerSocketStatus == UDTSTATUS::CLOSED || peerSocketStatus == UDTSTATUS::NONEXIST) { - int socket = it->first; + if(removeDisconnectedPeerCallback) + removeDisconnectedPeerCallback(it->second); + + if(peerSocketStatus == UDTSTATUS::BROKEN) + UDT::epoll_remove_usock(eid, socket); + Log::debug("UDT: Connection was broken to socket %d (peer most likely disconnected), removing peer", socket); it = peers.erase(it); Log::debug("UDT: Removed peer socket %d", socket); -- cgit v1.2.3