基于ACE的定时器模板类
ACETimerClockGenerator.h
ClockGeneratorIF.h
在类中定义一个结构体,在结构体中定义一个函数。
在结构体中定义一个函数,这样做有什么好呢? TimerHandler.h
用了模板的方法去构造定时器类。有助于底层调用上层。在构造的时候就初始化一个类中最大的定时器个数,及模板类(也就是parent)。
TimerHandler(T *parent, int numTimers) : timers(numTimers, -)
{ //初始化向量,赋值给私有的成员变量。
this->parent = parent;
this->numTimers = numTimers;
} 用到了STL向量:
std::vector<int> timers; ACE中
. startTimer(int timerType, const ACE_Time_Value &delay)
ACE_Reactor::instance()->schedule_timer()
. stopTimer(int timerType)
ACE_Reactor::instance()->cancel_timer(timers[timerType]);
. showTimers(bool showAll)
ACE_Timer_Queue *reactor_timerQ = ACE_Reactor::instance()->timer_queue();
ACE_Timer_Queue_Iterator &iter = reactor_timerQ->iter(); #ifndef _TimerHandler_h
#define _TimerHandler_h #include <vector>
#include "ace/Timer_Queue.h"
#include "ace/Date_Time.h"
#include "ace/Event_Handler.h"
#include "ace/Reactor.h" #include "TraceUtils.h" template <class T>
class TimerHandler : public ACE_Event_Handler
{
std::vector<int> timers;
T *parent;
int numTimers;
public:
TimerHandler(T *parent, int numTimers) : timers(numTimers, -)
{
this->parent = parent;
this->numTimers = numTimers;
} ~TimerHandler()
{
for (unsigned int i = ; i < timers.size(); i++)
{
if (timers[i] != -)
ACE_Reactor::instance()->cancel_timer(timers[i]);
}
} int handle_timeout (const ACE_Time_Value ¤t_time,
const void *arg)
{
long int timerType = (long int)arg;
timers[timerType] = -;
parent->handleTimeout(timerType);
return ;
} int startTimer(int timerType, const ACE_Time_Value &delay)
{
if (timerType > numTimers-) // No such timer type
return -;
if (timerType < )
return -;
if (timers[timerType] != -) // Timer already running
return -;
timers[timerType] =
ACE_Reactor::instance()->schedule_timer(this,
(const void *)timerType,
delay);
return timers[timerType];
} int stopTimer(int timerType)
{
if (timerType > numTimers- || // No such timer type
timerType < ||
timers[timerType] == -) // Timer not already running
return -;
ACE_Reactor::instance()->cancel_timer(timers[timerType]);
timers[timerType] = -;
return ;
} bool timerStarted (int timerType)
{
return (timers[timerType] != -);
} void showTimers(bool showAll)
{
ACE_Timer_Queue *reactor_timerQ = ACE_Reactor::instance()->timer_queue();
ACE_Timer_Queue_Iterator &iter = reactor_timerQ->iter(); if (reactor_timerQ->is_empty())
{
TRACE_DEBUG("No Timers in Queue\n");
} int total_timers=, hndlr_timers=; TRACE_DEBUG("Timers in queue:\n");
for (; !iter.isdone (); iter.next ())
{
ACE_Timer_Node *tn = iter.item (); total_timers++;
if (tn->get_type() == this)
hndlr_timers++; if (showAll || (tn->get_type() == this))
{
char str[];
ACE_Date_Time dt; dt.update(tn->get_timer_value());
sprintf(str,
"%02ld/%02ld/%04ld %02ld:%02ld:%02ld.%03ld",
dt.day(),
dt.month(),
dt.year(),
dt.hour(),
dt.minute(),
dt.second(),
dt.microsec() / );
TRACE_DEBUG("Timer Id #%d: Hndlr=0x%x, Timer/Act: %ld, Interval: %d, Expiry= %s\n",
tn->get_timer_id (),
tn->get_type(),
(long int)tn->get_act(),
tn->get_interval().sec(),
str);
}
} char str[];
ACE_Date_Time dt;
dt.update(reactor_timerQ->earliest_time()); sprintf(str,
"%02ld/%02ld/%04ld %02ld:%02ld:%02ld.%03ld",
dt.day(),
dt.month(),
dt.year(),
dt.hour(),
dt.minute(),
dt.second(),
dt.microsec() / ); TRACE_INFO("Total Timers= %d, Timers for Handler[ 0x%x ]= %d, Skew=%d, Earliest= %s\n",
total_timers, this, hndlr_timers, reactor_timerQ->timer_skew().sec(), str); return;
}
}; template <class T>
class subTimerHandler : public ACE_Event_Handler
{
std::vector<int> timers;
T *parent;
int numTimers;
public:
subTimerHandler(T *parent, int numTimers) : timers(numTimers, -)
{
this->parent = parent;
this->numTimers = numTimers;
} subTimerHandler()
{
for (unsigned int i = ; i < timers.size(); i++)
{
if (timers[i] != -)
ACE_Reactor::instance()->cancel_timer(timers[i]);
}
} int handle_timeout (const ACE_Time_Value ¤t_time,
const void *arg)
{
long int timerType = (long int)arg;
timers[timerType] = -;
parent->handleSubTimeout(timerType);
return ;
} int startTimer(int timerType, const ACE_Time_Value &delay)
{
if (timerType > numTimers-) // No such timer type
return -;
if (timerType < )
return -;
if (timers[timerType] != -) // Timer already running
return -;
timers[timerType] =
ACE_Reactor::instance()->schedule_timer(this,
(const void *)timerType,
delay);
return timers[timerType];
} int stopTimer(int timerType)
{
if (timerType > numTimers- || // No such timer type
timerType < ||
timers[timerType] == -) // Timer not already running
return -;
ACE_Reactor::instance()->cancel_timer(timers[timerType]);
timers[timerType] = -;
return ;
} bool timerStarted (int timerType)
{
return (timers[timerType] != -);
} }; #endif /* _TimerHandler_h */
基于ACE的定时器模板类的更多相关文章
- 基于ace后台管理系统模板--CMS(Thinkphp框架)的筹划
临近春节,准备自己做一个关于宠物的cms网站,特写下此博客提醒自己,尽量争取在过年前做好.废号少说,先梳理下接下来准备使用的工具.. 由于最近在学习thinkphp,所以打算用这个框架来作为主体,可能 ...
- 并发编程概述 委托(delegate) 事件(event) .net core 2.0 event bus 一个简单的基于内存事件总线实现 .net core 基于NPOI 的excel导出类,支持自定义导出哪些字段 基于Ace Admin 的菜单栏实现 第五节:SignalR大杂烩(与MVC融合、全局的几个配置、跨域的应用、C/S程序充当Client和Server)
并发编程概述 前言 说实话,在我软件开发的头两年几乎不考虑并发编程,请求与响应把业务逻辑尽快完成一个星期的任务能两天完成绝不拖三天(剩下时间各种浪),根本不会考虑性能问题(能接受范围内).但随着工 ...
- c++ 跨平台线程同步对象那些事儿——基于 ace
前言 ACE (Adaptive Communication Environment) 是早年间很火的一个 c++ 开源通讯框架,当时 c++ 的库比较少,以至于谈 c++ 网络通讯就绕不开 ACE, ...
- 开涛spring3(7.2) - 对JDBC的支持 之 7.2 JDBC模板类
7.2 JDBC模板类 7.2.1 概述 Spring JDBC抽象框架core包提供了JDBC模板类,其中JdbcTemplate是core包的核心类,所以其他模板类都是基于它封装完成的,JDB ...
- C++模板类与Qt信号槽混用
一.正文 目前正在做一个视频处理相关的项目.项目的技术栈是这样的,UI层采用Qt来实现基本的数据展示和交互,底层音视频采用的是一套基于FFmpeg的视频处理框架.这是一套类似Microsoft Med ...
- (转)JDBC模板类。
Spring JDBC抽象框架core包提供了JDBC模板类,其中JdbcTemplate是core包的核心类,所以其他模板类都是基于它封装完成的,JDBC模板类是第一种工作模式. JdbcTempl ...
- spring3:对JDBC的支持 之 JDBC模板类
7.2 JDBC模板类 7.2.1 概述 Spring JDBC抽象框架core包提供了JDBC模板类,其中JdbcTemplate是core包的核心类,所以其他模板类都是基于它封装完成的,JDB ...
- QCache 缓存(模板类,类似于map,逻辑意义上的缓存,方便管理,和CPU缓存无关。自动获得被插入对象的所有权,超过一定数量就会抛弃某些值)
在软件开发中,我们经常需要在内存中存储一些临时数据用于后续相关计算.我们一般把这些数据存储到某个数组里,或者STL中的某个合适的容器中.其实,在Qt中直接为我们提供了一个QCache类专用于这种需求. ...
- C++之Stack模板类
假设有这样一种情况:某人将一车文件交给小王.倘若小王的抽屉是空的,那么小王从车上取出最上面的文件将其放入抽屉:倘若抽屉是满的,小王从抽屉中取出最上面的文件,放入垃圾篓:倘若抽屉即不空也未满,那么小王抛 ...
随机推荐
- pes and ts stream, how to convert
http://stackoverflow.com/questions/4145575/transport-stream-mpeg-file-fromat What you are probably w ...
- elasticsearch的基本用法
开始学习使用 elasticsearch, 把步骤记录在这里: 最大的特点: 1. 数据库的 database, 就是 index 2. 数据库的 table, 就是 tag 3. 不要使用bro ...
- Oracle- 用户管理
Oracle一个数据库里可以分配多个用户,用户创建自己的表,自己创建的表如果不想分配给其他用户使用,其他用户是看不到自己的创建的表的. 用户管理: 创建用户: create user chunxiao ...
- SQLite 入门教程(一)基本控制台(终端)命令 (转)
转于: SQLite 入门教程(一)基本控制台(终端)命令 一.基本简介 SQLite 是一个自持的(self-contained).无服务器的.零配置的.事务型的关系型数据库引擎.因为他很小,所 ...
- Spring Framework 5.0.0.M3中文文档 翻译记录 introduction
翻译自: http://docs.spring.io/spring/docs/5.0.0.M3/spring-framework-reference/htmlsingle/#spring.tld.ha ...
- LINQ to JavaScript
JSLINQ 是一个将LINQ对象转化为JavaScript对象的工具 .它是构建在JavaScript的数组对象的基础上进行转换的,如果您使用的是一个数组,你可以使用LINQ到javascript ...
- get-random生成电话号码
"138"+((0..9|Get-Random -count 10) -join $null) From:http://blog.csdn.net/shrekz/article/d ...
- 判断文件是否存在(exist)
set datedir=%date:~0,4%%date:~5,2%%date:~8,2%if exist d:\rollback\%datedir%\Server\ (rename d:\rollb ...
- 杂谈:你选择coco 还是unity3d?
当一个人喜欢的时候,那么这样的兴趣是非常难改变的.你是否会改变自己想法?眼下而言,如今adobe 对flash开发处于维护的状态.为什么?是由于前期错误政策流失非常多人才,这一点也非常难避免.当今年湖 ...
- CSS3:clip-path具体解释
我的一个学生,Heather Banks,想要实现他在Squarespace看到的一个效果: 依据她的以往经验,这个站点的HTML和CSS是全然在她的能力范围以内,于是我帮助她完毕了这个效果.显示na ...