Handle client disappearance broadcasts.

This commit is contained in:
Bartosz Taudul 2020-09-20 22:40:38 +02:00
parent 0a5de933c4
commit 1a91acc661

View File

@ -524,30 +524,37 @@ static void DrawContents()
const auto ipNumerical = addr.GetNumber(); const auto ipNumerical = addr.GetNumber();
const auto clientId = uint64_t( ipNumerical ) | ( uint64_t( listenPort ) << 32 ); const auto clientId = uint64_t( ipNumerical ) | ( uint64_t( listenPort ) << 32 );
auto it = clients.find( clientId ); auto it = clients.find( clientId );
if( it == clients.end() ) if( activeTime >= 0 )
{ {
std::string ip( address ); if( it == clients.end() )
resolvLock.lock();
if( resolvMap.find( ip ) == resolvMap.end() )
{ {
resolvMap.emplace( ip, ip ); std::string ip( address );
resolv.Query( ipNumerical, [ip] ( std::string&& name ) { resolvLock.lock();
std::lock_guard<std::mutex> lock( resolvLock ); if( resolvMap.find( ip ) == resolvMap.end() )
auto it = resolvMap.find( ip ); {
assert( it != resolvMap.end() ); resolvMap.emplace( ip, ip );
std::swap( it->second, name ); resolv.Query( ipNumerical, [ip] ( std::string&& name ) {
} ); std::lock_guard<std::mutex> lock( resolvLock );
auto it = resolvMap.find( ip );
assert( it != resolvMap.end() );
std::swap( it->second, name );
} );
}
resolvLock.unlock();
clients.emplace( clientId, ClientData { time, protoVer, activeTime, listenPort, procname, std::move( ip ) } );
}
else
{
it->second.time = time;
it->second.activeTime = activeTime;
it->second.port = listenPort;
if( it->second.protocolVersion != protoVer ) it->second.protocolVersion = protoVer;
if( strcmp( it->second.procName.c_str(), procname ) != 0 ) it->second.procName = procname;
} }
resolvLock.unlock();
clients.emplace( clientId, ClientData { time, protoVer, activeTime, listenPort, procname, std::move( ip ) } );
} }
else else if( it != clients.end() )
{ {
it->second.time = time; clients.erase( it );
it->second.activeTime = activeTime;
it->second.port = listenPort;
if( it->second.protocolVersion != protoVer ) it->second.protocolVersion = protoVer;
if( strcmp( it->second.procName.c_str(), procname ) != 0 ) it->second.procName = procname;
} }
} }
} }