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 
00012 
00013 namespace pion {    // begin namespace pion
00014 namespace net {     // begin namespace net (Pion Network Library)
00015 
00016 
00017 // TCPTimer member functions
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 }   // end namespace net
00052 }   // end namespace pion

Generated on Fri Apr 30 14:48:53 2010 for pion-net by  doxygen 1.4.7