注:这段代码来自于网络,虽然很粗糙,但是对libevent的使用方法已经说明的很清楚了.
附源码:
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <event.h>
#include <stdio.h>
#include <time.h>
void connection_time(int fd, short event, struct event *arg)
{
char buf[32];
struct tm t;
time_t now;
time(&now);
localtime_r(&now, &t);
asctime_r(&t, buf);
write(fd, buf, strlen(buf));
shutdown(fd, SHUT_RDWR);
free(arg);
}
void connection_accept(int fd, short event, void *arg)
{
/* for debugging */
fprintf(stderr, "%s(): fd = %d, event = %d.\n", __func__, fd, event);
/* Accept a new connection. */
struct sockaddr_in s_in;
socklen_t len = sizeof(s_in);
int ns = accept(fd, (struct sockaddr *) &s_in, &len);
if (ns < 0) {
perror("accept");
return;
}
/* Install time server. */
struct event *ev = malloc(sizeof(struct event));
event_set(ev, ns, EV_WRITE, (void *) connection_time, ev);
event_add(ev, NULL);
}
int main(void)
{
/* Request socket. */
int s = socket(PF_INET, SOCK_STREAM, 0);
if (s < 0) {
perror("socket");
exit(1);
}
/* bind() */
struct sockaddr_in s_in;
bzero(&s_in, sizeof(s_in));
s_in.sin_family = AF_INET;
s_in.sin_port = htons(7000);
s_in.sin_addr.s_addr = INADDR_ANY;
if (bind(s, (struct sockaddr *) &s_in, sizeof(s_in)) < 0) {
perror("bind");
exit(1);
}
/* listen() */
if (listen(s, 5) < 0) {
perror("listen");
exit(1);
}
/* Initial libevent. */
event_init();
/* Create event. */
struct event ev;
event_set(&ev, s, EV_READ | EV_PERSIST, connection_accept, &ev);
/* Add event. */
event_add(&ev, NULL);
event_dispatch();
return 0;
}
在写 Nonblocking Network Program 通常要处理 Buffering 的问题,但并不好写,主要是因为 read() 或 recv() 不保证可以一次读到一行的份量进来。
在 libevent 里面提供相当不错的 Buffer Library 可以用,完整的说明在 man event 的时候可以看到,最常用的应该就是以 evbuffer_add()、evbuffer_readline() 这两个 Function,其他的知道存在就可以了,需要的时候再去看详细的用法。
下面直接提供 libevent-buff.c 当作范例,编译后看执行结果,再回头来看 source code 应该就有感觉了:
#include <sys/time.h>
#include <event.h>
#include <stdio.h>
void printbuf(struct evbuffer *evbuf)
{
for (;;) {
char *buf = evbuffer_readline(evbuf);
printf("* buf = %p, the string = \"\e[1;33m%s\e[m\"\n", buf, buf);
if (buf == NULL)
break;
free(buf);
}
}
int main(void)
{
struct evbuffer *evbuf;
evbuf = evbuffer_new();
if (evbuf == NULL) {
fprintf(stderr, "%s(): evbuffer_new() failed.\n", __func__);
exit(1);
}
/* Add "gslin" into buffer. */
u_char *buf1 = "gslin";
printf("* Add \"\e[1;33m%s\e[m\".\n", buf1);
evbuffer_add(evbuf, buf1, strlen(buf1));
printbuf(evbuf);
u_char *buf2 = " is reading.\nAnd he is at home.\nLast.";
printf("* Add \"\e[1;33m%s\e[m\".\n", buf2);
evbuffer_add(evbuf, buf2, strlen(buf2));
printbuf(evbuf);
evbuffer_free(evbuf);
}
最后的 event_dispatch() 表示进入 event loop,当 Queue 里面的任何一个 File Description 发生事件的时候就会进入 callback function 执行。