]>
Commit | Line | Data |
---|---|---|
30c45bdd VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/private/selectdispatcher.h | |
3 | // Purpose: wxSelectDispatcher class | |
b46b1d59 | 4 | // Authors: Lukasz Michalski and Vadim Zeitlin |
30c45bdd VZ |
5 | // Created: December 2006 |
6 | // Copyright: (c) Lukasz Michalski | |
7 | // RCS-ID: $Id$ | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifndef _WX_PRIVATE_SELECTDISPATCHER_H_ | |
12 | #define _WX_PRIVATE_SELECTDISPATCHER_H_ | |
13 | ||
14 | #include "wx/defs.h" | |
15 | ||
c3938816 DE |
16 | #include <sys/types.h> |
17 | ||
b46b1d59 | 18 | #include "wx/private/fdiodispatcher.h" |
30c45bdd | 19 | |
b46b1d59 VZ |
20 | // helper class storing all the select() fd sets |
21 | class WXDLLIMPEXP_BASE wxSelectSets | |
30c45bdd VZ |
22 | { |
23 | public: | |
b46b1d59 VZ |
24 | // ctor zeroes out all fd_sets |
25 | wxSelectSets(); | |
30c45bdd | 26 | |
b46b1d59 | 27 | // default copy ctor, assignment operator and dtor are ok |
72fa3e8a | 28 | |
30c45bdd | 29 | |
b46b1d59 VZ |
30 | // return true if fd appears in any of the sets |
31 | bool HasFD(int fd) const; | |
30c45bdd | 32 | |
b46b1d59 VZ |
33 | // add or remove FD to our sets depending on whether flags contains |
34 | // wxFDIO_INPUT/OUTPUT/EXCEPTION bits | |
35 | bool SetFD(int fd, int flags); | |
30c45bdd | 36 | |
b46b1d59 VZ |
37 | // same as SetFD() except it unsets the bits set in the flags for the given |
38 | // fd | |
39 | bool ClearFD(int fd, int flags) | |
30c45bdd | 40 | { |
b46b1d59 | 41 | return SetFD(fd, wxFDIO_ALL & ~flags); |
30c45bdd VZ |
42 | } |
43 | ||
30c45bdd | 44 | |
b46b1d59 VZ |
45 | // call select() with our sets: the other parameters are the same as for |
46 | // select() itself | |
47 | int Select(int nfds, struct timeval *tv); | |
48 | ||
49 | // call the handler methods corresponding to the sets having this fd | |
50 | void Handle(int fd, wxFDIOHandler& handler) const; | |
51 | ||
52 | private: | |
53 | typedef void (wxFDIOHandler::*Callback)(); | |
54 | ||
55 | // the FD sets indices | |
56 | enum | |
57 | { | |
58 | Read, | |
59 | Write, | |
60 | Except, | |
61 | Max | |
62 | }; | |
63 | ||
64 | // the sets used with select() | |
65 | fd_set m_fds[Max]; | |
66 | ||
67 | // the wxFDIO_XXX flags, functions and names (used for debug messages only) | |
68 | // corresponding to the FD sets above | |
69 | static int ms_flags[Max]; | |
70 | static const char *ms_names[Max]; | |
71 | static Callback ms_handlers[Max]; | |
72 | }; | |
73 | ||
ad8d42f8 | 74 | class WXDLLIMPEXP_BASE wxSelectDispatcher : public wxMappedFDIODispatcher |
b46b1d59 VZ |
75 | { |
76 | public: | |
77 | // returns the unique instance of this class, the pointer shouldn't be | |
78 | // deleted and is normally never NULL | |
79 | static wxSelectDispatcher *Get(); | |
30c45bdd | 80 | |
b46b1d59 VZ |
81 | // if we have any registered handlers, check for any pending events to them |
82 | // and dispatch them -- this is used from wxX11 and wxDFB event loops | |
83 | // implementation | |
84 | static void DispatchPending(); | |
30c45bdd | 85 | |
b46b1d59 VZ |
86 | // implement pure virtual methods of the base class |
87 | virtual bool RegisterFD(int fd, wxFDIOHandler *handler, int flags = wxFDIO_ALL); | |
88 | virtual bool ModifyFD(int fd, wxFDIOHandler *handler, int flags = wxFDIO_ALL); | |
ad8d42f8 | 89 | virtual bool UnregisterFD(int fd, int flags = wxFDIO_ALL); |
b46b1d59 | 90 | virtual void RunLoop(int timeout = TIMEOUT_INFINITE); |
30c45bdd VZ |
91 | |
92 | protected: | |
b46b1d59 | 93 | wxSelectDispatcher(); |
30c45bdd VZ |
94 | |
95 | private: | |
b46b1d59 VZ |
96 | // common part of RegisterFD() and ModifyFD() |
97 | bool DoUpdateFDAndHandler(int fd, wxFDIOHandler *handler, int flags); | |
30c45bdd | 98 | |
b46b1d59 VZ |
99 | // call the handlers for the fds present in the given sets |
100 | void ProcessSets(const wxSelectSets& sets); | |
30c45bdd | 101 | |
b46b1d59 VZ |
102 | // helper of ProcessSets(): call the handler if its fd is in the set |
103 | void DoProcessFD(int fd, const fd_set& fds, wxFDIOHandler *handler, | |
104 | const char *name); | |
30c45bdd | 105 | |
30c45bdd | 106 | |
b46b1d59 VZ |
107 | // the select sets containing all the registered fds |
108 | wxSelectSets m_sets; | |
109 | ||
110 | // the highest registered fd value or -1 if none | |
111 | int m_maxFD; | |
30c45bdd VZ |
112 | }; |
113 | ||
114 | ||
115 | #endif // _WX_PRIVATE_SOCKETEVTDISPATCH_H_ |