pion  5.0.6
src/tcp_timer.cpp
00001 // ---------------------------------------------------------------------
00002 // pion:  a Boost C++ framework for building lightweight HTTP interfaces
00003 // ---------------------------------------------------------------------
00004 // Copyright (C) 2007-2014 Splunk Inc.  (https://github.com/splunk/pion)
00005 //
00006 // Distributed under the Boost Software License, Version 1.0.
00007 // See http://www.boost.org/LICENSE_1_0.txt
00008 //
00009 
00010 #include <pion/tcp/timer.hpp>
00011 #include <boost/bind.hpp>
00012 
00013 
00014 namespace pion {    // begin namespace pion
00015 namespace tcp {     // begin namespace tcp
00016 
00017 
00018 // timer member functions
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 }   // end namespace tcp
00053 }   // end namespace pion