把Proactor与Reactor事件集成的演示代码

//file: main.cpp
//author: StoneJiang 
  
  
   
    http://www.tao-studio.net
  
  
//date: 2008-12-14
//desc: Integrating Proactor and Reactor Events on Windows


#include "ace/Proactor.h"
#include "ace/WIN32_Proactor.h"
#include "ace/Atomic_Op.h"
#include "ace/OS_NS_unistd.h"
 
#include "ace/Reactor.h"
#include "ace/Time_Value.h"

class Timeout_Handler : public ACE_Handler, public ACE_Event_Handler
{
    // = TITLE
    //     Generic timeout handler.

public:
    Timeout_Handler (void)
    {
    }

    // This is called by the Proactor. This is declared in ACE_Handler.
    virtual void handle_time_out (const ACE_Time_Value &tv,
        const void *arg)
    {
        // Print out when timeouts occur.
        ACE_DEBUG ((LM_DEBUG, "(%t|%P) %d timeout occurred for %s @ %d./n",
            ++count_,
            (char *) arg,
            tv.sec ()));

        // Since there is only one thread that can do the timeouts in
        // Reactor, lets keep the handle_timeout short for that
        // thread.
        if (ACE_OS::strcmp ((char *) arg, "Proactor") == 0)
            // Sleep for a while
            ACE_OS::sleep (1);
    }

    // This method is declared in ACE_Event_Handler.
    virtual int handle_timeout (const ACE_Time_Value &tv,
        const void *arg)
    {
        this->handle_time_out (tv, arg);
        return 0;
    }

private:
    ACE_Atomic_Op 
 
 
  
  int> count_;
};



  
  int
ACE_TMAIN (
  
  int, ACE_TCHAR *[])
{
    ACE_DEBUG ((LM_DEBUG, "
  
  (%t|%P) work starup/n"));
    ACE_Proactor::close_singleton (); 

    ACE_WIN32_Proactor *impl = 
  
  new ACE_WIN32_Proactor (0, 1); 
    ACE_Proactor::instance (
  
  new ACE_Proactor (impl, 1), 1);

     ACE_Reactor::instance ()->register_handler(impl, impl->get_handle ());

    Timeout_Handler handler;
    
  
  // Register a 2 second timer.
    ACE_Time_Value foo_tv (2);
    
  
  if (ACE_Proactor::instance()->schedule_timer (handler,
        (
  
  void *) "
  
  Proactor",
        ACE_Time_Value::zero,
        foo_tv) == -1)
        ACE_ERROR_RETURN ((LM_ERROR, "
  
  %p/n", "
  
  schedule_timer"), -1);

    
  
  // Register a 3 second timer.
    ACE_Time_Value bar_tv (3);
    
  
  if (ACE_Reactor::instance ()->schedule_timer (&handler,
        (
  
  void *) "
  
  Reactor",
        ACE_Time_Value::zero,
        bar_tv) == -1)
        ACE_ERROR_RETURN ((LM_ERROR, "
  
  %p/n", "
  
  schedule_timer"), -1);


    ACE_Reactor::instance()->run_event_loop();
    ACE_Reactor::instance ()->remove_handler (impl,
        ACE_Event_Handler::DONT_CALL);
    ACE_DEBUG ((LM_DEBUG, "
  
  (%t|%P) work complete/n"));
    
  
  return 0;
}
 
 
完成源代码下载download
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值