Use libevent to write Linux Services (2)

Source: Internet
Author: User

Use libevent to write Linux Services (2)

4. process connections

So far, you have completed event settings, added events, and entered an event loop. But what should I do when an event occurs (connection is established here? Smart users will think of the previously specified callback function accept_handle during event settings. Yes. When the connection is established, the callback function accept_handle is automatically called.

Read/write of the buffer zone is hard to handle in non-blocking network programming. Fortunately, libevent provides bufferevent and evbuf to complete this task for us. Here we use bufferevent for processing.

(1) generate a bufferevent object

Use the bufferevent_new object to generate the bufferevent object, and specify the processing functions and function input parameters for read, write, and connection errors respectively.

(2) set the read volume

After the read event of bufferevent is activated, even if the user has not read the data in the bufferevent buffer,
The bufferevent read event will not be activated again. Because the read event of bufferevent is activated by the read event of the descriptor it monitors, the read event is activated only when the descriptor is readable. You can set wm_read.high to control the amount of data that bufferevent reads from the descriptor buffer.

(3) add events to the event queue

As before, after setting events, you need to add events to the event queue. However, bufferevent has its own special function bufferevent_base_set and function bufferevent_enable.

Bufferevent receives two parameter event base event objects. The former is used to specify the event base to which the event will be added. The latter indicates that the bufferevnet event needs to be added. (In the case of multiple threads, each thread may have its own root event)

After bufferevent Initialization is complete, you can use bufferevent_enable and bufferevent_disable to repeatedly activate and disable events. The receiving parameters are the event object and event flag. The flag parameters are ev_read and ev_write.

Void accept_handle (const int SFD, const short event, void * Arg ){

Struct sockaddr_in ADDR;

Socklen_t addrlen = sizeof (ADDR );

Int FD = accept (SFD, (struct sockaddr *) & ADDR, & addrlen); // process the connection

Buf_ev = bufferevent_new (FD, buffered_on_read, null, null, FD)

Buf_ev-> wm_read.high = 4096

Bufferevent_base_set (main_base, buf_ev );

Bufferevent_enable (buf_ev, ev_read );

}

5 read the buffer

When the buffer zone is ready, the previously registered buffer READ function is automatically activated. We can use the bufferevent_read function to read the buffer zone.

The bufferevent_read function parameters are the event buffer to be read, the location where data is read, and the number of bytes to be read. The number of bytes actually read.

Note: If the buffer zone is not read in time, the event will not be activated again (unless there is data again ). Therefore, it is necessary to read the data repeatedly until all the data is read.

6. write back to the client

Bufferevent functions not only support reading the buffer, but also support writing the buffer (return the result to the client ).

Void buffered_on_read (struct bufferevent * Bev, void * Arg ){

Char buffer [4096]

Ret = bufferevent_read (BEV, & buffers, 4096 );

Bufferevent_write (BEF, (void *) & bufferevent_write, 4096 );

}

Conclusion 3

So far, we can use libevent to write a non-blocking event driver server. It supports connection establishment, socket readable, and other event processing.

However, in an event-driven server, a thread is usually used to process connections and multiple threads are used to process requests. I will continue to introduce how to use libevent To Write multi-threaded servers.

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.