pion-net  4.0.9
net/src/TCPTimer.cpp
00001 // ------------------------------------------------------------------
00002 // pion-net: a C++ framework for building lightweight HTTP interfaces
00003 // ------------------------------------------------------------------
00004 // Copyright (C) 2007-2010 Atomic Labs, Inc.  (http://www.atomiclabs.com)
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/net/TCPTimer.hpp>
00011 #include <boost/bind.hpp>
00012 
00013 
00014 namespace pion {    // begin namespace pion
00015 namespace net {     // begin namespace net (Pion Network Library)
00016 
00017 
00018 // TCPTimer member functions
00019 
00020 TCPTimer::TCPTimer(TCPConnectionPtr& conn_ptr)
00021     : m_conn_ptr(conn_ptr), m_timer(conn_ptr->getIOService()),
00022     m_timer_active(false), m_was_cancelled(false)
00023 {
00024 }
00025 
00026 void TCPTimer::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(&TCPTimer::timerCallback,
00032         shared_from_this(), _1));
00033 }
00034 
00035 void TCPTimer::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 TCPTimer::timerCallback(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->close();
00049 }
00050 
00051 
00052 }   // end namespace net
00053 }   // end namespace pion