]> git.saurik.com Git - wxWidgets.git/blob - include/wx/gsocket.h
d19b9daf3a239ae74401fe9626feb4a829290d9f
[wxWidgets.git] / include / wx / gsocket.h
1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket)
3 * Name: gsocket.h
4 * Author: Guilhem Lavaux
5 * Guillermo Rodriguez Garcia <guille@iies.es> (maintainer)
6 * Purpose: GSocket include file (system independent)
7 * CVSID: $Id$
8 * -------------------------------------------------------------------------
9 */
10
11 #ifndef __GSOCKET_H
12 #define __GSOCKET_H
13
14 #ifdef __WINDOWS__
15 /* #define wxUSE_GSOCKET_CPLUSPLUS 1 */
16 #undef wxUSE_GSOCKET_CPLUSPLUS
17 #else
18 /* DFE: Define this and compile gsocket.cpp instead of gsocket.c and
19 compile existing GUI gsock*.c as C++ to try out the new GSocket. */
20 #undef wxUSE_GSOCKET_CPLUSPLUS
21 #endif
22 #if !defined(__cplusplus) && defined(wxUSE_GSOCKET_CPLUSPLUS)
23 #error "You need to compile this file (probably a GUI gsock peice) as C++"
24 #endif
25
26 #ifndef __GSOCKET_STANDALONE__
27 #include "wx/setup.h"
28 #include "wx/platform.h"
29
30 #include "wx/dlimpexp.h" /* for WXDLLIMPEXP_NET */
31
32 #endif
33
34 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
35
36 #include <stddef.h>
37
38 /*
39 Including sys/types.h under cygwin results in the warnings about "fd_set
40 having been defined in sys/types.h" when winsock.h is included later and
41 doesn't seem to be necessary anyhow. It's not needed under Mac neither.
42 */
43 #if !defined(__WXMAC__) && !defined(__CYGWIN__) && !defined(__WXWINCE__)
44 #include <sys/types.h>
45 #endif
46
47 #ifdef __WXWINCE__
48 #include <stdlib.h>
49 #endif
50
51 #ifdef wxUSE_GSOCKET_CPLUSPLUS
52 # ifdef __WINDOWS__
53 class GSocket;
54 # else
55 typedef class GSocketBSD GSocket;
56 # endif
57 #endif
58
59 #ifdef __cplusplus
60 extern "C" {
61 #endif
62
63 #ifndef wxUSE_GSOCKET_CPLUSPLUS
64 typedef struct _GSocket GSocket;
65 #endif
66 typedef struct _GAddress GAddress;
67
68 typedef enum {
69 GSOCK_NOFAMILY = 0,
70 GSOCK_INET,
71 GSOCK_INET6,
72 GSOCK_UNIX
73 } GAddressType;
74
75 typedef enum {
76 GSOCK_STREAMED,
77 GSOCK_UNSTREAMED
78 } GSocketStream;
79
80 typedef enum {
81 GSOCK_NOERROR = 0,
82 GSOCK_INVOP,
83 GSOCK_IOERR,
84 GSOCK_INVADDR,
85 GSOCK_INVSOCK,
86 GSOCK_NOHOST,
87 GSOCK_INVPORT,
88 GSOCK_WOULDBLOCK,
89 GSOCK_TIMEDOUT,
90 GSOCK_MEMERR,
91 GSOCK_OPTERR,
92 } GSocketError;
93
94 /* See below for an explanation on how events work.
95 */
96 typedef enum {
97 GSOCK_INPUT = 0,
98 GSOCK_OUTPUT = 1,
99 GSOCK_CONNECTION = 2,
100 GSOCK_LOST = 3,
101 GSOCK_MAX_EVENT = 4
102 } GSocketEvent;
103
104 enum {
105 GSOCK_INPUT_FLAG = 1 << GSOCK_INPUT,
106 GSOCK_OUTPUT_FLAG = 1 << GSOCK_OUTPUT,
107 GSOCK_CONNECTION_FLAG = 1 << GSOCK_CONNECTION,
108 GSOCK_LOST_FLAG = 1 << GSOCK_LOST
109 };
110
111 typedef int GSocketEventFlags;
112
113 typedef void (*GSocketCallback)(GSocket *socket, GSocketEvent event,
114 char *cdata);
115
116
117 /* Functions tables for internal use by GSocket code: */
118
119 #ifndef __WINDOWS__
120 struct GSocketBaseFunctionsTable
121 {
122 void (*Detected_Read)(GSocket *socket);
123 void (*Detected_Write)(GSocket *socket);
124 };
125 #endif
126
127 #if defined(__WINDOWS__) && defined(wxUSE_GSOCKET_CPLUSPLUS)
128 /* Actually this is a misnomer now, but reusing this name means I don't
129 have to ifdef app traits or common socket code */
130 class GSocketGUIFunctionsTable
131 {
132 public:
133 virtual bool OnInit() = 0;
134 virtual void OnExit() = 0;
135 virtual bool CanUseEventLoop() = 0;
136 virtual bool Init_Socket(GSocket *socket) = 0;
137 virtual void Destroy_Socket(GSocket *socket) = 0;
138 #ifndef __WINDOWS__
139 virtual void Install_Callback(GSocket *socket, GSocketEvent event) = 0;
140 virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event) = 0;
141 #endif
142 virtual void Enable_Events(GSocket *socket) = 0;
143 virtual void Disable_Events(GSocket *socket) = 0;
144 };
145
146 #else
147 struct GSocketGUIFunctionsTable
148 {
149 int (*GUI_Init)(void);
150 void (*GUI_Cleanup)(void);
151 int (*GUI_Init_Socket)(GSocket *socket);
152 void (*GUI_Destroy_Socket)(GSocket *socket);
153 #ifndef __WINDOWS__
154 void (*Install_Callback)(GSocket *socket, GSocketEvent event);
155 void (*Uninstall_Callback)(GSocket *socket, GSocketEvent event);
156 #endif
157 void (*Enable_Events)(GSocket *socket);
158 void (*Disable_Events)(GSocket *socket);
159 };
160 #endif /* defined(__WINDOWS__) && defined(wxUSE_GSOCKET_CPLUSPLUS) */
161
162
163 /* Global initializers */
164
165 /* Sets GUI functions callbacks. Must be called *before* GSocket_Init
166 if the app uses async sockets. */
167 void GSocket_SetGUIFunctions(struct GSocketGUIFunctionsTable *guifunc);
168
169 /* GSocket_Init() must be called at the beginning */
170 int GSocket_Init(void);
171
172 /* GSocket_Cleanup() must be called at the end */
173 void GSocket_Cleanup(void);
174
175
176 /* Constructors / Destructors */
177
178 GSocket *GSocket_new(void);
179 #if !defined(__WINDOWS__) || !defined(wxUSE_GSOCKET_CPLUSPLUS)
180 void GSocket_destroy(GSocket *socket);
181 #endif
182
183
184 #ifndef wxUSE_GSOCKET_CPLUSPLUS
185
186 /* GSocket_Shutdown:
187 * Disallow further read/write operations on this socket, close
188 * the fd and disable all callbacks.
189 */
190 void GSocket_Shutdown(GSocket *socket);
191
192 /* Address handling */
193
194 /* GSocket_SetLocal:
195 * GSocket_GetLocal:
196 * GSocket_SetPeer:
197 * GSocket_GetPeer:
198 * Set or get the local or peer address for this socket. The 'set'
199 * functions return GSOCK_NOERROR on success, an error code otherwise.
200 * The 'get' functions return a pointer to a GAddress object on success,
201 * or NULL otherwise, in which case they set the error code of the
202 * corresponding GSocket.
203 *
204 * Error codes:
205 * GSOCK_INVSOCK - the socket is not valid.
206 * GSOCK_INVADDR - the address is not valid.
207 */
208 GSocketError GSocket_SetLocal(GSocket *socket, GAddress *address);
209 GSocketError GSocket_SetPeer(GSocket *socket, GAddress *address);
210 GAddress *GSocket_GetLocal(GSocket *socket);
211 GAddress *GSocket_GetPeer(GSocket *socket);
212
213 /* Server specific parts */
214
215 /* GSocket_SetServer:
216 * Sets up this socket as a server. The local address must have been
217 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
218 * Returns GSOCK_NOERROR on success, one of the following otherwise:
219 *
220 * Error codes:
221 * GSOCK_INVSOCK - the socket is in use.
222 * GSOCK_INVADDR - the local address has not been set.
223 * GSOCK_IOERR - low-level error.
224 */
225 GSocketError GSocket_SetServer(GSocket *socket);
226
227 /* GSocket_WaitConnection:
228 * Waits for an incoming client connection. Returns a pointer to
229 * a GSocket object, or NULL if there was an error, in which case
230 * the last error field will be updated for the calling GSocket.
231 *
232 * Error codes (set in the calling GSocket)
233 * GSOCK_INVSOCK - the socket is not valid or not a server.
234 * GSOCK_TIMEDOUT - timeout, no incoming connections.
235 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
236 * GSOCK_MEMERR - couldn't allocate memory.
237 * GSOCK_IOERR - low-level error.
238 */
239 GSocket *GSocket_WaitConnection(GSocket *socket);
240
241
242 /* Client specific parts */
243
244 /* GSocket_Connect:
245 * For stream (connection oriented) sockets, GSocket_Connect() tries
246 * to establish a client connection to a server using the peer address
247 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
248 * connection has been succesfully established, or one of the error
249 * codes listed below. Note that for nonblocking sockets, a return
250 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
251 * request can be completed later; you should use GSocket_Select()
252 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
253 * corresponding asynchronous events.
254 *
255 * For datagram (non connection oriented) sockets, GSocket_Connect()
256 * just sets the peer address established with GSocket_SetPeer() as
257 * default destination.
258 *
259 * Error codes:
260 * GSOCK_INVSOCK - the socket is in use or not valid.
261 * GSOCK_INVADDR - the peer address has not been established.
262 * GSOCK_TIMEDOUT - timeout, the connection failed.
263 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
264 * GSOCK_MEMERR - couldn't allocate memory.
265 * GSOCK_IOERR - low-level error.
266 */
267 GSocketError GSocket_Connect(GSocket *socket, GSocketStream stream);
268
269 /* GSocket_SetReusable:
270 * Simply sets the m_resuable flag on the socket. GSocket_SetServer will
271 * make the appropriate setsockopt() call.
272 * Implemented as a GSocket function because clients (ie, wxSocketServer)
273 * don't have access to the GSocket struct information.
274 * Returns TRUE if the flag was set correctly, FALSE if an error occured
275 * (ie, if the parameter was NULL)
276 */
277 int GSocket_SetReusable(GSocket *socket);
278
279 /* Datagram sockets */
280
281 /* GSocket_SetNonOriented:
282 * Sets up this socket as a non-connection oriented (datagram) socket.
283 * Before using this function, the local address must have been set
284 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
285 * on success, or one of the following otherwise.
286 *
287 * Error codes:
288 * GSOCK_INVSOCK - the socket is in use.
289 * GSOCK_INVADDR - the local address has not been set.
290 * GSOCK_IOERR - low-level error.
291 */
292 GSocketError GSocket_SetNonOriented(GSocket *socket);
293
294
295 /* Generic IO */
296
297 /* Like recv(), send(), ... */
298
299 /* For datagram sockets, the incoming / outgoing addresses
300 * are stored as / read from the 'peer' address field.
301 */
302 int GSocket_Read(GSocket *socket, char *buffer, int size);
303 int GSocket_Write(GSocket *socket, const char *buffer,
304 int size);
305
306 /* GSocket_Select:
307 * Polls the socket to determine its status. This function will
308 * check for the events specified in the 'flags' parameter, and
309 * it will return a mask indicating which operations can be
310 * performed. This function won't block, regardless of the
311 * mode (blocking | nonblocking) of the socket.
312 */
313 GSocketEventFlags GSocket_Select(GSocket *socket, GSocketEventFlags flags);
314
315 GSocketError GSocket_GetSockOpt(GSocket *socket, int level, int optname,
316 void *optval, int *optlen);
317
318 GSocketError GSocket_SetSockOpt(GSocket *socket, int level, int optname,
319 const void *optval, int optlen);
320
321 /* Attributes */
322
323 /* GSocket_SetNonBlocking:
324 * Sets the socket to non-blocking mode. All IO calls will return
325 * immediately.
326 */
327 void GSocket_SetNonBlocking(GSocket *socket, int non_block);
328
329 /* GSocket_SetTimeout:
330 * Sets the timeout for blocking calls. Time is expressed in
331 * milliseconds.
332 */
333 void GSocket_SetTimeout(GSocket *socket, unsigned long millisec);
334
335 #endif /* ndef wxUSE_GSOCKET_CPLUSPLUS */
336
337 /* GSocket_GetError:
338 * Returns the last error occured for this socket. Note that successful
339 * operations do not clear this back to GSOCK_NOERROR, so use it only
340 * after an error.
341 */
342 GSocketError WXDLLIMPEXP_NET GSocket_GetError(GSocket *socket);
343
344 #ifndef wxUSE_GSOCKET_CPLUSPLUS
345
346 /* Callbacks */
347
348 /* GSOCK_INPUT:
349 * There is data to be read in the input buffer. If, after a read
350 * operation, there is still data available, the callback function will
351 * be called again.
352 * GSOCK_OUTPUT:
353 * The socket is available for writing. That is, the next write call
354 * won't block. This event is generated only once, when the connection is
355 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
356 * when the output buffer empties again. This means that the app should
357 * assume that it can write since the first OUTPUT event, and no more
358 * OUTPUT events will be generated unless an error occurs.
359 * GSOCK_CONNECTION:
360 * Connection succesfully established, for client sockets, or incoming
361 * client connection, for server sockets. Wait for this event (also watch
362 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
363 * GSOCK_LOST:
364 * The connection is lost (or a connection request failed); this could
365 * be due to a failure, or due to the peer closing it gracefully.
366 */
367
368 /* GSocket_SetCallback:
369 * Enables the callbacks specified by 'flags'. Note that 'flags'
370 * may be a combination of flags OR'ed toghether, so the same
371 * callback function can be made to accept different events.
372 * The callback function must have the following prototype:
373 *
374 * void function(GSocket *socket, GSocketEvent event, char *cdata)
375 */
376 void GSocket_SetCallback(GSocket *socket, GSocketEventFlags flags,
377 GSocketCallback fallback, char *cdata);
378
379 /* GSocket_UnsetCallback:
380 * Disables all callbacks specified by 'flags', which may be a
381 * combination of flags OR'ed toghether.
382 */
383 void GSocket_UnsetCallback(GSocket *socket, GSocketEventFlags flags);
384
385 #endif /* ndef wxUSE_GSOCKET_CPLUSPLUS */
386
387
388 /* GAddress */
389
390 GAddress *GAddress_new(void);
391 GAddress *GAddress_copy(GAddress *address);
392 void GAddress_destroy(GAddress *address);
393
394 void GAddress_SetFamily(GAddress *address, GAddressType type);
395 GAddressType GAddress_GetFamily(GAddress *address);
396
397 /* The use of any of the next functions will set the address family to
398 * the specific one. For example if you use GAddress_INET_SetHostName,
399 * address family will be implicitly set to AF_INET.
400 */
401
402 GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname);
403 GSocketError GAddress_INET_SetAnyAddress(GAddress *address);
404 GSocketError GAddress_INET_SetHostAddress(GAddress *address,
405 unsigned long hostaddr);
406 GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port,
407 const char *protocol);
408 GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port);
409
410 GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname,
411 size_t sbuf);
412 unsigned long GAddress_INET_GetHostAddress(GAddress *address);
413 unsigned short GAddress_INET_GetPort(GAddress *address);
414
415 /* TODO: Define specific parts (INET6, UNIX) */
416
417 GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path);
418 GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf);
419
420 #ifdef __cplusplus
421 }
422 #endif /* __cplusplus */
423
424 #ifdef wxUSE_GSOCKET_CPLUSPLUS
425 # ifdef __WINDOWS__
426 # include "wx/msw/gsockmsw.h"
427 # else
428 # include "wx/unix/gsockunx.h"
429 # endif
430 #endif
431
432 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
433
434 #endif /* __GSOCKET_H */