文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>libevent的简单使用

libevent的简单使用

时间:2010-09-12  来源:itlanger

Libevent 是一个轻量级的开源高性能网络库,用C语言编写。
其特点有:
  • 事件驱动(event-driven),高性能;
  • 轻量级,专注于网络,不如 ACE 那么臃肿庞大;
  • 源代码相当精炼、易读;跨平台,支持 Windows、Linux、*BSD 和 Mac Os;
  • 支持多种 I/O 多路复用技术, epoll、poll、dev/poll、select 和 kqueue 等;
  • 支持 I/O,定时器和信号等事件;
  • 注册事件优先级;
一. 安装:
sudo yum install libevent
或者去http://monkey.org/~provos/libevent/下载原代码:
./configure --prefix=/usr
make && make install进行安装

二. 下面是三个简单的使用实例,参照 libevent/samples目录下的实例来的。
1. 最简单的timer事件:


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <event.h>

//compile: gcc -o etimer etimer.c -Wall -g -levent -lrt

void on_timer(int fd, short type, void *args)
{
    struct event *timer_event = (struct event *)args;
    printf("on_timer function...\n");
    printf("timer handles ... \n");
    struct timeval tv;
    tv.tv_sec = 2;
    tv.tv_usec = 0;
    
    event_add(timer_event, &tv);
    
    return ;
}
int main()
{
    struct event_base *base = event_init();
    struct event timer_event;
    struct timeval tv;
    tv.tv_sec = 2;
    tv.tv_usec = 0;

    int fd = -1;
    int type = 0;

    evtimer_set(&timer_event, on_timer, &timer_event);
    //event_set(&timer_event, fd, type, on_timer, &timer_event);
    event_add(&timer_event, &tv);
    //event_base_dispatch(base);
    event_dispatch();
    
    return 0;
}


2. signal信号处理(以SIGINT为例,键盘Ctrl-C即可产生INTERRUPT信号)


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <event.h>

void signal_cb(int fd, short type, void *arg)
{
    struct event *signal_event = (struct event *)arg;
    static int count = 0;
    printf("in function signal_cb...\n");
    printf("handle signal: %d\n", EVENT_SIGNAL(signal_event));
    if (count >= 3)
        event_del(signal_event);
    count ++;
}

int main()
{
    struct event signal_event;
    event_init();
    event_set(&signal_event, SIGINT, EV_SIGNAL|EV_PERSIST, signal_cb, &signal_event);
    event_add(&signal_event, NULL);
    event_dispatch();
    
    return 0;
}


3. IO事件处理

/*
 * Compile with:
 * cc -I/usr/local/include -o event-test event-test.c -L/usr/local/lib -levent
 */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/queue.h>
#include <unistd.h>
#include <sys/time.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <event.h>

void fifo_read(int fd, short event, void *arg)
{
    char buf[255];
    int len;
    struct event *ev = arg;

    /* Reschedule this event */
    event_add(ev, NULL);

    fprintf(stderr, "fifo_read called with fd: %d, event: %d, arg: %p\n",
        fd, event, arg);

    len = read(fd, buf, sizeof(buf) - 1);

    if (len == -1) {
        perror("read");
        return;
    } else if (len == 0) {
        fprintf(stderr, "Connection closed\n");
        return;
    }

    buf[len] = '\0';
    fprintf(stdout, "Read: %s\n", buf);
}

int
main (int argc, char **argv)
{
    struct event evfifo;
    struct stat st;
    char *fifo = "event.fifo";
    int socket;
 
    if (lstat (fifo, &st) == 0) {
        if (S_ISREG(st.st_mode)){
            errno = EEXIST;
            perror("lstat");
            exit (1);
        }
    }

    unlink (fifo);
    if (mkfifo (fifo, 0600) == -1) {
        perror("mkfifo");
        exit (1);
    }

    /* Linux pipes are broken, we need O_RDWR instead of O_RDONLY */
    socket = open (fifo, O_RDWR | O_NONBLOCK, 0);

    if (socket == -1) {
        perror("open");
        exit (1);
    }

    fprintf(stderr, "Write data to %s\n", fifo);
    /* Initalize the event library */
    event_init();
    /* Initalize one event */
    event_set(&evfifo, socket, EV_READ, fifo_read, &evfifo);
    /* Add it to the active events, without a timeout */
    event_add(&evfifo, NULL);
    
    event_dispatch();
    return (0);
}

//运行时需要在另一个shell中向fifo写入数据: echo "aaa" > event.fifo


注意: 如不加-lrt会出现链接错误:

gcc -o event-test event-test.c -Wall -g -levent
/usr/lib/gcc/i686-redhat-linux/4.4.4/../../../libevent.a(event.o): In function `gettime':
(.text+0x2a7): undefined reference to `clock_gettime'
/usr/lib/gcc/i686-redhat-linux/4.4.4/../../../libevent.a(event.o): In function `event_base_new':
(.text+0x58a): undefined reference to `clock_gettime'
collect2: ld 返回 1


还有,安装到/usr目录下就不要用-I和-L了

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载