Code Monkey home page Code Monkey logo

libevproxy's Introduction

libevproxy

libevproxy是一个用C语言编写的,基于reactor模式的事件驱动库,目前只支持Linux平台。支持I/O多路复用技术,不过目前只支持epoll和select。 支持各种不同类型的事件,目前支持的有I/O事件,定时器事件,信号事件和用户自定义事件。

livevproxy使用

创建一个事件代理

首先我们创建一个事件代理,这个代替就是负责管理各种事件的角色。

#include "event.h"
event_proxy *proxy=createEventProxy(setsize);

参数setsize表示这个代理默认管理的事件的个数的大小,当然如果后续添加的事件超过这个大小,代理会自动管理的。而不用重新设置大小。

创建一个事件

第二步就是创建我们需要的事件类型

// 创建一个I/O事件,fd为I/O事件对应的文件描述符
event_base *event=createEventFile(proxy,fd);
// 创建一个定时器事件,ms为定时的时间
event_base *event=createEventTimeout(proxy,ms);
// 创建一个信号事件,signo为对应的信号,sigset为信号事件触发的时候需要阻塞的信号集
event_base *event=createEventSignal(proxy,signo,sigset);
// 创建一个用户自定义事件,maxtimes为最多触发的次数,triggerProc为用户自定义的触发函数,clientData为传给triggerProc的参数
event_base *event=createEventUser(proxy,maxtimes,triggerProc,clientData);
注册事件

创建完一个事件后,接着就是注册到代理中了

// 注册I/O事件,mask为需要触发的事件类型,如可读或可写。fileProc为事件发生时执行的处理函数,clientData为传给fileProc的参数
addFileEvent(proxy,event,mask,fileProc,clientData);
//注册信号事件,signaProc为信号事件发生时的处理函数
addSignalEvent(proxy,event,signalProc,clientData);
// 注册一个定时器事件,timeProc为定时器事件发生时的处理函数
addTimeoutEvent(proxy,event,timeProc,*clientData);
// 注册一个用户自定义事件,userProc为事件发生时的处理函数,cleanProc为清理函数
addUserEvent(proxy,ev,userProc,cleanProc,clientData);
启动代理

最后一步就是启动代理了,这之后所有的事件就会被代理监控,如果发生了,就会执行注册时的函数

eventLoop(proxy);

目前该事件库还在完善中,如果有兴趣的同学可以一起完善。如果有问题可以联系我。

libevproxy's People

Contributors

longxq avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.