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