Libevent事件的创建-scoke服务的创建-特征的获取和配置
生活随笔
收集整理的這篇文章主要介紹了
Libevent事件的创建-scoke服务的创建-特征的获取和配置
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Libevent 簡介
Libevent是一款事件驅(qū)動的網(wǎng)絡(luò)開發(fā)包,由于采用C語言開發(fā)體積小巧,跨平臺,速度極快。大量開源項目使用了Libevent比如谷歌的瀏覽器和分布式的高速緩存系統(tǒng)memcached。libevent支持kqueue,select,poll,epoll,iocp。內(nèi)部事件機制完全獨立于公開事件API,libevent支持跨平臺可以在Linux,*BSD,MacOSX,Solaris,Windows等平臺上編譯。
學(xué)習(xí)條件:具有一定的C/C++基礎(chǔ),熟悉Linux
環(huán)境搭建
- 配置zlib庫
- 配置openssl庫
- 配置libevent環(huán)境
實戰(zhàn)實例
創(chuàng)建event_base
僅僅實現(xiàn)創(chuàng)建上下文
/**** 創(chuàng)建event base* */#include <event2/event.h> #include <iostream> using namespace std; int main() {std::cout << "test libevent!\n"; //創(chuàng)建libevent的上下文event_base * base = event_base_new();if (base){cout << "event_base_new success!" << endl;}return 0; }創(chuàng)建test_server
test_server中說明了如何使用libevent創(chuàng)建一個socket監(jiān)聽
evconnlistener_new_bind一個接口完成了socket的創(chuàng)建,綁定和監(jiān)聽。
/**** 創(chuàng)建event base* */#include <event2/event.h> #include <iostream> #include <signal.h> #include <event2/listener.h> #include <string.h> #include "event_interface.h"using namespace std;/**A callback that we invoke when a listener has a new connection.@param listener The evconnlistener@param fd The new file descriptor@param addr The source address of the connection@param socklen The length of addr@param user_arg the pointer passed to evconnlistener_new()*/ void listen_cb(struct evconnlistener * evConnListener, evutil_socket_t evUtilSockFd, struct sockaddr * sockAddr, int socklen, void *data) {cout << "listen cb is called" << endl; }int main(int argc, char *argv[]) {//1. 忽略管道信號,發(fā)送數(shù)據(jù)給已關(guān)閉的socket//一些socket程序莫名宕掉的原因if(signal(SIGPIPE, SIG_IGN) == SIG_ERR){cout << "ignal pipe signal" << endl;}std::cout << "test libevent!\n"; //創(chuàng)建libevent的上下文event_base * base = event_base_new();if (!base){cout << "event_base_new failed." << endl;return -1;}else{cout << "event_base_new success!" << endl;}//監(jiān)聽端口//socket, bind, listensockaddr_in sockIn;memset(&sockIn, 0, sizeof(sockIn));sockIn.sin_family = AF_INET;sockIn.sin_port = htons(SERVER_PORT);/* 地址沒有指定因為對sockIn進行了了memset,地址賦值為0代表著可以為任意可以用的地址 */struct evconnlistener *pEvListener = evconnlistener_new_bind(base, /* libevent的上下文 */listen_cb, /* 接收到連接的回調(diào) */base, /* 回調(diào)函數(shù)參數(shù) */LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_FREE, /* 地址重用,evconnlistenner關(guān)閉同時關(guān)閉socket */10, /* 連接隊列的大小,對應(yīng)的listen函數(shù) */(sockaddr *)&sockIn, /* 綁定地址和端口 */sizeof(sockIn));//事件分發(fā)處理if(base)event_base_dispatch(base);if(pEvListener)evconnlistener_free(pEvListener);if(base)event_base_free(base);return 0; }創(chuàng)建test_conf
test_conf主要是實現(xiàn)了,測試當(dāng)前系統(tǒng)中支持的方法類型和事件特征的支持情況。
support methods epoll poll selectEV_FEATURE_ET events are supported. EV_FEATURE_O1 events are supported. EV_FEATURE_FDS events are not supports. EV_FEATURE_EARLY_CLOSE events are supported. event base new with config sucess #include <event2/event.h> #include <event2/thread.h> #include <event2/listener.h> #include <signal.h> #include <iostream> #include <string.h> #include "event_interface.h"using namespace std;int main() {//忽略管道信號,發(fā)送數(shù)據(jù)給已關(guān)閉的socketif (signal(SIGPIPE, SIG_IGN) == SIG_ERR)return 1;//創(chuàng)建配置上下文event_config *config = event_config_new();//顯示支持的網(wǎng)絡(luò)模式const char **methods = event_get_supported_methods();cout << "support methods " << endl;for(int i = 0; methods[i] != NULL; i++){cout << methods[i] << endl;}//設(shè)置特征,確認特征時候生效//這個features在linux中設(shè)置沒有效果,因為linux中本來就是支持ET模式的,邊緣觸發(fā)模式// 設(shè)置了EV_FEATURE_FDS其他特征嗯就無法設(shè)置//也就是所支持了EV_FEATURE_FDS 其他的特征都是無法支持的int ret = event_config_require_features(config, EV_FEATURE_ET|EV_FEATURE_EARLY_CLOSE);if(OK != ret){cerr << "event config require features failed." << endl;return ERROR; }//初始化libevent上下文event_base *base = event_base_new_with_config(config);//config一旦配置好就不需要在使用了event_config_free(config);if(!base){cerr << "event base new with config failed!" << endl;//首次失敗就創(chuàng)建一個base取默認值,若是再次失敗就返回失敗base = event_base_new();if(!base){cerr << "event base new failed." << endl; return ERROR;}}else{//確認特征那些生效int f = event_base_get_features(base);if(f&EV_FEATURE_ET){cout << "EV_FEATURE_ET events are supported." << endl;}else{cout << "EV_FEATURE_ET events are not supports." << endl;}if(f&EV_FEATURE_O1){cout << "EV_FEATURE_O1 events are supported." << endl;}else{cout << "EV_FEATURE_O1 events are not supports." << endl;}if(f&EV_FEATURE_FDS){cout << "EV_FEATURE_FDS events are supported." << endl;}else{cout << "EV_FEATURE_FDS events are not supports." << endl;}if(f&EV_FEATURE_EARLY_CLOSE){cout << "EV_FEATURE_EARLY_CLOSE events are supported." << endl;}else{cout << "EV_FEATURE_EARLY_CLOSE events are not supports." << endl;}cout << "event base new with config sucess" << endl;event_base_free(base);}return 0; }總結(jié)
以上是生活随笔為你收集整理的Libevent事件的创建-scoke服务的创建-特征的获取和配置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 作者:周武柏,男,美国佛罗里达国际大学计
- 下一篇: 作者:张丹(1991-),女,中南大学信