#include<event2/event.h>#include<event2/bufferevent.h>#include<event2/buffer.h>#include<event2/util.h>#include<stdlib.h>#include<errno.h>#include<string.h>structinfo {constchar*name;size_t total_drained;};voidread_callback(structbufferevent*bev,void*ctx){structinfo*inf= ctx;structevbuffer*input=bufferevent_get_input(bev);size_t len =evbuffer_get_length(input);if (len) {inf->total_drained += len;evbuffer_drain(input, len);printf("Drained %lu bytes from %s\n", (unsignedlong) len,inf->name); }}voidevent_callback(structbufferevent*bev,short events,void*ctx){structinfo*inf= ctx;structevbuffer*input=bufferevent_get_input(bev);int finished =0;if (events & BEV_EVENT_EOF) {size_t len =evbuffer_get_length(input);printf("Got a close from %s. We drained %lu bytes from it, ""and have %lu left.\n",inf->name, (unsignedlong)inf->total_drained, (unsignedlong)len); finished =1; }if (events & BEV_EVENT_ERROR) {printf("Got an error from %s: %s\n",inf->name,evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR())); finished =1; }if (finished) {free(ctx);bufferevent_free(bev); }}structbufferevent*setup_bufferevent(void){structbufferevent*b1=NULL;structinfo*info1; info1 =malloc(sizeof(struct info));info1->name ="buffer 1";info1->total_drained =0; /* ... Here we should set up the bufferevent and make sure it gets connected... */ /* Trigger the read callback only whenever there is at least 128 bytes of data in the buffer. */bufferevent_setwatermark(b1, EV_READ,128,0);bufferevent_setcb(b1, read_callback,NULL, event_callback, info1);bufferevent_enable(b1, EV_READ); /* Start reading. */return b1;}