# UEvent **Repository Path**: hevake_lcj/UEvent ## Basic Information - **Project Name**: UEvent - **Description**: 极小型事件驱动模块 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2018-02-03 - **Last Updated**: 2022-03-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # UEvent UEvent是一个极简单的事件驱动模块。 它只实现了 **文件描符** 与 **定时器** 驱动两种功能,适用于硬件资源较为紧缺的Linux平台。 依赖库: - ptr_vector https://gitee.com/hevake_lcj/ptr_vector.git ## 造轮子起因 为什么要造这个轮子?是libevent, libuv 不好用吗?不是的,是因为用不了。 在嵌入式Linux平台上,我们需要多路并发的开发要求。需要同时监控:网络socket、串口数据、SPI数据、GPIO事件。 嵌入式本台有一个共性:资源非常紧缺。如:存储空间为8MB,运行空间为16MB。在这种“艰苦”的环境下,引入任何一个库都要考虑空间大小。libevent作为被公认的库,功能很齐全,但体量非常庞大,难于取舍。 于是我们需要一个只实现事件驱动基本功能的库。不要求支持多线程、多平台,仅仅是事件驱动。 这个库没有使用复杂的小堆排序实现定时器,也不需要用高性能的epoll实现并发。于是,本人造了UEvent。 ## 1.接口函数 ### 1.1 EventLoop对象的创建与销毁 ``` UEventLoop* UEvent_NewLoop(); UEventRetCode UEvent_RunLoop(UEventLoop *loop); UEventRetCode UEvent_BreakLoop(UEventLoop *loop); UEventRetCode UEvent_DeleteLoop(UEventLoop *loop); ``` ### 1.2 文件描述符操作 ``` UEventRetCode UEvent_ReadFileDesc(UEventLoop *loop, int fd, UEventFileDescDetail *detail); UEventRetCode UEvent_UpdateFileDesc(UEventLoop *loop, int fd, const UEventFileDescDetail *detail); UEventRetCode UEvent_RemoveFileDesc(UEventLoop *loop, int fd); ``` ### 1.3 定时器操作 ``` UEventRetCode UEvent_RunAt (UEventLoop *loop, int timer_id, uint32_t execute_time_ms, const UEventTimerDetail *detail); UEventRetCode UEvent_RunAfter(UEventLoop *loop, int timer_id, uint16_t wait_time_ms, const UEventTimerDetail *detail); UEventRetCode UEvent_RunEvery(UEventLoop *loop, int timer_id, uint32_t interval_ms, const UEventTimerDetail *detail); UEventRetCode UEvent_RunTimer(UEventLoop *loop, int timer_id, uint32_t wait_time_ms, uint32_t interval_ms, const UEventTimerDetail *detail); UEventRetCode UEvent_StopTimer(UEventLoop *loop, int timer_id); ``` ## 2.示例 ### 2.1 文件描述符 ``` void _FDProc(int timer_id, void *arg) { char tmp[11] = {0}; int rsize = read(fd, tmp, 10); printf("stdio input --> len:%d, data:[%s]\n", rsize, tmp); } int TestFD() { UEventLoop *loop = UEvent_NewLoop(); UEventFileDescDetail detail = {_FDProc, NULL}; UEvent_UpdateFileDesc(loop, STDIN_FILENO, &detail); UEvent_RunLoop(loop); UEvent_DeleteLoop(loop); } ``` ### 2.2 定时器 ``` void _TimerProc(int timer_id, void *arg) { puts("==timer=="); } int TestTimer() { UEventLoop *loop = UEvent_NewLoop(); UEventTimerDetail detail = {_TimerProc, NULL}; UEvent_RunEvery(loop, 1, 1000, &detail); UEvent_RunLoop(loop); UEvent_DeleteLoop(loop); } ``` 完整示例,请参考 example/