前端之家收集整理的这篇文章主要介绍了
把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",bar_tv) == -1)
ACE_ERROR_RETURN ((LM_ERROR,-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;
}