当前位置:网站首页>Libenent and libev
Libenent and libev
2022-07-25 04:29:00 【HjasnJH】
libevent and libev All are c Language implementation of asynchronous event library ; By registering asynchronous events , The library detection event triggers , Thus, the library is based on
Sequence of events , Call the corresponding callback function for processing ;
Events include : The Internet io event , Timing events , Signal event ;
The event loop : Wait and distribute events ; Used to manage events ;
libevent and libev It mainly encapsulates the interaction between asynchronous event library and operating system ; Let users not pay attention to the differences of platforms , Just wait
Specific handling of hand events ;
libevent and libev Yes window The support is quite poor , So there is libuv library ,libuv be based on libev, however window
It's sealed with iocp;
difference
Starting from the design concept ,libev It's for improvement libevent Some architectural decisions in , for example , The use of global variables makes it possible to
It is difficult to use safely in a threaded environment libevent,watcher The data structure design of is too large , Because they will I/O、 Time and letter
The processing number is placed in a structure , Additional components such as http 、dns、openssl, Due to the poor implementation quality of the server and
The security problems arising from this , The timer is not accurate , Can't handle time events well .
libev By not using global variables , Instead, the context is passed by passing parameters to all callback functions ; And according to different event types
Build different data structures , This reduces the coupling of events ;
libev Small and efficient ; Only focus on event handling ;
libevent The main interface of
// initialization libevent; To understand epoll_create
struct event_base *event_base_new(void);
// Create an event , initialization event And the corresponding callback function
struct event * event_new(struct event_base *base, evutil_socket_t fd, short events, void
(*cb)(evutil_socket_t, short, void *), void *arg)
// Set up events
void event_set(struct event *ev, evutil_socket_t fd, short events,
void (*callback)(evutil_socket_t, short, void *), void *arg)
// establish event And event_base The mapping relation of
int event_base_set(struct event_base *eb, struct event *ev);
// Registration events , Including time events ; amount to epoll_ctl;
int event_add(struct event *ev, const struct timeval *tv)
// The cancellation of the event
int event_del(struct event *ev)
// Enter event loop
int event_base_loop(struct event_base *base, int flags)Libev
libev The main data structure of
EV_WATCHER
/* shared by all watchers */
#define EV_WATCHER(type) \\
int active; /* Express watcher Is it active ,active = 1 It means it hasn't been stop fall */ \\
int pending; /* Storage watcher stay pendings Index in . Greater than zero indicates that it has not been processed .
* watcher After the callback function of is called , Will be set to 0. */ \\
int priority; /* events based priority */ \\
void *data; /* The data required by the callback function */ \\
void (*cb)(EV_P_ struct type *w, int revents); /* Callback function */
effect : Common information of different event types .ev_io
typedef struct ev_io
{
EV_WATCHER_LIST (ev_io)
int fd;
int events;
} ev_io;
// effect : Record IO Basic information of the event .
// ev_io comparison ev_watcher Added next, fd, events Properties of .ev_loop
struct ev_loop {
double ev_rt_now; /* The current timestamp */
int backend; /* Which multiplexing method is adopted , e.g. SELECT/POLL/EPOLL */
int activecnt; /* total number of active events ("refcount") */
int loop_done; /* Flag of the end of the event cycle ,signal by ev_break */
int backend_fd; /* e.g. epoll fd, created by epoll_create*/
void (*backend_modify)(EV_P_ int fd, int oev, int nev)); /* Corresponding epoll_ctl */
void (*backend_poll)(EV_P_ ev_tstamp timeout)); /* Corresponding epoll_wait */
void (*invoke_cb)(struct ev_loop *loop);
ANFD *anfds; /* After the initialization of the ev_io The structure is bound to anfds[fd].head On the event list , Convenient basis fd
Direct search .*/
int *fdchanges; /* Storage needs epoll Monitoring fd */
ANPENDING *pendings [NUMPRI]; /* Store waiting to be called callback Of watcher */
} /
/ effect : It basically includes loop All the information needed for the cycle , To make the annotation easier to understand epoll To illustrate
libev Main interface :
// initialization watcher Of fd/events/callback
#define ev_io_init(ev,cb,fd,events) do { ev_init ((ev), (cb)); ev_io_set ((ev),
(fd),(events)); } while (0)
// Register and bind io watcher To ev_loop
void ev_io_start(struct ev_loop *loop, ev_io *w)
// Register and bind timer watcher To ev_loop
void ev_timer_start(struct ev_loop *loop, ev_timer *w)
// Open change ev_loop The event loop of
int ev_run(struct ev_loop *loop, int flags)边栏推荐
- Installation and tutorial of MATLAB curling simulation game
- Numpy overview
- ADS1256 debugging notes based on stm32hal Library
- Debezium series: in depth interpretation of important JMX indicators of debezium
- Penetration test target combat SQL injection getshell
- Kubesphere 3.3.0 offline installation tutorial
- 有个问题想请教下,我想用来同步数据库,但我看他是根据mysql 的binlog同步的,如果是大表,一
- 今天很重要
- EMQ Yingyun technology was successfully selected into the 2022 "cutting edge 100" list of Chinese entrepreneurs
- 实战|记一次攻防演练打点
猜你喜欢

Application of AI in testing

RGB and SATA function switching module based on Quanzhi rk3568j

LVGL 8.2 Span

数据链路层协议 ——— 以太网协议
![Function and technical principle of data desensitization [detailed explanation]](/img/bb/5890d8fd140982ea7b994942093cc7.png)
Function and technical principle of data desensitization [detailed explanation]

Maker concept design to adapt to popular education

Construction of data center (I): background of the emergence of data center

Spirng security (VIII) multiple filter chains coexist

Open source summer interview | "after 00" PMC member Bai Zeping

Method of setting document comments in idea (graphic version)
随机推荐
暗黑王者|ZEGO 低照度图像增强技术解析
Swagger simple quick start tutorial
Bubble mart's market value evaporated by HK $21billion in seven days, which can't be sold in China, and its future at sea is uncertain
LVGL 8.2 Message box
C language: string processing function
Network engineering case: integrated network design of CII company
[golang from introduction to practice] poker licensing game
13. Talk about the expiration strategy of redis
Xrrunner, a domestic performance testing tool for palm smart, officially unveiled qecon
Optimize bubble sorting
GetData table table data join MySQL method
运筹学基础【一】 之 导论
Permanently solve Matplotlib Chinese garbled code
Press the switch for the minimum number of times to turn on all lights
Analyze the exploration in high-quality steam Education
C language: Escape Character
Maker concept design to adapt to popular education
Dry goods | Ctrip Hongmeng application development practice
自然的状态最好
Chart is code: build a new generation of graphics library in a coded way -- feekin