]>
git.saurik.com Git - wxWidgets.git/blob - 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
7 // Copyright: (c) 2006 Lukasz Michalski
8 // License: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #if wxUSE_SELECT_DISPATCHER
28 #include "wx/private/selectdispatcher.h"
29 #include "wx/unix/private.h"
37 #if defined(HAVE_SYS_SELECT_H) || defined(__WATCOMC__)
39 #include <sys/select.h>
44 #define wxSelectDispatcher_Trace wxT("selectdispatcher")
46 // ============================================================================
48 // ============================================================================
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 int wxSelectSets::ms_flags
[wxSelectSets::Max
] =
61 const char *wxSelectSets::ms_names
[wxSelectSets::Max
] =
68 wxSelectSets::Callback
wxSelectSets::ms_handlers
[wxSelectSets::Max
] =
70 &wxFDIOHandler::OnReadWaiting
,
71 &wxFDIOHandler::OnWriteWaiting
,
72 &wxFDIOHandler::OnExceptionWaiting
,
75 wxSelectSets::wxSelectSets()
77 for ( int n
= 0; n
< Max
; n
++ )
83 bool wxSelectSets::HasFD(int fd
) const
85 for ( int n
= 0; n
< Max
; n
++ )
87 if ( wxFD_ISSET(fd
, (fd_set
*) &m_fds
[n
]) )
94 bool wxSelectSets::SetFD(int fd
, int flags
)
96 wxCHECK_MSG( fd
>= 0, false, wxT("invalid descriptor") );
98 for ( int n
= 0; n
< Max
; n
++ )
100 if ( flags
& ms_flags
[n
] )
102 wxFD_SET(fd
, &m_fds
[n
]);
104 else if ( wxFD_ISSET(fd
, (fd_set
*) &m_fds
[n
]) )
106 wxFD_CLR(fd
, &m_fds
[n
]);
113 int wxSelectSets::Select(int nfds
, struct timeval
*tv
)
115 return select(nfds
, &m_fds
[Read
], &m_fds
[Write
], &m_fds
[Except
], tv
);
118 bool wxSelectSets::Handle(int fd
, wxFDIOHandler
& handler
) const
120 for ( int n
= 0; n
< Max
; n
++ )
122 if ( wxFD_ISSET(fd
, (fd_set
*) &m_fds
[n
]) )
124 wxLogTrace(wxSelectDispatcher_Trace
,
125 wxT("Got %s event on fd %d"), ms_names
[n
], fd
);
126 (handler
.*ms_handlers
[n
])();
127 // callback can modify sets and destroy handler
128 // this forces that one event can be processed at one time
136 // ----------------------------------------------------------------------------
137 // wxSelectDispatcher
138 // ----------------------------------------------------------------------------
140 bool wxSelectDispatcher::RegisterFD(int fd
, wxFDIOHandler
*handler
, int flags
)
142 if ( !wxMappedFDIODispatcher::RegisterFD(fd
, handler
, flags
) )
145 if ( !m_sets
.SetFD(fd
, flags
) )
151 wxLogTrace(wxSelectDispatcher_Trace
,
152 wxT("Registered fd %d: input:%d, output:%d, exceptional:%d"), fd
, (flags
& wxFDIO_INPUT
) == wxFDIO_INPUT
, (flags
& wxFDIO_OUTPUT
), (flags
& wxFDIO_EXCEPTION
) == wxFDIO_EXCEPTION
);
156 bool wxSelectDispatcher::ModifyFD(int fd
, wxFDIOHandler
*handler
, int flags
)
158 if ( !wxMappedFDIODispatcher::ModifyFD(fd
, handler
, flags
) )
161 wxASSERT_MSG( fd
<= m_maxFD
, wxT("logic error: registered fd > m_maxFD?") );
163 wxLogTrace(wxSelectDispatcher_Trace
,
164 wxT("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
);
165 return m_sets
.SetFD(fd
, flags
);
168 bool wxSelectDispatcher::UnregisterFD(int fd
)
172 if ( !wxMappedFDIODispatcher::UnregisterFD(fd
) )
175 // remove the handler if we don't need it any more
176 if ( !m_sets
.HasFD(fd
) )
180 // need to find new max fd
182 for ( wxFDIOHandlerMap::const_iterator it
= m_handlers
.begin();
183 it
!= m_handlers
.end();
186 if ( it
->first
> m_maxFD
)
194 wxLogTrace(wxSelectDispatcher_Trace
,
195 wxT("Removed fd %d, current max: %d"), fd
, m_maxFD
);
199 int wxSelectDispatcher::ProcessSets(const wxSelectSets
& sets
)
202 for ( int fd
= 0; fd
<= m_maxFD
; fd
++ )
204 if ( !sets
.HasFD(fd
) )
207 wxFDIOHandler
* const handler
= FindHandler(fd
);
210 wxFAIL_MSG( wxT("NULL handler in wxSelectDispatcher?") );
214 if ( sets
.Handle(fd
, *handler
) )
221 int wxSelectDispatcher::DoSelect(wxSelectSets
& sets
, int timeout
) const
225 if ( timeout
!= TIMEOUT_INFINITE
)
229 tv
.tv_usec
= timeout
*1000;
236 int ret
= sets
.Select(m_maxFD
+ 1, ptv
);
238 // TODO: we need to restart select() in this case but for now just return
239 // as if timeout expired
240 if ( ret
== -1 && errno
== EINTR
)
246 bool wxSelectDispatcher::HasPending() const
248 wxSelectSets
sets(m_sets
);
249 return DoSelect(sets
, 0) > 0;
252 int wxSelectDispatcher::Dispatch(int timeout
)
254 wxSelectSets
sets(m_sets
);
255 switch ( DoSelect(sets
, timeout
) )
258 wxLogSysError(_("Failed to monitor I/O channels"));
262 // timeout expired without anything happening
266 return ProcessSets(sets
);
270 #endif // wxUSE_SELECT_DISPATCHER