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