]>
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"
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
]);
98 wxLogTrace(wxSelectDispatcher_Trace
,
99 _T("Registered fd %d for %s events"), fd
, ms_names
[n
]);
101 else if ( wxFD_ISSET(fd
, (fd_set
*) &m_fds
[n
]) )
103 wxFD_CLR(fd
, &m_fds
[n
]);
104 wxLogTrace(wxSelectDispatcher_Trace
,
105 _T("Unregistered fd %d from %s events"), fd
, ms_names
[n
]);
112 int wxSelectSets::Select(int nfds
, struct timeval
*tv
)
114 return select(nfds
, &m_fds
[Read
], &m_fds
[Write
], &m_fds
[Except
], tv
);
117 void wxSelectSets::Handle(int fd
, wxFDIOHandler
& handler
) const
119 for ( int n
= 0; n
< Max
; n
++ )
121 if ( wxFD_ISSET(fd
, (fd_set
*) &m_fds
[n
]) )
123 wxLogTrace(wxSelectDispatcher_Trace
,
124 _T("Got %s event on fd %d"), ms_names
[n
], fd
);
125 (handler
.*ms_handlers
[n
])();
130 // ----------------------------------------------------------------------------
131 // wxSelectDispatcher
132 // ----------------------------------------------------------------------------
135 wxSelectDispatcher
*wxSelectDispatcher::Create()
137 return new wxSelectDispatcher
;
140 wxSelectDispatcher::wxSelectDispatcher()
145 bool wxSelectDispatcher::RegisterFD(int fd
, wxFDIOHandler
*handler
, int flags
)
147 if ( !wxMappedFDIODispatcher::RegisterFD(fd
, handler
, flags
) )
150 if ( !m_sets
.SetFD(fd
, flags
) )
159 bool wxSelectDispatcher::ModifyFD(int fd
, wxFDIOHandler
*handler
, int flags
)
161 if ( !wxMappedFDIODispatcher::ModifyFD(fd
, handler
, flags
) )
164 wxASSERT_MSG( fd
<= m_maxFD
, _T("logic error: registered fd > m_maxFD?") );
166 return m_sets
.SetFD(fd
, flags
);
169 bool wxSelectDispatcher::UnregisterFD(int fd
)
173 if ( !wxMappedFDIODispatcher::UnregisterFD(fd
) )
176 // remove the handler if we don't need it any more
177 if ( !m_sets
.HasFD(fd
) )
181 // need to find new max fd
183 for ( wxFDIOHandlerMap::const_iterator it
= m_handlers
.begin();
184 it
!= m_handlers
.end();
187 if ( it
->first
> m_maxFD
)
196 void wxSelectDispatcher::ProcessSets(const wxSelectSets
& sets
)
198 for ( int fd
= 0; fd
<= m_maxFD
; fd
++ )
200 if ( !sets
.HasFD(fd
) )
203 wxFDIOHandler
* const handler
= FindHandler(fd
);
206 wxFAIL_MSG( _T("NULL handler in wxSelectDispatcher?") );
210 sets
.Handle(fd
, *handler
);
214 void wxSelectDispatcher::Dispatch(int timeout
)
218 if ( timeout
!= TIMEOUT_INFINITE
)
222 tv
.tv_usec
= timeout
*1000;
229 wxSelectSets sets
= m_sets
;
231 const int ret
= sets
.Select(m_maxFD
+ 1, ptv
);
235 if ( errno
!= EINTR
)
237 wxLogSysError(_("Failed to monitor I/O channels"));
242 // timeout expired without anything happening
250 #endif // wxUSE_SELECT_DISPATCHER