libevent用 bufferevent_set_timeouts 函数来设置读和写的超时时间timeout,比如设置读超时时间,如果接收超时就会调用设置的错误处理函数。以tcp服务器为例,在accept的时候设置接收超时,但对某写tcp client不能设置接收超时(这个时候就需要取消accept的时候设置的接收超时)。查看 bufferevent_set_timeouts 的源码,超时时间参数传入NULL就会清掉对应的读或者写的超时时间设置,实际测试发现调用bufferevent_set_timeouts 传入NULL,并没有效果,依然会产生超时timeout事件。
那么该如何在设置超时后再次取消超时呢?
正确的做法是,在超时timeout事件处理函数里面,调用 bufferevent_set_timeouts 来取消超时设置,并且enable read事件。
完整的例子如下,红色部分就是取消超时设置:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <ctype.h>
#include <arpa/inet.h>
#include <signal.h>
#include <errno.h>
#include <sys/wait.h>
#include <event2/event.h>
#include <event2/bufferevent.h>
#include <event2/listener.h>
#define SERVERPORT 8888
#define MAXBYTES 1024
static struct event_base * base;
int st =0;
void write_buf_cb(struct bufferevent* bev, void* cbarg)
{
printf("%s\n", __FUNCTION__);
}
void read_buf_cb(struct bufferevent* bev, void* cbarg)
{
int ret, i;
char buf[MAXBYTES];
ret = bufferevent_read(bev, buf, sizeof(buf));
printf("read_buf_cd length %d\n", ret);
for(i = 0; i < ret; i++)
{
buf[i] = toupper(buf[i]);
}
bufferevent_write(bev, buf, ret);
}
void event_cb(struct bufferevent* bev, short event, void* cbarg)
{
if(BEV_EVENT_READING & event)
puts("BEV_EVENT_READING");
if(BEV_EVENT_WRITING & event)
puts("BEV_EVENT_WRITING");
if(BEV_EVENT_ERROR & event)
puts("BEV_EVENT_ERROR");
if(BEV_EVENT_EOF & event)
{
puts("BEV_EVENT_EOF");
bufferevent_free(bev);
}
if(BEV_EVENT_TIMEOUT & event)
{
puts("timeout");
bufferevent_set_timeouts(bev, NULL, NULL);
bufferevent_enable (bev, EV_READ);
}
}
void accept_cb(struct evconnlistener *listener,
evutil_socket_t clientfd, struct sockaddr *addr
, int len, void *arg)
{
struct bufferevent* bev;
struct event_base* base = (struct event_base*)arg;
puts("Accept client connect");
evutil_make_socket_nonblocking(clientfd);
bev = bufferevent_socket_new(base, clientfd, BEV_OPT_CLOSE_ON_FREE
| BEV_OPT_DEFER_CALLBACKS);
bufferevent_setcb(bev, (bufferevent_data_cb)read_buf_cb
, (bufferevent_data_cb)write_buf_cb, (bufferevent_event_cb)event_cb, NULL);
struct timeval timeout_read = {10, 0};
bufferevent_set_timeouts(bev, &timeout_read, NULL);
bufferevent_setwatermark(bev, EV_READ, 1, 0);
bufferevent_setwatermark(bev, EV_WRITE, 1, 0);
bufferevent_enable(bev, EV_READ | EV_WRITE);
}
void main_loop(struct sockaddr_in * addr)
{
struct evconnlistener *evcon;
base = event_base_new();
evcon = evconnlistener_new_bind(base, (evconnlistener_cb)accept_cb
, (void*)base, LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE
, 128, (struct sockaddr*)addr, sizeof(struct sockaddr_in));
puts("server begin listenning...");
event_base_dispatch(base);
evconnlistener_free(evcon);
event_base_free(base);
}
int main(int argc, char** argv)
{
int serverfd;
socklen_t serveraddrlen;
struct sockaddr_in serveraddr;
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(SERVERPORT);
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serverfd = socket(AF_INET, SOCK_STREAM, 0);
serveraddrlen = sizeof(serveraddr);
main_loop( &serveraddr);
return 0;
}