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