]>
jfr.im git - irc/quakenet/newserv.git/blob - core/events-poll.c
2 * events.c: the event handling core, poll() version
11 #include "../lib/array.h"
22 * OK, new data structure format for here to make everything
23 * faster. Two fixed static arrays, one of struct pollfds
24 * (for feeding to poll) in no particular order. Second array
25 * has an array of reghandler structs; this is indexed by FD for
29 struct pollfd
*eventfds
;
30 reghandler
*eventhandlers
;
38 /* How many fds are currently registered */
41 void eventstats(int hooknum
, void *arg
);
45 eventadds
=eventdels
=eventexes
=0;
47 eventfds
=(struct pollfd
*)malloc(maxfds
*sizeof(struct pollfd
));
48 memset(eventfds
,0,maxfds
*sizeof(struct pollfd
));
49 eventhandlers
=(reghandler
*)malloc(maxfds
*sizeof(reghandler
));
50 memset(eventhandlers
,0,maxfds
*sizeof(reghandler
));
51 registerhook(HOOK_CORE_STATSREQUEST
, &eventstats
);
56 * Given the number of a new file descriptor, makes sure that the arrays
57 * will be big enough to deal with it.
60 void checkindex(unsigned index
) {
67 while (maxfds
<=index
) {
71 eventfds
=(struct pollfd
*)realloc((void *)eventfds
,maxfds
*sizeof(struct pollfd
));
72 memset(&eventfds
[oldmax
],0,maxfds
-oldmax
);
73 eventhandlers
=(reghandler
*)realloc((void *)eventhandlers
,maxfds
*sizeof(reghandler
));
74 memset(&eventhandlers
[oldmax
],0,maxfds
-oldmax
);
79 * Add an fd to the poll() array.
84 int registerhandler(int fd
, short events
, FDHandler handler
) {
87 /* Check that it's not already registered */
88 if (eventhandlers
[fd
].handler
!=NULL
) {
92 eventhandlers
[fd
].handler
=handler
;
93 eventhandlers
[fd
].fdarraypos
=regfds
;
95 eventfds
[regfds
].fd
=fd
;
96 eventfds
[regfds
].events
=events
;
97 eventfds
[regfds
].revents
=0;
105 * deregisterhandler():
106 * Remove an fd from the poll() array.
108 * The poll() version can't save any time if doclose is set, so
109 * we just do the same work and then close the socket if it's set.
115 int deregisterhandler(int fd
, int doclose
) {
119 /* Check that the handler exists */
120 if (eventhandlers
[fd
].handler
==NULL
)
123 /* We need to rearrange the fds array slightly to handle the delete */
127 oldfdpos
=eventhandlers
[fd
].fdarraypos
;
128 lastreggedfd
=eventfds
[regfds
].fd
;
129 if (lastreggedfd
!=fd
) {
130 /* We need to move the "lastreggedfd" into "oldfdpos" */
131 memcpy(&eventfds
[oldfdpos
],&eventfds
[regfds
],sizeof(struct pollfd
));
132 eventhandlers
[lastreggedfd
].fdarraypos
=oldfdpos
;
136 eventhandlers
[fd
].handler
=NULL
;
137 eventhandlers
[fd
].fdarraypos
=-1;
148 * Call poll() and handle and call appropiate handlers
149 * for any sockets that come up.
154 int handleevents(int timeout
) {
157 for (i
=0;i
<regfds
;i
++) {
158 eventfds
[i
].revents
=0;
161 res
=poll(eventfds
,regfds
,timeout
);
166 for (i
=0;i
<regfds
;i
++) {
167 if(eventfds
[i
].revents
>0) {
168 (eventhandlers
[eventfds
[i
].fd
].handler
)(eventfds
[i
].fd
, eventfds
[i
].revents
);
175 void eventstats(int hooknum
, void *arg
) {
177 long level
=(long) arg
;
180 sprintf(buf
,"Events :%7d fds registered, %7d fds deregistered",eventadds
,eventdels
);
181 triggerhook(HOOK_CORE_STATSREPLY
,(void *)buf
);
182 sprintf(buf
,"Events :%7d events triggered, %6d fds active",eventexes
,regfds
);
183 triggerhook(HOOK_CORE_STATSREPLY
,(void *)buf
);