]>
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"
39 #define wxSelectDispatcher_Trace wxT("selectdispatcher")
41 // ============================================================================
43 // ============================================================================
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 int wxSelectSets::ms_flags
[wxSelectSets::Max
] =
56 const char *wxSelectSets::ms_names
[wxSelectSets::Max
] =
63 wxSelectSets::Callback
wxSelectSets::ms_handlers
[wxSelectSets::Max
] =
65 &wxFDIOHandler::OnReadWaiting
,
66 &wxFDIOHandler::OnWriteWaiting
,
67 &wxFDIOHandler::OnExceptionWaiting
,
70 wxSelectSets::wxSelectSets()
72 for ( int n
= 0; n
< Max
; n
++ )
78 bool wxSelectSets::HasFD(int fd
) const
80 for ( int n
= 0; n
< Max
; n
++ )
82 if ( wxFD_ISSET(fd
, (fd_set
*) &m_fds
[n
]) )
89 bool wxSelectSets::SetFD(int fd
, int flags
)
91 wxCHECK_MSG( fd
>= 0, false, wxT("invalid descriptor") );
93 for ( int n
= 0; n
< Max
; n
++ )
95 if ( flags
& ms_flags
[n
] )
97 wxFD_SET(fd
, &m_fds
[n
]);
99 else if ( wxFD_ISSET(fd
, (fd_set
*) &m_fds
[n
]) )
101 wxFD_CLR(fd
, &m_fds
[n
]);
108 int wxSelectSets::Select(int nfds
, struct timeval
*tv
)
110 return select(nfds
, &m_fds
[Read
], &m_fds
[Write
], &m_fds
[Except
], tv
);
113 bool wxSelectSets::Handle(int fd
, wxFDIOHandler
& handler
) const
115 for ( int n
= 0; n
< Max
; n
++ )
117 if ( wxFD_ISSET(fd
, (fd_set
*) &m_fds
[n
]) )
119 wxLogTrace(wxSelectDispatcher_Trace
,
120 wxT("Got %s event on fd %d"), ms_names
[n
], fd
);
121 (handler
.*ms_handlers
[n
])();
122 // callback can modify sets and destroy handler
123 // this forces that one event can be processed at one time
131 // ----------------------------------------------------------------------------
132 // wxSelectDispatcher
133 // ----------------------------------------------------------------------------
135 bool wxSelectDispatcher::RegisterFD(int fd
, wxFDIOHandler
*handler
, int flags
)
137 if ( !wxMappedFDIODispatcher::RegisterFD(fd
, handler
, flags
) )
140 if ( !m_sets
.SetFD(fd
, flags
) )
146 wxLogTrace(wxSelectDispatcher_Trace
,
147 wxT("Registered fd %d: input:%d, output:%d, exceptional:%d"), fd
, (flags
& wxFDIO_INPUT
) == wxFDIO_INPUT
, (flags
& wxFDIO_OUTPUT
), (flags
& wxFDIO_EXCEPTION
) == wxFDIO_EXCEPTION
);
151 bool wxSelectDispatcher::ModifyFD(int fd
, wxFDIOHandler
*handler
, int flags
)
153 if ( !wxMappedFDIODispatcher::ModifyFD(fd
, handler
, flags
) )
156 wxASSERT_MSG( fd
<= m_maxFD
, wxT("logic error: registered fd > m_maxFD?") );
158 wxLogTrace(wxSelectDispatcher_Trace
,
159 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
);
160 return m_sets
.SetFD(fd
, flags
);
163 bool wxSelectDispatcher::UnregisterFD(int fd
)
167 if ( !wxMappedFDIODispatcher::UnregisterFD(fd
) )
170 // remove the handler if we don't need it any more
171 if ( !m_sets
.HasFD(fd
) )
175 // need to find new max fd
177 for ( wxFDIOHandlerMap::const_iterator it
= m_handlers
.begin();
178 it
!= m_handlers
.end();
181 if ( it
->first
> m_maxFD
)
189 wxLogTrace(wxSelectDispatcher_Trace
,
190 wxT("Removed fd %d, current max: %d"), fd
, m_maxFD
);
194 int wxSelectDispatcher::ProcessSets(const wxSelectSets
& sets
)
197 for ( int fd
= 0; fd
<= m_maxFD
; fd
++ )
199 if ( !sets
.HasFD(fd
) )
202 wxFDIOHandler
* const handler
= FindHandler(fd
);
205 wxFAIL_MSG( wxT("NULL handler in wxSelectDispatcher?") );
209 if ( sets
.Handle(fd
, *handler
) )
216 int wxSelectDispatcher::DoSelect(wxSelectSets
& sets
, int timeout
) const
220 if ( timeout
!= TIMEOUT_INFINITE
)
224 tv
.tv_usec
= timeout
*1000;
231 int ret
= sets
.Select(m_maxFD
+ 1, ptv
);
233 // TODO: we need to restart select() in this case but for now just return
234 // as if timeout expired
235 if ( ret
== -1 && errno
== EINTR
)
241 bool wxSelectDispatcher::HasPending() const
243 wxSelectSets
sets(m_sets
);
244 return DoSelect(sets
, 0) > 0;
247 int wxSelectDispatcher::Dispatch(int timeout
)
249 wxSelectSets
sets(m_sets
);
250 switch ( DoSelect(sets
, timeout
) )
253 wxLogSysError(_("Failed to monitor I/O channels"));
257 // timeout expired without anything happening
261 return ProcessSets(sets
);
265 #endif // wxUSE_SELECT_DISPATCHER