mac complaining about const to non-const cast
[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 #include "wx/private/selectdispatcher.h"
23 #include "wx/module.h"
24 #include "wx/timer.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 wxLogTrace(wxSelectDispatcher_Trace,
99 _T("Registered fd %d for %s events"), fd, ms_names[n]);
100 }
101 else if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) )
102 {
103 wxFD_CLR(fd, &m_fds[n]);
104 wxLogTrace(wxSelectDispatcher_Trace,
105 _T("Unregistered fd %d from %s events"), fd, ms_names[n]);
106 }
107 }
108
109 return true;
110 }
111
112 int wxSelectSets::Select(int nfds, struct timeval *tv)
113 {
114 return select(nfds, &m_fds[Read], &m_fds[Write], &m_fds[Except], tv);
115 }
116
117 void wxSelectSets::Handle(int fd, wxFDIOHandler& handler) const
118 {
119 for ( int n = 0; n < Max; n++ )
120 {
121 if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) )
122 {
123 wxLogTrace(wxSelectDispatcher_Trace,
124 _T("Got %s event on fd %d"), ms_names[n], fd);
125 (handler.*ms_handlers[n])();
126 }
127 }
128 }
129
130 // ----------------------------------------------------------------------------
131 // wxSelectDispatcher
132 // ----------------------------------------------------------------------------
133
134 static wxSelectDispatcher *gs_selectDispatcher = NULL;
135
136 /* static */
137 wxSelectDispatcher *wxSelectDispatcher::Get()
138 {
139 if ( !gs_selectDispatcher )
140 {
141 // the dispatcher should be only created from one thread so it should
142 // be ok to use a global without any protection here
143 gs_selectDispatcher = new wxSelectDispatcher;
144 }
145
146 return gs_selectDispatcher;
147 }
148
149 /* static */
150 void wxSelectDispatcher::DispatchPending()
151 {
152 if ( gs_selectDispatcher )
153 gs_selectDispatcher->RunLoop(0);
154 }
155
156 wxSelectDispatcher::wxSelectDispatcher()
157 {
158 m_maxFD = -1;
159 }
160
161 bool wxSelectDispatcher::RegisterFD(int fd, wxFDIOHandler *handler, int flags)
162 {
163 if ( !wxFDIODispatcher::RegisterFD(fd, handler, flags) )
164 return false;
165
166 if ( !m_sets.SetFD(fd, flags) )
167 return false;
168
169 if ( fd > m_maxFD )
170 m_maxFD = fd;
171
172 return true;
173 }
174
175 bool wxSelectDispatcher::ModifyFD(int fd, wxFDIOHandler *handler, int flags)
176 {
177 if ( !wxFDIODispatcher::ModifyFD(fd, handler, flags) )
178 return false;
179
180 wxASSERT_MSG( fd <= m_maxFD, _T("logic error: registered fd > m_maxFD?") );
181
182 return m_sets.SetFD(fd, flags);
183 }
184
185 wxFDIOHandler *wxSelectDispatcher::UnregisterFD(int fd, int flags)
186 {
187 wxFDIOHandler * const handler = wxFDIODispatcher::UnregisterFD(fd, flags);
188
189 m_sets.ClearFD(fd, flags);
190
191 // remove the handler if we don't need it any more
192 if ( !m_sets.HasFD(fd) )
193 {
194 if ( fd == m_maxFD )
195 {
196 // need to find new max fd
197 m_maxFD = -1;
198 for ( wxFDIOHandlerMap::const_iterator it = m_handlers.begin();
199 it != m_handlers.end();
200 ++it )
201 {
202 if ( it->first > m_maxFD )
203 m_maxFD = it->first;
204 }
205 }
206 }
207
208 return handler;
209 }
210
211 void wxSelectDispatcher::ProcessSets(const wxSelectSets& sets)
212 {
213 for ( int fd = 0; fd <= m_maxFD; fd++ )
214 {
215 if ( !sets.HasFD(fd) )
216 continue;
217
218 wxFDIOHandler * const handler = FindHandler(fd);
219 if ( !handler )
220 {
221 wxFAIL_MSG( _T("NULL handler in wxSelectDispatcher?") );
222 continue;
223 }
224
225 sets.Handle(fd, *handler);
226 }
227 }
228
229 void wxSelectDispatcher::RunLoop(int timeout)
230 {
231 struct timeval tv,
232 *ptv = NULL;
233 if ( timeout != TIMEOUT_INFINITE )
234 {
235 ptv = &tv;
236 tv.tv_sec = 0;
237 tv.tv_usec = timeout*1000;
238 }
239
240 for ( ;; )
241 {
242 wxSelectSets sets = m_sets;
243
244 wxStopWatch sw;
245 if ( ptv && timeout )
246 sw.Start(ptv->tv_usec/10);
247
248 const int ret = sets.Select(m_maxFD + 1, ptv);
249 switch ( ret )
250 {
251 case -1:
252 // continue if we were interrupted by a signal, else bail out
253 if ( errno != EINTR )
254 {
255 wxLogSysError(_("Failed to monitor I/O channels"));
256 return;
257 }
258 break;
259
260 case 0:
261 // timeout expired without anything happening
262 return;
263
264 default:
265 ProcessSets(sets);
266 }
267
268 if ( ptv )
269 {
270 timeout -= sw.Time();
271 if ( timeout <= 0 )
272 break;
273
274 ptv->tv_usec = timeout*1000;
275 }
276 }
277 }
278
279 // ----------------------------------------------------------------------------
280 // wxSelectDispatcherModule
281 // ----------------------------------------------------------------------------
282
283 class wxSelectDispatcherModule : public wxModule
284 {
285 public:
286 virtual bool OnInit() { return true; }
287 virtual void OnExit() { wxDELETE(gs_selectDispatcher); }
288
289 private:
290 DECLARE_DYNAMIC_CLASS(wxSelectDispatcherModule)
291 };
292
293 IMPLEMENT_DYNAMIC_CLASS(wxSelectDispatcherModule, wxModule)
294