Libevent應用(六)從bufferevent中取出evbuffer

嚇人的猿發表於2018-03-03

6 從bufferevent中取出evbuffer

struct evbuffer* bufferevent_get_input(struct bufferevent *bufev); //取出輸入緩衝區
struct evbuffer* bufferevent_get_output(struct bufferevent *bufev); //取出輸出緩衝區

/*對evbuffer的操作*/

//讀到的一行內容
char *evbuffer_readln(struct evbuffer*buffer, size_t *n_read_out,enum evbuffer_eol_style eol_style); 
enum evbuffer_eol_style {
    EVBUFFER_EOL_ANY,            // 任意數量的\r和\n
    EVBUFFER_EOL_CRLF,           // \r或者\r\n
    EVBUFFER_EOL_CRLF_STRICT,    // \r\n
    EVBUFFER_EOL_LF,             // \n
    EVBUFFER_EOL_NUL             // \0
};

//將資料新增到evbuffer的結尾
int evbuffer_add(struct evbuffer *buf,const void *data, size_t datlen);  

//從evbuffer讀取資料到data
int evbuffer_remove(struct evbuffer*buf, void *data, size_t datlen); 

//丟掉len位元組的資料
int evbuffer_drain (struct evbuffer *buf, size_t len);

//返回evbuffer中儲存的位元組長度
size_t evbuffer_get_length(const structevbuffer *buf);  

/*在buffer中搜尋指定字串
第一個引數是搜尋在evbuffer中,
第二個引數what是要搜尋的字串。
第三個引數len為what的字串長度,
第四個引數,如果start不為空,則會從start中所指定的位置開始搜尋,為NULL則從頭開始找。
返回值:如果找到struct evbuffer_ptr的成員pos返回對應的索引,沒有找到則pos返回-1*/
struct evbuffer_ptr evbuffer_search (struct evbuffer *buffer,
                                     const char *what,
                                     size_t len,
                                     const struct evbuffer_ptr *start);
//第二個函式的不同是指定了一個搜尋範圍;
struct evbuffer_ptr evbuffer_search_range (struct evbuffer *buffer,
                                           const char *what,
                                           size_t len,
                                           const struct evbuffer_ptr *start,
                                           const struct evbuffer_ptr *end);

struct evbuffer_ptr {
    ev_ssize_t pos;  //位置
    struct {/* internal fields */} _internal;
};

伺服器綜合程式碼:

/*************************************************************************
# File Name: tcp_server.c
# Author: wenong
# mail: huangwenlong@520it.com
# Created Time: 2016年09月03日 星期六 21時51分08秒
 ************************************************************************/

#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/buffer.h>
#include <event2/listener.h>
#define SERVERPORT 8888
#define MAXBYTES 1024

static struct event_base * base;

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];
    struct evbuffer* input_evbuffer = bufferevent_get_input(bev);   

    struct evbuffer_ptr begin, end;
    while(1)
    {
        /*讀取a開頭,b結尾的資料包*/
        begin = evbuffer_search(input_evbuffer, "a", 1, NULL); 
        printf("find a index %ld\n", begin.pos);
        if(begin.pos >= 0)
        {
            evbuffer_drain(input_evbuffer, begin.pos);
            end = evbuffer_search(input_evbuffer, "b", 1, NULL);
            if(end.pos > 0)
            {
                ret = evbuffer_remove(input_evbuffer, buf, end.pos - begin.pos + 1);
                buf[ret] = '\0';
                printf("read_buf_cd length %d\n", ret);
                for(i = 0; i < ret; i++)
                    buf[i] = toupper(buf[i]);
                bufferevent_write(bev, buf, ret);
            }
            else
                break;
        }
        else
        {
            bufferevent_flush(bev, EV_READ, BEV_NORMAL);
            break;
        }

    }

}
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("BEV_EVENT_TIMEOUT");
        bufferevent_free(bev);
    }
}


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;
    timeout_read.tv_sec = 60;
    timeout_read.tv_usec = 0;
    bufferevent_set_timeouts(bev, &timeout_read, NULL);  
    bufferevent_setwatermark(bev, EV_READ, 10, 0);

    bufferevent_enable(bev, EV_READ);
}

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

相關文章