]> git.saurik.com Git - wxWidgets.git/blame - include/wx/gsocket.h
added shortcut functions AddSpacer and AddStretchSpacer (plus Prepend and Insert...
[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,
bfa7bf7d
VZ
82 GSOCK_MEMERR,
83 GSOCK_OPTERR,
a324a7bc
GL
84} GSocketError;
85
9bf10d6b
GRG
86/* See below for an explanation on how events work.
87 */
a324a7bc
GL
88typedef enum {
89 GSOCK_INPUT = 0,
90 GSOCK_OUTPUT = 1,
91 GSOCK_CONNECTION = 2,
92 GSOCK_LOST = 3,
93 GSOCK_MAX_EVENT = 4
94} GSocketEvent;
95
96enum {
97 GSOCK_INPUT_FLAG = 1 << GSOCK_INPUT,
98 GSOCK_OUTPUT_FLAG = 1 << GSOCK_OUTPUT,
99 GSOCK_CONNECTION_FLAG = 1 << GSOCK_CONNECTION,
31528cd3 100 GSOCK_LOST_FLAG = 1 << GSOCK_LOST
a324a7bc
GL
101};
102
103typedef int GSocketEventFlags;
104
39b91eca 105typedef void (*GSocketCallback)(GSocket *socket, GSocketEvent event,
31528cd3 106 char *cdata);
a324a7bc 107
a324a7bc 108
38bb138f
VS
109/* Functions tables for internal use by GSocket code: */
110
111#ifndef __WINDOWS__
112struct GSocketBaseFunctionsTable
113{
114 void (*Detected_Read)(GSocket *socket);
115 void (*Detected_Write)(GSocket *socket);
116};
117#endif
118
119struct GSocketGUIFunctionsTable
120{
121 int (*GUI_Init)(void);
122 void (*GUI_Cleanup)(void);
123 int (*GUI_Init_Socket)(GSocket *socket);
124 void (*GUI_Destroy_Socket)(GSocket *socket);
125#ifndef __WINDOWS__
126 void (*Install_Callback)(GSocket *socket, GSocketEvent event);
127 void (*Uninstall_Callback)(GSocket *socket, GSocketEvent event);
128#endif
129 void (*Enable_Events)(GSocket *socket);
130 void (*Disable_Events)(GSocket *socket);
131};
132
133
e9e3e3ab 134/* Global initializers */
a58d5df4 135
38bb138f
VS
136/* Sets GUI functions callbacks. Must be called *before* GSocket_Init
137 if the app uses async sockets. */
138void GSocket_SetGUIFunctions(struct GSocketGUIFunctionsTable *guifunc);
139
a58d5df4 140/* GSocket_Init() must be called at the beginning */
5c9eff30 141int GSocket_Init(void);
9bf10d6b
GRG
142
143/* GSocket_Cleanup() must be called at the end */
da051b23 144void GSocket_Cleanup(void);
a58d5df4 145
9bf10d6b 146
a324a7bc
GL
147/* Constructors / Destructors */
148
da051b23 149GSocket *GSocket_new(void);
a324a7bc
GL
150void GSocket_destroy(GSocket *socket);
151
9bf10d6b 152
09e6e5ec 153#ifndef wxUSE_GSOCKET_CPLUSPLUS
9bf10d6b
GRG
154
155/* GSocket_Shutdown:
156 * Disallow further read/write operations on this socket, close
157 * the fd and disable all callbacks.
158 */
a324a7bc
GL
159void GSocket_Shutdown(GSocket *socket);
160
161/* Address handling */
162
9bf10d6b
GRG
163/* GSocket_SetLocal:
164 * GSocket_GetLocal:
165 * GSocket_SetPeer:
166 * GSocket_GetPeer:
167 * Set or get the local or peer address for this socket. The 'set'
168 * functions return GSOCK_NOERROR on success, an error code otherwise.
169 * The 'get' functions return a pointer to a GAddress object on success,
170 * or NULL otherwise, in which case they set the error code of the
171 * corresponding GSocket.
172 *
173 * Error codes:
174 * GSOCK_INVSOCK - the socket is not valid.
175 * GSOCK_INVADDR - the address is not valid.
176 */
a324a7bc
GL
177GSocketError GSocket_SetLocal(GSocket *socket, GAddress *address);
178GSocketError GSocket_SetPeer(GSocket *socket, GAddress *address);
179GAddress *GSocket_GetLocal(GSocket *socket);
180GAddress *GSocket_GetPeer(GSocket *socket);
181
a324a7bc
GL
182/* Server specific parts */
183
e9e3e3ab 184/* GSocket_SetServer:
9bf10d6b
GRG
185 * Sets up this socket as a server. The local address must have been
186 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
187 * Returns GSOCK_NOERROR on success, one of the following otherwise:
33ac7e6f 188 *
9bf10d6b
GRG
189 * Error codes:
190 * GSOCK_INVSOCK - the socket is in use.
191 * GSOCK_INVADDR - the local address has not been set.
33ac7e6f 192 * GSOCK_IOERR - low-level error.
e9e3e3ab 193 */
a324a7bc
GL
194GSocketError GSocket_SetServer(GSocket *socket);
195
e9e3e3ab 196/* GSocket_WaitConnection:
9bf10d6b
GRG
197 * Waits for an incoming client connection. Returns a pointer to
198 * a GSocket object, or NULL if there was an error, in which case
199 * the last error field will be updated for the calling GSocket.
200 *
201 * Error codes (set in the calling GSocket)
202 * GSOCK_INVSOCK - the socket is not valid or not a server.
203 * GSOCK_TIMEDOUT - timeout, no incoming connections.
204 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
205 * GSOCK_MEMERR - couldn't allocate memory.
33ac7e6f 206 * GSOCK_IOERR - low-level error.
e9e3e3ab 207 */
a324a7bc
GL
208GSocket *GSocket_WaitConnection(GSocket *socket);
209
9bf10d6b 210
a324a7bc
GL
211/* Client specific parts */
212
e9e3e3ab 213/* GSocket_Connect:
9bf10d6b
GRG
214 * For stream (connection oriented) sockets, GSocket_Connect() tries
215 * to establish a client connection to a server using the peer address
216 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
217 * connection has been succesfully established, or one of the error
218 * codes listed below. Note that for nonblocking sockets, a return
219 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
220 * request can be completed later; you should use GSocket_Select()
221 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
222 * corresponding asynchronous events.
223 *
224 * For datagram (non connection oriented) sockets, GSocket_Connect()
225 * just sets the peer address established with GSocket_SetPeer() as
226 * default destination.
227 *
228 * Error codes:
229 * GSOCK_INVSOCK - the socket is in use or not valid.
230 * GSOCK_INVADDR - the peer address has not been established.
231 * GSOCK_TIMEDOUT - timeout, the connection failed.
232 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
233 * GSOCK_MEMERR - couldn't allocate memory.
33ac7e6f 234 * GSOCK_IOERR - low-level error.
e9e3e3ab 235 */
a324a7bc
GL
236GSocketError GSocket_Connect(GSocket *socket, GSocketStream stream);
237
9bf10d6b
GRG
238
239/* Datagram sockets */
240
241/* GSocket_SetNonOriented:
242 * Sets up this socket as a non-connection oriented (datagram) socket.
243 * Before using this function, the local address must have been set
244 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
245 * on success, or one of the following otherwise.
246 *
247 * Error codes:
248 * GSOCK_INVSOCK - the socket is in use.
249 * GSOCK_INVADDR - the local address has not been set.
33ac7e6f 250 * GSOCK_IOERR - low-level error.
9bf10d6b
GRG
251 */
252GSocketError GSocket_SetNonOriented(GSocket *socket);
253
254
a324a7bc
GL
255/* Generic IO */
256
257/* Like recv(), send(), ... */
e9e3e3ab 258
9bf10d6b
GRG
259/* For datagram sockets, the incoming / outgoing addresses
260 * are stored as / read from the 'peer' address field.
e9e3e3ab 261 */
a324a7bc
GL
262int GSocket_Read(GSocket *socket, char *buffer, int size);
263int GSocket_Write(GSocket *socket, const char *buffer,
31528cd3 264 int size);
e9e3e3ab
GRG
265
266/* GSocket_Select:
267 * Polls the socket to determine its status. This function will
268 * check for the events specified in the 'flags' parameter, and
269 * it will return a mask indicating which operations can be
270 * performed. This function won't block, regardless of the
9bf10d6b 271 * mode (blocking | nonblocking) of the socket.
e9e3e3ab
GRG
272 */
273GSocketEventFlags GSocket_Select(GSocket *socket, GSocketEventFlags flags);
a324a7bc 274
bfa7bf7d
VZ
275GSocketError GSocket_GetSockOpt(GSocket *socket, int level, int optname,
276 void *optval, int *optlen);
277
278GSocketError GSocket_SetSockOpt(GSocket *socket, int level, int optname,
279 const void *optval, int optlen);
7df69621 280GSocketError GSocket_SetReuseAddr(GSocket *socket);
bfa7bf7d
VZ
281
282void GSocket_Streamed(GSocket *socket);
283void GSocket_Unstreamed(GSocket *socket);
39b91eca 284
9bf10d6b 285/* Attributes */
a324a7bc 286
e9e3e3ab 287/* GSocket_SetNonBlocking:
9bf10d6b
GRG
288 * Sets the socket to non-blocking mode. All IO calls will return
289 * immediately.
e9e3e3ab 290 */
5c9eff30 291void GSocket_SetNonBlocking(GSocket *socket, int non_block);
a324a7bc 292
9bf10d6b
GRG
293/* GSocket_SetTimeout:
294 * Sets the timeout for blocking calls. Time is expressed in
295 * milliseconds.
296 */
297void GSocket_SetTimeout(GSocket *socket, unsigned long millisec);
298
b6db2e91 299#endif /* ndef wxUSE_GSOCKET_CPLUSPLUS */
09e6e5ec 300
e9e3e3ab 301/* GSocket_GetError:
9bf10d6b
GRG
302 * Returns the last error occured for this socket. Note that successful
303 * operations do not clear this back to GSOCK_NOERROR, so use it only
304 * after an error.
e9e3e3ab 305 */
7c4728f6 306GSocketError WXDLLIMPEXP_NET GSocket_GetError(GSocket *socket);
a324a7bc 307
09e6e5ec 308#ifndef wxUSE_GSOCKET_CPLUSPLUS
9bf10d6b 309
a324a7bc
GL
310/* Callbacks */
311
9bf10d6b
GRG
312/* GSOCK_INPUT:
313 * There is data to be read in the input buffer. If, after a read
314 * operation, there is still data available, the callback function will
315 * be called again.
316 * GSOCK_OUTPUT:
33ac7e6f 317 * The socket is available for writing. That is, the next write call
9bf10d6b
GRG
318 * won't block. This event is generated only once, when the connection is
319 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
320 * when the output buffer empties again. This means that the app should
321 * assume that it can write since the first OUTPUT event, and no more
322 * OUTPUT events will be generated unless an error occurs.
323 * GSOCK_CONNECTION:
324 * Connection succesfully established, for client sockets, or incoming
325 * client connection, for server sockets. Wait for this event (also watch
326 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
327 * GSOCK_LOST:
328 * The connection is lost (or a connection request failed); this could
329 * be due to a failure, or due to the peer closing it gracefully.
e9e3e3ab
GRG
330 */
331
332/* GSocket_SetCallback:
333 * Enables the callbacks specified by 'flags'. Note that 'flags'
334 * may be a combination of flags OR'ed toghether, so the same
335 * callback function can be made to accept different events.
336 * The callback function must have the following prototype:
337 *
338 * void function(GSocket *socket, GSocketEvent event, char *cdata)
339 */
340void GSocket_SetCallback(GSocket *socket, GSocketEventFlags flags,
39b91eca 341 GSocketCallback fallback, char *cdata);
a324a7bc 342
e9e3e3ab
GRG
343/* GSocket_UnsetCallback:
344 * Disables all callbacks specified by 'flags', which may be a
345 * combination of flags OR'ed toghether.
346 */
347void GSocket_UnsetCallback(GSocket *socket, GSocketEventFlags flags);
a324a7bc 348
b6db2e91 349#endif /* ndef wxUSE_GSOCKET_CPLUSPLUS */
09e6e5ec 350
9bf10d6b 351
a324a7bc
GL
352/* GAddress */
353
da051b23 354GAddress *GAddress_new(void);
a324a7bc
GL
355GAddress *GAddress_copy(GAddress *address);
356void GAddress_destroy(GAddress *address);
357
358void GAddress_SetFamily(GAddress *address, GAddressType type);
359GAddressType GAddress_GetFamily(GAddress *address);
360
e9e3e3ab
GRG
361/* The use of any of the next functions will set the address family to
362 * the specific one. For example if you use GAddress_INET_SetHostName,
363 * address family will be implicitly set to AF_INET.
364 */
a324a7bc
GL
365
366GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname);
9bf10d6b 367GSocketError GAddress_INET_SetAnyAddress(GAddress *address);
a324a7bc
GL
368GSocketError GAddress_INET_SetHostAddress(GAddress *address,
369 unsigned long hostaddr);
5a96d2f4
GL
370GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port,
371 const char *protocol);
a324a7bc
GL
372GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port);
373
374GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname,
375 size_t sbuf);
376unsigned long GAddress_INET_GetHostAddress(GAddress *address);
377unsigned short GAddress_INET_GetPort(GAddress *address);
378
379/* TODO: Define specific parts (INET6, UNIX) */
380
381GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path);
382GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf);
383
a324a7bc 384#ifdef __cplusplus
ba14d986 385}
d422d01e
RR
386#endif /* __cplusplus */
387
09e6e5ec
DE
388#ifdef wxUSE_GSOCKET_CPLUSPLUS
389#include "wx/unix/gsockunx.h"
b6db2e91 390#endif
d422d01e 391
0ce742cf 392#endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
a324a7bc 393
e9e3e3ab 394#endif /* __GSOCKET_H */