设为首页 加入收藏

TOP

基于ACE的定时器模板类(二)
2014-07-19 23:03:58 来源: 作者: 【 】 浏览:134
Tags:基于 ACE 定时器 模板

 

  64 int handle_timeout (const ACE_Time_Value ¤t_time,

  65 const void *arg)

  66 {

  67 long int timerType = (long int)arg;

  68 timers[timerType] = -1;

  69 parent->handleTimeout(timerType);

  70 return 0;

  71 }

  72

  73 int startTimer(int timerType, const ACE_Time_Value &delay)

  74 {

  75 if (timerType > numTimers-1) // No such timer type

  76 return -2;

  77 if (timerType < 1)

  78 return -1;

  79 if (timers[timerType] != -1) // Timer already running

  80 return -3;

  81 timers[timerType] =

  82 ACE_Reactor::instance()->schedule_timer(this,

  83 (const void *)timerType,

  84 delay);

  85 return timers[timerType];

  86 }

  87

  88 int stopTimer(int timerType)

  89 {

  90 if (timerType > numTimers-1 || // No such timer type

  91 timerType < 1 ||

  92 timers[timerType] == -1) // Timer not already running

  93 return -1;

  94 ACE_Reactor::instance()->cancel_timer(timers[timerType]);

  95 timers[timerType] = -1;

  96 return 0;

  97 }

  98

  99 bool timerStarted (int timerType)

  100 {

  101 return (timers[timerType] != -1);

  102 }

  103

  104 void showTimers(bool showAll)

  105 {

  106 ACE_Timer_Queue *reactor_timerQ = ACE_Reactor::instance()->timer_queue();

  107 ACE_Timer_Queue_Iterator &iter = reactor_timerQ->iter();

  108

  109 if (reactor_timerQ->is_empty())

  110 {

  111 TRACE_DEBUG("No Timers in Queue\n");

  112 }

  113

  114 int total_timers=0, hndlr_timers=0;

  115

  116 TRACE_DEBUG("Timers in queue:\n");

  117 for (; !iter.isdone (); iter.next ())

  118 {

  119 ACE_Timer_Node *tn = iter.item ();

  120

  121 total_timers++;

  122 if (tn->get_type() == this)

  123 hndlr_timers++;

  124

  125 if (showAll || (tn->get_type() == this))

  126 {

  127 char str[64];

  128 ACE_Date_Time dt;

  129

  130 dt.update(tn->get_timer_value());

  131 sprintf(str,

  132 "%02ld/%02ld/%04ld %02ld:%02ld:%02ld.%03ld",

  133 dt.day(),

  134 dt.month(),

  135 dt.year(),

  136 dt.hour(),

  137 dt.minute(),

  138 dt.second(),

  139 dt.microsec() / 1000);

  140 TRACE_DEBUG("Timer Id #%d: Hndlr=0x%x, Timer/Act: %ld, Interval: %d, Expiry= %s\n",

  141 tn->get_timer_id (),

  142 tn->get_type(),

  143 (long int)tn->get_act(),

  144 tn->get_interval().sec(),

  145 str);

  146 }

  147 }

  148

  149 char str[64];

  150 ACE_Date_Time dt;

  151 dt.update(reactor_timerQ->earliest_time());

  152

  153 sprintf(str,

  154 "%02ld/%02ld/%04ld %02ld:%02ld:%02ld.%03ld",

  155 dt.day(),

  156 dt.month(),

  157 dt.year(),

  158 dt.hour(),

  159 dt.minute(),

  160 dt.second(),

  161 dt.microsec() / 1000);

  162

  163 TRACE_INFO("Total Timers= %d, Timers for Handler[ 0x%x ]= %d, Skew=%d, Earliest= %s\n",

  164 total_timers, this, hndlr_timers, reactor_timerQ->timer_skew().sec(), str);

  165

  166 return;

  167 }

  168 };

  169

  170 template

  171 class subTimerHandler : public ACE_Event_Handler

  172 {

  173 std::vector timers;

  174 T *parent;

  175 int numTimers;

  176 public:

  177 subTimerHandler(T *parent, int numTimers) : timers(numTimers, -1)

  178 {

  179 this->parent = parent;

  180 this->numTimers = numTimers;

  181 }

  182

  183 subTimerHandler()

  184 {

  185 for (unsigned int i = 0; i < timers.size(); i++)

  186 {

  187 if (timers[i] != -1)

  188 ACE_Reactor::instance()->cancel_timer(timers[i]);

  189 }

  190 }

  191

  192 int handle_timeout (const ACE_Time_Value ¤t_time,

  193 const void *arg)

  194 {

  195 long int timerType = (long int)arg;

  196 timers[timerType] = -1;

  197 parent->handleSubTimeout(timerType);

  198 return 0;

  199 }

  200

  201 int startTimer(int timerType, const ACE_Time_Value &delay)

  202 {

  203 if (timerType > numTimers-1) // No such timer type

  204 return -2;

  205 if (timerType < 1)

  206 return -1;

  207 if (timers[timerType] != -1) // Timer already running

  208 return -3;

  209 timers[timerType] =

  210 ACE_Reactor::instance()->schedule_timer(this,

  211 (const void *)timerType,

  212 delay);

  213 return timers[timerType];

  214 }

  215

  216 int stopTimer(int timerType)

  217 {

  218 if (timerType > numTimers-1 || // No such timer type

  219 timerType < 1 ||

  220 timers[timerType] == -1) // Timer not already running

  221 return -1;

  222 ACE_Reactor::instance()->cancel_timer(timers[timerType]);

  223 timers[timerType] = -1;

  224 return 0;

  225 }

  226

  227 bool timerStarted (int timerType)

  228 {

  229 return (timers[timerType] != -1);

  230 }

  231

  232 };

  233

  234 #endif /* _TimerHandler_h */

      

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++ 类模板和模板类的深入解.. 下一篇STL中map与hash_map的比较

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·About - Redis (2025-12-26 08:20:56)
·Redis: A Comprehens (2025-12-26 08:20:53)
·Redis - The Real-ti (2025-12-26 08:20:50)
·Bash 脚本教程——Li (2025-12-26 07:53:35)
·实战篇!Linux shell (2025-12-26 07:53:32)