|
最近开始学习ACE,想把其中的JAWS3部分内容抽取出来,封装成一个动态库,供MFC或者在DELPHI中都可使用。ACE中自带的JAWS3是不完整的,为了了解它的运行流程,就使用JAWS3目录下的子目录small,用vc6在其中新建一个Console类型的工程,然后把所有文件都包含进来,并把JAWS3目录下的main.cpp文件也拷贝过来,- int
- main (int argc, ACE_TCHAR *argv[])
- {
- ACE_REACTOR_INSTANCE_INIT;
- //以下两行代码是增加的
- TeraSS_Acceptor acp;
- acp.init(argc, argv);
- JAWS_Signal_Task::instance ();
- if (ACE_Service_Config::open (argc, argv) == -1
- && errno != ENOENT)
- ACE_ERROR_RETURN ((LM_ERROR,
- ACE_TEXT ("%p\n"),
- ACE_TEXT ("open")),
- 1);
- // Run forever, performing the configured services until we
- // shutdown.
- JAWS_Event_Dispatcher::run_event_loop ();
- ACE_Thread_Manager::instance ()->wait ();
- return 0;
- }
复制代码 jaws.conf配置文件中的 JAWS_IO = REACTIVE ,并在SS_State_READ.cpp中的service和transition函数打断点,
这样运行后,程序能够正确进入以上两个函数。
现在让Reactor不在主线程中运行(要应用到MFC中时,它是不可能在主线程中运行的),修改了Event_Dispatcher.cpp
文件,是ACE_Reactor::run_event_loop ()在另一个线程中启动,如下- JAWS_Event_Dispatcher_Proactor_Event_Loop (void *)
- {
- ACE_Proactor::run_event_loop ();
- return 0;
- }
- static ACE_THR_FUNC_RETURN
- JAWS_Event_Dispatcher_Reactor_Event_Loop (void *)
- {
- //ACE_REACTOR_INSTANCE_INIT;
- int result;
- result = ACE_Thread::spawn ( JAWS_Event_Dispatcher_Proactor_Event_Loop, 0, THR_BOUND);
- if (result < 0)
- {
- ACE_ERROR ((LM_ERROR, "%p\n", "ACE_Thread::spawn"));
- return 0;
- }
- // Now, enter the reactor's event loop.
- ACE_Reactor::run_event_loop ();
- // End the proactor's event loop if reactor was interrupted.
- ACE_Proactor::end_event_loop ();
-
- return 0;
- }
- void
- JAWS_Event_Dispatcher::end_event_loop (void)
- {
- ACE_Reactor::end_event_loop ();
- }
- void
- JAWS_Event_Dispatcher::run_event_loop (void)
- {
- // First, initiate the proactor thread
- int result;
- result = ACE_Thread::spawn (JAWS_Event_Dispatcher_Reactor_Event_Loop, 0, THR_BOUND);
- if (result < 0)
- {
- ACE_ERROR ((LM_ERROR, "%p\n", "ACE_Thread::spawn"));
- return;
- }
- }
复制代码 即把原来在JAWS_Event_Dispatcher::run_event_loop中执行的内容放在另一个线程中执行了。这时,再执行程序通讯时,能进service函数,但进不了transition函数。如果修改aws.conf配置文件中的 JAWS_IO 为SYNCH或ASYNCH,则
都能进入,为什么尼? Reactor框架必须在主线程中执行吗? |
|