X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/72fa3e8a218ea7a521410dda24803a608e3f04ca..7344108e8a129a3f9b4df5ab0f98a1713db03b89:/include/wx/private/selectdispatcher.h diff --git a/include/wx/private/selectdispatcher.h b/include/wx/private/selectdispatcher.h index 2b43b03aa7..5c17fe650e 100644 --- a/include/wx/private/selectdispatcher.h +++ b/include/wx/private/selectdispatcher.h @@ -1,8 +1,7 @@ ///////////////////////////////////////////////////////////////////////////// // Name: wx/private/selectdispatcher.h // Purpose: wxSelectDispatcher class -// Authors: Lukasz Michalski -// Modified by: +// Authors: Lukasz Michalski and Vadim Zeitlin // Created: December 2006 // Copyright: (c) Lukasz Michalski // RCS-ID: $Id$ @@ -14,83 +13,115 @@ #include "wx/defs.h" -#include "wx/hashmap.h" +#if wxUSE_SELECT_DISPATCHER -static const int wxSELECT_TIMEOUT_INFINITE = -1; +#if defined(HAVE_SYS_SELECT_H) || defined(__WATCOMC__) + #include + #include +#endif -// handler used to process events on descriptors -class wxFDIOHandler -{ -public: - // called when descriptor is available for non-blocking read - virtual void OnReadWaiting(int fd) = 0; +#ifdef __WATCOMC__ + #include + #include + #include +#else + #include +#endif - // called when descriptor is available for non-blocking write - virtual void OnWriteWaiting(int fd) = 0; +#include "wx/private/fdiodispatcher.h" - // called when there is exception on descriptor - virtual void OnExceptionWaiting(int fd) = 0; +// helper class storing all the select() fd sets +class WXDLLIMPEXP_BASE wxSelectSets +{ +public: + // ctor zeroes out all fd_sets + wxSelectSets(); - // virtual dtor for the base class - virtual ~wxFDIOHandler() { } -}; + // default copy ctor, assignment operator and dtor are ok -// those flags describes sets where descriptor should be added -enum wxSelectDispatcherEntryFlags -{ - wxSelectInput = 1, - wxSelectOutput = 2, - wxSelectException = 4, - wxSelectAll = wxSelectInput | wxSelectOutput | wxSelectException -}; -WX_DECLARE_HASH_MAP( - int, - wxFDIOHandler*, - wxIntegerHash, - wxIntegerEqual, - wxFDIOHandlerMap -); + // return true if fd appears in any of the sets + bool HasFD(int fd) const; -class WXDLLIMPEXP_CORE wxSelectDispatcher -{ -public: - // returns instance of the table - static wxSelectDispatcher& Get(); + // add or remove FD to our sets depending on whether flags contains + // wxFDIO_INPUT/OUTPUT/EXCEPTION bits + bool SetFD(int fd, int flags); - virtual ~wxSelectDispatcher() + // same as SetFD() except it unsets the bits set in the flags for the given + // fd + bool ClearFD(int fd) { + return SetFD(fd, 0); } - // register descriptor in sets. - void RegisterFD(int fd, wxFDIOHandler* handler, int flags = wxSelectAll); - // unregister descriptor from sets and return handler for cleanup - wxFDIOHandler* UnregisterFD(int fd, int flags = wxSelectAll); + // call select() with our sets: the other parameters are the same as for + // select() itself + int Select(int nfds, struct timeval *tv); + + // call the handler methods corresponding to the sets having this fd if it + // is present in any set and return true if it is + bool Handle(int fd, wxFDIOHandler& handler) const; - // return handler for descriptor or null if fd is not registered - wxFDIOHandler* FindHandler(int fd); +private: + typedef void (wxFDIOHandler::*Callback)(); - // calls select on registered descriptors and - void RunLoop(int timeout = wxSELECT_TIMEOUT_INFINITE); + // the FD sets indices + enum + { + Read, + Write, + Except, + Max + }; + + // the sets used with select() + fd_set m_fds[Max]; + + // the wxFDIO_XXX flags, functions and names (used for debug messages only) + // corresponding to the FD sets above + static int ms_flags[Max]; + static const char *ms_names[Max]; + static Callback ms_handlers[Max]; +}; + +class WXDLLIMPEXP_BASE wxSelectDispatcher : public wxMappedFDIODispatcher +{ +public: + // default ctor + wxSelectDispatcher() { m_maxFD = -1; } -protected: - wxSelectDispatcher() { } + // implement pure virtual methods of the base class + virtual bool RegisterFD(int fd, wxFDIOHandler *handler, int flags = wxFDIO_ALL); + virtual bool ModifyFD(int fd, wxFDIOHandler *handler, int flags = wxFDIO_ALL); + virtual bool UnregisterFD(int fd); + virtual bool HasPending() const; + virtual int Dispatch(int timeout = TIMEOUT_INFINITE); private: - void ProcessSets(fd_set* readset, fd_set* writeset, fd_set* exeptset, int max_fd); + // common part of RegisterFD() and ModifyFD() + bool DoUpdateFDAndHandler(int fd, wxFDIOHandler *handler, int flags); - fd_set m_readset; - fd_set m_writeset; - fd_set m_exeptset; + // call the handlers for the fds present in the given sets, return the + // number of handlers we called + int ProcessSets(const wxSelectSets& sets); - int m_maxFD; - wxFDIOHandlerMap m_handlers; + // helper of ProcessSets(): call the handler if its fd is in the set + void DoProcessFD(int fd, const fd_set& fds, wxFDIOHandler *handler, + const char *name); + + // common part of HasPending() and Dispatch(): calls select() with the + // specified timeout + int DoSelect(wxSelectSets& sets, int timeout) const; - static wxSelectDispatcher *ms_instance; - friend class wxSelectDispatcherModule; + // the select sets containing all the registered fds + wxSelectSets m_sets; + + // the highest registered fd value or -1 if none + int m_maxFD; }; +#endif // wxUSE_SELECT_DISPATCHER #endif // _WX_PRIVATE_SOCKETEVTDISPATCH_H_