From a24651db2639ca7a459aecac2cdfb0fddaf3f526 Mon Sep 17 00:00:00 2001 From: Artyom Beilis Date: Sun, 23 Aug 2009 22:09:38 +0000 Subject: [PATCH] Added timer class --- Makefile.am | 3 ++- aio_timer.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ aio_timer.h | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 aio_timer.cpp create mode 100644 aio_timer.h diff --git a/Makefile.am b/Makefile.am index 3b6944d..772ef3d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -47,7 +47,8 @@ libcppcms_la_SOURCES = \ scgi_api.cpp \ fastcgi_api.cpp \ http_api.cpp \ - atomic_counter.cpp + atomic_counter.cpp \ + aio_timer.cpp diff --git a/aio_timer.cpp b/aio_timer.cpp new file mode 100644 index 0000000..d98ff7e --- /dev/null +++ b/aio_timer.cpp @@ -0,0 +1,49 @@ +#define CPPCMS_SOURCE +#include "asio_config.h" +#include "aio_timer.h" +#include "service.h" +#include "service_impl.h" + +#include +#include + +namespace cppcms { namespace aio { + + struct timer::data { + boost::asio::deadline_timer timer; + data(boost::asio::io_service &srv) : timer(srv) {} + }; + + timer::timer(service &srv) : d(new timer::data(srv.impl().get_io_service())) {} + timer::~timer() {} + + namespace { + void adapter(boost::system::error_code const &e,timer::handler const &h) + { + h(e); + } + } + void timer::async_wait(handler const &h) + { + d->timer.async_wait(boost::bind(adapter,boost::asio::placeholders::error,h)); + } + void timer::expires_from_now(int seconds) + { + d->timer.expires_from_now(boost::posix_time::seconds(seconds)); + } + void timer::expires_from_now(int seconds,int mili) + { + d->timer.expires_from_now(boost::posix_time::seconds(seconds) + boost::posix_time::milliseconds(mili)); + } + void timer::expires_at(time_t t) + { + d->timer.expires_at(boost::posix_time::from_time_t(t)); + } + void timer::cancel() + { + d->timer.cancel(); + } + + +}} // cppcms::aio + diff --git a/aio_timer.h b/aio_timer.h new file mode 100644 index 0000000..e5a24dd --- /dev/null +++ b/aio_timer.h @@ -0,0 +1,34 @@ +#ifndef CPPCMS_AIO_TIMER_H +#define CPPCMS_AIO_TIMER_H + +#include "defs.h" +#include "noncopyable.h" +#include "callback1.h" +#include "hold_ptr.h" + +namespace cppcms { + class service; + namespace aio { + class CPPCMS_API timer : public util::noncopyable { + public: + timer(service &srv); + ~timer(); + + typedef util::callback1 handler; + // true if was error or cancelation + void async_wait(handler const &h); + void cancel(); + + void expires_from_now(int seconds); + void expires_from_now(int seconds,int milliseconds); + void expires_at(time_t at); + private: + struct data; + util::hold_ptr d; + }; + } +} + + + +#endif