赞
踩
Libevent 是一个用C语言编写的、轻量级的开源高性能网络库,主要有以下几个亮点:事件驱动( event-driven),高性能;轻量级,专注于网络,不如 ACE 那么臃肿庞大;源代码相当精炼、易读;跨平台,支持 Windows、 Linux、 *BSD 和 Mac Os;支持多种 I/O 多路复用技术, epoll、 poll、 dev/poll、 select 和 kqueue 等;支持 I/O,定时器和信号等事件;注册事件优先级。
Libevent 已经被广泛的应用,作为底层的网络库;比如 memcached、 Vomit、 Nylon、 Netchat等等。Libevent之于C语言网络编程,类似于Nettty之于Java Web编程。学习Netty的小伙伴,不防看下Libevent的实现,会加深对Netty框架的理解
安装方式(1):
$ apt-get download libevent-dev
$ dpkg -x libevent-dev_2.1.12-stable-1build3_amd64.deb libevent
安装方式():
struct event-base
struct event
两者关系:
event_init
event_base new
event_set
event_assign
event_add
evconnlistener_new_bing
fifo-read.c
#include <event.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <unistd.h> void fifo_read(evutil_socket_t fd, short events, void *arg) { char buf[32] = {0}; int rt = read(fd, buf, sizeof(buf)); if (-1 == rt) { perror("read"); exit(-1); } printf("read <%s>\n", buf); return; } int main() { int ret = mkfifo("fifo.tmp", 0700); if (-1 == ret) { perror("mkfifo"); exit(-1); } int fd = open("fifo", O_RDONLY); if (-1 == fd) { perror("open"); exit(-1); } struct event ev; event_init(); event_set(&ev, fd, EV_READ, fifo_read, NULL); event_add(&ev, NULL); event_dispatch(); return 0; }
fifo-write.c
#include <fcntl.h> #include <stdio.h> #include <string.h> #include <unistd.h> int main() { int fd = open("fifo.tmp", O_WRONLY); if (-1 == fd) { perror("open"); exit(-1); } char buf[32] = {0}; while (1) { scanf("%s", buf); int ret = write(fd, buf, sizeof(buf)); if (-1 == ret) { perror("write"); exit(-1); } if (!strcmp(buf, "bye")) { break; } memset(buf, 0, sizeof(buf)); } return 0; }
signal.c
#include <event2/event_compat.h> #include <event2/event_struct.h> #include <signal.h> static int signal_count = 0; void signal_handler(evutil_socket_t fd, short events, void *args) { printf("<sig: %d>\n", fd); ++signal_count; if (signal_count >= 2) { event_del((struct event *)args); } } int main() { struct event_base *base = event_base_new(); struct event ev; event_assign(&ev, base, SIGINT, EV_SIGNAL | EV_PERSIST, signal_handler, &ev); event_add(ev, NULL); event_base_dispatch(); event_base_free(base); return 0; }
#include <arpa/inet.h> #include <cstddef> #include <event2/bufferevent.h> #include <event2/bufferevent_struct.h> #include <event2/event_compat.h> #include <event2/listener.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> void read_cb(struct bufferevent *bev, void *ctx) { char buf[128] = {0}; size_t ret = bufferevent_read(bev, buf, sizeof(buf)); if (ret < 0) { exit(-1); } printf("read from: <%d>\n", *(int *)ctx); } void event_cb(struct bufferevent *bev, short what, void *ctx) { if (what & BEV_EVENT_EOF) printf("Client: <d> down", *(int *)ctx); bufferevent_free(bev); } void listen_cb(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *addr, int socklen, void *arg) { static int gFd = -1; printf("Accept: <%d>\n", fd); gFd = fd; struct event_base *base = arg; struct bufferevent *bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE); if (NULL == bev) { exit(-1); } bufferevent_setcb(bev, read_cb, NULL, event_cb, &gFd); bufferevent_enable(bev, EV_READ); } int main() { struct event_base *base = event_base_new(); if (NULL == base) { printf("event_base \n"); exit(-1); } struct sockaddr_in server_addr; memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = 8000; server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); struct evconnlistener *listen = evconnlistener_new_bind( base, listen_cb, NULL, LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, 10, (const struct sockaddr *)&server_addr, sizeof(server_addr)); event_base_dispatch(base); evconnlistener_free(listen); event_base_free(base); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。