0x00 在ubuntu 26.04中简单学习 libevent
#include <event2/event.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void cb_func(evutil_socket_t fd, short what, void *arg)
{
const char * data = arg;
printf("Got an event on socket %d:%s:%s:%s:%s [%s]\n", (int)fd,
(what & EV_TIMEOUT) ? " timeout" : "",
(what & EV_READ) ? "read" : "" ,
(what & EV_WRITE) ? "write" : "",
(what & EV_SIGNAL) ? "signal" : "",
data);
}
void main_loop(evutil_socket_t fd1, evuitil_socket_t fd2)
{
struct event * ev1, *ev2;
struct timeval five_seconds[] = {5 , 0};
struct event_base * base = event_base_new();
ev1 = event_new(base, fd1, EV_TIMEOUT|EV_READ|EV_PERSIST, cb_func,
(char * )"Reading event");
ev2 = event_new(base, fd2, EV_WRITE, cb_func,
(char *)"Writeing event");
event_add(ev1, &five_seconds);
event_add(ev2, NULL);
event_base_dispatch(base);
}
int main(void)
{
const char * fifo = "event.fifo";
int socket1, socket2;
unlink(fifo);
if(mkfifo(fifo, 0644) == -1){
perror("mkfifo");
exit(1);
}
socket1 = open(fifo, O_RDONLY|O_NONBLOCK);
socket2 = open(fifo, O_WRONLY|O_NONBLOCK);
main_loop(socket1, socket2);
return 0;
}
输出结果:
Got an event on socket 4:::wirte: [Writeing event]
Got an event on socket 3: timeout::: [Reading event]