Chinaunix首页 | 论坛 | 博客
  • 博客访问: 329203
  • 博文数量: 49
  • 博客积分: 653
  • 博客等级: 上士
  • 技术积分: 646
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-01 22:43
文章分类

全部博文(49)

文章存档

2018年(1)

2017年(4)

2015年(1)

2014年(6)

2013年(8)

2012年(24)

2011年(5)

分类: C/C++

2012-01-31 10:40:11


  1. /*
  2.  * main.cc
  3.  *
  4.  * Created on: 2011-12-1
  5.  * Author: simondu
  6.  */

  7. #include "head.h"
  8. using namespace std;

  9. inline int min( int a, int b)
  10. {
  11.     return a>b?b:a;
  12. }
  13. class Ping_Pong : public ACE_Event_Handler
  14. {
  15. public:
  16.     Ping_Pong (char *b)
  17.     : len (min(strlen (b) + 1, BUFSIZ))
  18.     {
  19.         strncpy (this->buf, b, BUFSIZ);
  20.     }
  21.     virtual int handle_input (ACE_HANDLE handle)
  22.     {
  23.         return read (handle, this->buf, BUFSIZ);
  24.     }
  25.     virtual int handle_output (ACE_HANDLE handle)
  26.     {
  27.         return write (handle, this->buf, this->len);
  28.     }
  29.     virtual int handle_signal (int signum)
  30.     {
  31.         this->finished = 1;
  32.         return 0;
  33.     }
  34.     virtual int handle_timeout(const ACE_Time_Value &time, const void* d)
  35.     {
  36.         this->finished = 1;
  37.         return 0;
  38.     }

  39.     bool done (void)
  40.     {
  41.         return this->finished == 1;
  42.     }
  43. private:
  44.     sig_atomic_t finished;
  45.     char buf[BUFSIZ];
  46.     size_t len;
  47. };
  48. static int init_handles (ACE_HANDLE handles[])
  49. {
  50.     if (pipe (handles) == -1)
  51.         printf("error\n");
  52.     //LM_ERROR ((LOG_ERROR, "%p\n%a", "pipe", 1));

  53.     // Enable message-oriented mode instead of

  54.     // bytestream mode.
  55.     int arg = RMSGN;
  56.     if (ioctl (handles[0], I_SRDOPT, arg) == -1 || ioctl (handles[1], I_SRDOPT, arg) == -1)
  57.         return -1;
  58. }

  59. int main(int argc, char* argv[])
  60. {
  61.     printf("Starting ...\n");


  62.     ACE_HANDLE handles[2];
  63.     ACE_Reactor reactor;
  64.     init_handles (handles);
  65.     pid_t pid = fork ();
  66.     Ping_Pong callback (argv[1]);
  67.     // Register I/O-based event handler
  68.     reactor.register_handler (
  69.             handles[pid == 0],
  70.             &callback,
  71.             ACE_Event_Handler::READ_MASK | ACE_Event_Handler::WRITE_MASK);

  72.     // Register signal-based event handler
  73.     reactor.register_handler(SIGINT, &callback);
  74.     // Register timer-based event handler
  75.     // reactor.schedule_timer(&callback, 0, 10);
  76.     /* Main event loop (run in each process) */
  77.     while (callback.done () == false)
  78.         reactor.handle_events ();


  79.     printf("Ending ...\n");
  80.     return 0;
  81. }



基本上所有的ACE相关文章都是来自ACE技术论文集等电子书






阅读(1341) | 评论(0) | 转发(0) |
0

上一篇:GDB介绍

下一篇:protobuf 和TCMalloc 安装介绍

给主人留下些什么吧!~~