很顯然,文章的標(biāo)題決定了我們是在linux下使用ACE。我們知道ACE在linux下缺省是用select來實現(xiàn)Reactor的,epoll相對于select的好處這里就不再啰嗦了,我們直接講操作步驟:
第一:重新編譯ACE庫
ACE庫中通過ACE_Dev_Poll_Reactor類來支持epoll,但是ACE庫缺省的安裝是沒有編譯這個類的,我們要做的就是將ACE_Dev_Poll_Reactor編譯連接到ACE庫中(faint,又要重新編譯ACE,在我那臺破服務(wù)器上編譯一次需要一個多小時).我的操作系統(tǒng)是Redhat linux AS4.0,ACE的版本是5.4.10。根據(jù)ACE壓縮包中的ACE-INSTALL.html,我是用”Building ACE with GNU Autoconf“這種方式來安裝的,安裝步驟如下(很簡單,就不翻譯了):
1 cd to the top-level ACE_wrappers directory.
2.Create a subdirectory to hold your build’s configuration and built ACE version, and then change to the new directory:
mkdir build
cd build
3.Note that you do not run the create_ace_build.pl utility mentioned in the Cloning the Source Tree section. The configure script takes care of creating all files and links that are needed.
Configure ACE for your platform by issuing the following command: c
../configure [options]
4.Build ACE by typing make.
5. Install ACE by typing make install.
好,現(xiàn)在終于可以講如何將ACE_Dev_Poll_Reactor編譯到ACE庫中去了。在上述的第一步和第二步之間修改ACE_wrappers/ace/config-linux.h,增加一行:#define ACE_HAS_EVENT_POLL,然后執(zhí)行第2、3步,第3步../configure執(zhí)行完之后,build目錄下會生成一些文件和目錄,打開ACE_wrappers/build/ace/config.h,增加一行:#define ACE_HAS_EVENT_POLL。然后執(zhí)行第4步make和第5步make install.OK,在漫長的編譯以后,支持epoll的ACE庫總算完成了。
第二:修改應(yīng)用程序
應(yīng)用程序修改很簡單,兩行代碼搞掂,在應(yīng)用程序初始化時(必須是在第一次使用ACE_Reactor::instance()之間)加入:
m_pDevPollReactor=new ACE_Dev_Poll_Reactor;
ACE_Reactor::instance(new ACE_Reactor(m_pDevPollReactor));
那么在后續(xù)的對ACE_Reactor::instance()的調(diào)用就是使用ACE_Dev_Poll_Reactor的實現(xiàn)了。
第三:重新編譯應(yīng)用程序
在應(yīng)用程序的makefile中加入 -DACE_HAS_EVENT_POLL,重新make應(yīng)用程序。OK,打完收工。