]> git.saurik.com Git - wxWidgets.git/blob - src/common/selectdispatcher.cpp
Added wxVariantData::Clone and wxVariant::Unshare
[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 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 */
135 wxSelectDispatcher *wxSelectDispatcher::Create()
136 {
137 return new wxSelectDispatcher;
138 }
139
140 wxSelectDispatcher::wxSelectDispatcher()
141 {
142 m_maxFD = -1;
143 }
144
145 bool wxSelectDispatcher::RegisterFD(int fd, wxFDIOHandler *handler, int flags)
146 {
147 if ( !wxMappedFDIODispatcher::RegisterFD(fd, handler, flags) )
148 return false;
149
150 if ( !m_sets.SetFD(fd, flags) )
151 return false;
152
153 if ( fd > m_maxFD )
154 m_maxFD = fd;
155
156 return true;
157 }
158
159 bool wxSelectDispatcher::ModifyFD(int fd, wxFDIOHandler *handler, int flags)
160 {
161 if ( !wxMappedFDIODispatcher::ModifyFD(fd, handler, flags) )
162 return false;
163
164 wxASSERT_MSG( fd <= m_maxFD, _T("logic error: registered fd > m_maxFD?") );
165
166 return m_sets.SetFD(fd, flags);
167 }
168
169 bool wxSelectDispatcher::UnregisterFD(int fd)
170 {
171 m_sets.ClearFD(fd);
172
173 if ( !wxMappedFDIODispatcher::UnregisterFD(fd) )
174 return false;
175
176 // remove the handler if we don't need it any more
177 if ( !m_sets.HasFD(fd) )
178 {
179 if ( fd == m_maxFD )
180 {
181 // need to find new max fd
182 m_maxFD = -1;
183 for ( wxFDIOHandlerMap::const_iterator it = m_handlers.begin();
184 it != m_handlers.end();
185 ++it )
186 {
187 if ( it->first > m_maxFD )
188 m_maxFD = it->first;
189 }
190 }
191 }
192
193 return true;
194 }
195
196 void wxSelectDispatcher::ProcessSets(const wxSelectSets& sets)
197 {
198 for ( int fd = 0; fd <= m_maxFD; fd++ )
199 {
200 if ( !sets.HasFD(fd) )
201 continue;
202
203 wxFDIOHandler * const handler = FindHandler(fd);
204 if ( !handler )
205 {
206 wxFAIL_MSG( _T("NULL handler in wxSelectDispatcher?") );
207 continue;
208 }
209
210 sets.Handle(fd, *handler);
211 }
212 }
213
214 void wxSelectDispatcher::Dispatch(int timeout)
215 {
216 struct timeval tv,
217 *ptv;
218 if ( timeout != TIMEOUT_INFINITE )
219 {
220 ptv = &tv;
221 tv.tv_sec = 0;
222 tv.tv_usec = timeout*1000;
223 }
224 else // no timeout
225 {
226 ptv = NULL;
227 }
228
229 wxSelectSets sets = m_sets;
230
231 const int ret = sets.Select(m_maxFD + 1, ptv);
232 switch ( ret )
233 {
234 case -1:
235 if ( errno != EINTR )
236 {
237 wxLogSysError(_("Failed to monitor I/O channels"));
238 }
239 break;
240
241 case 0:
242 // timeout expired without anything happening
243 break;
244
245 default:
246 ProcessSets(sets);
247 }
248 }
249
250 #endif // wxUSE_SELECT_DISPATCHER