120 lines
2.3 KiB
C++
120 lines
2.3 KiB
C++
#include "RuntimeServices.h"
|
|
|
|
#include "ControlServer.h"
|
|
#include "OscServer.h"
|
|
#include "RuntimeControlBridge.h"
|
|
#include "RuntimeHost.h"
|
|
|
|
#include <windows.h>
|
|
|
|
RuntimeServices::RuntimeServices() :
|
|
mControlServer(std::make_unique<ControlServer>()),
|
|
mOscServer(std::make_unique<OscServer>()),
|
|
mPollRunning(false),
|
|
mRegistryChanged(false),
|
|
mReloadRequested(false),
|
|
mPollFailed(false)
|
|
{
|
|
}
|
|
|
|
RuntimeServices::~RuntimeServices()
|
|
{
|
|
Stop();
|
|
}
|
|
|
|
bool RuntimeServices::Start(OpenGLComposite& composite, RuntimeHost& runtimeHost, std::string& error)
|
|
{
|
|
Stop();
|
|
|
|
if (!StartRuntimeControlServices(composite, runtimeHost, *mControlServer, *mOscServer, error))
|
|
{
|
|
Stop();
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void RuntimeServices::BeginPolling(RuntimeHost& runtimeHost)
|
|
{
|
|
StartPolling(runtimeHost);
|
|
}
|
|
|
|
void RuntimeServices::Stop()
|
|
{
|
|
StopPolling();
|
|
|
|
if (mOscServer)
|
|
mOscServer->Stop();
|
|
|
|
if (mControlServer)
|
|
mControlServer->Stop();
|
|
}
|
|
|
|
void RuntimeServices::BroadcastState()
|
|
{
|
|
if (mControlServer)
|
|
mControlServer->BroadcastState();
|
|
}
|
|
|
|
RuntimePollEvents RuntimeServices::ConsumePollEvents()
|
|
{
|
|
RuntimePollEvents events;
|
|
events.registryChanged = mRegistryChanged.exchange(false);
|
|
events.reloadRequested = mReloadRequested.exchange(false);
|
|
events.failed = mPollFailed.exchange(false);
|
|
|
|
if (events.failed)
|
|
{
|
|
std::lock_guard<std::mutex> lock(mPollErrorMutex);
|
|
events.error = mPollError;
|
|
}
|
|
|
|
return events;
|
|
}
|
|
|
|
void RuntimeServices::StartPolling(RuntimeHost& runtimeHost)
|
|
{
|
|
if (mPollRunning.exchange(true))
|
|
return;
|
|
|
|
mPollThread = std::thread([this, &runtimeHost]() { PollLoop(runtimeHost); });
|
|
}
|
|
|
|
void RuntimeServices::StopPolling()
|
|
{
|
|
if (!mPollRunning.exchange(false))
|
|
return;
|
|
|
|
if (mPollThread.joinable())
|
|
mPollThread.join();
|
|
}
|
|
|
|
void RuntimeServices::PollLoop(RuntimeHost& runtimeHost)
|
|
{
|
|
while (mPollRunning)
|
|
{
|
|
bool registryChanged = false;
|
|
bool reloadRequested = false;
|
|
std::string runtimeError;
|
|
if (!runtimeHost.PollFileChanges(registryChanged, reloadRequested, runtimeError))
|
|
{
|
|
{
|
|
std::lock_guard<std::mutex> lock(mPollErrorMutex);
|
|
mPollError = runtimeError;
|
|
}
|
|
mPollFailed = true;
|
|
}
|
|
else
|
|
{
|
|
if (registryChanged)
|
|
mRegistryChanged = true;
|
|
if (reloadRequested)
|
|
mReloadRequested = true;
|
|
}
|
|
|
|
for (int i = 0; i < 25 && mPollRunning; ++i)
|
|
Sleep(10);
|
|
}
|
|
}
|