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