1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| #include "AsioClock.h"
CAsioClock::CAsioClock()
:m_io_service(),
m_timer(m_io_service),
m_stop(true)
{
}
CAsioClock::~CAsioClock()
{
Reset();
}
void CAsioClock::Run()
{
if (m_stop)
{
m_io_service.reset();
m_stop = false;
}
m_timer.expires_from_now( boost::posix_time::milliseconds(static_cast<int>(m_sampling_time * 1000)) );
m_timer.async_wait(boost::bind(&CAsioClock::HandleCount,
this,
boost::asio::placeholders::error)
);
// Launch io_service
m_thread.reset(new boost::thread(boost::bind(&boost::asio::io_service::run, boost::ref(m_io_service))));
}
void CAsioClock::Stop()
{
if (!m_stop)
{
m_stop = true;
m_io_service.stop();
m_thread->join();
}
}
bool CAsioClock::IsRunning()
{
return !m_stop;
}
/*void CAsioClock::Resume()
{
m_time += m_sampling_time;
Run();
}*/
void CAsioClock::Reset()
{
Stop();
m_time = GetBeginTime();
}
void CAsioClock::Next()
{
try{
m_time += m_sampling_time;
m_signal();
}
catch(std::exception & e)
{
std::cerr<<e.what()<<std::endl;
}
}
void CAsioClock::HandleCount (const boost::system::error_code& error)
{
try{
if (!error && (m_stop == false) )
{
m_time += m_sampling_time;
if(m_time > m_end_time && m_end_time > m_begin_time)
m_stop = true;
// Execute signals
m_signal();
m_timer.expires_at(m_timer.expires_at() + boost::posix_time::millisec(static_cast<int>(m_sampling_time * 1000) ));
m_timer.async_wait(boost::bind(&CAsioClock::HandleCount,
this,
boost::asio::placeholders::error)
);
}
}
catch(std::exception & e)
{
std::cerr<<e.what()<<std::endl;
}
} |
Partager