1 /***********************************************************************
2 *
3 * event.h
4 *
5 * Abstraction of select call into "event-handling" to make programming
6 * easier.
7 *
8 * Copyright (C) 2001-2003 Roaring Penguin Software Inc.
9 *
10 * This program may be distributed according to the terms of the GNU
11 * General Public License, version 2 or (at your option) any later version.
12 *
13 ***********************************************************************/
14 
15 #define DEBUG_EVENT
16 
17 #ifndef INCLUDE_EVENT_H
18 #define INCLUDE_EVENT_H 1
19 
20 struct EventSelector_t;
21 
22 /* Callback function */
23 typedef void (*EventCallbackFunc)(struct EventSelector_t *es,
24 				 int fd, unsigned int flags,
25 				 void *data);
26 
27 #include "eventpriv.h"
28 
29 /* Create an event selector */
30 extern EventSelector *Event_CreateSelector(void);
31 
32 /* Destroy the event selector */
33 extern void Event_DestroySelector(EventSelector *es);
34 
35 /* Handle one event */
36 #ifdef EVENT_USE_POLL
37 extern int Event_HandleEventUsingPoll(EventSelector *es);
38 #define Event_HandleEvent Event_HandleEventUsingPoll
39 #else
40 extern int Event_HandleEventUsingSelect(EventSelector *es);
41 #define Event_HandleEvent Event_HandleEventUsingSelect
42 #endif
43 
44 /* Add a handler for a ready file descriptor */
45 extern EventHandler *Event_AddHandler(EventSelector *es,
46 				      int fd,
47 				      unsigned int flags,
48 				      EventCallbackFunc fn, void *data);
49 
50 /* Add a handler for a ready file descriptor with associated timeout*/
51 extern EventHandler *Event_AddHandlerWithTimeout(EventSelector *es,
52 						 int fd,
53 						 unsigned int flags,
54 						 struct timeval t,
55 						 EventCallbackFunc fn,
56 						 void *data);
57 
58 
59 /* Add a timer handler */
60 extern EventHandler *Event_AddTimerHandler(EventSelector *es,
61 					   struct timeval t,
62 					   EventCallbackFunc fn,
63 					   void *data);
64 
65 /* Delete a handler */
66 extern int Event_DelHandler(EventSelector *es,
67 			    EventHandler *eh);
68 
69 /* Retrieve callback function from a handler */
70 extern EventCallbackFunc Event_GetCallback(EventHandler *eh);
71 
72 /* Retrieve data field from a handler */
73 extern void *Event_GetData(EventHandler *eh);
74 
75 /* Set callback and data to new values */
76 extern void Event_SetCallbackAndData(EventHandler *eh,
77 				     EventCallbackFunc fn,
78 				     void *data);
79 
80 extern int Event_EnableDebugging(char const *fname);
81 
82 extern void set_cloexec(int fd);
83 extern int set_nonblocking(int fd);
84 
85 #ifdef DEBUG_EVENT
86 extern void Event_DebugMsg(char const *fmt, ...);
87 #define EVENT_DEBUG(x) Event_DebugMsg x
88 #else
89 #define EVENT_DEBUG(x) ((void) 0)
90 #endif
91 
92 /* Flags */
93 #define EVENT_FLAG_READABLE 1
94 #define EVENT_FLAG_WRITEABLE 2
95 #define EVENT_FLAG_WRITABLE EVENT_FLAG_WRITEABLE
96 
97 /* This is strictly a timer event */
98 #define EVENT_FLAG_TIMER 4
99 
100 /* This is a read or write event with an associated timeout */
101 #define EVENT_FLAG_TIMEOUT 8
102 
103 #define EVENT_TIMER_BITS (EVENT_FLAG_TIMER | EVENT_FLAG_TIMEOUT)
104 #endif
105