Libevent (1)

Source: Internet
Author: User
What is libevent?    The libevent library is not replaced. select(), poll()Or other mechanisms. Instead, a package is added to the implementation using the most efficient high-performance solution for each platform. To process each request, the libevent Library provides an event mechanism, which acts as the package at the underlying network backend. The event system makes it easy to add processing functions for connections and reduces the underlying I/O complexity. This is the core of the libevent system. In general, libevent is an encapsulation of select, poll, epoll, and other mechanisms, making network programming more convenient and reducing the complexity of network programming. Libevent has the following features: Portability: programs written using the libevent library can run on all platforms that support libevent, and are instantly on platforms that do not support non-blocking Io, the libevent library can also support General I/O methods. Efficient: programs written using the libevent library use the most efficient non-blocking Io model on each platform, rather than introducing too many other IO mechanisms. High scalability: applications written in the libevent library can process a large number of network connections. Convenience: libevent is an encapsulation of the underlying I/O mechanism. It can write unified programs to reduce the complexity of I/O programming. How to Use LibeventThe overall API call process of libevent is as follows: (1) event_base_new () // Initialize an event_base (2) event_set () // Initialize an event (event) struct, set the file descriptor, event type (read Io event or write Io event) of the event, callback function after the event is triggered, callback function parameters (3) event_base_set () // set event_base (4) event_add () for the event // Add the event to the queue (ready queue, active queue, etc.). The queue is managed by the event_base corresponding to the event (5) event_base_dispatch () // start the polling relationship between event_base and event. One event_base manages multiple events.
1 int main () 2 {3 4 int serfd; 5 struct sockaddr_in seraddr; 6 7 memset (& seraddr, 0, sizeof (seraddr); 8 seraddr. sin_family = af_inet; 9 seraddr. sin_addr.s_addr = inet_addr (""); 10 seraddr. sin_port = htons (8888); 11 12 13 serfd = socket (af_inet, sock_stream, 0); 14 BIND (serfd, (struct sockaddr *) & seraddr, sizeof (seraddr )); 15 listen (serfd, 10); 16 17 // Initialize an event_base18 base = event_base_new (); 19 20 struct event listenevent; 21 22 // Initialize an event struct, set the file descriptor and Event Type of the event (read Io event or write Io event) 23 // The onaccept callback function after the event is triggered [this function needs to be implemented by ourselves]. The callback function parameter 24 // ev_persist indicates that after the event callback function is executed, the event listenevent will not be removed from the base 25 event_set (& listenevent, serfd, ev_read | ev_persist, onaccept, null); 26 27 // set the corresponding event_base28 event_base_set (base, & listenevent); 29 30 // Add events to the queue (ready queue, active queue, etc.). The queue is managed by event_base corresponding to the event 31 event_add (& listenevent, null ); 32 33 // start round robin 34 event_base_dispatch (base); 35 36 return 0; 37}

Callback function:

1 // The callback function contains three parameters 2 // parameter 1: file descriptor of the event 3 // parameter 2: action of the event ev_read | ev_persist4 // parameter 3: pointer 5 void onaccept (INT serfd, short ievent, void * Arg) 6 {7 // listenevent Event Callback Function 8}



Libevent (1)

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: 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.