項目的簡介
我理解libevent是一個輕量級的,跨平臺+高效的(C語言實現)事件驅動庫,類似于ACE項目中的ACE_Reactor,它實現了網絡通訊套接口I/O事件,定時器事件,信號事件的監聽和事件處理函數回調機制。從項目主頁可以了解到libevent已經支持?/dev/poll,?kqueue(2),?event ports,?POSIX?select(2),?Windows?select(),?poll(2), and?epoll(4)方式的事件監測和驅動機制
項目主頁:http://libevent.org/
維基百科:http://zh.wikipedia.org/wiki/Libevent
參考資料:http://blog.csdn.net/sparkliang/article/details/4957667
PS:在分析開源項目代碼之前,需首先了解該項目的特性,應用場景和價值,這些信息一方面可以從項目的主頁獲取,另一方面可以通過搜索引擎從技術論壇,博客等方面獲取。最好選擇和自己工作/興趣比較相關的項目,這樣有利于分析的深入和堅持,并及時體現收益。
下載源代碼
從項目主頁可以很方便的下載當前版本的源碼,我下載的版本是libevent-2.0.17-stable.tar.gz
代碼量分析
通過Wine運行SourceCounter工具對該項目進行代碼量統計,可以看到該項目代碼量大概5W多行,且代碼工程結構簡單,比較適合像我這樣對開源項目代碼分析經驗不足的人
PS:在開始分析項目源碼之前,分析該項目的代碼量可以大致評估該項目的難度和分析計劃,分析工程結構可以大致評估該項目的重點部分,以免一開始就滿腔熱血地栽在一個深坑里(比較復雜的開源項目),而后面又不了了之
編譯和安裝
在將源碼包在本地解壓后即可以編譯和安裝。這里和其他開源項目差不多,沒有什么特別的,只是為了方便后面通過調試的手段來分析源碼,編譯的時候最好編譯成debug模式,如下
#./configure --enable-debug-mode --enable-static?--enable-thread-support
#make
#make install
安裝完成后,libevent庫的頭文件會安裝在/usr/local/include目錄下,而庫文件會安裝在/usr/local/lib目錄下,故需確保/usr/local/lib在LD_LIBRARY_PATH變量包含的路徑中
PS:卸載的方法
#make uninstall
#make clean
編寫測試應用代碼
該項目源碼包中的sample目錄中其實已經有些例子,但我還是情愿參考樣例自己寫一個,好久沒Coding了 :)
mytimer.c : 實現一個定時器事件處理函數,并通過libevent的事件驅動機制定時調用
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <event2/event-config.h>
#include <event2/event.h>
#include <event2/event_struct.h>
#include <event2/util.h>
static void handle_timer(evutil_socket_t fd, short event, void* arg)
{
printf("handle_timer function is called \n");
fflush(stdout);
}
int main(int argc, char** argv)
{
/* Initalize the event library */
struct event_base* base = event_base_new();
if (NULL == base)
{
return -1;
}
/* Initalize one timeout event */
struct event timeout = {0};
event_assign(&timeout, base, -1, EV_PERSIST, handle_timer, (void*)&timeout);
/* Register the event */
struct timeval tv;
evutil_timerclear(&tv);
tv.tv_sec = 2;
event_add(&timeout, &tv);
/*event dispatch*/
event_base_dispatch(base);
event_base_free(base);
return 0;
}
編譯 : gcc -g -I/usr/local/include -o mytimer mytimer.c -L/usr/local/lib -levent
運行 :?$ ./mytimer?
handle_timer function is called?
handle_timer function is called?
handle_timer function is called?
^C
通過例程調試libevent
通過gdb去調試mytimer時發現其鏈接的是libevent的動態庫,且無法在libevent庫的函數上設置斷點 :(
安裝glibc的靜態庫:# yum install glibc-static libstdc++-static
靜態編譯命令:gcc -g -I/usr/local/include -o mytimer mytimer.c -L/usr/local/lib -static -levent -lc -lrt
這樣就可以通過gdb調試例程時,在libevent庫的函數上設置斷點