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

View File

@ -297,9 +297,14 @@ 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(); const auto t = std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::system_clock::now().time_since_epoch() ).count();
if( msg ) if( msg )
{ {
uint32_t protoVer; assert( len <= sizeof( tracy::BroadcastMessage ) );
memcpy( &protoVer, msg, sizeof( uint32_t ) ); tracy::BroadcastMessage bm;
auto procname = msg + sizeof( uint32_t ); memcpy( &bm, msg, len );
if( bm.broadcastVersion == tracy::BroadcastVersion )
{
const uint32_t protoVer = bm.protocolVersion;
const auto procname = bm.programName;
auto address = addr.GetText(); auto address = addr.GetText();
auto it = clients.find( addr.GetNumber() ); auto it = clients.find( addr.GetNumber() );
@ -315,6 +320,7 @@ int main( int argc, char** argv )
if( strcmp( it->second.address.c_str(), address ) != 0 ) it->second.address = address; if( strcmp( it->second.address.c_str(), address ) != 0 ) it->second.address = address;
} }
} }
}
auto it = clients.begin(); auto it = clients.begin();
while( it != clients.end() ) while( it != clients.end() )
{ {