Simple implementation of libevent Library

Posted by luxluxlux on Wed, 22 Sep 2021 19:37:28 +0200

1. Definitions
libevent is a lightweight event triggered network library, open source and cross platform, focusing on the underlying network library; It supports a variety of IO multiplexing technologies, timers, asynchronous IO and signal events. libevent API provides a mechanism to call the callback function when the condition of an event occurs
2. Characteristics
libevent is an asynchronous communication model based on "event". Synchronous communication is time-dependent parallel access to prevent sequence disorder. Asynchronous means that the registration time of a function and the return call are not at the same time. It is necessary to wait until a certain condition is met to call the kernel to implement callback.
3.libevent framework
① Create event_base
struct event_base *event_base_new(void);
Each event_ The base structure holds a collection of events that can be detected to determine which event is active. Each event should be represented by base.
② Create event

struct event *event_new(struct event base *base, evutil_socket_t fd, short what, event_callback_fn cb, void *arg)

Add the created event to the base, bind the file descriptor to the event, and realize the monitoring and operation of the event. What describes what the file descriptor needs to monitor: read and write exceptions, monitor the callback callback function after the event call, and arg is the parameter of the callback function
what:
EV_READ one read event
EV_WRITE one write event
EV_PERSIST is triggered continuously in combination with event_ base_ The dispatch function uses
The return value of this function is the successfully created event event
③ Add event
int event_add(struct event *ev, const struct timeval *tv)
ev: event_ Return value of new()
tv: NULL will not timeout until the event is triggered and the callback function will be called
Non 0, waiting period. Check that the event is not triggered. When the time is up, the callback function is still called
④ Start cycle
int event_base_dispatch(struct event_base *base)
event_base_loopexit stops the loop at the specified time
int event_base_loopbreak immediately stops the loop
⑤ Release event
void event_base_free(struct event_base *base)
⑥ Destruction event
int event_free(struct event *ev)
⑦ Other related functions
See what multi-channel IO is supported
const char **event_get_supported_method(void);
View the current multi-channel IO
const char *event_base_get_method(struct event_base *base);
Initialize child process event_base
int event_reinit(struct event_base *base); Unlike the parent process base, there is no need to repeat new_base
Example: fifo reading and writing

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include<event2/event.h>
#include<sys/stat.h>

void read_cd(evutil_socket_t fd, short what, void *arg){
    char buf[1024];
    int len = read(fd, buf, sizeof(buf));
    printf("read from write : %s\n", buf);
    sleep(1);
    return ;
}

int main()
{
    //Create fifo
    unlink("testfifo"); //If the created fifo already exists, delete it
    mkfifo("testfifo", 0664);
    int fd = open("testfifo" , O_RDONLY | O_NONBLOCK);
    struct event_base *base = event_base_new();
    //Create an event and add the event to event_base
    struct event *ev = NULL;
    ev = event_new(base, fd, EV_READ | EV_PERSIST, read_cd, NULL);
    event_add(ev, NULL);
    //Start cycle
    event_base_dispatch(base);
    event_base_free(base);
    return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include<event2/event.h>
#include<sys/stat.h>

void write_cd(evutil_socket_t fd, short what, void *arg){
    char buf[] = "hello world";
    write(fd, buf, strlen(buf)+1);
    sleep(1);
    return ;
}

int main()
{
    //Open fifo
    int fd = open("testfifo" , O_WRONLY | O_NONBLOCK);
    struct event_base *base = event_base_new();
    //Create an event and add the event to event_base
    struct event *ev = NULL;
    ev = event_new(base, fd, EV_WRITE | EV_PERSISIT, write_cd, NULL);
    event_add(ev, NULL);
    //Start cycle
    event_base_dispatch(base);
    event_base_free(base);
    return 0;
}

When an event is written in, it can be triggered and read out

Topics: lua