Browse Source

Added timer class

master
Artyom Beilis 15 years ago
parent
commit
a24651db26
3 changed files with 85 additions and 1 deletions
  1. +2
    -1
      Makefile.am
  2. +49
    -0
      aio_timer.cpp
  3. +34
    -0
      aio_timer.h

+ 2
- 1
Makefile.am View File

@@ -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





+ 49
- 0
aio_timer.cpp View File

@@ -0,0 +1,49 @@
#define CPPCMS_SOURCE
#include "asio_config.h"
#include "aio_timer.h"
#include "service.h"
#include "service_impl.h"

#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

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


+ 34
- 0
aio_timer.h View File

@@ -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<bool> 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<data> d;
};
}
}



#endif

Loading…
Cancel
Save