pion  5.0.6
tcp_timer.cpp
1 // ---------------------------------------------------------------------
2 // pion: a Boost C++ framework for building lightweight HTTP interfaces
3 // ---------------------------------------------------------------------
4 // Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion)
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // See http://www.boost.org/LICENSE_1_0.txt
8 //
9 
10 #include <pion/tcp/timer.hpp>
11 #include <boost/bind.hpp>
12 
13 
14 namespace pion { // begin namespace pion
15 namespace tcp { // begin namespace tcp
16 
17 
18 // timer member functions
19 
20 timer::timer(tcp::connection_ptr& conn_ptr)
21  : m_conn_ptr(conn_ptr), m_timer(conn_ptr->get_io_service()),
22  m_timer_active(false), m_was_cancelled(false)
23 {
24 }
25 
26 void timer::start(const boost::uint32_t seconds)
27 {
28  boost::mutex::scoped_lock timer_lock(m_mutex);
29  m_timer_active = true;
30  m_timer.expires_from_now(boost::posix_time::seconds(seconds));
31  m_timer.async_wait(boost::bind(&timer::timer_callback,
32  shared_from_this(), _1));
33 }
34 
35 void timer::cancel(void)
36 {
37  boost::mutex::scoped_lock timer_lock(m_mutex);
38  m_was_cancelled = true;
39  if (m_timer_active)
40  m_timer.cancel();
41 }
42 
43 void timer::timer_callback(const boost::system::error_code& ec)
44 {
45  boost::mutex::scoped_lock timer_lock(m_mutex);
46  m_timer_active = false;
47  if (! m_was_cancelled)
48  m_conn_ptr->cancel();
49 }
50 
51 
52 } // end namespace tcp
53 } // end namespace pion
timer(tcp::connection_ptr &conn_ptr)
Definition: tcp_timer.cpp:20
void cancel(void)
cancel the timer (operation completed)
Definition: tcp_timer.cpp:35
void start(const boost::uint32_t seconds)
Definition: tcp_timer.cpp:26