00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 #include <pion/tcp/timer.hpp>
00011 #include <boost/bind.hpp>
00012 
00013 
00014 namespace pion {    
00015 namespace tcp {     
00016 
00017 
00018 
00019 
00020 timer::timer(tcp::connection_ptr& conn_ptr)
00021     : m_conn_ptr(conn_ptr), m_timer(conn_ptr->get_io_service()),
00022     m_timer_active(false), m_was_cancelled(false)
00023 {
00024 }
00025 
00026 void timer::start(const boost::uint32_t seconds)
00027 {
00028     boost::mutex::scoped_lock timer_lock(m_mutex);
00029     m_timer_active = true;
00030     m_timer.expires_from_now(boost::posix_time::seconds(seconds));
00031     m_timer.async_wait(boost::bind(&timer::timer_callback,
00032         shared_from_this(), _1));
00033 }
00034 
00035 void timer::cancel(void)
00036 {
00037     boost::mutex::scoped_lock timer_lock(m_mutex);
00038     m_was_cancelled = true;
00039     if (m_timer_active)
00040         m_timer.cancel();
00041 }
00042 
00043 void timer::timer_callback(const boost::system::error_code& ec)
00044 {
00045     boost::mutex::scoped_lock timer_lock(m_mutex);
00046     m_timer_active = false;
00047     if (! m_was_cancelled)
00048         m_conn_ptr->cancel();
00049 }
00050 
00051 
00052 }   
00053 }