libevent設定超時後取消超時(bufferevent_set_timeouts取消超時無效問題)

glen30發表於2020-11-07

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;
}

 

相關文章