]>
Commit | Line | Data |
---|---|---|
04889c59 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/private/gsocket.h | |
3 | // Purpose: GSocket implementation | |
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 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef _WX_PRIVATE_GSOCKET_H_ | |
13 | #define _WX_PRIVATE_GSOCKET_H_ | |
a324a7bc | 14 | |
2ecf902b | 15 | #include "wx/defs.h" |
ab8af15f | 16 | |
2804f77d | 17 | #if wxUSE_SOCKETS |
d422d01e | 18 | |
2804f77d | 19 | #include "wx/dlimpexp.h" /* for WXDLLIMPEXP_NET */ |
d422d01e | 20 | |
53a161e1 VZ |
21 | class WXDLLIMPEXP_FWD_NET wxSocketBase; |
22 | ||
98781fa3 | 23 | #include <stddef.h> |
58071ea0 VZ |
24 | |
25 | /* | |
d80170bd | 26 | Including sys/types.h under Cygwin results in the warnings about "fd_set |
58071ea0 VZ |
27 | having been defined in sys/types.h" when winsock.h is included later and |
28 | doesn't seem to be necessary anyhow. It's not needed under Mac neither. | |
29 | */ | |
882dfc67 | 30 | #if !defined(__WXMAC__) && !defined(__CYGWIN__) && !defined(__WXWINCE__) |
a324a7bc | 31 | #include <sys/types.h> |
a02bb143 | 32 | #endif |
a324a7bc | 33 | |
882dfc67 JS |
34 | #ifdef __WXWINCE__ |
35 | #include <stdlib.h> | |
36 | #endif | |
37 | ||
d932a670 VZ |
38 | // include the header defining timeval: under Windows this struct is used only |
39 | // with sockets so we need to include winsock.h which we do via windows.h | |
40 | #ifdef __WXMSW__ | |
41 | #include "wx/msw/wrapwin.h" | |
42 | #else | |
43 | #include <sys/time.h> // for timeval | |
44 | #endif | |
87c0312a | 45 | |
2804f77d VZ |
46 | enum GAddressType |
47 | { | |
a324a7bc GL |
48 | GSOCK_NOFAMILY = 0, |
49 | GSOCK_INET, | |
50 | GSOCK_INET6, | |
51 | GSOCK_UNIX | |
2804f77d | 52 | }; |
a324a7bc | 53 | |
2804f77d VZ |
54 | enum GSocketStream |
55 | { | |
a324a7bc GL |
56 | GSOCK_STREAMED, |
57 | GSOCK_UNSTREAMED | |
2804f77d | 58 | }; |
a324a7bc | 59 | |
2804f77d VZ |
60 | enum GSocketError |
61 | { | |
a324a7bc GL |
62 | GSOCK_NOERROR = 0, |
63 | GSOCK_INVOP, | |
64 | GSOCK_IOERR, | |
65 | GSOCK_INVADDR, | |
66 | GSOCK_INVSOCK, | |
67 | GSOCK_NOHOST, | |
f439844b | 68 | GSOCK_INVPORT, |
98781fa3 | 69 | GSOCK_WOULDBLOCK, |
aa6d9706 | 70 | GSOCK_TIMEDOUT, |
bfa7bf7d | 71 | GSOCK_MEMERR, |
98bdbbe3 | 72 | GSOCK_OPTERR |
2804f77d | 73 | }; |
a324a7bc | 74 | |
9bf10d6b GRG |
75 | /* See below for an explanation on how events work. |
76 | */ | |
2804f77d VZ |
77 | enum GSocketEvent |
78 | { | |
a324a7bc GL |
79 | GSOCK_INPUT = 0, |
80 | GSOCK_OUTPUT = 1, | |
81 | GSOCK_CONNECTION = 2, | |
82 | GSOCK_LOST = 3, | |
83 | GSOCK_MAX_EVENT = 4 | |
2804f77d | 84 | }; |
a324a7bc | 85 | |
2804f77d VZ |
86 | enum |
87 | { | |
a324a7bc GL |
88 | GSOCK_INPUT_FLAG = 1 << GSOCK_INPUT, |
89 | GSOCK_OUTPUT_FLAG = 1 << GSOCK_OUTPUT, | |
90 | GSOCK_CONNECTION_FLAG = 1 << GSOCK_CONNECTION, | |
31528cd3 | 91 | GSOCK_LOST_FLAG = 1 << GSOCK_LOST |
a324a7bc GL |
92 | }; |
93 | ||
94 | typedef int GSocketEventFlags; | |
95 | ||
d80170bd | 96 | struct GAddress; |
2804f77d VZ |
97 | class GSocket; |
98 | ||
39b91eca | 99 | typedef void (*GSocketCallback)(GSocket *socket, GSocketEvent event, |
31528cd3 | 100 | char *cdata); |
a324a7bc | 101 | |
2804f77d VZ |
102 | /* |
103 | Class providing hooks abstracting the differences between console and GUI | |
104 | applications for socket code. | |
105 | ||
106 | We also have different implementations of this class for different platforms | |
107 | allowing us to keep more things in the common code but the main reason for | |
108 | its existence is that we want the same socket code work differently | |
109 | depending on whether it's used from a console or a GUI program. This is | |
110 | achieved by implementing the virtual methods of this class differently in | |
111 | the objects returned by wxConsoleAppTraits::GetSocketFunctionsTable() and | |
112 | the same method in wxGUIAppTraits. | |
113 | */ | |
114 | class GSocketManager | |
4b4d23c7 DE |
115 | { |
116 | public: | |
2804f77d VZ |
117 | // set the manager to use, we don't take ownership of it |
118 | // | |
119 | // this should be called before GSocket_Init(), i.e. before the first | |
120 | // wxSocket object is created, otherwise the manager returned by | |
121 | // wxAppTraits::GetSocketManager() will be used | |
122 | static void Set(GSocketManager *manager); | |
123 | ||
124 | // return the manager to use | |
125 | // | |
126 | // this initializes the manager at first use | |
127 | static GSocketManager *Get() | |
128 | { | |
129 | if ( !ms_manager ) | |
130 | Init(); | |
131 | ||
132 | return ms_manager; | |
133 | } | |
134 | ||
135 | // called before the first wxSocket is created and should do the | |
136 | // initializations needed in order to use the network | |
137 | // | |
138 | // return true if initialized successfully | |
4b4d23c7 | 139 | virtual bool OnInit() = 0; |
2804f77d VZ |
140 | |
141 | // undo the initializations of OnInit() | |
4b4d23c7 | 142 | virtual void OnExit() = 0; |
2804f77d VZ |
143 | |
144 | ||
f0fbbe23 VZ |
145 | // do manager-specific socket initializations: called in the beginning of |
146 | // the socket initialization | |
4b4d23c7 | 147 | virtual bool Init_Socket(GSocket *socket) = 0; |
f0fbbe23 VZ |
148 | |
149 | // called when the socket is being closed | |
150 | // | |
151 | // TODO: merge this with Destroy_Socket(), currently 2 separate functions | |
152 | // are needed because Init_Socket() always allocates manager-specific | |
153 | // resources in GSocket and Destroy_Socket() must be called even if | |
154 | // the socket has never been opened, but if the allocation were done | |
155 | // on demand, then Destroy_Socket() could be called from | |
156 | // GSocket::Close() and we wouldn't need Close_Socket() at all | |
157 | virtual void Close_Socket(GSocket *socket) = 0; | |
158 | ||
159 | // undo Init_Socket(): called from GSocket dtor | |
4b4d23c7 | 160 | virtual void Destroy_Socket(GSocket *socket) = 0; |
2804f77d | 161 | |
2804f77d | 162 | |
f0fbbe23 VZ |
163 | // these functions enable or disable monitoring of the given socket for the |
164 | // specified events inside the currently running event loop (but notice | |
165 | // that both BSD and Winsock implementations actually use socket->m_server | |
166 | // value to determine what exactly should be monitored so it needs to be | |
167 | // set before calling these functions) | |
168 | virtual void Install_Callback(GSocket *socket, | |
169 | GSocketEvent event = GSOCK_MAX_EVENT) = 0; | |
170 | virtual void Uninstall_Callback(GSocket *socket, | |
171 | GSocketEvent event = GSOCK_MAX_EVENT) = 0; | |
2804f77d VZ |
172 | |
173 | virtual ~GSocketManager() { } | |
174 | ||
175 | private: | |
176 | // get the manager to use if we don't have it yet | |
177 | static void Init(); | |
178 | ||
179 | static GSocketManager *ms_manager; | |
4b4d23c7 DE |
180 | }; |
181 | ||
f0db5d75 VZ |
182 | /* |
183 | Base class providing functionality common to BSD and Winsock sockets. | |
184 | ||
185 | TODO: merge this in wxSocket itself, there is no reason to maintain the | |
186 | separation between wxSocket and GSocket. | |
187 | */ | |
188 | class GSocketBase | |
189 | { | |
190 | public: | |
53a161e1 VZ |
191 | // static factory function: creates the low-level socket associated with |
192 | // the given wxSocket (and inherits its attributes such as timeout) | |
193 | static GSocket *Create(wxSocketBase& wxsocket); | |
eb97543d VZ |
194 | |
195 | virtual ~GSocketBase(); | |
196 | ||
02564412 VZ |
197 | void SetTimeout(unsigned long millisec); |
198 | ||
199 | GSocketError SetLocal(GAddress *address); | |
200 | GSocketError SetPeer(GAddress *address); | |
201 | GAddress *GetLocal(); | |
202 | GAddress *GetPeer(); | |
203 | ||
f0db5d75 VZ |
204 | GSocketEventFlags Select(GSocketEventFlags flags); |
205 | ||
53a161e1 VZ |
206 | virtual GSocket *WaitConnection(wxSocketBase& wxsocket) = 0; |
207 | ||
f0fbbe23 | 208 | void Close(); |
eb97543d VZ |
209 | virtual void Shutdown(); |
210 | ||
3a6ec3c8 VZ |
211 | void SetInitialSocketBuffers(int recv, int send) |
212 | { | |
213 | m_initialRecvBufferSize = recv; | |
214 | m_initialSendBufferSize = send; | |
215 | } | |
216 | ||
83a7ab5c VZ |
217 | // notify m_wxsocket about the given socket event by calling its (inaptly |
218 | // named) OnRequest() method | |
219 | void NotifyOnStateChange(GSocketEvent event); | |
220 | ||
53107283 VZ |
221 | // FIXME: making these functions virtual is a hack necessary to make the |
222 | // wxBase library link without requiring wxNet under Unix where | |
223 | // GSocketSelectManager (part of wxBase) uses them, they don't | |
224 | // really need to be virtual at all | |
225 | virtual void Detected_Read() { } | |
226 | virtual void Detected_Write() { } | |
227 | ||
ff55f837 VZ |
228 | // this is officially SOCKET (unsigned int) under Windows but we don't want |
229 | // to include winsock.h which defines SOCKET from here so just use int | |
230 | // under all platforms | |
f0db5d75 | 231 | int m_fd; |
f0db5d75 | 232 | |
f0db5d75 VZ |
233 | int m_initialRecvBufferSize; |
234 | int m_initialSendBufferSize; | |
235 | ||
236 | GAddress *m_local; | |
237 | GAddress *m_peer; | |
238 | GSocketError m_error; | |
239 | ||
240 | bool m_non_blocking; | |
241 | bool m_server; | |
242 | bool m_stream; | |
243 | bool m_establishing; | |
244 | bool m_reusable; | |
245 | bool m_broadcast; | |
246 | bool m_dobind; | |
247 | ||
f0db5d75 | 248 | struct timeval m_timeout; |
f0db5d75 VZ |
249 | |
250 | GSocketEventFlags m_detected; | |
eb97543d VZ |
251 | |
252 | protected: | |
53a161e1 VZ |
253 | GSocketBase(wxSocketBase& wxsocket); |
254 | ||
53a161e1 VZ |
255 | private: |
256 | // set in ctor and never changed except that it's reset to NULL when the | |
257 | // socket is shut down | |
258 | wxSocketBase *m_wxsocket; | |
259 | ||
260 | DECLARE_NO_COPY_CLASS(GSocketBase) | |
f0db5d75 VZ |
261 | }; |
262 | ||
2804f77d VZ |
263 | #if defined(__WINDOWS__) |
264 | #include "wx/msw/gsockmsw.h" | |
265 | #else | |
266 | #include "wx/unix/gsockunx.h" | |
267 | #endif | |
38bb138f | 268 | |
2804f77d | 269 | /* Global initializers */ |
38bb138f | 270 | |
2804f77d VZ |
271 | /* GSocket_Init() must be called at the beginning (but after calling |
272 | * GSocketManager::Set() if a custom manager should be used) */ | |
273 | bool GSocket_Init(); | |
9bf10d6b GRG |
274 | |
275 | /* GSocket_Cleanup() must be called at the end */ | |
2804f77d | 276 | void GSocket_Cleanup(); |
a58d5df4 | 277 | |
9bf10d6b | 278 | |
a324a7bc GL |
279 | /* GAddress */ |
280 | ||
d80170bd VZ |
281 | // Represents a socket endpoint, i.e. -- in spite of its name -- not an address |
282 | // but an (address, port) pair | |
283 | struct GAddress | |
284 | { | |
285 | struct sockaddr *m_addr; | |
286 | size_t m_len; | |
287 | ||
288 | GAddressType m_family; | |
289 | int m_realfamily; | |
290 | ||
291 | GSocketError m_error; | |
292 | }; | |
293 | ||
2804f77d | 294 | GAddress *GAddress_new(); |
a324a7bc GL |
295 | GAddress *GAddress_copy(GAddress *address); |
296 | void GAddress_destroy(GAddress *address); | |
297 | ||
298 | void GAddress_SetFamily(GAddress *address, GAddressType type); | |
299 | GAddressType GAddress_GetFamily(GAddress *address); | |
300 | ||
e9e3e3ab GRG |
301 | /* The use of any of the next functions will set the address family to |
302 | * the specific one. For example if you use GAddress_INET_SetHostName, | |
303 | * address family will be implicitly set to AF_INET. | |
304 | */ | |
a324a7bc GL |
305 | |
306 | GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname); | |
60edcf45 | 307 | GSocketError GAddress_INET_SetBroadcastAddress(GAddress *address); |
9bf10d6b | 308 | GSocketError GAddress_INET_SetAnyAddress(GAddress *address); |
a324a7bc GL |
309 | GSocketError GAddress_INET_SetHostAddress(GAddress *address, |
310 | unsigned long hostaddr); | |
5a96d2f4 GL |
311 | GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port, |
312 | const char *protocol); | |
a324a7bc GL |
313 | GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port); |
314 | ||
315 | GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, | |
316 | size_t sbuf); | |
317 | unsigned long GAddress_INET_GetHostAddress(GAddress *address); | |
318 | unsigned short GAddress_INET_GetPort(GAddress *address); | |
319 | ||
d80170bd VZ |
320 | GSocketError _GAddress_translate_from(GAddress *address, |
321 | struct sockaddr *addr, int len); | |
322 | GSocketError _GAddress_translate_to (GAddress *address, | |
323 | struct sockaddr **addr, int *len); | |
324 | GSocketError _GAddress_Init_INET(GAddress *address); | |
325 | ||
8575ff50 VZ |
326 | #if wxUSE_IPV6 |
327 | ||
328 | GSocketError GAddress_INET6_SetHostName(GAddress *address, const char *hostname); | |
329 | GSocketError GAddress_INET6_SetAnyAddress(GAddress *address); | |
330 | GSocketError GAddress_INET6_SetHostAddress(GAddress *address, | |
331 | struct in6_addr hostaddr); | |
332 | GSocketError GAddress_INET6_SetPortName(GAddress *address, const char *port, | |
333 | const char *protocol); | |
334 | GSocketError GAddress_INET6_SetPort(GAddress *address, unsigned short port); | |
335 | ||
336 | GSocketError GAddress_INET6_GetHostName(GAddress *address, char *hostname, | |
337 | size_t sbuf); | |
338 | GSocketError GAddress_INET6_GetHostAddress(GAddress *address,struct in6_addr *hostaddr); | |
339 | unsigned short GAddress_INET6_GetPort(GAddress *address); | |
340 | ||
341 | #endif // wxUSE_IPV6 | |
342 | ||
d80170bd VZ |
343 | // these functions are available under all platforms but only implemented under |
344 | // Unix ones, elsewhere they just return GSOCK_INVADDR | |
345 | GSocketError _GAddress_Init_UNIX(GAddress *address); | |
a324a7bc GL |
346 | GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path); |
347 | GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf); | |
348 | ||
2804f77d | 349 | #endif /* wxUSE_SOCKETS */ |
a324a7bc | 350 | |
04889c59 | 351 | #endif /* _WX_PRIVATE_GSOCKET_H_ */ |