码迷,mamicode.com
首页 > 其他好文 > 详细

监控端主程序

时间:2019-11-23 12:25:46      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:amp   else   std   主程序   sys   初始   clu   str   fcntl   

#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include <fcntl.h>
#include <sys/epoll.h>

#include <main.h>

struct event_ext
{
int fd;
bool epolled;
uint32_t events;
void (*handler)(int fd,void *arg);
void *arg;
};

struct event_ext *epoll_event_create(int fd,uint32_t type,void (*handler)(int,void *), void *arg)
{
struct event_ext *e = calloc(1,sizeof(struct event_ext));

e->fd = fd;
e->events = type;
e->handler = handler;
e->arg = arg;

return e;
}

int epoll_add_event(int epfd,struct event_ext *ev)
{
struct epoll_event epv;
int op;

//2. 初始化epoll_event(将附加结构挂载到epoll_event)
epv.data.ptr = ev;
epv.events = ev->events;

if (ev->epolled)
{
op = EPOLL_CTL_MOD;
}
else
{
op = EPOLL_CTL_ADD;
ev->epolled = true;
}
//3. 将epoll_event加入epoll
epoll_ctl(epfd,op,ev->fd,&epv);

return 0;
}

int epoll_del_event(int epfd,struct event_ext *ev)
{

epoll_ctl(epfd,EPOLL_CTL_DEL,ev->fd,NULL);
ev->epolled = false;
return 0;
}

void epoll_event_free(struct event_ext * ev)
{
struct event_ext *e = ev;
free(e);
}


int main()
{
struct epoll_event events[512];
int fds;
int i;
uint32_t event;
struct event_ext *e;

srv_main = calloc(1,sizeof(struct server));

//1. 创建epoll
srv_main->epfd = epoll_create(512);

//2. 加入等待事件
srv_main->cam = cam_sys_init();
srv_main->srv = net_sys_init();

//3. 等待事件发生且处理
while (1)
{
fds = epoll_wait(srv_main->epfd,events,512,1000);

for(i=0;i<fds;i++)
{
event = events[i].events;
e = events[i].data.ptr;

if ((event&EPOLLIN)&&(e->events&EPOLLIN))
e->handler(e->fd,e->arg);

if ((event&EPOLLOUT)&&(e->events&EPOLLOUT))
e->handler(e->fd,e->arg);

if ((event&EPOLLERR)&&(e->events&EPOLLERR))
e->handler(e->fd,e->arg);
}
}

return -1;
}

监控端主程序

标签:amp   else   std   主程序   sys   初始   clu   str   fcntl   

原文地址:https://www.cnblogs.com/free-1122/p/11916853.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!