1
0
This repository has been archived on 2025-07-27. You can view files and clone it, but cannot push or open issues or pull requests.
Files
siadrive/SiaDrive.Api/SiaConsensus.cpp
2017-02-17 18:58:06 -06:00

52 lines
1.1 KiB
C++

#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 (ApiSuccess(_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);
}
}