| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/common/fdiodispatcher.cpp |
| 3 | // Purpose: Implementation of common wxFDIODispatcher methods |
| 4 | // Author: Vadim Zeitlin |
| 5 | // Created: 2007-05-13 |
| 6 | // RCS-ID: $Id$ |
| 7 | // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org> |
| 8 | // Licence: wxWindows licence |
| 9 | /////////////////////////////////////////////////////////////////////////////// |
| 10 | |
| 11 | // ============================================================================ |
| 12 | // declarations |
| 13 | // ============================================================================ |
| 14 | |
| 15 | // ---------------------------------------------------------------------------- |
| 16 | // headers |
| 17 | // ---------------------------------------------------------------------------- |
| 18 | |
| 19 | // for compilers that support precompilation, includes "wx.h". |
| 20 | #include "wx/wxprec.h" |
| 21 | |
| 22 | #ifdef __BORLANDC__ |
| 23 | #pragma hdrstop |
| 24 | #endif |
| 25 | |
| 26 | #ifndef WX_PRECOMP |
| 27 | #include "wx/module.h" |
| 28 | #endif //WX_PRECOMP |
| 29 | |
| 30 | #include "wx/private/fdiodispatcher.h" |
| 31 | |
| 32 | #include "wx/private/selectdispatcher.h" |
| 33 | #ifdef __UNIX__ |
| 34 | #include "wx/unix/private/epolldispatcher.h" |
| 35 | #endif |
| 36 | |
| 37 | wxFDIODispatcher *gs_dispatcher = NULL; |
| 38 | |
| 39 | // ============================================================================ |
| 40 | // implementation |
| 41 | // ============================================================================ |
| 42 | |
| 43 | // ---------------------------------------------------------------------------- |
| 44 | // wxFDIODispatcher |
| 45 | // ---------------------------------------------------------------------------- |
| 46 | |
| 47 | /* static */ |
| 48 | wxFDIODispatcher *wxFDIODispatcher::Get() |
| 49 | { |
| 50 | if ( !gs_dispatcher ) |
| 51 | { |
| 52 | #if wxUSE_EPOLL_DISPATCHER |
| 53 | gs_dispatcher = wxEpollDispatcher::Create(); |
| 54 | if ( !gs_dispatcher ) |
| 55 | #endif // wxUSE_EPOLL_DISPATCHER |
| 56 | #if wxUSE_SELECT_DISPATCHER |
| 57 | gs_dispatcher = wxSelectDispatcher::Create(); |
| 58 | #endif // wxUSE_SELECT_DISPATCHER |
| 59 | } |
| 60 | |
| 61 | wxASSERT_MSG( gs_dispatcher, _T("failed to create any IO dispatchers") ); |
| 62 | |
| 63 | return gs_dispatcher; |
| 64 | } |
| 65 | |
| 66 | /* static */ |
| 67 | void wxFDIODispatcher::DispatchPending() |
| 68 | { |
| 69 | if ( gs_dispatcher ) |
| 70 | gs_dispatcher->Dispatch(0); |
| 71 | } |
| 72 | |
| 73 | // ---------------------------------------------------------------------------- |
| 74 | // wxMappedFDIODispatcher |
| 75 | // ---------------------------------------------------------------------------- |
| 76 | |
| 77 | wxFDIOHandler *wxMappedFDIODispatcher::FindHandler(int fd) const |
| 78 | { |
| 79 | const wxFDIOHandlerMap::const_iterator it = m_handlers.find(fd); |
| 80 | |
| 81 | return it == m_handlers.end() ? NULL : it->second.handler; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | bool wxMappedFDIODispatcher::RegisterFD(int fd, wxFDIOHandler *handler, int flags) |
| 86 | { |
| 87 | wxUnusedVar(flags); |
| 88 | |
| 89 | wxCHECK_MSG( handler, false, _T("handler can't be NULL") ); |
| 90 | |
| 91 | // notice that it's not an error to register a handler for the same fd |
| 92 | // twice as it can be done with different flags -- but it is an error to |
| 93 | // register different handlers |
| 94 | wxFDIOHandlerMap::iterator i = m_handlers.find(fd); |
| 95 | if ( i != m_handlers.end() ) |
| 96 | { |
| 97 | wxASSERT_MSG( i->second.handler == handler, |
| 98 | _T("registering different handler for the same fd?") ); |
| 99 | wxASSERT_MSG( i->second.flags != flags, |
| 100 | _T("reregistering with the same flags?") ); |
| 101 | } |
| 102 | |
| 103 | m_handlers[fd] = wxFDIOHandlerEntry(handler, flags); |
| 104 | |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | bool wxMappedFDIODispatcher::ModifyFD(int fd, wxFDIOHandler *handler, int flags) |
| 109 | { |
| 110 | wxUnusedVar(flags); |
| 111 | |
| 112 | wxCHECK_MSG( handler, false, _T("handler can't be NULL") ); |
| 113 | |
| 114 | wxFDIOHandlerMap::iterator i = m_handlers.find(fd); |
| 115 | wxCHECK_MSG( i != m_handlers.end(), false, |
| 116 | _T("modifying unregistered handler?") ); |
| 117 | |
| 118 | i->second = wxFDIOHandlerEntry(handler, flags); |
| 119 | |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | bool wxMappedFDIODispatcher::UnregisterFD(int fd) |
| 124 | { |
| 125 | wxFDIOHandlerMap::iterator i = m_handlers.find(fd); |
| 126 | if ( i == m_handlers.end() ) |
| 127 | return false; |
| 128 | |
| 129 | m_handlers.erase(i); |
| 130 | |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | // ---------------------------------------------------------------------------- |
| 135 | // wxSelectDispatcherModule |
| 136 | // ---------------------------------------------------------------------------- |
| 137 | |
| 138 | class wxFDIODispatcherModule : public wxModule |
| 139 | { |
| 140 | public: |
| 141 | virtual bool OnInit() { return true; } |
| 142 | virtual void OnExit() { wxDELETE(gs_dispatcher); } |
| 143 | |
| 144 | private: |
| 145 | DECLARE_DYNAMIC_CLASS(wxFDIODispatcherModule) |
| 146 | }; |
| 147 | |
| 148 | IMPLEMENT_DYNAMIC_CLASS(wxFDIODispatcherModule, wxModule) |