]>
git.saurik.com Git - wxWidgets.git/blob - src/common/selectdispatcher.cpp
138fa3cc8c5721a8f6b6dd68f8bd18de1858d1e7
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"
22 #if wxUSE_SELECT_DISPATCHER
24 #include "wx/private/selectdispatcher.h"
25 #include "wx/unix/private.h"
33 #ifdef HAVE_SYS_SELECT_H
34 #include <sys/select.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, _T("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 void 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 _T("Got %s event on fd %d"), ms_names
[n
], fd
);
121 (handler
.*ms_handlers
[n
])();
122 // callback can modify sets and destroy handler, returning from
123 // here guarantees that one event is processed at a time
129 // ----------------------------------------------------------------------------
130 // wxSelectDispatcher
131 // ----------------------------------------------------------------------------
134 wxSelectDispatcher
*wxSelectDispatcher::Create()
136 return new wxSelectDispatcher
;
139 wxSelectDispatcher::wxSelectDispatcher()
144 bool wxSelectDispatcher::RegisterFD(int fd
, wxFDIOHandler
*handler
, int flags
)
146 if ( !wxMappedFDIODispatcher::RegisterFD(fd
, handler
, flags
) )
149 if ( !m_sets
.SetFD(fd
, flags
) )
155 wxLogTrace(wxSelectDispatcher_Trace
,
156 _T("Registered fd %d: input:%d, output:%d, exceptional:%d"), fd
, (flags
& wxFDIO_INPUT
) == wxFDIO_INPUT
, (flags
& wxFDIO_OUTPUT
), (flags
& wxFDIO_EXCEPTION
) == wxFDIO_EXCEPTION
);
160 bool wxSelectDispatcher::ModifyFD(int fd
, wxFDIOHandler
*handler
, int flags
)
162 if ( !wxMappedFDIODispatcher::ModifyFD(fd
, handler
, flags
) )
165 wxASSERT_MSG( fd
<= m_maxFD
, _T("logic error: registered fd > m_maxFD?") );
167 wxLogTrace(wxSelectDispatcher_Trace
,
168 _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
);
169 return m_sets
.SetFD(fd
, flags
);
172 bool wxSelectDispatcher::UnregisterFD(int fd
)
176 if ( !wxMappedFDIODispatcher::UnregisterFD(fd
) )
179 // remove the handler if we don't need it any more
180 if ( !m_sets
.HasFD(fd
) )
184 // need to find new max fd
186 for ( wxFDIOHandlerMap::const_iterator it
= m_handlers
.begin();
187 it
!= m_handlers
.end();
190 if ( it
->first
> m_maxFD
)
198 wxLogTrace(wxSelectDispatcher_Trace
,
199 _T("Removed fd %d, current max: %d"), fd
, m_maxFD
);
203 void wxSelectDispatcher::ProcessSets(const wxSelectSets
& sets
)
205 for ( int fd
= 0; fd
<= m_maxFD
; fd
++ )
207 if ( !sets
.HasFD(fd
) )
210 wxFDIOHandler
* const handler
= FindHandler(fd
);
213 wxFAIL_MSG( _T("NULL handler in wxSelectDispatcher?") );
217 sets
.Handle(fd
, *handler
);
221 void wxSelectDispatcher::Dispatch(int timeout
)
225 if ( timeout
!= TIMEOUT_INFINITE
)
229 tv
.tv_usec
= timeout
*1000;
236 wxSelectSets sets
= m_sets
;
238 const int ret
= sets
.Select(m_maxFD
+ 1, ptv
);
242 if ( errno
!= EINTR
)
244 wxLogSysError(_("Failed to monitor I/O channels"));
249 // timeout expired without anything happening
257 #endif // wxUSE_SELECT_DISPATCHER