当前位置:网站首页>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)边栏推荐
- 有个问题想请教下,我想用来同步数据库,但我看他是根据mysql 的binlog同步的,如果是大表,一
- Es- retrieve the selected field from the search
- LVGL 8.2 Tabview & Window
- Only list the data of the specified field GetData ($table, '*', $where, $order)
- GetData table table data join MySQL method
- Opencv4.5.x+cuda11.0.x source code compilation and yolov5 acceleration tutorial!
- How to test cookies
- 暗黑王者|ZEGO 低照度图像增强技术解析
- LVGL 8.2 Slider
- LVGL 8.2 Textarea
猜你喜欢
![[golang from introduction to practice] stone scissors paper game](/img/bb/5a5cdb9228949d263dc7bf62e34d97.png)
[golang from introduction to practice] stone scissors paper game

What are the models of asemi from the manufacturer of rectifier bridge and how about the electroplating process of the manufacturer of rectifier bridge?

Installation and tutorial of MATLAB curling simulation game

Dark king | analysis of zego low illumination image enhancement technology

Openharmony Mengxin contribution Guide

Analytic hierarchy process of MATLAB

Do you really understand images? (machine vision)

Roleapp focuses on Web3 and opens the door of Web3 with the key of "data can be held"

Maker concept design to adapt to popular education

Behind Senma's LP, "Wenzhou Department" is running into the venture capital circle
随机推荐
Debezium series: in depth interpretation of important JMX indicators of debezium
Opencv4.5.x+cuda11.0.x source code compilation and yolov5 acceleration tutorial!
When the development of the meta universe begins to show more and more the style of the Internet, we need to be vigilant
Spirng security (VIII) multiple filter chains coexist
Grafana visual configuration diagram histogram
DNS domain name resolution service
Sony announced the closure of Beijing mobile phone factory! The production line will be moved to Thailand, and the cost can be reduced by half!
[golang from introduction to practice] stone scissors paper game
Es- retrieve the selected field from the search
Current characteristics of steering gear with great resistance
Which securities company do retail investors choose for stock speculation? Is it safe to open an account on your mobile phone
Construction of data center (I): background of the emergence of data center
Beijing University of Posts and telecommunications | RIS assisted in-house multi robot communication system joint deep reinforcement learning
Huawei od maximum consumption JS
Behind Senma's LP, "Wenzhou Department" is running into the venture capital circle
Creativity: presentation of AI oil paintings with high imitation mineral pigments
Properties of trees
Application of AI in testing
JS absolute minimum value of the sum of Huawei od two numbers
Attack and defense world ----- ics-05