added wxFDIODispatcher::HasPending() and implemented correctly wxConsoleEventLoop...
[wxWidgets.git] / include / wx / private / selectdispatcher.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/private/selectdispatcher.h
3 // Purpose: wxSelectDispatcher class
4 // Authors: Lukasz Michalski and Vadim Zeitlin
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
16 #if wxUSE_SELECT_DISPATCHER
17
18 #ifdef __WATCOMC__
19 #include <types.h>
20 #include <sys/ioctl.h>
21 #include <sys/time.h>
22 #include <sys/select.h>
23 #include <tcpustd.h>
24 #else
25 #include <sys/types.h>
26 #endif
27
28 #include "wx/private/fdiodispatcher.h"
29
30 // helper class storing all the select() fd sets
31 class WXDLLIMPEXP_BASE wxSelectSets
32 {
33 public:
34 // ctor zeroes out all fd_sets
35 wxSelectSets();
36
37 // default copy ctor, assignment operator and dtor are ok
38
39
40 // return true if fd appears in any of the sets
41 bool HasFD(int fd) const;
42
43 // add or remove FD to our sets depending on whether flags contains
44 // wxFDIO_INPUT/OUTPUT/EXCEPTION bits
45 bool SetFD(int fd, int flags);
46
47 // same as SetFD() except it unsets the bits set in the flags for the given
48 // fd
49 bool ClearFD(int fd)
50 {
51 return SetFD(fd, 0);
52 }
53
54
55 // call select() with our sets: the other parameters are the same as for
56 // select() itself
57 int Select(int nfds, struct timeval *tv);
58
59 // call the handler methods corresponding to the sets having this fd if it
60 // is present in any set and return true if it is
61 bool Handle(int fd, wxFDIOHandler& handler) const;
62
63 private:
64 typedef void (wxFDIOHandler::*Callback)();
65
66 // the FD sets indices
67 enum
68 {
69 Read,
70 Write,
71 Except,
72 Max
73 };
74
75 // the sets used with select()
76 fd_set m_fds[Max];
77
78 // the wxFDIO_XXX flags, functions and names (used for debug messages only)
79 // corresponding to the FD sets above
80 static int ms_flags[Max];
81 static const char *ms_names[Max];
82 static Callback ms_handlers[Max];
83 };
84
85 class WXDLLIMPEXP_BASE wxSelectDispatcher : public wxMappedFDIODispatcher
86 {
87 public:
88 // default ctor
89 wxSelectDispatcher() { m_maxFD = -1; }
90
91 // implement pure virtual methods of the base class
92 virtual bool RegisterFD(int fd, wxFDIOHandler *handler, int flags = wxFDIO_ALL);
93 virtual bool ModifyFD(int fd, wxFDIOHandler *handler, int flags = wxFDIO_ALL);
94 virtual bool UnregisterFD(int fd);
95 virtual bool HasPending() const;
96 virtual int Dispatch(int timeout = TIMEOUT_INFINITE);
97
98 private:
99 // common part of RegisterFD() and ModifyFD()
100 bool DoUpdateFDAndHandler(int fd, wxFDIOHandler *handler, int flags);
101
102 // call the handlers for the fds present in the given sets, return the
103 // number of handlers we called
104 int ProcessSets(const wxSelectSets& sets);
105
106 // helper of ProcessSets(): call the handler if its fd is in the set
107 void DoProcessFD(int fd, const fd_set& fds, wxFDIOHandler *handler,
108 const char *name);
109
110 // common part of HasPending() and Dispatch(): calls select() with the
111 // specified timeout
112 int DoSelect(wxSelectSets& sets, int timeout) const;
113
114
115 // the select sets containing all the registered fds
116 wxSelectSets m_sets;
117
118 // the highest registered fd value or -1 if none
119 int m_maxFD;
120 };
121
122 #endif // wxUSE_SELECT_DISPATCHER
123
124 #endif // _WX_PRIVATE_SOCKETEVTDISPATCH_H_