Add threaded resolv service.

This commit is contained in:
Bartosz Taudul 2019-06-26 18:28:43 +02:00
parent a8cb257474
commit 913c1e57a6
4 changed files with 100 additions and 0 deletions

View File

@ -123,6 +123,7 @@
<ClCompile Include="..\..\src\imgui_impl_glfw.cpp" />
<ClCompile Include="..\..\src\imgui_impl_opengl3.cpp" />
<ClCompile Include="..\..\src\main.cpp" />
<ClCompile Include="..\..\src\ResolvService.cpp" />
<ClCompile Include="..\..\src\winmain.cpp">
<EnableEnhancedInstructionSet Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotSet</EnableEnhancedInstructionSet>
<EnableEnhancedInstructionSet Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotSet</EnableEnhancedInstructionSet>
@ -185,6 +186,7 @@
<ClInclude Include="..\..\src\imgui_freetype.h" />
<ClInclude Include="..\..\src\imgui_impl_glfw.h" />
<ClInclude Include="..\..\src\imgui_impl_opengl3.h" />
<ClInclude Include="..\..\src\ResolvService.hpp" />
<ClInclude Include="..\..\src\stb_image.h" />
</ItemGroup>
<ItemGroup>

View File

@ -99,6 +99,9 @@
<ClCompile Include="..\..\..\server\TracyPrint.cpp">
<Filter>server</Filter>
</ClCompile>
<ClCompile Include="..\..\src\ResolvService.cpp">
<Filter>src</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\common\tracy_lz4.hpp">
@ -269,6 +272,9 @@
<ClInclude Include="..\..\..\server\TracyPrint.hpp">
<Filter>server</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ResolvService.hpp">
<Filter>src</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Natvis Include="DebugVis.natvis" />

View File

@ -0,0 +1,55 @@
#ifdef _MSC_VER
# include <ws2tcpip.h>
#else
# include <arpa/inet.h>
# include <sys/socket.h>
# include <netdb.h>
#endif
#include "ResolvService.hpp"
ResolvService::ResolvService()
: m_exit( false )
, m_thread( [this] { Worker(); } )
{
}
ResolvService::~ResolvService()
{
m_exit.store( true, std::memory_order_relaxed );
m_cv.notify_one();
m_thread.join();
}
void ResolvService::Query( uint32_t ip, const std::function<void(std::string&&)>& callback )
{
std::lock_guard<std::mutex> lock( m_lock );
m_queue.emplace_back( QueueItem { ip, callback } );
m_cv.notify_one();
}
void ResolvService::Worker()
{
struct sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_port = 8086;
char buf[128];
for(;;)
{
std::unique_lock<std::mutex> lock( m_lock );
m_cv.wait( lock, [this] { return !m_queue.empty() || m_exit.load( std::memory_order_relaxed ); } );
if( m_exit.load( std::memory_order_relaxed ) ) return;
auto query = m_queue.back();
m_queue.pop_back();
lock.unlock();
addr.sin_addr.s_addr = query.ip;
if( getnameinfo( (const struct sockaddr*)&addr, sizeof( sockaddr_in ), buf, 128, nullptr, 0, NI_NOFQDN ) != 0 )
{
inet_ntop( AF_INET, &query.ip, buf, 17 );
}
query.callback( buf );
}
}

View File

@ -0,0 +1,37 @@
#ifndef __RESOLVSERVICE_HPP__
#define __RESOLVSERVICE_HPP__
#include <atomic>
#include <condition_variable>
#include <functional>
#include <mutex>
#include <stdint.h>
#include <string>
#include <thread>
#include <vector>
class ResolvService
{
struct QueueItem
{
uint32_t ip;
std::function<void(std::string&&)> callback;
};
public:
ResolvService();
~ResolvService();
void Query( uint32_t ip, const std::function<void(std::string&&)>& callback );
private:
void Worker();
std::atomic<bool> m_exit;
std::mutex m_lock;
std::condition_variable m_cv;
std::vector<QueueItem> m_queue;
std::thread m_thread;
};
#endif