Define broadcast message, add versioning.

This commit is contained in:
Bartosz Taudul 2019-06-18 20:26:40 +02:00
parent 0b394c3f53
commit 0e5a7263d9
3 changed files with 44 additions and 33 deletions

View File

@ -422,21 +422,18 @@ static const char* GetHostInfo()
return buf;
}
static const char* GetBroadcastMessage( const char* procname, int& len )
static BroadcastMessage& GetBroadcastMessage( const char* procname, size_t pnsz, int& len )
{
static char buf[1024];
char* ptr = buf;
static BroadcastMessage msg;
const uint32_t protoVer = ProtocolVersion;
memcpy( ptr, &protoVer, sizeof( protoVer ) );
ptr += sizeof( protoVer );
msg.broadcastVersion = BroadcastVersion;
msg.protocolVersion = ProtocolVersion;
const auto pnsz = strlen( procname );
memcpy( ptr, procname, pnsz );
ptr += pnsz;
memcpy( msg.programName, procname, pnsz );
memset( msg.programName + pnsz, 0, WelcomeMessageProgramNameSize - pnsz );
len = ptr - buf;
return buf;
len = offsetof( BroadcastMessage, programName ) + pnsz + 1;
return msg;
}
#ifdef _WIN32
@ -1108,9 +1105,6 @@ void Profiler::Worker()
}
}
const char* broadcastMsg = nullptr;
int broadcastLen = 0;
uint64_t lastBroadcast = 0;
#ifndef TRACY_NO_BROADCAST
m_broadcast = (UdpBroadcast*)tracy_malloc( sizeof( UdpBroadcast ) );
new(m_broadcast) UdpBroadcast();
@ -1120,12 +1114,12 @@ void Profiler::Worker()
tracy_free( m_broadcast );
m_broadcast = nullptr;
}
else
{
broadcastMsg = GetBroadcastMessage( procname, broadcastLen );
}
#endif
int broadcastLen = 0;
auto& broadcastMsg = GetBroadcastMessage( procname, pnsz, broadcastLen );
uint64_t lastBroadcast = 0;
// Connections loop.
// Each iteration of the loop handles whole connection. Multiple iterations will only
// happen in the on-demand mode or when handshake fails.
@ -1153,7 +1147,7 @@ void Profiler::Worker()
if( t - lastBroadcast > 3000000000 ) // 3s
{
lastBroadcast = t;
m_broadcast->Send( 8087, broadcastMsg, broadcastLen );
m_broadcast->Send( 8087, &broadcastMsg, broadcastLen );
}
}
}

View File

@ -10,6 +10,7 @@ namespace tracy
{
enum : uint32_t { ProtocolVersion = 8 };
enum : uint32_t { BroadcastVersion = 0 };
using lz4sz_t = uint32_t;
@ -79,6 +80,16 @@ struct OnDemandPayloadMessage
enum { OnDemandPayloadMessageSize = sizeof( OnDemandPayloadMessage ) };
struct BroadcastMessage
{
uint32_t broadcastVersion;
uint32_t protocolVersion;
char programName[WelcomeMessageProgramNameSize];
};
enum { BroadcastMessageSize = sizeof( BroadcastMessage ) };
#pragma pack()
}

View File

@ -297,22 +297,28 @@ int main( int argc, char** argv )
const auto t = std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::system_clock::now().time_since_epoch() ).count();
if( msg )
{
uint32_t protoVer;
memcpy( &protoVer, msg, sizeof( uint32_t ) );
auto procname = msg + sizeof( uint32_t );
auto address = addr.GetText();
assert( len <= sizeof( tracy::BroadcastMessage ) );
tracy::BroadcastMessage bm;
memcpy( &bm, msg, len );
auto it = clients.find( addr.GetNumber() );
if( it == clients.end() )
if( bm.broadcastVersion == tracy::BroadcastVersion )
{
clients.emplace( addr.GetNumber(), ClientData { t, protoVer, procname, address } );
}
else
{
it->second.time = t;
if( it->second.protocolVersion != protoVer ) it->second.protocolVersion = protoVer;
if( strcmp( it->second.procName.c_str(), procname ) != 0 ) it->second.procName = procname;
if( strcmp( it->second.address.c_str(), address ) != 0 ) it->second.address = address;
const uint32_t protoVer = bm.protocolVersion;
const auto procname = bm.programName;
auto address = addr.GetText();
auto it = clients.find( addr.GetNumber() );
if( it == clients.end() )
{
clients.emplace( addr.GetNumber(), ClientData { t, protoVer, procname, address } );
}
else
{
it->second.time = t;
if( it->second.protocolVersion != protoVer ) it->second.protocolVersion = protoVer;
if( strcmp( it->second.procName.c_str(), procname ) != 0 ) it->second.procName = procname;
if( strcmp( it->second.address.c_str(), address ) != 0 ) it->second.address = address;
}
}
}
auto it = clients.begin();