]> git.saurik.com Git - wxWidgets.git/blame - src/common/selectdispatcher.cpp
VC6 compilation fix for _WX_VARARG_FORMAT_STRING macro
[wxWidgets.git] / src / common / selectdispatcher.cpp
CommitLineData
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
30c45bdd 33#ifdef HAVE_SYS_SELECT_H
b46b1d59 34 #include <sys/select.h>
30c45bdd
VZ
35#endif
36
b46b1d59
VZ
37#include <errno.h>
38
30c45bdd
VZ
39#define wxSelectDispatcher_Trace wxT("selectdispatcher")
40
41// ============================================================================
42// implementation
43// ============================================================================
44
45// ----------------------------------------------------------------------------
b46b1d59 46// wxSelectSets
30c45bdd
VZ
47// ----------------------------------------------------------------------------
48
b46b1d59
VZ
49int wxSelectSets::ms_flags[wxSelectSets::Max] =
50{
51 wxFDIO_INPUT,
52 wxFDIO_OUTPUT,
53 wxFDIO_EXCEPTION,
54};
30c45bdd 55
b46b1d59 56const char *wxSelectSets::ms_names[wxSelectSets::Max] =
30c45bdd 57{
b46b1d59
VZ
58 "input",
59 "output",
60 "exceptional",
61};
30c45bdd 62
b46b1d59 63wxSelectSets::Callback wxSelectSets::ms_handlers[wxSelectSets::Max] =
30c45bdd 64{
b46b1d59
VZ
65 &wxFDIOHandler::OnReadWaiting,
66 &wxFDIOHandler::OnWriteWaiting,
67 &wxFDIOHandler::OnExceptionWaiting,
68};
30c45bdd 69
b46b1d59
VZ
70wxSelectSets::wxSelectSets()
71{
72 for ( int n = 0; n < Max; n++ )
30c45bdd 73 {
b46b1d59 74 wxFD_ZERO(&m_fds[n]);
30c45bdd 75 }
b46b1d59 76}
30c45bdd 77
b46b1d59
VZ
78bool wxSelectSets::HasFD(int fd) const
79{
80 for ( int n = 0; n < Max; n++ )
30c45bdd 81 {
fa15eac7 82 if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) )
b46b1d59
VZ
83 return true;
84 }
30c45bdd 85
b46b1d59 86 return false;
30c45bdd
VZ
87}
88
b46b1d59 89bool wxSelectSets::SetFD(int fd, int flags)
30c45bdd 90{
b46b1d59 91 wxCHECK_MSG( fd >= 0, false, _T("invalid descriptor") );
30c45bdd 92
b46b1d59 93 for ( int n = 0; n < Max; n++ )
30c45bdd 94 {
b46b1d59
VZ
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 }
fa15eac7 101 else if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) )
b46b1d59
VZ
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 }
30c45bdd
VZ
107 }
108
b46b1d59
VZ
109 return true;
110}
30c45bdd 111
b46b1d59
VZ
112int wxSelectSets::Select(int nfds, struct timeval *tv)
113{
114 return select(nfds, &m_fds[Read], &m_fds[Write], &m_fds[Except], tv);
115}
30c45bdd 116
b46b1d59
VZ
117void wxSelectSets::Handle(int fd, wxFDIOHandler& handler) const
118{
119 for ( int n = 0; n < Max; n++ )
30c45bdd 120 {
fa15eac7 121 if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) )
30c45bdd 122 {
b46b1d59
VZ
123 wxLogTrace(wxSelectDispatcher_Trace,
124 _T("Got %s event on fd %d"), ms_names[n], fd);
125 (handler.*ms_handlers[n])();
126 }
127 }
30c45bdd
VZ
128}
129
b46b1d59
VZ
130// ----------------------------------------------------------------------------
131// wxSelectDispatcher
132// ----------------------------------------------------------------------------
133
b46b1d59 134/* static */
5e1eac14 135wxSelectDispatcher *wxSelectDispatcher::Create()
b46b1d59 136{
5e1eac14 137 return new wxSelectDispatcher;
b46b1d59 138}
30c45bdd 139
b46b1d59
VZ
140wxSelectDispatcher::wxSelectDispatcher()
141{
142 m_maxFD = -1;
143}
144
145bool wxSelectDispatcher::RegisterFD(int fd, wxFDIOHandler *handler, int flags)
146{
ad8d42f8 147 if ( !wxMappedFDIODispatcher::RegisterFD(fd, handler, flags) )
b46b1d59
VZ
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
159bool wxSelectDispatcher::ModifyFD(int fd, wxFDIOHandler *handler, int flags)
160{
ad8d42f8 161 if ( !wxMappedFDIODispatcher::ModifyFD(fd, handler, flags) )
b46b1d59
VZ
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
af57c51a 169bool wxSelectDispatcher::UnregisterFD(int fd)
b46b1d59 170{
af57c51a
VZ
171 m_sets.ClearFD(fd);
172
173 if ( !wxMappedFDIODispatcher::UnregisterFD(fd) )
174 return false;
b46b1d59
VZ
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 )
30c45bdd 180 {
b46b1d59
VZ
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 )
30c45bdd 186 {
b46b1d59
VZ
187 if ( it->first > m_maxFD )
188 m_maxFD = it->first;
189 }
190 }
191 }
192
ad8d42f8 193 return true;
30c45bdd
VZ
194}
195
b46b1d59 196void wxSelectDispatcher::ProcessSets(const wxSelectSets& sets)
30c45bdd 197{
b46b1d59
VZ
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}
30c45bdd 213
7523de90 214void wxSelectDispatcher::Dispatch(int timeout)
30c45bdd 215{
b46b1d59 216 struct timeval tv,
7523de90 217 *ptv;
b46b1d59 218 if ( timeout != TIMEOUT_INFINITE )
30c45bdd
VZ
219 {
220 ptv = &tv;
221 tv.tv_sec = 0;
b46b1d59
VZ
222 tv.tv_usec = timeout*1000;
223 }
7523de90 224 else // no timeout
30c45bdd 225 {
7523de90
VZ
226 ptv = NULL;
227 }
b46b1d59 228
7523de90 229 wxSelectSets sets = m_sets;
b46b1d59 230
7523de90
VZ
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;
b46b1d59 240
7523de90
VZ
241 case 0:
242 // timeout expired without anything happening
243 break;
b46b1d59 244
7523de90
VZ
245 default:
246 ProcessSets(sets);
b46b1d59 247 }
30c45bdd
VZ
248}
249
a1873279 250#endif // wxUSE_SELECT_DISPATCHER