]>
Commit | Line | Data |
---|---|---|
30c45bdd VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/private/selectdispatcher.h | |
3 | // Purpose: wxSelectDispatcher class | |
b46b1d59 | 4 | // Authors: Lukasz Michalski and Vadim Zeitlin |
30c45bdd VZ |
5 | // Created: December 2006 |
6 | // Copyright: (c) Lukasz Michalski | |
7 | // RCS-ID: $Id$ | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifndef _WX_PRIVATE_SELECTDISPATCHER_H_ | |
12 | #define _WX_PRIVATE_SELECTDISPATCHER_H_ | |
13 | ||
14 | #include "wx/defs.h" | |
15 | ||
a1873279 VZ |
16 | #if wxUSE_SELECT_DISPATCHER |
17 | ||
d1418268 VZ |
18 | #if defined(HAVE_SYS_SELECT_H) || defined(__WATCOMC__) |
19 | #include <sys/time.h> | |
20 | #include <sys/select.h> | |
21 | #endif | |
22 | ||
ddfed775 | 23 | #ifdef __WATCOMC__ |
d1418268 VZ |
24 | #include <types.h> |
25 | #include <sys/ioctl.h> | |
26 | #include <tcpustd.h> | |
ddfed775 | 27 | #else |
d1418268 | 28 | #include <sys/types.h> |
ddfed775 | 29 | #endif |
c3938816 | 30 | |
b46b1d59 | 31 | #include "wx/private/fdiodispatcher.h" |
30c45bdd | 32 | |
b46b1d59 VZ |
33 | // helper class storing all the select() fd sets |
34 | class WXDLLIMPEXP_BASE wxSelectSets | |
30c45bdd VZ |
35 | { |
36 | public: | |
b46b1d59 VZ |
37 | // ctor zeroes out all fd_sets |
38 | wxSelectSets(); | |
30c45bdd | 39 | |
b46b1d59 | 40 | // default copy ctor, assignment operator and dtor are ok |
72fa3e8a | 41 | |
30c45bdd | 42 | |
b46b1d59 VZ |
43 | // return true if fd appears in any of the sets |
44 | bool HasFD(int fd) const; | |
30c45bdd | 45 | |
b46b1d59 VZ |
46 | // add or remove FD to our sets depending on whether flags contains |
47 | // wxFDIO_INPUT/OUTPUT/EXCEPTION bits | |
48 | bool SetFD(int fd, int flags); | |
30c45bdd | 49 | |
b46b1d59 VZ |
50 | // same as SetFD() except it unsets the bits set in the flags for the given |
51 | // fd | |
af57c51a | 52 | bool ClearFD(int fd) |
30c45bdd | 53 | { |
af57c51a | 54 | return SetFD(fd, 0); |
30c45bdd VZ |
55 | } |
56 | ||
30c45bdd | 57 | |
b46b1d59 VZ |
58 | // call select() with our sets: the other parameters are the same as for |
59 | // select() itself | |
60 | int Select(int nfds, struct timeval *tv); | |
61 | ||
a12698ab VZ |
62 | // call the handler methods corresponding to the sets having this fd if it |
63 | // is present in any set and return true if it is | |
64 | bool Handle(int fd, wxFDIOHandler& handler) const; | |
b46b1d59 VZ |
65 | |
66 | private: | |
67 | typedef void (wxFDIOHandler::*Callback)(); | |
68 | ||
69 | // the FD sets indices | |
70 | enum | |
71 | { | |
72 | Read, | |
73 | Write, | |
74 | Except, | |
75 | Max | |
76 | }; | |
77 | ||
78 | // the sets used with select() | |
79 | fd_set m_fds[Max]; | |
80 | ||
81 | // the wxFDIO_XXX flags, functions and names (used for debug messages only) | |
82 | // corresponding to the FD sets above | |
83 | static int ms_flags[Max]; | |
84 | static const char *ms_names[Max]; | |
85 | static Callback ms_handlers[Max]; | |
86 | }; | |
87 | ||
ad8d42f8 | 88 | class WXDLLIMPEXP_BASE wxSelectDispatcher : public wxMappedFDIODispatcher |
b46b1d59 VZ |
89 | { |
90 | public: | |
fdf7ff73 VZ |
91 | // default ctor |
92 | wxSelectDispatcher() { m_maxFD = -1; } | |
30c45bdd | 93 | |
b46b1d59 VZ |
94 | // implement pure virtual methods of the base class |
95 | virtual bool RegisterFD(int fd, wxFDIOHandler *handler, int flags = wxFDIO_ALL); | |
96 | virtual bool ModifyFD(int fd, wxFDIOHandler *handler, int flags = wxFDIO_ALL); | |
af57c51a | 97 | virtual bool UnregisterFD(int fd); |
a12698ab VZ |
98 | virtual bool HasPending() const; |
99 | virtual int Dispatch(int timeout = TIMEOUT_INFINITE); | |
30c45bdd | 100 | |
30c45bdd | 101 | private: |
b46b1d59 VZ |
102 | // common part of RegisterFD() and ModifyFD() |
103 | bool DoUpdateFDAndHandler(int fd, wxFDIOHandler *handler, int flags); | |
30c45bdd | 104 | |
a12698ab VZ |
105 | // call the handlers for the fds present in the given sets, return the |
106 | // number of handlers we called | |
107 | int ProcessSets(const wxSelectSets& sets); | |
30c45bdd | 108 | |
b46b1d59 VZ |
109 | // helper of ProcessSets(): call the handler if its fd is in the set |
110 | void DoProcessFD(int fd, const fd_set& fds, wxFDIOHandler *handler, | |
111 | const char *name); | |
30c45bdd | 112 | |
a12698ab VZ |
113 | // common part of HasPending() and Dispatch(): calls select() with the |
114 | // specified timeout | |
115 | int DoSelect(wxSelectSets& sets, int timeout) const; | |
116 | ||
30c45bdd | 117 | |
b46b1d59 VZ |
118 | // the select sets containing all the registered fds |
119 | wxSelectSets m_sets; | |
120 | ||
121 | // the highest registered fd value or -1 if none | |
122 | int m_maxFD; | |
30c45bdd VZ |
123 | }; |
124 | ||
a1873279 | 125 | #endif // wxUSE_SELECT_DISPATCHER |
30c45bdd VZ |
126 | |
127 | #endif // _WX_PRIVATE_SOCKETEVTDISPATCH_H_ |