#include "asio_service.h"
#include "threads/thread.h"
#include <iostream>
{
public:
protected:
void onTimer(
bool canceled)
override
{
std::cout << "Asio timer " << (canceled ? "canceled" : "expired") << std::endl;
}
void onError(
int error,
const std::string& category,
const std::string& message)
override
{
std::cout << "Asio timer caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
};
int main(int argc, char** argv)
{
auto service = std::make_shared<AsioService>();
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
auto timer = std::make_shared<AsioTimer>(service);
timer->Setup(CppCommon::UtcTime() + CppCommon::Timespan::seconds(1));
timer->WaitSync();
timer->Setup(CppCommon::Timespan::seconds(1));
timer->WaitAsync();
CppCommon::Thread::Sleep(2000);
timer->Setup(CppCommon::Timespan::seconds(1));
timer->WaitAsync();
CppCommon::Thread::Sleep(500);
timer->Cancel();
CppCommon::Thread::Sleep(500);
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}
Timer(const std::shared_ptr< Service > &service)
Initialize timer with a given Asio service.
virtual void onTimer(bool canceled)
Handle timer notification.
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.