]>
Commit | Line | Data |
---|---|---|
30c45bdd | 1 | /////////////////////////////////////////////////////////////////////////////// |
b46b1d59 | 2 | // Name: src/common/selectdispatcher.cpp |
30c45bdd | 3 | // Purpose: implements dispatcher for select() call |
b46b1d59 | 4 | // Author: Lukasz Michalski and Vadim Zeitlin |
30c45bdd VZ |
5 | // Created: December 2006 |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2006 Lukasz Michalski | |
8 | // License: wxWindows licence | |
9 | /////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | // for compilers that support precompilation, includes "wx.h". | |
20 | #include "wx/wxprec.h" | |
21 | ||
a1873279 VZ |
22 | #if wxUSE_SELECT_DISPATCHER |
23 | ||
30c45bdd | 24 | #include "wx/private/selectdispatcher.h" |
30c45bdd | 25 | #include "wx/unix/private.h" |
30c45bdd VZ |
26 | |
27 | #ifndef WX_PRECOMP | |
28 | #include "wx/hash.h" | |
b5ef33b2 VS |
29 | #include "wx/log.h" |
30 | #include "wx/intl.h" | |
30c45bdd VZ |
31 | #endif |
32 | ||
7f6b7a5a | 33 | #if defined(HAVE_SYS_SELECT_H) || defined(__WATCOMC__) |
47f63c5b | 34 | #include <sys/time.h> |
b46b1d59 | 35 | #include <sys/select.h> |
30c45bdd VZ |
36 | #endif |
37 | ||
b46b1d59 VZ |
38 | #include <errno.h> |
39 | ||
30c45bdd VZ |
40 | #define wxSelectDispatcher_Trace wxT("selectdispatcher") |
41 | ||
42 | // ============================================================================ | |
43 | // implementation | |
44 | // ============================================================================ | |
45 | ||
46 | // ---------------------------------------------------------------------------- | |
b46b1d59 | 47 | // wxSelectSets |
30c45bdd VZ |
48 | // ---------------------------------------------------------------------------- |
49 | ||
b46b1d59 VZ |
50 | int wxSelectSets::ms_flags[wxSelectSets::Max] = |
51 | { | |
52 | wxFDIO_INPUT, | |
53 | wxFDIO_OUTPUT, | |
54 | wxFDIO_EXCEPTION, | |
55 | }; | |
30c45bdd | 56 | |
b46b1d59 | 57 | const char *wxSelectSets::ms_names[wxSelectSets::Max] = |
30c45bdd | 58 | { |
b46b1d59 VZ |
59 | "input", |
60 | "output", | |
61 | "exceptional", | |
62 | }; | |
30c45bdd | 63 | |
b46b1d59 | 64 | wxSelectSets::Callback wxSelectSets::ms_handlers[wxSelectSets::Max] = |
30c45bdd | 65 | { |
b46b1d59 VZ |
66 | &wxFDIOHandler::OnReadWaiting, |
67 | &wxFDIOHandler::OnWriteWaiting, | |
68 | &wxFDIOHandler::OnExceptionWaiting, | |
69 | }; | |
30c45bdd | 70 | |
b46b1d59 VZ |
71 | wxSelectSets::wxSelectSets() |
72 | { | |
73 | for ( int n = 0; n < Max; n++ ) | |
30c45bdd | 74 | { |
b46b1d59 | 75 | wxFD_ZERO(&m_fds[n]); |
30c45bdd | 76 | } |
b46b1d59 | 77 | } |
30c45bdd | 78 | |
b46b1d59 VZ |
79 | bool wxSelectSets::HasFD(int fd) const |
80 | { | |
81 | for ( int n = 0; n < Max; n++ ) | |
30c45bdd | 82 | { |
fa15eac7 | 83 | if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) ) |
b46b1d59 VZ |
84 | return true; |
85 | } | |
30c45bdd | 86 | |
b46b1d59 | 87 | return false; |
30c45bdd VZ |
88 | } |
89 | ||
b46b1d59 | 90 | bool wxSelectSets::SetFD(int fd, int flags) |
30c45bdd | 91 | { |
b46b1d59 | 92 | wxCHECK_MSG( fd >= 0, false, _T("invalid descriptor") ); |
30c45bdd | 93 | |
b46b1d59 | 94 | for ( int n = 0; n < Max; n++ ) |
30c45bdd | 95 | { |
b46b1d59 VZ |
96 | if ( flags & ms_flags[n] ) |
97 | { | |
98 | wxFD_SET(fd, &m_fds[n]); | |
b46b1d59 | 99 | } |
fa15eac7 | 100 | else if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) ) |
b46b1d59 VZ |
101 | { |
102 | wxFD_CLR(fd, &m_fds[n]); | |
b46b1d59 | 103 | } |
30c45bdd VZ |
104 | } |
105 | ||
b46b1d59 VZ |
106 | return true; |
107 | } | |
30c45bdd | 108 | |
b46b1d59 VZ |
109 | int wxSelectSets::Select(int nfds, struct timeval *tv) |
110 | { | |
111 | return select(nfds, &m_fds[Read], &m_fds[Write], &m_fds[Except], tv); | |
112 | } | |
30c45bdd | 113 | |
b46b1d59 VZ |
114 | void wxSelectSets::Handle(int fd, wxFDIOHandler& handler) const |
115 | { | |
116 | for ( int n = 0; n < Max; n++ ) | |
30c45bdd | 117 | { |
fa15eac7 | 118 | if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) ) |
30c45bdd | 119 | { |
b46b1d59 VZ |
120 | wxLogTrace(wxSelectDispatcher_Trace, |
121 | _T("Got %s event on fd %d"), ms_names[n], fd); | |
122 | (handler.*ms_handlers[n])(); | |
2804f77d VZ |
123 | // callback can modify sets and destroy handler |
124 | // this forces that one event can be processed at one time | |
125 | return; | |
b46b1d59 VZ |
126 | } |
127 | } | |
30c45bdd VZ |
128 | } |
129 | ||
b46b1d59 VZ |
130 | // ---------------------------------------------------------------------------- |
131 | // wxSelectDispatcher | |
132 | // ---------------------------------------------------------------------------- | |
133 | ||
b46b1d59 VZ |
134 | bool wxSelectDispatcher::RegisterFD(int fd, wxFDIOHandler *handler, int flags) |
135 | { | |
ad8d42f8 | 136 | if ( !wxMappedFDIODispatcher::RegisterFD(fd, handler, flags) ) |
b46b1d59 VZ |
137 | return false; |
138 | ||
139 | if ( !m_sets.SetFD(fd, flags) ) | |
140 | return false; | |
141 | ||
142 | if ( fd > m_maxFD ) | |
143 | m_maxFD = fd; | |
144 | ||
2804f77d VZ |
145 | wxLogTrace(wxSelectDispatcher_Trace, |
146 | _T("Registered fd %d: input:%d, output:%d, exceptional:%d"), fd, (flags & wxFDIO_INPUT) == wxFDIO_INPUT, (flags & wxFDIO_OUTPUT), (flags & wxFDIO_EXCEPTION) == wxFDIO_EXCEPTION); | |
b46b1d59 VZ |
147 | return true; |
148 | } | |
149 | ||
150 | bool wxSelectDispatcher::ModifyFD(int fd, wxFDIOHandler *handler, int flags) | |
151 | { | |
ad8d42f8 | 152 | if ( !wxMappedFDIODispatcher::ModifyFD(fd, handler, flags) ) |
b46b1d59 VZ |
153 | return false; |
154 | ||
155 | wxASSERT_MSG( fd <= m_maxFD, _T("logic error: registered fd > m_maxFD?") ); | |
156 | ||
2804f77d VZ |
157 | wxLogTrace(wxSelectDispatcher_Trace, |
158 | _T("Modified fd %d: input:%d, output:%d, exceptional:%d"), fd, (flags & wxFDIO_INPUT) == wxFDIO_INPUT, (flags & wxFDIO_OUTPUT) == wxFDIO_OUTPUT, (flags & wxFDIO_EXCEPTION) == wxFDIO_EXCEPTION); | |
b46b1d59 VZ |
159 | return m_sets.SetFD(fd, flags); |
160 | } | |
161 | ||
af57c51a | 162 | bool wxSelectDispatcher::UnregisterFD(int fd) |
b46b1d59 | 163 | { |
af57c51a VZ |
164 | m_sets.ClearFD(fd); |
165 | ||
166 | if ( !wxMappedFDIODispatcher::UnregisterFD(fd) ) | |
167 | return false; | |
b46b1d59 VZ |
168 | |
169 | // remove the handler if we don't need it any more | |
170 | if ( !m_sets.HasFD(fd) ) | |
171 | { | |
172 | if ( fd == m_maxFD ) | |
30c45bdd | 173 | { |
b46b1d59 VZ |
174 | // need to find new max fd |
175 | m_maxFD = -1; | |
176 | for ( wxFDIOHandlerMap::const_iterator it = m_handlers.begin(); | |
177 | it != m_handlers.end(); | |
178 | ++it ) | |
30c45bdd | 179 | { |
b46b1d59 | 180 | if ( it->first > m_maxFD ) |
2804f77d | 181 | { |
b46b1d59 | 182 | m_maxFD = it->first; |
2804f77d | 183 | } |
b46b1d59 VZ |
184 | } |
185 | } | |
186 | } | |
187 | ||
2804f77d VZ |
188 | wxLogTrace(wxSelectDispatcher_Trace, |
189 | _T("Removed fd %d, current max: %d"), fd, m_maxFD); | |
ad8d42f8 | 190 | return true; |
30c45bdd VZ |
191 | } |
192 | ||
b46b1d59 | 193 | void wxSelectDispatcher::ProcessSets(const wxSelectSets& sets) |
30c45bdd | 194 | { |
b46b1d59 VZ |
195 | for ( int fd = 0; fd <= m_maxFD; fd++ ) |
196 | { | |
197 | if ( !sets.HasFD(fd) ) | |
198 | continue; | |
199 | ||
200 | wxFDIOHandler * const handler = FindHandler(fd); | |
201 | if ( !handler ) | |
202 | { | |
203 | wxFAIL_MSG( _T("NULL handler in wxSelectDispatcher?") ); | |
204 | continue; | |
205 | } | |
206 | ||
207 | sets.Handle(fd, *handler); | |
208 | } | |
209 | } | |
30c45bdd | 210 | |
7523de90 | 211 | void wxSelectDispatcher::Dispatch(int timeout) |
30c45bdd | 212 | { |
b46b1d59 | 213 | struct timeval tv, |
7523de90 | 214 | *ptv; |
b46b1d59 | 215 | if ( timeout != TIMEOUT_INFINITE ) |
30c45bdd VZ |
216 | { |
217 | ptv = &tv; | |
218 | tv.tv_sec = 0; | |
b46b1d59 VZ |
219 | tv.tv_usec = timeout*1000; |
220 | } | |
7523de90 | 221 | else // no timeout |
30c45bdd | 222 | { |
7523de90 VZ |
223 | ptv = NULL; |
224 | } | |
b46b1d59 | 225 | |
7523de90 | 226 | wxSelectSets sets = m_sets; |
b46b1d59 | 227 | |
7523de90 VZ |
228 | const int ret = sets.Select(m_maxFD + 1, ptv); |
229 | switch ( ret ) | |
230 | { | |
231 | case -1: | |
232 | if ( errno != EINTR ) | |
233 | { | |
234 | wxLogSysError(_("Failed to monitor I/O channels")); | |
235 | } | |
236 | break; | |
b46b1d59 | 237 | |
7523de90 VZ |
238 | case 0: |
239 | // timeout expired without anything happening | |
240 | break; | |
b46b1d59 | 241 | |
7523de90 VZ |
242 | default: |
243 | ProcessSets(sets); | |
b46b1d59 | 244 | } |
30c45bdd VZ |
245 | } |
246 | ||
a1873279 | 247 | #endif // wxUSE_SELECT_DISPATCHER |