Libevent Getting Started

Source: Internet
Author: User

Libevent API

===============================

Evtimer_new


Evtimer_new (Base, callback, NULL)

Used to make a timer, that is, call the callback function callback when it reaches a certain time. Activate the timer with Evtimer_add.

Like what:

my_node->ev_expect_ping = Evtimer_new (My_node->base,expected_leader_ping_period,                                              (void*) my_node); Evtimer_del (my_node->ev_expect_ping); Evtimer_add (my_node->ev_expect_ping,&my_node->config.expect_ Ping_timeval);

We create a timer my_node->ev_expect_ping on the my->base and bind the timed event expected_leader_ping_period on this timer. Then first disable the timer with Evtimer_del. Then use the Evtimer_add to enable the timer, set expect_ping_timeval after the trigger.


===============================

Bufferevent


struct Bufferevent {               struct event_base *ev_base;        const struct Bufferevent_ops *be_ops;        struct event ev_read;        struct event ev_write;        struct Evbuffer *input;        struct Evbuffer *OUTPUT;......BUFFEREVENT_DATA_CB READCB;        BUFFEREVENT_DATA_CB WRITECB;BUFFEREVENT_EVENT_CB ERRORCB, ...}

The bufferevent contains two event (read/write) and corresponding buffers, and when the data is read (input), READCB is called and WRITECB is called when output is finished.

when there is an error in Network I/O, such as link interruption. Time-out or other error. ERRORCB are called, such as:bev_event_error

Suppose Bufferevent This event indicates that an error occurred while the operation was occurring. Suppose you are using bufferevent as a socket communication. This error generally means that the socket connection is disconnected.

Many other error messages need to be called evutil_socket_error ().


The bufferevent is transmitted by a lower-level port (such as a socket). A read buffer and a write buffer are composed. Unlike the usual event at the bottom of the transport port is ready to be able to read or write when a callback is run, the bufferevent invokes the user-provided callback after reading or writing sufficient amounts of data.


Bufferevent_write (struct bufferevent *bufev, const void *data, size_t size)

writes data to a bufferevent buffer, which is used to write data to a file descriptive descriptor. When the data becomes writable, it is actively written to the descriptive descriptor itself.


struct bufferevent * bufferevent_socket_new (struct event_base *base,           evutil_socket_t fd, int options)

bufferevent_socket_new Creating a new socket bufferevent,bufferevent on an existing socket (FD) is a layer of encapsulation on the event and Evbuffer. Provides an event for each input and output (Ev_read and Ev_write respectively) and a corresponding buffer.


   void Bufferevent_setcb (struct bufferevent *bufev, BUFFEREVENT_DATA_CB           READCB, BUFFEREVENT_DATA_CB WRITECB, BUFFEREVENT_EVENT_CB EVENTCB,           void *cbarg)

The BUFFEREVENT_SETCB () function changes one or more callbacks of the bufferevent. The READCB, WRITECB, and EVENTCB functions are called when sufficient data has been read, enough data has been written, or an error occurs.

The first parameter of each callback function is the bufferevent of the event, and the last parameter is the user-supplied cbarg when calling BUFFEREVENT_SETCB (): The ability to pass data through it to the callback.


int bufferevent_enable (struct bufferevent *bufev, short event)
when Bufferevent is initialized, call bufferevent_enable to activate. The reference event specifies the events that bufferevent can perform, such as, if Ev_read, is read.


===============================

Evconnlistener_new_bind & Evconnlistener_new


With Evconnlistener_new_bind, you can complete the creation of a server socket.

struct Evconnlistener *evconnlistener_new_bind (struct event_base *base,    EVCONNLISTENER_CB cb, void *ptr, unsigned Flags, int backlog,    const struct SOCKADDR *sa, int socklen);

Assigns and returns a new Connection listener object, which is the event_base that the listener uses to listen for connections. That is, the connection listener uses Event_base to know when a new TCP connection is available on a given listening socket const struct SOCKADDR *sa (including IP and port) when a new connection arrives. The listener calls you to give the callback function CB.


struct Evconnlistener *evconnlistener_new (struct event_base *base,    EVCONNLISTENER_CB cb, void *ptr, unsigned flags, int backlog,    evutil_socket_t FD);

The above two evconnlistener_new* () functions are assigned and return a new connection listener object.

The connection listener uses Event_base to get notifications. When there is a new TCP connection on a listening socket. When a new connection arrives. It will invoke the callback function you provided.

In both functions, the listener listens for connections using a base parameter of type Event_base. When a new connection is accepted. The callback function CB will be called, and if the callback CB is NULL, the listener will be considered disabled until the callback is set. The Ptr pointer is passed to the callback. The Flags parameter controls the behavior of the listener. The backlog parameter controls the maximum number of pending connections, that is, the number of states that the network stack should agree to wait, no matter what time it takes. Assuming the backlog is negative, Libevent will attempt to set an appropriate value for the backlog. Assuming it is zero, Libevent will assume that you have called listen () on the socket.

These two functions are different in how they are set to listen for sockets. The Evconnlistener_new () function if you already have a socket that is already bound to port and is passed as an FD parameter. If you want to let libevent assign and bind to a socket, call Evconnlistener_new_bind (), pass the SOCKADDR and its length.

the listener to release the connection. Please pass it to Evconnlistener_free ().


===============================

Evbuffer_remove


int evbuffer_remove (struct evbuffer *buf, void *data, size_t datlen);

The evbuffer_remove () function will copy and delete the buf bytes from the preceding Datlen into the in-memory data. Assuming that less than Datlen bytes are available, the function copies all of the bytes. The value returned by

Failure is-1. Otherwise, the number of bytes copied is returned.

Demo sample:

 struct evbuffer* eVB = bufferevent_get_input (BEV); Evbuffer_remove (evb,msg_buf,sys_msg_header_size+data_size); 




Libevent Getting Started

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.