ana/src/asio_timers.cpp

Go to the documentation of this file.
00001 /* $Id: asio_timers.cpp 52533 2012-01-07 02:35:17Z shadowmaster $*/
00002 
00003 /**
00004  * @file
00005  * @brief Implementation details for the ana project dealing with timers.
00006  *
00007  * ana: Asynchronous Network API.
00008  * Copyright (C) 2010 - 2012 Guillermo Biset.
00009  *
00010  * This file is part of the ana project.
00011  *
00012  * System:         ana
00013  * Language:       C++
00014  *
00015  * Author:         Guillermo Biset
00016  * E-Mail:         billybiset AT gmail DOT com
00017  *
00018  * ana is free software: you can redistribute it and/or modify
00019  * it under the terms of the GNU General Public License as published by
00020  * the Free Software Foundation, either version 2 of the License, or
00021  * (at your option) any later version.
00022  *
00023  * ana is distributed in the hope that it will be useful,
00024  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00025  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00026  * GNU General Public License for more details.
00027  *
00028  * You should have received a copy of the GNU General Public License
00029  * along with ana.  If not, see <http://www.gnu.org/licenses/>.
00030  *
00031  */
00032 
00033 #include "../api/ana.hpp"
00034 #include <boost/bind.hpp>
00035 #include <boost/thread.hpp>
00036 
00037 namespace ana
00038 {
00039     namespace detail
00040     {
00041         // Get the current time.
00042         time_t_traits::time_type time_t_traits::now()
00043         {
00044             return std::time(0);
00045         }
00046 
00047         // Add a duration to a time.
00048         time_t_traits::time_type time_t_traits::add(const time_type& t, const duration_type& d)
00049         {
00050             return t + d.value;
00051         }
00052 
00053         // Subtract one time from another.
00054         time_t_traits::duration_type time_t_traits::subtract(const time_type& t1,
00055                                                              const time_type& t2)
00056         {
00057             return duration_type(t1 - t2);
00058         }
00059 
00060         // Test whether one time is less than another.
00061         bool time_t_traits::less_than(const time_t_traits::time_type& t1,
00062                                       const time_t_traits::time_type& t2)
00063         {
00064             return t1 < t2;
00065         }
00066 
00067         // Convert to POSIX duration type.
00068         boost::posix_time::time_duration time_t_traits::to_posix_duration(const duration_type& d)
00069         {
00070             return boost::posix_time::seconds(d.value);
00071         }
00072     }
00073 
00074 
00075     timer::timer() :
00076         holds_fresh_io_service_( true ),
00077         io_service_(new boost::asio::io_service() ),
00078         timer_thread_( NULL ),
00079         timer_(*io_service_)
00080     {
00081     }
00082 
00083     timer::timer( boost::asio::io_service& io ) :
00084         holds_fresh_io_service_( false ),
00085         io_service_( &io ),
00086         timer_thread_( NULL ),
00087         timer_(*io_service_)
00088     {
00089     }
00090 
00091     timer::~timer()
00092     {
00093         if ( holds_fresh_io_service_ )
00094         {
00095             io_service_->stop();
00096             timer_thread_->join();
00097             delete timer_thread_;
00098         }
00099     }
00100 
00101     void timer::run()
00102     {
00103         if ( holds_fresh_io_service_ )
00104             io_service_->run_one();
00105     }
00106 
00107     namespace detail
00108     {
00109         void timed_sender::set_timeouts(timeout_policy type, size_t ms)
00110         {
00111             timeout_milliseconds_ = ms;
00112             if (ms > 0)
00113                 timeout_type_     = type;
00114             else
00115                 timeout_type_     = NoTimeouts;
00116         }
00117 
00118         bool timed_sender::timeouts_enabled() const
00119         {
00120             return (timeout_milliseconds_ != 0) && (timeout_type_ != NoTimeouts);
00121         }
00122 
00123         timed_sender::timed_sender() :
00124             timeout_type_( NoTimeouts ),
00125             timeout_milliseconds_( 0 )
00126         {
00127         }
00128     }
00129 }
00130 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Wed May 23 2012 01:02:33 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs