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