Libevent+bufferevent Summary

Source: Internet
Author: User

Libevent+bufferevent Summary 1 Study reference URL

Libevent Study Website: http://blog.csdn.net/feitianxuxue/article/details/9372535

Http://www.cnblogs.com/hustcat/archive/2010/08/31/1814022.html

Http://www.cppblog.com/mysileng/archive/2013/02/04/197719.html

Bufferevent Study Website: http://blog.csdn.net/feitianxuxue/article/details/9386843

http://blog.csdn.net/feitianxuxue/article/details/9386843

2 basic knowledge of libevent and Bufferevent 2.1 event_base

struct Event_base {
const struct EVENTOP *evsel;
void *evbase;
int event_count; /* Total number of events */
int event_count_active; /* Total number of active events */

int event_gotterm; /* Set the terminating event loop */
int event_break; /* Set immediately terminate event loop */

/* Active Event Management */
struct Event_list **activequeues; Array of pointers, array indexes are priority priorities
int nactivequeues;

/* Signal Processing information */
struct Evsignal_info sig;

struct Event_list eventqueue; List of registered events
struct Timeval event_tv;

struct MIN_HEAP timeheap;

struct Timeval tv_cache;
};


The structure of Event_base is as follows:

2.2 Bufferevent_new ()

bufferevent_new (int fd, EVBUFFERCB READCB, EVBUFFERCBWRITECB,EVERRORCB errorcb, void *cbarg)

The function of bufferevent_new in CDN/CLS code is simply to initialize the parameters. Libevent's bufferevent maintains a buffer on the basis of the event, which has the following structure: its own read-write event, read-write buffer, and read-write callback function.

struct Bufferevent {

struct Event_base *ev_base;

struct event ev_read;

struct event ev_write;

struct Evbuffer *input;

struct Evbuffer *output;

struct Event_watermark wm_read;

struct Event_watermark wm_write;

EVBUFFERCB READCB;

EVBUFFERCB WRITECB;

EVERRORCB ERRORCB;

void *cbarg;

int timeout_read; /* units are seconds * /

int timeout_write; /* units are seconds * /

Short enabled; /* whether the event is available * /

};


2.3 Event_base_set (struct event_base *base, struct event *ev)

Modifies the Structevent event structure to which the event_base belongs to the specified event_base. The libevnet contains a global event_base structure. In multiple thread applications, if multiple threads require a libevent event loop, you need to call Event_base_set to modify the event structure based on the event_base. Bufferevent_base_set () is to place read and write events as specified values.

2.4 Event_base_loopexit (M_pevbase, &TV);

Event_base_loopexit () lets event_base stop looping after a given time. It will not return until the end of the event that is being done.

2.5 event_base_loop (m_pevbase, 0)

int Event_base_loop (struct event_base *, int);

Wait for the event to be triggered, and then call their callback function. This is a more flexible version of Event_base_dispatch. By default, the loop runs until there are no events added, or until Event_base_loopbreak () or Evenet_base_loopexit () is called. You can modify this behavior by using the flags parameter.

The parameter 1:eb represents the Event_base struct body.

Parameter 2:flags is Evloop_once | A combination of evloop_nonblock.

The return value: 0 indicates success, 1 indicates an error has occurred, and 1 indicates that no event has been registered.

Evloop_once: Blocks until an active event is executed and then exits with a callback for the active events.

Evloop_nonblock: Do not block, check which event is ready, call the highest priority one, and then exit.

3 Instructions for use

The main functions that Libevent uses to implement the main functions require:

Event_new (); Initialize an event

Event_base_set (); Point the event pointer to an event that you want to use now

Event_base_loop () Listener event, execute callback function

The representation in the code is as follows:

Bufferevent_new (Ssockethead.ifd,cb_read, Cb_write, Cb_error, (void*) this);

The initialization of the parameters is realized;

Bufferevent_base_set (M_pevbase, Psocketmsg->pbufev); Point the event pointer to the event you want to run, in fact the pointer to the corresponding read-write event points to the event that needs to be run.

Event_base_loop (m_pevbase, 0);

Listens for events and executes callback functions.

Libevent+bufferevent Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.