- 事件处理逻辑流程图
- 测试了libevent 事件处理(linux,Ctrl+C与kill)
事件处理逻辑流程图 测试了libevent 事件处理(linux,Ctrl+C与kill)
#include#include #ifndef _WIN32 #include #endif // !_ using namespace std; //sock文件描述符, which事件类型, arg传递的参数 static void Ctrl_C(int sock, short which, void* arg) { cout << "INPUT:>> Ctrl+C" << endl; } static void Kill(int sock, short which, void* arg) { cout << "INPUT:>> Kill" << endl; //如果处于非待决 event* ev = (event*)arg; if (!evsignal_pending(ev, NULL)) { event_del(ev); event_add(ev, NULL); } } int main(int argc, char** argv) { #if _WIN32 //windowns 初始化socket库 WSADATA wsa; WSAStartup(MAKEWORD(2, 2), &wsa); #else //linux 忽略管道信号,发送数据给已关闭的socket if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) return 1; #endif event_base* base = event_base_new(); #ifndef _WIN32 //添加信号Ctrl+C 处于no_pending(隐藏的状态:EV_SIGNAL|EV_PERSIST) event* c_signal = evsignal_new(base, SIGINT, Ctrl_C, base); if (!c_signal){ cerr << "SIGINT evsignal_new failed n"; return -1; } //添加事件到pending if (event_add(c_signal, 0) != 0) { cerr << "SIGINT event_add failed n"; return -1; } //添加kill信号 (非持久,一次性, event_self_cbarg传递当前event) event* k_signal = event_new(base, SIGTERM, EV_SIGNAL, Kill, event_self_cbarg()); if (!k_signal) { cerr << "k_signal evsignal_new failed n"; return -1; } //添加事件到pending if (event_add(k_signal, 0) != 0) { cerr << "SIGINT k_signal failed n"; return -1; } //进入事件主循环 event_base_dispatch(base); event_free(c_signal); event_base_free(base); #endif // _WIN32 #ifdef _WIN32 WSACleanup(); #endif // _WIN32 return 0; }
效果如下:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)