pion-net  4.0.9
TCPTimer.cpp
1 // ------------------------------------------------------------------
2 // pion-net: a C++ framework for building lightweight HTTP interfaces
3 // ------------------------------------------------------------------
4 // Copyright (C) 2007-2010 Atomic Labs, Inc. (http://www.atomiclabs.com)
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/net/TCPTimer.hpp>
11 #include <boost/bind.hpp>
12 
13 
14 namespace pion { // begin namespace pion
15 namespace net { // begin namespace net (Pion Network Library)
16 
17 
18 // TCPTimer member functions
19 
20 TCPTimer::TCPTimer(TCPConnectionPtr& conn_ptr)
21  : m_conn_ptr(conn_ptr), m_timer(conn_ptr->getIOService()),
22  m_timer_active(false), m_was_cancelled(false)
23 {
24 }
25 
26 void TCPTimer::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(&TCPTimer::timerCallback,
32  shared_from_this(), _1));
33 }
34 
35 void TCPTimer::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 TCPTimer::timerCallback(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->close();
49 }
50 
51 
52 } // end namespace net
53 } // end namespace pion
TCPTimer(TCPConnectionPtr &conn_ptr)
Definition: TCPTimer.cpp:20
void start(const boost::uint32_t seconds)
Definition: TCPTimer.cpp:26
void cancel(void)
cancel the timer (operation completed)
Definition: TCPTimer.cpp:35