[Libevent] Reactor design mode, libeventreactor

Source: Internet
Author: User

[Libevent] Reactor design mode, libeventreactor

The design mode of the object behavior class to sort and dispatch synchronization events. Alias: Dispatcher) 

The Reactor mode is a common mode for processing concurrent I/O. It is used to synchronize I/O,The central idea is to register all the I/O events to be processed on a central I/O multiplexing, and the main thread is congested on the multiplexing; once an I/O event arrives or is ready(The difference is whether the Multiplex is edge-triggered or horizontal-triggered ),The multiplexing returns and distributes the corresponding I/O events to the corresponding processor.

Reactor event handling mechanism

Common function call Mechanism: The program calls a function-> function execution, the program waits-> the function returns the result and control to the program-> the program continues to process. The so-calledEvent-driven, simply put, refers to what buttons you click (that is, what events are generated) And what operations the computer performs (that is, what functions are called ).

Event-driven model

 

Reactor Definition"Reactors" are an event-driven mechanism.Unlike common function calls, an application does not actively call an API to complete processing. Instead, the Reactor reverses the event processing process, the application must provide the corresponding interfaces and register them with the Reactor. If the corresponding time occurs, the Reactor will actively call the interfaces registered by the application. These interfaces are also called "Callback functions ". When Libevent is used, it also registers corresponding events and callback functions to the Libevent framework, libevent calls these callback functions to process relevant events (I/O read/write, timing, and signals ).

The Reactor mode is very similar to the Observer mode in some aspects: When a subject changes, all dependent bodies are notified. However, the observer mode is associated with a single event source, while the reactor mode is associated with multiple event sources. 

Advantages of Reactor Mode

The Reactor mode isOne of the essential technologies for writing High-Performance Network ServersIt has the following advantages:

Reactor mode framework

The following components are required to use the Reactor model:Event source, Reactor framework, multiplexing mechanism, and event processing program.

Overall Reactor model framework


Reactor Model UML

 

  • Handle event Source

Handle represents the resources managed by the operating system, including network links, opened files, timers, and synchronization objects. Linux is a file descriptor, while Windows is a Socket or Handle, which is called a "Handle set". A program registers events of interest on the specified Handle, such as I/O events.

  • Event demultiplexer-multi-channel event distribution mechanism

I/O multiplexing mechanisms provided by the operating system, such as select and epoll. The program first registers the handle (event source) and its event to event demultiplexer;

When an event arrives, event demultiplexer will send a notification "in the registered handle set, one or more handle events are ready". After the program receives the notification, you can process the event without blocking it.It still corresponds to libevent, such as select, poll, and epoll. However, libevent uses the structure eventop for encapsulation and uses a unified interface to support these I/O multiplexing mechanisms, to hide the underlying system mechanism.

The event splitter is provided by the operating system. In linux, it is generally called by systems such as select, poll, and epoll, and waits for the event to occur in a Handle set. Accept the client connection, establish the Event Handler of the corresponding client, and register the Event processor with the Event distributor.

  • Reactor -- Reactor

A Reactor is an interface for event management. It uses event demultiplexer to register and deregister events internally, and runs an event loop. When an event enters the "ready" status, call the callback function of the registration event to process the event.

Interfaces are provided: Register, delete, and dispatch Event Handler. Event Demultiplexer waits for the Event to occur. When a new Event is detected, the Event is handed over to Initiation Dispatcher, which calls back the Event Handler.

Corresponding to the libevent, which is the event_base struct.A typical Reactor declaration method

class Reactor { public: int register_handler(Event_Handler *pHandler, int event); int remove_handler(Event_Handler *pHandler, int event); void handle_events(timeval *ptv); // ... }; 

  • Event Handler -- Event Handler

The event handler provides a set of interfaces, each of which corresponds to a type of event for the Reactor to call when the corresponding event occurs and execute the corresponding event processing. Usually it binds a valid handle.

The event processor is responsible for processing functions of specific events. Generally, there will be further layers based on the Basic Handler to abstract processes such as decode, process, and encoder. For example, for Web Server, decode is usually used to parse HTTP requests. The process further involves the call of Listner and Servlet. To simplify the design, Event Handler is usually designed as a state machine and implemented according to the GoF state pattern.Corresponds to the libevent, which is the event struct.The following two typical Event Handler class declaration methods have advantages and disadvantages for each other.

class Event_Handler { public: virtual void handle_read() = 0; virtual void handle_write() = 0; virtual void handle_timeout() = 0; virtual void handle_close() = 0; virtual HANDLE get_handle() = 0; // ... }; class Event_Handler { public: // events maybe read/write/timeout/close .etc virtual void handle_events(int events) = 0; virtual HANDLE get_handle() = 0; // ... }; 
  • Concrete Event Handler

Inherit the class above to implement the Hook method. The application registers the Concrete Event Handler to the Reactor and waits for the Event to be processed. When an event occurs, these methods are called back.

Use Cases

Scenario: When a coach is on the road, someone gets on the bus and gets off the bus, but the passenger always hopes to have a rest on the coach.

Traditional Practice: At intervals (or on every station), the driver or conductor asks each passenger whether to get off the bus.

Reactor practice: a car is the subject of a passenger's access (Reactor). After a passenger gets on the bus, he registers with the conductor (acceptor). Then the passenger can rest and go to bed, after arriving at the passenger's destination, the conductor can wake it up.

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.