| 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 | #if defined(HAVE_SYS_SELECT_H) || defined(__WATCOMC__) |
| 34 | #include <sys/time.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 | } |
| 100 | else if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) ) |
| 101 | { |
| 102 | wxFD_CLR(fd, &m_fds[n]); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | int wxSelectSets::Select(int nfds, struct timeval *tv) |
| 110 | { |
| 111 | return select(nfds, &m_fds[Read], &m_fds[Write], &m_fds[Except], tv); |
| 112 | } |
| 113 | |
| 114 | bool wxSelectSets::Handle(int fd, wxFDIOHandler& handler) const |
| 115 | { |
| 116 | for ( int n = 0; n < Max; n++ ) |
| 117 | { |
| 118 | if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) ) |
| 119 | { |
| 120 | wxLogTrace(wxSelectDispatcher_Trace, |
| 121 | _T("Got %s event on fd %d"), ms_names[n], fd); |
| 122 | (handler.*ms_handlers[n])(); |
| 123 | // callback can modify sets and destroy handler |
| 124 | // this forces that one event can be processed at one time |
| 125 | return true; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | // ---------------------------------------------------------------------------- |
| 133 | // wxSelectDispatcher |
| 134 | // ---------------------------------------------------------------------------- |
| 135 | |
| 136 | bool wxSelectDispatcher::RegisterFD(int fd, wxFDIOHandler *handler, int flags) |
| 137 | { |
| 138 | if ( !wxMappedFDIODispatcher::RegisterFD(fd, handler, flags) ) |
| 139 | return false; |
| 140 | |
| 141 | if ( !m_sets.SetFD(fd, flags) ) |
| 142 | return false; |
| 143 | |
| 144 | if ( fd > m_maxFD ) |
| 145 | m_maxFD = fd; |
| 146 | |
| 147 | wxLogTrace(wxSelectDispatcher_Trace, |
| 148 | _T("Registered fd %d: input:%d, output:%d, exceptional:%d"), fd, (flags & wxFDIO_INPUT) == wxFDIO_INPUT, (flags & wxFDIO_OUTPUT), (flags & wxFDIO_EXCEPTION) == wxFDIO_EXCEPTION); |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | bool wxSelectDispatcher::ModifyFD(int fd, wxFDIOHandler *handler, int flags) |
| 153 | { |
| 154 | if ( !wxMappedFDIODispatcher::ModifyFD(fd, handler, flags) ) |
| 155 | return false; |
| 156 | |
| 157 | wxASSERT_MSG( fd <= m_maxFD, _T("logic error: registered fd > m_maxFD?") ); |
| 158 | |
| 159 | wxLogTrace(wxSelectDispatcher_Trace, |
| 160 | _T("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); |
| 161 | return m_sets.SetFD(fd, flags); |
| 162 | } |
| 163 | |
| 164 | bool wxSelectDispatcher::UnregisterFD(int fd) |
| 165 | { |
| 166 | m_sets.ClearFD(fd); |
| 167 | |
| 168 | if ( !wxMappedFDIODispatcher::UnregisterFD(fd) ) |
| 169 | return false; |
| 170 | |
| 171 | // remove the handler if we don't need it any more |
| 172 | if ( !m_sets.HasFD(fd) ) |
| 173 | { |
| 174 | if ( fd == m_maxFD ) |
| 175 | { |
| 176 | // need to find new max fd |
| 177 | m_maxFD = -1; |
| 178 | for ( wxFDIOHandlerMap::const_iterator it = m_handlers.begin(); |
| 179 | it != m_handlers.end(); |
| 180 | ++it ) |
| 181 | { |
| 182 | if ( it->first > m_maxFD ) |
| 183 | { |
| 184 | m_maxFD = it->first; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | wxLogTrace(wxSelectDispatcher_Trace, |
| 191 | _T("Removed fd %d, current max: %d"), fd, m_maxFD); |
| 192 | return true; |
| 193 | } |
| 194 | |
| 195 | int wxSelectDispatcher::ProcessSets(const wxSelectSets& sets) |
| 196 | { |
| 197 | int numEvents = 0; |
| 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 | if ( sets.Handle(fd, *handler) ) |
| 211 | numEvents++; |
| 212 | } |
| 213 | |
| 214 | return numEvents; |
| 215 | } |
| 216 | |
| 217 | int wxSelectDispatcher::DoSelect(wxSelectSets& sets, int timeout) const |
| 218 | { |
| 219 | struct timeval tv, |
| 220 | *ptv; |
| 221 | if ( timeout != TIMEOUT_INFINITE ) |
| 222 | { |
| 223 | ptv = &tv; |
| 224 | tv.tv_sec = 0; |
| 225 | tv.tv_usec = timeout*1000; |
| 226 | } |
| 227 | else // no timeout |
| 228 | { |
| 229 | ptv = NULL; |
| 230 | } |
| 231 | |
| 232 | int ret = sets.Select(m_maxFD + 1, ptv); |
| 233 | |
| 234 | // TODO: we need to restart select() in this case but for now just return |
| 235 | // as if timeout expired |
| 236 | if ( ret == -1 && errno == EINTR ) |
| 237 | ret = 0; |
| 238 | |
| 239 | return ret; |
| 240 | } |
| 241 | |
| 242 | bool wxSelectDispatcher::HasPending() const |
| 243 | { |
| 244 | wxSelectSets sets(m_sets); |
| 245 | return DoSelect(sets, 0) > 0; |
| 246 | } |
| 247 | |
| 248 | int wxSelectDispatcher::Dispatch(int timeout) |
| 249 | { |
| 250 | wxSelectSets sets(m_sets); |
| 251 | switch ( DoSelect(sets, timeout) ) |
| 252 | { |
| 253 | case -1: |
| 254 | wxLogSysError(_("Failed to monitor I/O channels")); |
| 255 | return -1; |
| 256 | |
| 257 | case 0: |
| 258 | // timeout expired without anything happening |
| 259 | return 0; |
| 260 | |
| 261 | default: |
| 262 | return ProcessSets(sets); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | #endif // wxUSE_SELECT_DISPATCHER |