]>
Commit | Line | Data |
---|---|---|
51fe4b60 | 1 | ///////////////////////////////////////////////////////////////////////////// |
60913641 | 2 | // Name: wx/unix/private/sockunix.h |
51fe4b60 VZ |
3 | // Purpose: wxSocketImpl implementation for Unix systems |
4 | // Authors: Guilhem Lavaux, Vadim Zeitlin | |
5 | // Created: April 1997 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 1997 Guilhem Lavaux | |
8 | // (c) 2008 Vadim Zeitlin | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
483249fc | 11 | |
2804f77d VZ |
12 | #ifndef _WX_UNIX_GSOCKUNX_H_ |
13 | #define _WX_UNIX_GSOCKUNX_H_ | |
d422d01e | 14 | |
36b6a928 | 15 | #include <unistd.h> |
51fe4b60 | 16 | #include <sys/ioctl.h> |
a9d859df | 17 | #include "wx/private/fdiodispatcher.h" |
36b6a928 | 18 | |
a9d859df VZ |
19 | class wxSocketImplUnix : public wxSocketImpl, |
20 | public wxFDIOHandler | |
ba2a81d7 DE |
21 | { |
22 | public: | |
9123889f VZ |
23 | wxSocketImplUnix(wxSocketBase& wxsocket) |
24 | : wxSocketImpl(wxsocket) | |
25 | { | |
26 | m_fds[0] = | |
27 | m_fds[1] = -1; | |
28 | ||
acd523a9 | 29 | m_enabledCallbacks = 0; |
9123889f | 30 | } |
53a161e1 | 31 | |
2b036c4b VZ |
32 | virtual wxSocketError GetLastError() const; |
33 | ||
df21920b VZ |
34 | virtual void ReenableEvents(wxSocketEventFlags flags) |
35 | { | |
36 | // enable the notifications about input/output being available again in | |
37 | // case they were disabled by OnRead/WriteWaiting() | |
38 | // | |
39 | // notice that we'd like to enable the events here only if there is | |
40 | // nothing more left on the socket right now as otherwise we're going | |
41 | // to get a "ready for whatever" notification immediately (well, during | |
42 | // the next event loop iteration) and disable the event back again | |
43 | // which is rather inefficient but unfortunately doing it like this | |
44 | // doesn't work because the existing code (e.g. src/common/sckipc.cpp) | |
45 | // expects to keep getting notifications about the data available from | |
46 | // the socket even if it didn't read all the data the last time, so we | |
47 | // absolutely have to continue generating them | |
48 | EnableEvents(flags); | |
49 | } | |
50 | ||
a9d859df VZ |
51 | // wxFDIOHandler methods |
52 | virtual void OnReadWaiting(); | |
53 | virtual void OnWriteWaiting(); | |
54 | virtual void OnExceptionWaiting(); | |
8c029a5b | 55 | |
39b61b05 | 56 | // Unix-specific functions used by wxSocketFDIOManager only |
acd523a9 VZ |
57 | bool HasAnyEnabledCallbacks() const { return m_enabledCallbacks != 0; } |
58 | void EnableCallback(wxFDIODispatcherEntryFlags flag) | |
59 | { m_enabledCallbacks |= flag; } | |
60 | void DisableCallback(wxFDIODispatcherEntryFlags flag) | |
61 | { m_enabledCallbacks &= ~flag; } | |
62 | int GetEnabledCallbacks() const { return m_enabledCallbacks; } | |
63 | ||
f0fbbe23 | 64 | private: |
51fe4b60 VZ |
65 | virtual void DoClose() |
66 | { | |
7d66cdcc | 67 | DisableEvents(); |
51fe4b60 VZ |
68 | |
69 | close(m_fd); | |
70 | } | |
71 | ||
72 | virtual void UnblockAndRegisterWithEventLoop() | |
73 | { | |
74 | int trueArg = 1; | |
75 | ioctl(m_fd, FIONBIO, &trueArg); | |
76 | ||
77 | EnableEvents(); | |
78 | } | |
79 | ||
22185a1f | 80 | // enable or disable notifications for socket input/output events |
df21920b VZ |
81 | void EnableEvents(int flags = wxSOCKET_INPUT_FLAG | wxSOCKET_OUTPUT_FLAG) |
82 | { DoEnableEvents(flags, true); } | |
83 | void DisableEvents(int flags = wxSOCKET_INPUT_FLAG | wxSOCKET_OUTPUT_FLAG) | |
84 | { DoEnableEvents(flags, false); } | |
f0fbbe23 | 85 | |
22185a1f | 86 | // really enable or disable socket input/output events |
df21920b | 87 | void DoEnableEvents(int flags, bool enable); |
f0fbbe23 | 88 | |
51fe4b60 | 89 | protected: |
51fe4b60 VZ |
90 | // descriptors for input and output event notification channels associated |
91 | // with the socket | |
92 | int m_fds[2]; | |
53a161e1 | 93 | |
acd523a9 VZ |
94 | // the events which are currently enabled for this socket, combination of |
95 | // wxFDIO_INPUT and wxFDIO_OUTPUT values | |
acd523a9 VZ |
96 | int m_enabledCallbacks; |
97 | ||
53a161e1 VZ |
98 | private: |
99 | // notify the associated wxSocket about a change in socket state and shut | |
51fe4b60 VZ |
100 | // down the socket if the event is wxSOCKET_LOST |
101 | void OnStateChange(wxSocketNotify event); | |
102 | ||
df21920b VZ |
103 | // check if there is any input available, return 1 if yes, 0 if no or -1 on |
104 | // error | |
105 | int CheckForInput(); | |
106 | ||
107 | ||
51fe4b60 VZ |
108 | // give it access to our m_fds |
109 | friend class wxSocketFDBasedManager; | |
a324a7bc GL |
110 | }; |
111 | ||
51fe4b60 VZ |
112 | // A version of wxSocketManager which uses FDs for socket IO |
113 | class wxSocketFDBasedManager : public wxSocketManager | |
2804f77d VZ |
114 | { |
115 | public: | |
116 | // no special initialization/cleanup needed when using FDs | |
117 | virtual bool OnInit() { return true; } | |
118 | virtual void OnExit() { } | |
119 | ||
2804f77d VZ |
120 | protected: |
121 | // identifies either input or output direction | |
122 | // | |
123 | // NB: the values of this enum shouldn't change | |
124 | enum SocketDir | |
125 | { | |
126 | FD_INPUT, | |
127 | FD_OUTPUT | |
128 | }; | |
129 | ||
51fe4b60 VZ |
130 | // get the FD index corresponding to the given wxSocketNotify |
131 | SocketDir GetDirForEvent(wxSocketImpl *socket, wxSocketNotify event) | |
2804f77d VZ |
132 | { |
133 | switch ( event ) | |
134 | { | |
135 | default: | |
c363ead1 VZ |
136 | wxFAIL_MSG( "unknown socket event" ); |
137 | return FD_INPUT; // we must return something | |
2804f77d | 138 | |
51fe4b60 | 139 | case wxSOCKET_LOST: |
c363ead1 VZ |
140 | wxFAIL_MSG( "unexpected socket event" ); |
141 | return FD_INPUT; // as above | |
2804f77d | 142 | |
51fe4b60 | 143 | case wxSOCKET_INPUT: |
2804f77d VZ |
144 | return FD_INPUT; |
145 | ||
51fe4b60 | 146 | case wxSOCKET_OUTPUT: |
2804f77d VZ |
147 | return FD_OUTPUT; |
148 | ||
51fe4b60 | 149 | case wxSOCKET_CONNECTION: |
c363ead1 VZ |
150 | // for server sockets we're interested in events indicating |
151 | // that a new connection is pending, i.e. that accept() will | |
152 | // succeed and this is indicated by socket becoming ready for | |
153 | // reading, while for the other ones we're interested in the | |
154 | // completion of non-blocking connect() which is indicated by | |
155 | // the socket becoming ready for writing | |
2b036c4b | 156 | return socket->IsServer() ? FD_INPUT : FD_OUTPUT; |
2804f77d VZ |
157 | } |
158 | } | |
159 | ||
160 | // access the FDs we store | |
a9d859df | 161 | int& FD(wxSocketImplUnix *socket, SocketDir d) |
2804f77d | 162 | { |
a9d859df | 163 | return socket->m_fds[d]; |
2804f77d VZ |
164 | } |
165 | }; | |
166 | ||
167 | // Common base class for all ports using X11-like (and hence implemented in | |
168 | // X11, Motif and GTK) AddInput() and RemoveInput() functions | |
51fe4b60 | 169 | class wxSocketInputBasedManager : public wxSocketFDBasedManager |
2804f77d VZ |
170 | { |
171 | public: | |
a9d859df | 172 | virtual void Install_Callback(wxSocketImpl *socket_, wxSocketNotify event) |
2804f77d | 173 | { |
a9d859df VZ |
174 | wxSocketImplUnix * const |
175 | socket = static_cast<wxSocketImplUnix *>(socket_); | |
176 | ||
2804f77d VZ |
177 | wxCHECK_RET( socket->m_fd != -1, |
178 | "shouldn't be called on invalid socket" ); | |
179 | ||
180 | const SocketDir d = GetDirForEvent(socket, event); | |
181 | ||
182 | int& fd = FD(socket, d); | |
183 | if ( fd != -1 ) | |
184 | RemoveInput(fd); | |
185 | ||
a9d859df | 186 | fd = AddInput(socket, socket->m_fd, d); |
2804f77d VZ |
187 | } |
188 | ||
a9d859df | 189 | virtual void Uninstall_Callback(wxSocketImpl *socket_, wxSocketNotify event) |
2804f77d | 190 | { |
a9d859df VZ |
191 | wxSocketImplUnix * const |
192 | socket = static_cast<wxSocketImplUnix *>(socket_); | |
193 | ||
2804f77d VZ |
194 | const SocketDir d = GetDirForEvent(socket, event); |
195 | ||
196 | int& fd = FD(socket, d); | |
197 | if ( fd != -1 ) | |
198 | { | |
199 | RemoveInput(fd); | |
200 | fd = -1; | |
201 | } | |
202 | } | |
203 | ||
204 | private: | |
205 | // these functions map directly to XtAdd/RemoveInput() or | |
206 | // gdk_input_add/remove() | |
a9d859df | 207 | virtual int AddInput(wxFDIOHandler *handler, int fd, SocketDir d) = 0; |
2804f77d VZ |
208 | virtual void RemoveInput(int fd) = 0; |
209 | }; | |
d422d01e | 210 | |
2804f77d | 211 | #endif /* _WX_UNIX_GSOCKUNX_H_ */ |