Extract connection history functionality.

This commit is contained in:
Bartosz Taudul 2022-07-25 22:57:20 +02:00
parent 31b6a88923
commit a356340783
No known key found for this signature in database
GPG Key ID: B7FE2008B7575DF3
5 changed files with 134 additions and 68 deletions

View File

@ -195,6 +195,7 @@
<ClCompile Include="..\..\..\zstd\dictBuilder\divsufsort.c" /> <ClCompile Include="..\..\..\zstd\dictBuilder\divsufsort.c" />
<ClCompile Include="..\..\..\zstd\dictBuilder\fastcover.c" /> <ClCompile Include="..\..\..\zstd\dictBuilder\fastcover.c" />
<ClCompile Include="..\..\..\zstd\dictBuilder\zdict.c" /> <ClCompile Include="..\..\..\zstd\dictBuilder\zdict.c" />
<ClCompile Include="..\..\src\ConnectionHistory.cpp" />
<ClCompile Include="..\..\src\Fonts.cpp" /> <ClCompile Include="..\..\src\Fonts.cpp" />
<ClCompile Include="..\..\src\HttpRequest.cpp" /> <ClCompile Include="..\..\src\HttpRequest.cpp" />
<ClCompile Include="..\..\src\imgui\imgui_impl_glfw.cpp" /> <ClCompile Include="..\..\src\imgui\imgui_impl_glfw.cpp" />
@ -314,6 +315,7 @@
<ClInclude Include="..\..\..\zstd\zdict.h" /> <ClInclude Include="..\..\..\zstd\zdict.h" />
<ClInclude Include="..\..\..\zstd\zstd.h" /> <ClInclude Include="..\..\..\zstd\zstd.h" />
<ClInclude Include="..\..\..\zstd\zstd_errors.h" /> <ClInclude Include="..\..\..\zstd\zstd_errors.h" />
<ClInclude Include="..\..\src\ConnectionHistory.hpp" />
<ClInclude Include="..\..\src\Fonts.hpp" /> <ClInclude Include="..\..\src\Fonts.hpp" />
<ClInclude Include="..\..\src\font\DroidSans.hpp" /> <ClInclude Include="..\..\src\font\DroidSans.hpp" />
<ClInclude Include="..\..\src\font\FiraCodeRetina.hpp" /> <ClInclude Include="..\..\src\font\FiraCodeRetina.hpp" />

View File

@ -342,6 +342,9 @@
<ClCompile Include="..\..\src\WindowPosition.cpp"> <ClCompile Include="..\..\src\WindowPosition.cpp">
<Filter>src</Filter> <Filter>src</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\ConnectionHistory.cpp">
<Filter>src</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\..\server\TracyEvent.hpp"> <ClInclude Include="..\..\..\server\TracyEvent.hpp">
@ -695,6 +698,9 @@
<ClInclude Include="..\..\src\WindowPosition.hpp"> <ClInclude Include="..\..\src\WindowPosition.hpp">
<Filter>src</Filter> <Filter>src</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\ConnectionHistory.hpp">
<Filter>src</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Natvis Include="DebugVis.natvis" /> <Natvis Include="DebugVis.natvis" />

View File

@ -0,0 +1,83 @@
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include "ConnectionHistory.hpp"
#include "../../server/tracy_pdqsort.h"
#include "../../server/TracyStorage.hpp"
ConnectionHistory::ConnectionHistory()
: m_fn( tracy::GetSavePath( "connection.history" ) )
{
FILE* f = fopen( m_fn.c_str(), "rb" );
if( !f ) return;
uint64_t sz;
fread( &sz, 1, sizeof( sz ), f );
for( uint64_t i=0; i<sz; i++ )
{
uint64_t ssz, cnt;
fread( &ssz, 1, sizeof( ssz ), f );
assert( ssz < 1024 );
char tmp[1024];
fread( tmp, 1, ssz, f );
fread( &cnt, 1, sizeof( cnt ), f );
m_connHistMap.emplace( std::string( tmp, tmp+ssz ), cnt );
}
fclose( f );
Rebuild();
}
ConnectionHistory::~ConnectionHistory()
{
FILE* f = fopen( m_fn.c_str(), "wb" );
if( !f ) return;
uint64_t sz = uint64_t( m_connHistMap.size() );
fwrite( &sz, 1, sizeof( uint64_t ), f );
for( auto& v : m_connHistMap )
{
sz = uint64_t( v.first.size() );
fwrite( &sz, 1, sizeof( uint64_t ), f );
fwrite( v.first.c_str(), 1, sz, f );
fwrite( &v.second, 1, sizeof( v.second ), f );
}
fclose( f );
}
void ConnectionHistory::Rebuild()
{
std::vector<std::unordered_map<std::string, uint64_t>::const_iterator> vec;
vec.reserve( m_connHistMap.size() );
for( auto it = m_connHistMap.begin(); it != m_connHistMap.end(); ++it )
{
vec.emplace_back( it );
}
tracy::pdqsort_branchless( vec.begin(), vec.end(), []( const auto& lhs, const auto& rhs ) { return lhs->second > rhs->second; } );
std::swap( m_connHistVec, vec );
}
void ConnectionHistory::Count( const char* name )
{
std::string addr( name );
auto it = m_connHistMap.find( addr );
if( it != m_connHistMap.end() )
{
it->second++;
}
else
{
m_connHistMap.emplace( std::move( addr ), 1 );
}
Rebuild();
}
void ConnectionHistory::Erase( size_t idx )
{
assert( idx < m_connHistVec.size() );
m_connHistMap.erase( m_connHistVec[idx] );
Rebuild();
}

View File

@ -0,0 +1,32 @@
#ifndef __CONNECTIONHISTORY_HPP__
#define __CONNECTIONHISTORY_HPP__
#include <stdint.h>
#include <string>
#include <unordered_map>
#include <vector>
class ConnectionHistory
{
public:
ConnectionHistory();
~ConnectionHistory();
const std::string& Name( size_t idx ) const { return m_connHistVec[idx]->first; }
void Count( const char* name );
void Erase( size_t idx );
bool empty() const { return m_connHistVec.empty(); }
size_t size() const { return m_connHistVec.size(); }
private:
void Rebuild();
std::string m_fn;
std::unordered_map<std::string, uint64_t> m_connHistMap;
std::vector<std::unordered_map<std::string, uint64_t>::const_iterator> m_connHistVec;
};
#endif

View File

@ -50,6 +50,7 @@
#include "icon.hpp" #include "icon.hpp"
#include "Fonts.hpp" #include "Fonts.hpp"
#include "ConnectionHistory.hpp"
#include "HttpRequest.hpp" #include "HttpRequest.hpp"
#include "NativeWindow.hpp" #include "NativeWindow.hpp"
#include "ResolvService.hpp" #include "ResolvService.hpp"
@ -79,18 +80,6 @@ static void WindowRefreshCallback( GLFWwindow* window )
DrawContents(); DrawContents();
} }
std::vector<std::unordered_map<std::string, uint64_t>::const_iterator> RebuildConnectionHistory( const std::unordered_map<std::string, uint64_t>& connHistMap )
{
std::vector<std::unordered_map<std::string, uint64_t>::const_iterator> ret;
ret.reserve( connHistMap.size() );
for( auto it = connHistMap.begin(); it != connHistMap.end(); ++it )
{
ret.emplace_back( it );
}
tracy::pdqsort_branchless( ret.begin(), ret.end(), []( const auto& lhs, const auto& rhs ) { return lhs->second > rhs->second; } );
return ret;
}
struct ClientData struct ClientData
{ {
int64_t time; int64_t time;
@ -115,8 +104,7 @@ static std::mutex resolvLock;
static tracy::unordered_flat_map<std::string, std::string> resolvMap; static tracy::unordered_flat_map<std::string, std::string> resolvMap;
static ResolvService resolv( port ); static ResolvService resolv( port );
static char addr[1024] = { "127.0.0.1" }; static char addr[1024] = { "127.0.0.1" };
static std::unordered_map<std::string, uint64_t> connHistMap; static ConnectionHistory* connHist;
static std::vector<std::unordered_map<std::string, uint64_t>::const_iterator> connHistVec;
static std::atomic<ViewShutdown> viewShutdown { ViewShutdown::False }; static std::atomic<ViewShutdown> viewShutdown { ViewShutdown::False };
static double animTime = 0; static double animTime = 0;
static float dpiScale = 1.f; static float dpiScale = 1.f;
@ -200,28 +188,10 @@ int main( int argc, char** argv )
} }
WindowPosition winPos; WindowPosition winPos;
ConnectionHistory connHistory;
connHist = &connHistory;
std::string connHistFile = tracy::GetSavePath( "connection.history" );
{
FILE* f = fopen( connHistFile.c_str(), "rb" );
if( f )
{
uint64_t sz;
fread( &sz, 1, sizeof( sz ), f );
for( uint64_t i=0; i<sz; i++ )
{
uint64_t ssz, cnt;
fread( &ssz, 1, sizeof( ssz ), f );
assert( ssz < 1024 );
char tmp[1024];
fread( tmp, 1, ssz, f );
fread( &cnt, 1, sizeof( cnt ), f );
connHistMap.emplace( std::string( tmp, tmp+ssz ), cnt );
}
fclose( f );
connHistVec = RebuildConnectionHistory( connHistMap );
}
}
std::string filtersFile = tracy::GetSavePath( "client.filters" ); std::string filtersFile = tracy::GetSavePath( "client.filters" );
{ {
FILE* f = fopen( filtersFile.c_str(), "rb" ); FILE* f = fopen( filtersFile.c_str(), "rb" );
@ -390,22 +360,6 @@ int main( int argc, char** argv )
glfwTerminate(); glfwTerminate();
{
FILE* f = fopen( connHistFile.c_str(), "wb" );
if( f )
{
uint64_t sz = uint64_t( connHistMap.size() );
fwrite( &sz, 1, sizeof( uint64_t ), f );
for( auto& v : connHistMap )
{
sz = uint64_t( v.first.size() );
fwrite( &sz, 1, sizeof( uint64_t ), f );
fwrite( v.first.c_str(), 1, sz, f );
fwrite( &v.second, 1, sizeof( v.second ), f );
}
fclose( f );
}
}
{ {
FILE* f = fopen( filtersFile.c_str(), "wb" ); FILE* f = fopen( filtersFile.c_str(), "wb" );
if( f ) if( f )
@ -650,16 +604,16 @@ static void DrawContents()
ImGui::TextUnformatted( "Client address" ); ImGui::TextUnformatted( "Client address" );
bool connectClicked = false; bool connectClicked = false;
connectClicked |= ImGui::InputTextWithHint( "###connectaddress", "Enter address", addr, 1024, ImGuiInputTextFlags_EnterReturnsTrue ); connectClicked |= ImGui::InputTextWithHint( "###connectaddress", "Enter address", addr, 1024, ImGuiInputTextFlags_EnterReturnsTrue );
if( !connHistVec.empty() ) if( !connHist->empty() )
{ {
ImGui::SameLine(); ImGui::SameLine();
if( ImGui::BeginCombo( "##frameCombo", nullptr, ImGuiComboFlags_NoPreview ) ) if( ImGui::BeginCombo( "##frameCombo", nullptr, ImGuiComboFlags_NoPreview ) )
{ {
int idxRemove = -1; int idxRemove = -1;
const auto sz = std::min<size_t>( 5, connHistVec.size() ); const auto sz = std::min<size_t>( 5, connHist->size() );
for( size_t i=0; i<sz; i++ ) for( size_t i=0; i<sz; i++ )
{ {
const auto& str = connHistVec[i]->first; const auto& str = connHist->Name( i );
if( ImGui::Selectable( str.c_str() ) ) if( ImGui::Selectable( str.c_str() ) )
{ {
memcpy( addr, str.c_str(), str.size() + 1 ); memcpy( addr, str.c_str(), str.size() + 1 );
@ -671,8 +625,7 @@ static void DrawContents()
} }
if( idxRemove >= 0 ) if( idxRemove >= 0 )
{ {
connHistMap.erase( connHistVec[idxRemove] ); connHist->Erase( idxRemove );
connHistVec = RebuildConnectionHistory( connHistMap );
} }
ImGui::EndCombo(); ImGui::EndCombo();
} }
@ -680,19 +633,9 @@ static void DrawContents()
connectClicked |= ImGui::Button( ICON_FA_WIFI " Connect" ); connectClicked |= ImGui::Button( ICON_FA_WIFI " Connect" );
if( connectClicked && *addr && !loadThread.joinable() ) if( connectClicked && *addr && !loadThread.joinable() )
{ {
const auto addrLen = strlen( addr ); connHist->Count( addr );
std::string addrStr( addr, addr+addrLen );
auto it = connHistMap.find( addrStr );
if( it != connHistMap.end() )
{
it->second++;
}
else
{
connHistMap.emplace( std::move( addrStr ), 1 );
}
connHistVec = RebuildConnectionHistory( connHistMap );
const auto addrLen = strlen( addr );
auto ptr = addr + addrLen - 1; auto ptr = addr + addrLen - 1;
while( ptr > addr && *ptr != ':' ) ptr--; while( ptr > addr && *ptr != ':' ) ptr--;
if( *ptr == ':' ) if( *ptr == ':' )