]>
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 | ||
29 | m_use_events = false; | |
acd523a9 | 30 | m_enabledCallbacks = 0; |
9123889f | 31 | } |
53a161e1 | 32 | |
eb97543d | 33 | virtual void Shutdown(); |
51fe4b60 VZ |
34 | virtual wxSocketImpl *WaitConnection(wxSocketBase& wxsocket); |
35 | ||
09e6e5ec DE |
36 | int Read(char *buffer, int size); |
37 | int Write(const char *buffer, int size); | |
2804f77d VZ |
38 | //attach or detach from main loop |
39 | void Notify(bool flag); | |
a9d859df VZ |
40 | |
41 | // wxFDIOHandler methods | |
42 | virtual void OnReadWaiting(); | |
43 | virtual void OnWriteWaiting(); | |
44 | virtual void OnExceptionWaiting(); | |
8c029a5b | 45 | |
acd523a9 VZ |
46 | // Unix-specific functions |
47 | bool HasAnyEnabledCallbacks() const { return m_enabledCallbacks != 0; } | |
48 | void EnableCallback(wxFDIODispatcherEntryFlags flag) | |
49 | { m_enabledCallbacks |= flag; } | |
50 | void DisableCallback(wxFDIODispatcherEntryFlags flag) | |
51 | { m_enabledCallbacks &= ~flag; } | |
52 | int GetEnabledCallbacks() const { return m_enabledCallbacks; } | |
53 | ||
f0fbbe23 | 54 | private: |
51fe4b60 VZ |
55 | virtual wxSocketError DoHandleConnect(int ret); |
56 | virtual void DoClose() | |
57 | { | |
58 | wxSocketManager * const manager = wxSocketManager::Get(); | |
59 | if ( manager ) | |
60 | { | |
61 | manager->Uninstall_Callback(this, wxSOCKET_INPUT); | |
62 | manager->Uninstall_Callback(this, wxSOCKET_OUTPUT); | |
63 | } | |
64 | ||
65 | close(m_fd); | |
66 | } | |
67 | ||
68 | virtual void UnblockAndRegisterWithEventLoop() | |
69 | { | |
70 | int trueArg = 1; | |
71 | ioctl(m_fd, FIONBIO, &trueArg); | |
72 | ||
73 | EnableEvents(); | |
74 | } | |
75 | ||
f0fbbe23 VZ |
76 | // enable or disable notifications for socket input/output events but only |
77 | // if m_use_events is true; do nothing otherwise | |
51fe4b60 | 78 | virtual void EnableEvents() |
f0fbbe23 VZ |
79 | { |
80 | if ( m_use_events ) | |
81 | DoEnableEvents(true); | |
82 | } | |
83 | ||
84 | void DisableEvents() | |
85 | { | |
86 | if ( m_use_events ) | |
87 | DoEnableEvents(false); | |
88 | } | |
89 | ||
90 | // really enable or disable socket input/output events, regardless of | |
91 | // m_use_events value | |
92 | void DoEnableEvents(bool enable); | |
93 | ||
94 | ||
95 | // enable or disable events for the given event if m_use_events; do nothing | |
96 | // otherwise | |
97 | // | |
98 | // notice that these functions also update m_detected: EnableEvent() clears | |
99 | // the corresponding bit in it and DisableEvent() sets it | |
51fe4b60 VZ |
100 | void EnableEvent(wxSocketNotify event); |
101 | void DisableEvent(wxSocketNotify event); | |
f0fbbe23 VZ |
102 | |
103 | ||
51fe4b60 VZ |
104 | wxSocketError Input_Timeout(); |
105 | wxSocketError Output_Timeout(); | |
09e6e5ec DE |
106 | int Recv_Stream(char *buffer, int size); |
107 | int Recv_Dgram(char *buffer, int size); | |
108 | int Send_Stream(const char *buffer, int size); | |
109 | int Send_Dgram(const char *buffer, int size); | |
a324a7bc | 110 | |
acd523a9 | 111 | |
51fe4b60 VZ |
112 | protected: |
113 | // true if socket should fire events | |
114 | bool m_use_events; | |
2804f77d | 115 | |
51fe4b60 VZ |
116 | // descriptors for input and output event notification channels associated |
117 | // with the socket | |
118 | int m_fds[2]; | |
53a161e1 | 119 | |
acd523a9 VZ |
120 | // the events which are currently enabled for this socket, combination of |
121 | // wxFDIO_INPUT and wxFDIO_OUTPUT values | |
122 | // | |
123 | // TODO: this overlaps with m_detected but the semantics of the latter are | |
124 | // very unclear so I don't dare to remove it right now | |
125 | int m_enabledCallbacks; | |
126 | ||
53a161e1 VZ |
127 | private: |
128 | // notify the associated wxSocket about a change in socket state and shut | |
51fe4b60 VZ |
129 | // down the socket if the event is wxSOCKET_LOST |
130 | void OnStateChange(wxSocketNotify event); | |
131 | ||
132 | // give it access to our m_fds | |
133 | friend class wxSocketFDBasedManager; | |
a324a7bc GL |
134 | }; |
135 | ||
51fe4b60 VZ |
136 | // A version of wxSocketManager which uses FDs for socket IO |
137 | class wxSocketFDBasedManager : public wxSocketManager | |
2804f77d VZ |
138 | { |
139 | public: | |
140 | // no special initialization/cleanup needed when using FDs | |
141 | virtual bool OnInit() { return true; } | |
142 | virtual void OnExit() { } | |
143 | ||
2804f77d VZ |
144 | protected: |
145 | // identifies either input or output direction | |
146 | // | |
147 | // NB: the values of this enum shouldn't change | |
148 | enum SocketDir | |
149 | { | |
150 | FD_INPUT, | |
151 | FD_OUTPUT | |
152 | }; | |
153 | ||
51fe4b60 VZ |
154 | // get the FD index corresponding to the given wxSocketNotify |
155 | SocketDir GetDirForEvent(wxSocketImpl *socket, wxSocketNotify event) | |
2804f77d VZ |
156 | { |
157 | switch ( event ) | |
158 | { | |
159 | default: | |
160 | wxFAIL_MSG( "unexpected socket event" ); | |
161 | // fall through | |
162 | ||
51fe4b60 | 163 | case wxSOCKET_LOST: |
2804f77d VZ |
164 | // fall through |
165 | ||
51fe4b60 | 166 | case wxSOCKET_INPUT: |
2804f77d VZ |
167 | return FD_INPUT; |
168 | ||
51fe4b60 | 169 | case wxSOCKET_OUTPUT: |
2804f77d VZ |
170 | return FD_OUTPUT; |
171 | ||
51fe4b60 | 172 | case wxSOCKET_CONNECTION: |
2804f77d VZ |
173 | // FIXME: explain this? |
174 | return socket->m_server ? FD_INPUT : FD_OUTPUT; | |
175 | } | |
176 | } | |
177 | ||
178 | // access the FDs we store | |
a9d859df | 179 | int& FD(wxSocketImplUnix *socket, SocketDir d) |
2804f77d | 180 | { |
a9d859df | 181 | return socket->m_fds[d]; |
2804f77d VZ |
182 | } |
183 | }; | |
184 | ||
185 | // Common base class for all ports using X11-like (and hence implemented in | |
186 | // X11, Motif and GTK) AddInput() and RemoveInput() functions | |
51fe4b60 | 187 | class wxSocketInputBasedManager : public wxSocketFDBasedManager |
2804f77d VZ |
188 | { |
189 | public: | |
a9d859df | 190 | virtual void Install_Callback(wxSocketImpl *socket_, wxSocketNotify event) |
2804f77d | 191 | { |
a9d859df VZ |
192 | wxSocketImplUnix * const |
193 | socket = static_cast<wxSocketImplUnix *>(socket_); | |
194 | ||
2804f77d VZ |
195 | wxCHECK_RET( socket->m_fd != -1, |
196 | "shouldn't be called on invalid socket" ); | |
197 | ||
198 | const SocketDir d = GetDirForEvent(socket, event); | |
199 | ||
200 | int& fd = FD(socket, d); | |
201 | if ( fd != -1 ) | |
202 | RemoveInput(fd); | |
203 | ||
a9d859df | 204 | fd = AddInput(socket, socket->m_fd, d); |
2804f77d VZ |
205 | } |
206 | ||
a9d859df | 207 | virtual void Uninstall_Callback(wxSocketImpl *socket_, wxSocketNotify event) |
2804f77d | 208 | { |
a9d859df VZ |
209 | wxSocketImplUnix * const |
210 | socket = static_cast<wxSocketImplUnix *>(socket_); | |
211 | ||
2804f77d VZ |
212 | const SocketDir d = GetDirForEvent(socket, event); |
213 | ||
214 | int& fd = FD(socket, d); | |
215 | if ( fd != -1 ) | |
216 | { | |
217 | RemoveInput(fd); | |
218 | fd = -1; | |
219 | } | |
220 | } | |
221 | ||
222 | private: | |
223 | // these functions map directly to XtAdd/RemoveInput() or | |
224 | // gdk_input_add/remove() | |
a9d859df | 225 | virtual int AddInput(wxFDIOHandler *handler, int fd, SocketDir d) = 0; |
2804f77d VZ |
226 | virtual void RemoveInput(int fd) = 0; |
227 | }; | |
d422d01e | 228 | |
2804f77d | 229 | #endif /* _WX_UNIX_GSOCKUNX_H_ */ |