1 /* event.c -- event management stuff
3 * author: hackbard@hackdaworld.dyndns.org
9 int event_init(t_event *event,int outfd) {
11 dprintf(outfd,"[event] initializing event system ...\n");
17 FD_ZERO(&(event->rfds));
18 FD_ZERO(&(event->rfds_o));
19 FD_ZERO(&(event->wfds));
20 FD_ZERO(&(event->wfds_o));
25 int event_set_timeout(t_event *event,int sec,int usec) {
27 event->timeout.tv_sec=sec;
28 event->timeout.tv_usec=usec;
33 int event_math(int fd,t_event *event,char rw,char ar) {
36 if(rw==READ) FD_SET(fd,&(event->rfds_o));
37 else FD_SET(fd,&(event->wfds_o));
38 if(fd>event->maxfd) event->maxfd=fd;
40 if(rw==READ) FD_CLR(fd,&(event->rfds_o));
41 else FD_CLR(fd,&(event->wfds_o));
42 if(fd==event->maxfd) event->maxfd-=1;
45 dprintf(event->outfd,"[event] %c fd %d to %c file descriptor set\n",
46 ar==ADD?'+':'-',fd,rw==READ?'r':'w');
51 int event_start(t_event *event,void *ptr,
52 int (*callback0)(t_event *event,void *ptr),
53 int (*callback1)(t_event *event,void *ptr)) {
57 struct timeval *tv_ptr;
59 /* switch on event system */
60 event->status=ENABLED;
62 while(event->status&ENABLED) {
64 event->rfds=event->rfds_o;
65 event->wfds=event->wfds_o;
68 if((tv.tv_sec==0)&&(tv.tv_usec==0)) tv_ptr=NULL;
71 if((s_ret=select(event->maxfd+1,&(event->rfds),&(event->wfds),NULL,
73 perror("[event] select call");
78 /* 1+ fd(s) available for reading/writing */
79 if(callback0(event,ptr)==CALLBACK_ERROR) {
80 dprintf(event->outfd,"[event] callback0 returned error\n");
85 /* usual stuff to be executed */
86 if(callback1(event,ptr)==CALLBACK_ERROR) {
87 dprintf(event->outfd,"[event] callback returned error\n");
96 int event_stop(t_event *event) {
98 dprintf(event->outfd,"[event] shutdown\n");
99 event->status=DISABLED;