2017-09-12 23:54:22 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
# include <winsock2.h>
|
|
|
|
#else
|
|
|
|
# include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
|
2017-09-12 23:33:50 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
2017-09-13 21:40:28 +00:00
|
|
|
#include "../common/tracy_lz4.hpp"
|
|
|
|
#include "../common/TracyProtocol.hpp"
|
2017-09-12 23:54:22 +00:00
|
|
|
#include "../common/TracySocket.hpp"
|
2017-09-12 23:33:50 +00:00
|
|
|
#include "../common/TracySystem.hpp"
|
2017-09-13 21:40:28 +00:00
|
|
|
#include "../common/TracyQueue.hpp"
|
2017-09-12 23:33:50 +00:00
|
|
|
#include "TracyView.hpp"
|
|
|
|
|
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
|
|
|
static View* s_instance = nullptr;
|
|
|
|
|
|
|
|
View::View( const char* addr )
|
|
|
|
: m_addr( addr )
|
|
|
|
, m_shutdown( false )
|
|
|
|
{
|
|
|
|
assert( s_instance == nullptr );
|
|
|
|
s_instance = this;
|
|
|
|
|
|
|
|
m_thread = std::thread( [this] { Worker(); } );
|
|
|
|
SetThreadName( m_thread, "Tracy View" );
|
|
|
|
}
|
|
|
|
|
|
|
|
View::~View()
|
|
|
|
{
|
|
|
|
m_shutdown.store( true, std::memory_order_relaxed );
|
|
|
|
m_thread.join();
|
2017-09-13 21:36:40 +00:00
|
|
|
|
|
|
|
assert( s_instance != nullptr );
|
|
|
|
s_instance = nullptr;
|
2017-09-12 23:33:50 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 00:08:35 +00:00
|
|
|
bool View::ShouldExit()
|
|
|
|
{
|
|
|
|
return s_instance->m_shutdown.load( std::memory_order_relaxed );
|
|
|
|
}
|
|
|
|
|
2017-09-12 23:33:50 +00:00
|
|
|
void View::Worker()
|
|
|
|
{
|
2017-09-12 23:54:22 +00:00
|
|
|
Socket sock;
|
|
|
|
|
|
|
|
timeval tv;
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 10000;
|
|
|
|
|
2017-09-12 23:33:50 +00:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if( m_shutdown.load( std::memory_order_relaxed ) ) return;
|
2017-09-12 23:54:22 +00:00
|
|
|
if( !sock.Connect( m_addr.c_str(), "8086" ) ) continue;
|
|
|
|
|
|
|
|
uint8_t lz4;
|
2017-09-13 00:08:35 +00:00
|
|
|
|
|
|
|
if( !sock.Read( &m_timeBegin, sizeof( m_timeBegin ), &tv, ShouldExit ) ) goto close;
|
|
|
|
if( !sock.Read( &lz4, sizeof( lz4 ), &tv, ShouldExit ) ) goto close;
|
2017-09-12 23:54:22 +00:00
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if( m_shutdown.load( std::memory_order_relaxed ) ) return;
|
2017-09-13 21:40:28 +00:00
|
|
|
|
|
|
|
if( lz4 )
|
|
|
|
{
|
|
|
|
char buf[TargetFrameSize];
|
|
|
|
char lz4buf[LZ4Size];
|
|
|
|
lz4sz_t lz4sz;
|
|
|
|
if( !sock.Read( &lz4sz, sizeof( lz4sz ), &tv, ShouldExit ) ) goto close;
|
|
|
|
if( !sock.Read( lz4buf, lz4sz, &tv, ShouldExit ) ) goto close;
|
|
|
|
|
|
|
|
auto sz = LZ4_decompress_safe( lz4buf, buf, lz4sz, TargetFrameSize );
|
|
|
|
assert( sz >= 0 );
|
|
|
|
|
|
|
|
const char* ptr = buf;
|
|
|
|
const char* end = buf + sz;
|
|
|
|
while( ptr < end )
|
|
|
|
{
|
|
|
|
auto ev = (QueueItem*)ptr;
|
|
|
|
Process( *ev );
|
2017-09-13 23:05:08 +00:00
|
|
|
ptr += QueueDataSize[ev->hdr.idx];
|
2017-09-13 21:40:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QueueItem hdr;
|
|
|
|
if( !sock.Read( &hdr.hdr, sizeof( QueueHeader ), &tv, ShouldExit ) ) goto close;
|
2017-09-13 23:05:08 +00:00
|
|
|
if( !sock.Read( ((char*)&hdr) + sizeof( QueueHeader ), QueueDataSize[hdr.hdr.idx] - sizeof( QueueHeader ), &tv, ShouldExit ) ) goto close;
|
2017-09-13 21:40:28 +00:00
|
|
|
Process( hdr );
|
|
|
|
}
|
2017-09-12 23:54:22 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 00:00:22 +00:00
|
|
|
close:
|
2017-09-12 23:54:22 +00:00
|
|
|
sock.Close();
|
2017-09-12 23:33:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 21:40:28 +00:00
|
|
|
void View::Process( const QueueItem& ev )
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-12 23:33:50 +00:00
|
|
|
}
|