Libevent
使用Libevent的注意事项
Libevent和Libevent2
libevent是对于Kqueuer、Epool、IOCP(Windows)的封装,使用的目的是对于系统event的封装用起来更加方便,因为libevent对于 ** 将系统的各种事件进行统一处理 ** 方便了编程开发的效率,但是,对性能是有一定牺牲的,而且,对于 ** epool里水平触发和边缘触发 ** 的极限性能追求方面能力较弱。 其次,libevent屏蔽了系统间不同实现的区别,做到了应用层的兼容,部署的时候更加方便,甚至有些情况下在Windows NT Server下用IOCP的网络性能更加好些。 最后,libevent和libevent2不同之处主要在libevent2允许用户生成和管理event全局实例,但是,即便如此想要实现多实例几乎是不现实的,同时,也是没有必要的,在维护一致性付出的代价会冲抵多线程带来的优势,也因此,早期的多进程Server性能实际上是高出多线程的,因为在多线程中需要´护太多东西,调试和开发的难度都比多进程大太多。此外,事件和数据用eventbuffer统一起来有好有坏,个人感觉buffer还是要自己去管理和控制,毕竟Library这种通用化的东西通常较为笨重。
关于本项目中的使用情况
本项目中采用Libevent2来充当IO多路复用的事件驱动引擎,网络IO事件都交给其处理。
使用Libevent2
示例代码
/* For sockaddr_in */
#include <netinet/in.h>
/* For socket functions */
#include <sys/socket.h>
/* For fcntl */
#include <fcntl.h>
#include <event2/event.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#define MAX_LINE 16384
void do_read(evutil_socket_t fd, short events, void *arg);
void do_write(evutil_socket_t fd, short events, void *arg);
char
rot13_char(char c)
{
/* We don't want to use isalpha here; setting the locale would change
* which characters are considered alphabetical. */
if ((c >= 'a' && c <= 'm') || (c >= 'A' && c <= 'M'))
return c + 13;
else if ((c >= 'n' && c <= 'z') || (c >= 'N' && c <= 'Z'))
return c - 13;
else
return c;
}
struct fd_state {
char buffer[MAX_LINE];
size_t buffer_used;
size_t n_written;
size_t write_upto;
struct event *read_event;
struct event *write_event;
};
struct fd_state *
alloc_fd_state(struct event_base *base, evutil_socket_t fd)
{
struct fd_state *state = malloc(sizeof(struct fd_state));
if (!state)
return NULL;
state->read_event = event_new(base, fd, EV_READ|EV_PERSIST, do_read, state);
if (!state->read_event) {
free(state);
return NULL;
}
state->write_event =
event_new(base, fd, EV_WRITE|EV_PERSIST, do_write, state);
if (!state->write_event) {
event_free(state->read_event);
free(state);
return NULL;
}
state->buffer_used = state->n_written = state->write_upto = 0;
assert(state->write_event);
return state;
}
void
free_fd_state(struct fd_state *state)
{
event_free(state->read_event);
event_free(state->write_event);
free(state);
}
void
do_read(evutil_socket_t fd, short events, void *arg)
{
struct fd_state *state = arg;
char buf[1024];
int i;
ssize_t result;
while (1) {
assert(state->write_event);
result = recv(fd, buf, sizeof(buf), 0);
if (result <= 0)
break;
for (i=0; i < result; ++i) {
if (state->buffer_used < sizeof(state->buffer))
state->buffer[state->buffer_used++] = rot13_char(buf[i]);
if (buf[i] == '\n') {
assert(state->write_event);
event_add(state->write_event, NULL);
state->write_upto = state->buffer_used;
}
}
}
if (result == 0) {
free_fd_state(state);
} else if (result < 0) {
if (errno == EAGAIN) // XXXX use evutil macro
return;
perror("recv");
free_fd_state(state);
}
}
void
do_write(evutil_socket_t fd, short events, void *arg)
{
struct fd_state *state = arg;
while (state->n_written < state->write_upto) {
ssize_t result = send(fd, state->buffer + state->n_written,
state->write_upto - state->n_written, 0);
if (result < 0) {
if (errno == EAGAIN) // XXX use evutil macro
return;
free_fd_state(state);
return;
}
assert(result != 0);
state->n_written += result;
}
if (state->n_written == state->buffer_used)
state->n_written = state->write_upto = state->buffer_used = 1;
event_del(state->write_event);
}
void
do_accept(evutil_socket_t listener, short event, void *arg)
{
struct event_base *base = arg;
struct sockaddr_storage ss;
socklen_t slen = sizeof(ss);
int fd = accept(listener, (struct sockaddr*)&ss, &slen);
if (fd < 0) { // XXXX eagain??
perror("accept");
} else if (fd > FD_SETSIZE) {
close(fd); // XXX replace all closes with EVUTIL_CLOSESOCKET */
} else {
struct fd_state *state;
evutil_make_socket_nonblocking(fd);
state = alloc_fd_state(base, fd);
assert(state); /*XXX err*/
assert(state->write_event);
event_add(state->read_event, NULL);
}
}
void
run(void)
{
evutil_socket_t listener;
struct sockaddr_in sin;
struct event_base *base;
struct event *listener_event;
base = event_base_new();
if (!base)
return; /*XXXerr*/
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = 0;
sin.sin_port = htons(40713);
listener = socket(AF_INET, SOCK_STREAM, 0);
evutil_make_socket_nonblocking(listener);
#ifndef WIN32
{
int one = 1;
setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
}
#endif
if (bind(listener, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
perror("bind");
return;
}
if (listen(listener, 16)<0) {
perror("listen");
return;
}
listener_event = event_new(base, listener, EV_READ|EV_PERSIST, do_accept, (void*)base);
/*XXX check it */
event_add(listener_event, NULL);
event_base_dispatch(base);
}
int
main(int c, char **v)
{
setvbuf(stdout, NULL, _IONBF, 0);
run();
return 0;
}
使用libevent编写高并发HTTP server
libevent库使得高并发响应HTTP Server的编写变得很容易。 整个过程包括如下几部:初始化,创建HTTP Server, 指定callback, 进入事件循环。另外在回调函数中,可以获取客户端请求(request的HTTP Header和参数等),进行响应的处理,再将结果发送给客户端(response的HTTP Header和内容,如html代码)。 libevent除了设置generic的callback,还可以对特定的请求路径设置对应的callback(回调/处理函数)。
示例代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> //for getopt, fork
#include <string.h> //for strcat
//for struct evkeyvalq
#include <sys/queue.h>
#include <event.h>
//for http
#include <evhttp.h>
#include <signal.h>
#define MYHTTPD_SIGNATURE "myhttpd v 0.0.1"
//处理模块
void httpd_handler(struct evhttp_request *req, void *arg) {
char output[2048] = "\0";
char tmp[1024];
//获取客户端请求的URI(使用evhttp_request_uri或直接req->uri)
const char *uri;
uri = evhttp_request_uri(req);
sprintf(tmp, "uri=%s\n", uri);
strcat(output, tmp);
sprintf(tmp, "uri=%s\n", req->uri);
strcat(output, tmp);
//decoded uri
char *decoded_uri;
decoded_uri = evhttp_decode_uri(uri);
sprintf(tmp, "decoded_uri=%s\n", decoded_uri);
strcat(output, tmp);
//解析URI的参数(即GET方法的参数)
struct evkeyvalq params;
evhttp_parse_query(decoded_uri, ¶ms);
sprintf(tmp, "q=%s\n", evhttp_find_header(¶ms, "q"));
strcat(output, tmp);
sprintf(tmp, "s=%s\n", evhttp_find_header(¶ms, "s"));
strcat(output, tmp);
free(decoded_uri);
//获取POST方法的数据
char *post_data = (char *) EVBUFFER_DATA(req->input_buffer);
sprintf(tmp, "post_data=%s\n", post_data);
strcat(output, tmp);
/*
具体的:可以根据GET/POST的参数执行相应操作,然后将结果输出
...
*/
/* 输出到客户端 */
//HTTP header
evhttp_add_header(req->output_headers, "Server", MYHTTPD_SIGNATURE);
evhttp_add_header(req->output_headers, "Content-Type", "text/plain; charset=UTF-8");
evhttp_add_header(req->output_headers, "Connection", "close");
//输出的内容
struct evbuffer *buf;
buf = evbuffer_new();
evbuffer_add_printf(buf, "It works!\n%s\n", output);
evhttp_send_reply(req, HTTP_OK, "OK", buf);
evbuffer_free(buf);
}
void show_help() {
char *help = "written by Min (http://54min.com)\n\n"
"-l <ip_addr> interface to listen on, default is 0.0.0.0\n"
"-p <num> port number to listen on, default is 1984\n"
"-d run as a deamon\n"
"-t <second> timeout for a http request, default is 120 seconds\n"
"-h print this help and exit\n"
"\n";
fprintf(stderr, help);
}
//当向进程发出SIGTERM/SIGHUP/SIGINT/SIGQUIT的时候,终止event的事件侦听循环
void signal_handler(int sig) {
switch (sig) {
case SIGTERM:
case SIGHUP:
case SIGQUIT:
case SIGINT:
event_loopbreak(); //终止侦听event_dispatch()的事件侦听循环,执行之后的代码
break;
}
}
int main(int argc, char *argv[]) {
//自定义信号处理函数
signal(SIGHUP, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler);
signal(SIGQUIT, signal_handler);
//默认参数
char *httpd_option_listen = "0.0.0.0";
int httpd_option_port = 8080;
int httpd_option_daemon = 0;
int httpd_option_timeout = 120; //in seconds
//获取参数
int c;
while ((c = getopt(argc, argv, "l:p:dt:h")) != -1) {
switch (c) {
case 'l' :
httpd_option_listen = optarg;
break;
case 'p' :
httpd_option_port = atoi(optarg);
break;
case 'd' :
httpd_option_daemon = 1;
break;
case 't' :
httpd_option_timeout = atoi(optarg);
break;
case 'h' :
default :
show_help();
exit(EXIT_SUCCESS);
}
}
//判断是否设置了-d,以daemon运行
if (httpd_option_daemon) {
pid_t pid;
pid = fork();
if (pid < 0) {
perror("fork failed");
exit(EXIT_FAILURE);
}
if (pid > 0) {
//生成子进程成功,退出父进程
exit(EXIT_SUCCESS);
}
}
/* 使用libevent创建HTTP Server */
//初始化event API
event_init();
//创建一个http server
struct evhttp *httpd;
httpd = evhttp_start(httpd_option_listen, httpd_option_port);
evhttp_set_timeout(httpd, httpd_option_timeout);
//指定generic callback
evhttp_set_gencb(httpd, httpd_handler, NULL);
//也可以为特定的URI指定callback
//evhttp_set_cb(httpd, "/", specific_handler, NULL);
//循环处理events
event_dispatch();
evhttp_free(httpd);
return 0;
}
编译:gcc -o myhttpd -Wall -levent myhttpd.c 运行:./test 测试: 在浏览器中输入http://54min.com:8080/index.php?q=test&s=some thing,显示内容如下:
It works!
uri=/index.php?q=test&s=some%20thing
uri=/index.php?q=test&s=some%20thing
decoded_uri=/index.php?q=test&s=some thing
q=test
s=some thing
post_data=(null)
并使用Live Http Headers(Firefox addons)查看HTTP headers。
HTTP/1.1 200 OK
Server: myhttpd v 0.0.1
Content-Type: text/plain; charset=UTF-8
Connection: close
Date: Tue, 21 Jun 2011 06:30:30 GMT
Content-Length: 72
数据结构
struct evhttp_request
表示客户端请求,定义参看:http://monkey.org/~provos/libevent/doxygen-1.4.10/structevhttp__request.html,其中包含的主要域:
struct evkeyvalq *input_headers; //保存客户端请求的HTTP headers(key-value pairs)
struct evkeyvalq *output_headers; //保存将要发送到客户端的HTTP headers(key-value pairs)
//客户端的ip和port
char *remote_host;
u_short remote_port;
enum evhttp_request_kind kind; //可以是EVHTTP_REQUEST或EVHTTP_RESPONSE
enum evhttp_cmd_type type; //可以是EVHTTP_REQ_GET, EVHTTP_REQ_POST或EVHTTP_REQ_HEAD
char *uri; //客户端请求的uri
char major; //HTTP major number
char minor; //HTTP major number
int response_code; //HTTP response code
char *response_code_line; //readable response
struct evbuffer *input_buffer; //客户端POST的数据
struct evbuffer *output_buffer; //输出到客户端的数据
struct evkeyvalq
定义参看:http://monkey.org/~provos/libevent/doxygen-1.4.10/event_8h-source.html。struct evkeyvalq被定义为TAILQ_HEAD (evkeyvalq, evkeyval);,即struct evkeyval类型的tail queue。需要在代码之前包含
#include <sys/queue.h>
#include <event.h>
struct evkeyval为key-value queue(队列结构),主要用来保存HTTP headers,也可以被用来保存parse uri参数的结果。
/* Key-Value pairs. Can be used for HTTP headers but also for query argument parsing. */
struct evkeyval {
TAILQ_ENTRY(evkeyval) next; //队列
char *key;
char *value;
};
宏TAILQ_ENTRY(evkeyval)被定义为:
#define TAILQ_ENTRY(type) struct { struct type *tqe_next; //next element struct type **tqe_prev; //address of previous next element }
stuct evbuffer
定义参看:http://monkey.org/~provos/libevent/doxygen-1.4.10/event_8h-source.html。该结构体用于input和output的buffer。
/* These functions deal with buffering input and output */ struct evbuffer { u_char *buffer; u_char *orig_buffer; size_t misalign; size_t totallen; size_t off; void (*cb)(struct evbuffer *, size_t, size_t, void *); void *cbarg; };
另外定义宏方便获取evbuffer中保存的内容和大小:
#define EVBUFFER_LENGTH(x) (x)->off #define EVBUFFER_DATA(x) (x)->buffer
例如,获取客户端POST数据的内容和大小:
EVBUFFER_DATA(res->input_buffer); EVBUFFER_LENGTH(res->input_buffer);
另外struct evbuffer用如下函数创建添加和释放:
struct evbuffer *buf; buf = evbuffer_new(); //往buffer中添加内容 evbuffer_add_printf(buf, "It works! you just requested: %s\n", req->uri); //Append a formatted string to the end of an evbuffer. //将内容输出到客户端 evhttp_send_reply(req, HTTP_OK, "OK", buf); //释放掉buf evbuffer_free(buf);
关键函数
获取客户端请求的URI
使用req->uri或使用函数const char *evhttp_request_uri(struct evhttp_request *req);即(evhttp_request_uri(req);)。
对获取的URI进行解析和其他操作
使用函数void evhttp_parse_query(const char *uri, struct evkeyvalq *args);可对uri的参数进行解析,结果保存在struct evkeyvalq的key-value pairs中,例如:
char *uri = "http://foo.com/?q=test&s=some+thing"; struct evkeyvalq args; evhttp_parse_query(uri, &args); //然后通过evhttp_find_header等函数获取各个参数及对应的值 evhttp_find_header(&args, "q"); //得到test evhttp_find_header(&args, "s"); //得到some thing
如下两个函数对URI进行encode和decode:
char *evhttp_encode_uri(const char *uri); char *evhttp_decode_uri(const char *uri);
URI encode的结果是所有非alphanumeric及-_的字符都被类似于%和一个2位16进制字符替换(其中空格被+号替换)。如上两个函数返回的字符串需要free掉。
处理HTTP headers相关的函数 HTTP headers保存在struct evkeyvalq的结构体中(key-value pairs),使用如下函数可对其进行修改:
const char *evhttp_find_header(const struct evkeyvalq *, const char *); int evhttp_remove_header(struct evkeyvalq *, const char *); int evhttp_add_header(struct evkeyvalq *, const char *, const char *); void evhttp_clear_headers(struct evkeyvalq *);
Escape特殊的HTML符
char *evhttp_htmlescape(const char *html);
特殊字符:&被替换为&;”被替换为";’被替换为'; <被替换为<;>被替换为>。该函数返回的字符串需要free掉。