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