]>
Commit | Line | Data |
---|---|---|
b46b1d59 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/private/fdiodispatcher.h | |
3 | // Purpose: classes for dispatching IO notifications for file descriptors | |
4 | // Authors: Lukasz Michalski | |
5 | // Created: December 2006 | |
6 | // Copyright: (c) Lukasz Michalski | |
7 | // RCS-ID: $Id$ | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifndef _WX_PRIVATE_FDIODISPATCHER_H_ | |
12 | #define _WX_PRIVATE_FDIODISPATCHER_H_ | |
13 | ||
14 | #include "wx/hashmap.h" | |
15 | ||
16 | // handler used to process events on descriptors | |
17 | class wxFDIOHandler | |
18 | { | |
19 | public: | |
20 | // called when descriptor is available for non-blocking read | |
21 | virtual void OnReadWaiting() = 0; | |
22 | ||
23 | // called when descriptor is available for non-blocking write | |
24 | virtual void OnWriteWaiting() = 0; | |
25 | ||
26 | // called when there is exception on descriptor | |
27 | virtual void OnExceptionWaiting() = 0; | |
28 | ||
251e98cb VZ |
29 | // called to check if the handler is still valid, only used by |
30 | // wxSocketImplUnix currently | |
31 | virtual bool IsOk() const { return true; } | |
32 | ||
b46b1d59 VZ |
33 | // virtual dtor for the base class |
34 | virtual ~wxFDIOHandler() { } | |
35 | }; | |
36 | ||
37 | // those flags describes sets where descriptor should be added | |
38 | enum wxFDIODispatcherEntryFlags | |
39 | { | |
40 | wxFDIO_INPUT = 1, | |
41 | wxFDIO_OUTPUT = 2, | |
42 | wxFDIO_EXCEPTION = 4, | |
43 | wxFDIO_ALL = wxFDIO_INPUT | wxFDIO_OUTPUT | wxFDIO_EXCEPTION | |
44 | }; | |
45 | ||
ad8d42f8 VZ |
46 | // base class for wxSelectDispatcher and wxEpollDispatcher |
47 | class WXDLLIMPEXP_BASE wxFDIODispatcher | |
48 | { | |
49 | public: | |
50 | enum { TIMEOUT_INFINITE = -1 }; | |
51 | ||
5e1eac14 VZ |
52 | // return the global dispatcher to be used for IO events, can be NULL only |
53 | // if wxSelectDispatcher wasn't compiled into the library at all as | |
54 | // creating it never fails | |
55 | // | |
56 | // don't delete the returned pointer | |
57 | static wxFDIODispatcher *Get(); | |
58 | ||
59 | // if we have any registered handlers, check for any pending events to them | |
60 | // and dispatch them -- this is used from wxX11 and wxDFB event loops | |
61 | // implementation | |
62 | static void DispatchPending(); | |
63 | ||
ad8d42f8 VZ |
64 | // register handler for the given descriptor with the dispatcher, return |
65 | // true on success or false on error | |
66 | virtual bool RegisterFD(int fd, wxFDIOHandler *handler, int flags) = 0; | |
67 | ||
68 | // modify descriptor flags or handler, return true on success | |
69 | virtual bool ModifyFD(int fd, wxFDIOHandler *handler, int flags) = 0; | |
70 | ||
af57c51a VZ |
71 | // unregister descriptor previously registered with RegisterFD() |
72 | virtual bool UnregisterFD(int fd) = 0; | |
ad8d42f8 | 73 | |
a12698ab VZ |
74 | // check if any events are currently available without dispatching them |
75 | virtual bool HasPending() const = 0; | |
76 | ||
5a557d1e | 77 | // wait for an event for at most timeout milliseconds and process it; |
a12698ab VZ |
78 | // return the number of events processed (possibly 0 if timeout expired) or |
79 | // -1 if an error occurred | |
80 | virtual int Dispatch(int timeout = TIMEOUT_INFINITE) = 0; | |
ad8d42f8 VZ |
81 | |
82 | virtual ~wxFDIODispatcher() { } | |
83 | }; | |
84 | ||
85 | //entry for wxFDIOHandlerMap | |
b46b1d59 VZ |
86 | struct wxFDIOHandlerEntry |
87 | { | |
88 | wxFDIOHandlerEntry() | |
89 | { | |
90 | } | |
91 | ||
92 | wxFDIOHandlerEntry(wxFDIOHandler *handler_, int flags_) | |
93 | : handler(handler_), | |
94 | flags(flags_) | |
95 | { | |
96 | } | |
97 | ||
98 | wxFDIOHandler *handler; | |
99 | int flags; | |
100 | }; | |
101 | ||
102 | // this hash is used to map file descriptors to their handlers | |
103 | WX_DECLARE_HASH_MAP( | |
104 | int, | |
105 | wxFDIOHandlerEntry, | |
106 | wxIntegerHash, | |
107 | wxIntegerEqual, | |
108 | wxFDIOHandlerMap | |
109 | ); | |
110 | ||
ad8d42f8 VZ |
111 | // FDIODispatcher that holds map fd <-> FDIOHandler, this should be used if |
112 | // this map isn't maintained elsewhere already as it is usually needed anyhow | |
b46b1d59 | 113 | // |
ad8d42f8 | 114 | // notice that all functions for FD management have implementation |
b46b1d59 | 115 | // in the base class and should be called from the derived classes |
af57c51a VZ |
116 | class WXDLLIMPEXP_BASE wxMappedFDIODispatcher : public wxFDIODispatcher |
117 | { | |
b46b1d59 | 118 | public: |
b46b1d59 VZ |
119 | // find the handler for the given fd, return NULL if none |
120 | wxFDIOHandler *FindHandler(int fd) const; | |
af57c51a | 121 | |
b46b1d59 VZ |
122 | // register handler for the given descriptor with the dispatcher, return |
123 | // true on success or false on error | |
ad8d42f8 | 124 | virtual bool RegisterFD(int fd, wxFDIOHandler *handler, int flags); |
b46b1d59 VZ |
125 | |
126 | // modify descriptor flags or handler, return true on success | |
ad8d42f8 | 127 | virtual bool ModifyFD(int fd, wxFDIOHandler *handler, int flags); |
b46b1d59 | 128 | |
af57c51a VZ |
129 | // unregister descriptor previously registered with RegisterFD() |
130 | virtual bool UnregisterFD(int fd); | |
b46b1d59 | 131 | |
ad8d42f8 | 132 | virtual ~wxMappedFDIODispatcher() { } |
b46b1d59 VZ |
133 | |
134 | protected: | |
135 | // the fd -> handler map containing all the registered handlers | |
136 | wxFDIOHandlerMap m_handlers; | |
137 | }; | |
138 | ||
139 | #endif // _WX_PRIVATE_FDIODISPATCHER_H_ |