1
0

More background work/status

This commit is contained in:
Scott E. Graves
2017-02-17 14:56:22 -06:00
parent 91dd816c8c
commit 22207bfe17
11 changed files with 112 additions and 6 deletions

View 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);
}
}