// Created: December 2006
// RCS-ID: $Id$
// Copyright: (c) 2006 Lukasz Michalski
-// License: wxWindows licence
+// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
// ============================================================================
// for compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
+#ifdef __BORLANDC__
+ #pragma hdrstop
+#endif
+
#if wxUSE_SELECT_DISPATCHER
#include "wx/private/selectdispatcher.h"
#include "wx/intl.h"
#endif
-#if defined(HAVE_SYS_SELECT_H) || defined(__WATCOMC__)
- #include <sys/time.h>
- #include <sys/select.h>
-#endif
-
#include <errno.h>
#define wxSelectDispatcher_Trace wxT("selectdispatcher")
bool wxSelectSets::SetFD(int fd, int flags)
{
- wxCHECK_MSG( fd >= 0, false, _T("invalid descriptor") );
+ wxCHECK_MSG( fd >= 0, false, wxT("invalid descriptor") );
for ( int n = 0; n < Max; n++ )
{
return select(nfds, &m_fds[Read], &m_fds[Write], &m_fds[Except], tv);
}
-void wxSelectSets::Handle(int fd, wxFDIOHandler& handler) const
+bool wxSelectSets::Handle(int fd, wxFDIOHandler& handler) const
{
for ( int n = 0; n < Max; n++ )
{
if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) )
{
wxLogTrace(wxSelectDispatcher_Trace,
- _T("Got %s event on fd %d"), ms_names[n], fd);
+ wxT("Got %s event on fd %d"), ms_names[n], fd);
(handler.*ms_handlers[n])();
// callback can modify sets and destroy handler
// this forces that one event can be processed at one time
- return;
+ return true;
}
}
+
+ return false;
}
// ----------------------------------------------------------------------------
m_maxFD = fd;
wxLogTrace(wxSelectDispatcher_Trace,
- _T("Registered fd %d: input:%d, output:%d, exceptional:%d"), fd, (flags & wxFDIO_INPUT) == wxFDIO_INPUT, (flags & wxFDIO_OUTPUT), (flags & wxFDIO_EXCEPTION) == wxFDIO_EXCEPTION);
+ wxT("Registered fd %d: input:%d, output:%d, exceptional:%d"), fd, (flags & wxFDIO_INPUT) == wxFDIO_INPUT, (flags & wxFDIO_OUTPUT), (flags & wxFDIO_EXCEPTION) == wxFDIO_EXCEPTION);
return true;
}
if ( !wxMappedFDIODispatcher::ModifyFD(fd, handler, flags) )
return false;
- wxASSERT_MSG( fd <= m_maxFD, _T("logic error: registered fd > m_maxFD?") );
+ wxASSERT_MSG( fd <= m_maxFD, wxT("logic error: registered fd > m_maxFD?") );
wxLogTrace(wxSelectDispatcher_Trace,
- _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);
+ 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);
return m_sets.SetFD(fd, flags);
}
}
wxLogTrace(wxSelectDispatcher_Trace,
- _T("Removed fd %d, current max: %d"), fd, m_maxFD);
+ wxT("Removed fd %d, current max: %d"), fd, m_maxFD);
return true;
}
-bool wxSelectDispatcher::ProcessSets(const wxSelectSets& sets)
+int wxSelectDispatcher::ProcessSets(const wxSelectSets& sets)
{
- bool gotEvent = false;
+ int numEvents = 0;
for ( int fd = 0; fd <= m_maxFD; fd++ )
{
if ( !sets.HasFD(fd) )
wxFDIOHandler * const handler = FindHandler(fd);
if ( !handler )
{
- wxFAIL_MSG( _T("NULL handler in wxSelectDispatcher?") );
+ wxFAIL_MSG( wxT("NULL handler in wxSelectDispatcher?") );
continue;
}
- gotEvent = true;
-
- sets.Handle(fd, *handler);
+ if ( sets.Handle(fd, *handler) )
+ numEvents++;
}
- return gotEvent;
+ return numEvents;
}
-bool wxSelectDispatcher::Dispatch(int timeout)
+int wxSelectDispatcher::DoSelect(wxSelectSets& sets, int timeout) const
{
struct timeval tv,
*ptv;
ptv = NULL;
}
- wxSelectSets sets = m_sets;
+ int ret = sets.Select(m_maxFD + 1, ptv);
- const int ret = sets.Select(m_maxFD + 1, ptv);
- switch ( ret )
+ // TODO: we need to restart select() in this case but for now just return
+ // as if timeout expired
+ if ( ret == -1 && errno == EINTR )
+ ret = 0;
+
+ return ret;
+}
+
+bool wxSelectDispatcher::HasPending() const
+{
+ wxSelectSets sets(m_sets);
+ return DoSelect(sets, 0) > 0;
+}
+
+int wxSelectDispatcher::Dispatch(int timeout)
+{
+ wxSelectSets sets(m_sets);
+ switch ( DoSelect(sets, timeout) )
{
case -1:
- if ( errno != EINTR )
- {
- wxLogSysError(_("Failed to monitor I/O channels"));
- }
- break;
+ wxLogSysError(_("Failed to monitor I/O channels"));
+ return -1;
case 0:
// timeout expired without anything happening
- break;
+ return 0;
default:
- if ( ProcessSets(sets) )
- return true;
+ return ProcessSets(sets);
}
-
- // nothing happened
- return false;
}
#endif // wxUSE_SELECT_DISPATCHER