More background work/status
This commit is contained in:
52
SiaDrive.Api/SiaConsensus.cpp
Normal file
52
SiaDrive.Api/SiaConsensus.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include "stdafx.h"
|
||||
#include "SiaApi.h"
|
||||
|
||||
using namespace Sia::Api;
|
||||
|
||||
CSiaApi::_CSiaConsensus::_CSiaConsensus(CSiaCurl& siaCurl) :
|
||||
_siaCurl(siaCurl.GetHostConfig()), // IMPORTANT: Need a copy of CSiaCurl for use in thread
|
||||
_stopEvent(::CreateEvent(nullptr, FALSE, FALSE, nullptr))
|
||||
{
|
||||
StartRefreshThread();
|
||||
}
|
||||
|
||||
CSiaApi::_CSiaConsensus::~_CSiaConsensus()
|
||||
{
|
||||
StopRefreshThread();
|
||||
::CloseHandle(_stopEvent);
|
||||
}
|
||||
|
||||
void CSiaApi::_CSiaConsensus::StartRefreshThread()
|
||||
{
|
||||
if (!_refreshThread)
|
||||
{
|
||||
_refreshThread.reset(new std::thread([this]() {
|
||||
do
|
||||
{
|
||||
json result;
|
||||
if (API_SUCCESS(SiaCurlError, _siaCurl.Get(L"/consensus", result)))
|
||||
{
|
||||
SetHeight(result["height"].get<std::uint64_t>());
|
||||
SetSynced(result["synced"].get<bool>());
|
||||
SetCurrentBlock(CA2W(result["currentblock"].get<std::string>().c_str()).m_psz);
|
||||
}
|
||||
else
|
||||
{
|
||||
SetHeight(0);
|
||||
SetSynced(false);
|
||||
SetCurrentBlock(L"");
|
||||
}
|
||||
} while (::WaitForSingleObject(_stopEvent, 2000) == WAIT_TIMEOUT);
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
void CSiaApi::_CSiaConsensus::StopRefreshThread()
|
||||
{
|
||||
if (_refreshThread)
|
||||
{
|
||||
SetEvent(_stopEvent);
|
||||
_refreshThread->join();
|
||||
_refreshThread.reset(nullptr);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user