X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/f7b3c5ec4c7ee0a566b7e852e8f96bd407f322f3..09294e12fb454fa5a6b66b1ef031fee9cb43dd11:/src/common/selectdispatcher.cpp diff --git a/src/common/selectdispatcher.cpp b/src/common/selectdispatcher.cpp index 5ee2f0242d..9321e7c837 100644 --- a/src/common/selectdispatcher.cpp +++ b/src/common/selectdispatcher.cpp @@ -1,7 +1,7 @@ /////////////////////////////////////////////////////////////////////////////// -// Name: src/common/socketevtdispatch.cpp +// Name: src/common/selectdispatcher.cpp // Purpose: implements dispatcher for select() call -// Author: Lukasz Michalski +// Author: Lukasz Michalski and Vadim Zeitlin // Created: December 2006 // RCS-ID: $Id$ // Copyright: (c) 2006 Lukasz Michalski @@ -19,23 +19,24 @@ // for compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" +#if wxUSE_SELECT_DISPATCHER + #include "wx/private/selectdispatcher.h" -#include "wx/module.h" -#include "wx/timer.h" #include "wx/unix/private.h" -#include "wx/log.h" #ifndef WX_PRECOMP #include "wx/hash.h" + #include "wx/log.h" + #include "wx/intl.h" #endif -#include -#include - -#ifdef HAVE_SYS_SELECT_H -# include +#if defined(HAVE_SYS_SELECT_H) || defined(__WATCOMC__) + #include + #include #endif +#include + #define wxSelectDispatcher_Trace wxT("selectdispatcher") // ============================================================================ @@ -43,204 +44,208 @@ // ============================================================================ // ---------------------------------------------------------------------------- -// wxSelectDispatcher +// wxSelectSets // ---------------------------------------------------------------------------- -wxSelectDispatcher* wxSelectDispatcher::ms_instance = NULL; +int wxSelectSets::ms_flags[wxSelectSets::Max] = +{ + wxFDIO_INPUT, + wxFDIO_OUTPUT, + wxFDIO_EXCEPTION, +}; -/* static */ -wxSelectDispatcher& wxSelectDispatcher::Get() +const char *wxSelectSets::ms_names[wxSelectSets::Max] = { - if ( !ms_instance ) - ms_instance = new wxSelectDispatcher; - return *ms_instance; -} + "input", + "output", + "exceptional", +}; -void -wxSelectDispatcher::RegisterFD(int fd, wxFDIOHandler* handler, int flags) +wxSelectSets::Callback wxSelectSets::ms_handlers[wxSelectSets::Max] = { - if ((flags & wxSelectInput) == wxSelectInput) - { - wxFD_SET(fd, &m_readset); - wxLogTrace(wxSelectDispatcher_Trace,wxT("Registered fd %d for input events"),fd); - }; + &wxFDIOHandler::OnReadWaiting, + &wxFDIOHandler::OnWriteWaiting, + &wxFDIOHandler::OnExceptionWaiting, +}; - if ((flags & wxSelectOutput) == wxSelectOutput) +wxSelectSets::wxSelectSets() +{ + for ( int n = 0; n < Max; n++ ) { - wxFD_SET(fd, &m_writeset); - wxLogTrace(wxSelectDispatcher_Trace,wxT("Registered fd %d for output events"),fd); + wxFD_ZERO(&m_fds[n]); } +} - if ((flags & wxSelectException) == wxSelectException) +bool wxSelectSets::HasFD(int fd) const +{ + for ( int n = 0; n < Max; n++ ) { - wxFD_SET(fd, &m_exeptset); - wxLogTrace(wxSelectDispatcher_Trace,wxT("Registered fd %d for exception events"),fd); - }; + if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) ) + return true; + } - m_handlers[fd] = handler; - if (fd > m_maxFD) - m_maxFD = fd; + return false; } -wxFDIOHandler* -wxSelectDispatcher::UnregisterFD(int fd, int flags) +bool wxSelectSets::SetFD(int fd, int flags) { - // GSocket likes to unregister -1 descriptor - if (fd == -1) - return NULL; + wxCHECK_MSG( fd >= 0, false, _T("invalid descriptor") ); - if ((flags & wxSelectInput) == wxSelectInput) + for ( int n = 0; n < Max; n++ ) { - wxLogTrace(wxSelectDispatcher_Trace,wxT("Unregistered fd %d from input events"),fd); - wxFD_CLR(fd, &m_readset); + if ( flags & ms_flags[n] ) + { + wxFD_SET(fd, &m_fds[n]); + wxLogTrace(wxSelectDispatcher_Trace, + _T("Registered fd %d for %s events"), fd, ms_names[n]); + } + else if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) ) + { + wxFD_CLR(fd, &m_fds[n]); + wxLogTrace(wxSelectDispatcher_Trace, + _T("Unregistered fd %d from %s events"), fd, ms_names[n]); + } } - if ((flags & wxSelectOutput) == wxSelectOutput) - { - wxLogTrace(wxSelectDispatcher_Trace,wxT("Unregistered fd %d from output events"),fd); - wxFD_CLR(fd, &m_writeset); - } + return true; +} - if ((flags & wxSelectException) == wxSelectException) - { - wxLogTrace(wxSelectDispatcher_Trace,wxT("Unregistered fd %d from exeption events"),fd); - wxFD_CLR(fd, &m_exeptset); - }; +int wxSelectSets::Select(int nfds, struct timeval *tv) +{ + return select(nfds, &m_fds[Read], &m_fds[Write], &m_fds[Except], tv); +} - wxFDIOHandler* ret = NULL; - wxFDIOHandlerMap::const_iterator it = m_handlers.find(fd); - if (it != m_handlers.end()) +void wxSelectSets::Handle(int fd, wxFDIOHandler& handler) const +{ + for ( int n = 0; n < Max; n++ ) { - ret = it->second; - if (!wxFD_ISSET(fd,&m_readset) && !wxFD_ISSET(fd,&m_writeset) && !wxFD_ISSET(fd,&m_exeptset)) + if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) ) { - m_handlers.erase(it); - if ( m_handlers.empty() ) - m_maxFD = 0; - }; - }; - return ret; + wxLogTrace(wxSelectDispatcher_Trace, + _T("Got %s event on fd %d"), ms_names[n], fd); + (handler.*ms_handlers[n])(); + } + } } -void wxSelectDispatcher::ProcessSets(fd_set* readset, fd_set* writeset, fd_set* exeptset, int max_fd) +// ---------------------------------------------------------------------------- +// wxSelectDispatcher +// ---------------------------------------------------------------------------- + +/* static */ +wxSelectDispatcher *wxSelectDispatcher::Create() { - // it is safe to remove handler from onXXX methods, - // if you unregister descriptor first. - wxFDIOHandlerMap::const_iterator it = m_handlers.begin(); - for ( int i = 0; i < max_fd; i++ ) - { - wxFDIOHandler* handler = NULL; - if (wxFD_ISSET(i, readset)) - { - wxLogTrace(wxSelectDispatcher_Trace,wxT("Got read event on fd %d"),i); - handler = FindHandler(i); - if (handler != NULL && wxFD_ISSET(i,&m_readset)) - handler->OnReadWaiting(i); - else - { - wxLogError(wxT("Lost fd in read fdset: %d, removing"),i); - wxFD_CLR(i,&m_readset); - }; - }; + return new wxSelectDispatcher; +} - if (wxFD_ISSET(i, writeset)) - { - wxLogTrace(wxSelectDispatcher_Trace,wxT("Got write event on fd %d"),i); - if (handler == NULL) - handler = FindHandler(i); - if (handler != NULL && wxFD_ISSET(i,&m_writeset)) - handler->OnWriteWaiting(i); - else - { - wxLogError(wxT("Lost fd in write fdset: %d, removing"),i); - wxFD_CLR(i,&m_writeset); - }; - }; +wxSelectDispatcher::wxSelectDispatcher() +{ + m_maxFD = -1; +} - if (wxFD_ISSET(i, exeptset)) +bool wxSelectDispatcher::RegisterFD(int fd, wxFDIOHandler *handler, int flags) +{ + if ( !wxMappedFDIODispatcher::RegisterFD(fd, handler, flags) ) + return false; + + if ( !m_sets.SetFD(fd, flags) ) + return false; + + if ( fd > m_maxFD ) + m_maxFD = fd; + + return true; +} + +bool wxSelectDispatcher::ModifyFD(int fd, wxFDIOHandler *handler, int flags) +{ + if ( !wxMappedFDIODispatcher::ModifyFD(fd, handler, flags) ) + return false; + + wxASSERT_MSG( fd <= m_maxFD, _T("logic error: registered fd > m_maxFD?") ); + + return m_sets.SetFD(fd, flags); +} + +bool wxSelectDispatcher::UnregisterFD(int fd) +{ + m_sets.ClearFD(fd); + + if ( !wxMappedFDIODispatcher::UnregisterFD(fd) ) + return false; + + // remove the handler if we don't need it any more + if ( !m_sets.HasFD(fd) ) + { + if ( fd == m_maxFD ) { - wxLogTrace(wxSelectDispatcher_Trace,wxT("Got exception event on fd %d"),i); - if (handler == NULL) - handler = FindHandler(i); - if (handler != NULL && wxFD_ISSET(i,&m_writeset)) - handler->OnExceptionWaiting(i); - else + // need to find new max fd + m_maxFD = -1; + for ( wxFDIOHandlerMap::const_iterator it = m_handlers.begin(); + it != m_handlers.end(); + ++it ) { - wxLogError(wxT("Lost fd in exept fdset: %d, removing"),i); - wxFD_CLR(i,&m_exeptset); - }; - }; - }; + if ( it->first > m_maxFD ) + m_maxFD = it->first; + } + } + } + + return true; } -wxFDIOHandler* wxSelectDispatcher::FindHandler(int fd) +void wxSelectDispatcher::ProcessSets(const wxSelectSets& sets) { - wxFDIOHandlerMap::const_iterator it = m_handlers.find(fd); - if (it != m_handlers.end()) - return it->second; - return NULL; -}; + for ( int fd = 0; fd <= m_maxFD; fd++ ) + { + if ( !sets.HasFD(fd) ) + continue; + + wxFDIOHandler * const handler = FindHandler(fd); + if ( !handler ) + { + wxFAIL_MSG( _T("NULL handler in wxSelectDispatcher?") ); + continue; + } + + sets.Handle(fd, *handler); + } +} -void wxSelectDispatcher::RunLoop(int timeout) +void wxSelectDispatcher::Dispatch(int timeout) { - struct timeval tv, *ptv = NULL; - if ( timeout != wxSELECT_TIMEOUT_INFINITE ) + struct timeval tv, + *ptv; + if ( timeout != TIMEOUT_INFINITE ) { ptv = &tv; tv.tv_sec = 0; - tv.tv_usec = timeout*10; - }; - - int ret; - do + tv.tv_usec = timeout*1000; + } + else // no timeout { - fd_set readset = m_readset; - fd_set writeset = m_writeset; - fd_set exeptset = m_exeptset; - wxStopWatch sw; - if ( ptv && timeout ) - sw.Start(ptv->tv_usec/10); - ret = select(m_maxFD+1, &readset, &writeset, &exeptset, ptv); - switch ( ret ) - { - // TODO: handle unix signals here - case -1: - if ( !timeout ) - { - // it doesn't make sense to remain here - return; - } - - if ( ptv ) - { - ptv->tv_sec = 0; - ptv->tv_usec = timeout - sw.Time()*10; - } - break; - - // timeout - case 0: - break; - - default: - ProcessSets(&readset, &writeset, &exeptset, m_maxFD+1); - }; - } while (ret != 0); -} + ptv = NULL; + } -// ---------------------------------------------------------------------------- -// wxSelectDispatcherModule -// ---------------------------------------------------------------------------- + wxSelectSets sets = m_sets; -class wxSelectDispatcherModule: public wxModule -{ -public: - bool OnInit() { wxLog::AddTraceMask(wxSelectDispatcher_Trace); return true; } - void OnExit() { wxDELETE(wxSelectDispatcher::ms_instance); } + const int ret = sets.Select(m_maxFD + 1, ptv); + switch ( ret ) + { + case -1: + if ( errno != EINTR ) + { + wxLogSysError(_("Failed to monitor I/O channels")); + } + break; -private: - DECLARE_DYNAMIC_CLASS(wxSelectDispatcherModule) -}; + case 0: + // timeout expired without anything happening + break; -IMPLEMENT_DYNAMIC_CLASS(wxSelectDispatcherModule, wxModule) + default: + ProcessSets(sets); + } +} +#endif // wxUSE_SELECT_DISPATCHER