]> git.saurik.com Git - wxWidgets.git/blame - include/wx/gsocket.h
Commented out wxAutoNSAutoreleasePool in CallOnInit()
[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
RL
19
20#if !defined( __WXMSW__ ) && !defined( WXDLLEXPORT )
21#define WXDLLEXPORT
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
e9e3e3ab 98/* Global initializers */
a58d5df4
GL
99
100/* GSocket_Init() must be called at the beginning */
5c9eff30 101int GSocket_Init(void);
9bf10d6b
GRG
102
103/* GSocket_Cleanup() must be called at the end */
da051b23 104void GSocket_Cleanup(void);
a58d5df4 105
9bf10d6b 106
a324a7bc
GL
107/* Constructors / Destructors */
108
da051b23 109GSocket *GSocket_new(void);
a324a7bc
GL
110void GSocket_destroy(GSocket *socket);
111
9bf10d6b
GRG
112
113
114/* GSocket_Shutdown:
115 * Disallow further read/write operations on this socket, close
116 * the fd and disable all callbacks.
117 */
a324a7bc
GL
118void GSocket_Shutdown(GSocket *socket);
119
120/* Address handling */
121
9bf10d6b
GRG
122/* GSocket_SetLocal:
123 * GSocket_GetLocal:
124 * GSocket_SetPeer:
125 * GSocket_GetPeer:
126 * Set or get the local or peer address for this socket. The 'set'
127 * functions return GSOCK_NOERROR on success, an error code otherwise.
128 * The 'get' functions return a pointer to a GAddress object on success,
129 * or NULL otherwise, in which case they set the error code of the
130 * corresponding GSocket.
131 *
132 * Error codes:
133 * GSOCK_INVSOCK - the socket is not valid.
134 * GSOCK_INVADDR - the address is not valid.
135 */
a324a7bc
GL
136GSocketError GSocket_SetLocal(GSocket *socket, GAddress *address);
137GSocketError GSocket_SetPeer(GSocket *socket, GAddress *address);
138GAddress *GSocket_GetLocal(GSocket *socket);
139GAddress *GSocket_GetPeer(GSocket *socket);
140
a324a7bc
GL
141/* Server specific parts */
142
e9e3e3ab 143/* GSocket_SetServer:
9bf10d6b
GRG
144 * Sets up this socket as a server. The local address must have been
145 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
146 * Returns GSOCK_NOERROR on success, one of the following otherwise:
33ac7e6f 147 *
9bf10d6b
GRG
148 * Error codes:
149 * GSOCK_INVSOCK - the socket is in use.
150 * GSOCK_INVADDR - the local address has not been set.
33ac7e6f 151 * GSOCK_IOERR - low-level error.
e9e3e3ab 152 */
a324a7bc
GL
153GSocketError GSocket_SetServer(GSocket *socket);
154
e9e3e3ab 155/* GSocket_WaitConnection:
9bf10d6b
GRG
156 * Waits for an incoming client connection. Returns a pointer to
157 * a GSocket object, or NULL if there was an error, in which case
158 * the last error field will be updated for the calling GSocket.
159 *
160 * Error codes (set in the calling GSocket)
161 * GSOCK_INVSOCK - the socket is not valid or not a server.
162 * GSOCK_TIMEDOUT - timeout, no incoming connections.
163 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
164 * GSOCK_MEMERR - couldn't allocate memory.
33ac7e6f 165 * GSOCK_IOERR - low-level error.
e9e3e3ab 166 */
a324a7bc
GL
167GSocket *GSocket_WaitConnection(GSocket *socket);
168
9bf10d6b 169
a324a7bc
GL
170/* Client specific parts */
171
e9e3e3ab 172/* GSocket_Connect:
9bf10d6b
GRG
173 * For stream (connection oriented) sockets, GSocket_Connect() tries
174 * to establish a client connection to a server using the peer address
175 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
176 * connection has been succesfully established, or one of the error
177 * codes listed below. Note that for nonblocking sockets, a return
178 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
179 * request can be completed later; you should use GSocket_Select()
180 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
181 * corresponding asynchronous events.
182 *
183 * For datagram (non connection oriented) sockets, GSocket_Connect()
184 * just sets the peer address established with GSocket_SetPeer() as
185 * default destination.
186 *
187 * Error codes:
188 * GSOCK_INVSOCK - the socket is in use or not valid.
189 * GSOCK_INVADDR - the peer address has not been established.
190 * GSOCK_TIMEDOUT - timeout, the connection failed.
191 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
192 * GSOCK_MEMERR - couldn't allocate memory.
33ac7e6f 193 * GSOCK_IOERR - low-level error.
e9e3e3ab 194 */
a324a7bc
GL
195GSocketError GSocket_Connect(GSocket *socket, GSocketStream stream);
196
9bf10d6b
GRG
197
198/* Datagram sockets */
199
200/* GSocket_SetNonOriented:
201 * Sets up this socket as a non-connection oriented (datagram) socket.
202 * Before using this function, the local address must have been set
203 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
204 * on success, or one of the following otherwise.
205 *
206 * Error codes:
207 * GSOCK_INVSOCK - the socket is in use.
208 * GSOCK_INVADDR - the local address has not been set.
33ac7e6f 209 * GSOCK_IOERR - low-level error.
9bf10d6b
GRG
210 */
211GSocketError GSocket_SetNonOriented(GSocket *socket);
212
213
a324a7bc
GL
214/* Generic IO */
215
216/* Like recv(), send(), ... */
e9e3e3ab 217
9bf10d6b
GRG
218/* For datagram sockets, the incoming / outgoing addresses
219 * are stored as / read from the 'peer' address field.
e9e3e3ab 220 */
a324a7bc
GL
221int GSocket_Read(GSocket *socket, char *buffer, int size);
222int GSocket_Write(GSocket *socket, const char *buffer,
31528cd3 223 int size);
e9e3e3ab
GRG
224
225/* GSocket_Select:
226 * Polls the socket to determine its status. This function will
227 * check for the events specified in the 'flags' parameter, and
228 * it will return a mask indicating which operations can be
229 * performed. This function won't block, regardless of the
9bf10d6b 230 * mode (blocking | nonblocking) of the socket.
e9e3e3ab
GRG
231 */
232GSocketEventFlags GSocket_Select(GSocket *socket, GSocketEventFlags flags);
a324a7bc 233
39b91eca 234
9bf10d6b 235/* Attributes */
a324a7bc 236
e9e3e3ab 237/* GSocket_SetNonBlocking:
9bf10d6b
GRG
238 * Sets the socket to non-blocking mode. All IO calls will return
239 * immediately.
e9e3e3ab 240 */
5c9eff30 241void GSocket_SetNonBlocking(GSocket *socket, int non_block);
a324a7bc 242
9bf10d6b
GRG
243/* GSocket_SetTimeout:
244 * Sets the timeout for blocking calls. Time is expressed in
245 * milliseconds.
246 */
247void GSocket_SetTimeout(GSocket *socket, unsigned long millisec);
248
e9e3e3ab 249/* GSocket_GetError:
9bf10d6b
GRG
250 * Returns the last error occured for this socket. Note that successful
251 * operations do not clear this back to GSOCK_NOERROR, so use it only
252 * after an error.
e9e3e3ab 253 */
82805fe2 254GSocketError WXDLLEXPORT GSocket_GetError(GSocket *socket);
a324a7bc 255
9bf10d6b 256
a324a7bc
GL
257/* Callbacks */
258
9bf10d6b
GRG
259/* GSOCK_INPUT:
260 * There is data to be read in the input buffer. If, after a read
261 * operation, there is still data available, the callback function will
262 * be called again.
263 * GSOCK_OUTPUT:
33ac7e6f 264 * The socket is available for writing. That is, the next write call
9bf10d6b
GRG
265 * won't block. This event is generated only once, when the connection is
266 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
267 * when the output buffer empties again. This means that the app should
268 * assume that it can write since the first OUTPUT event, and no more
269 * OUTPUT events will be generated unless an error occurs.
270 * GSOCK_CONNECTION:
271 * Connection succesfully established, for client sockets, or incoming
272 * client connection, for server sockets. Wait for this event (also watch
273 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
274 * GSOCK_LOST:
275 * The connection is lost (or a connection request failed); this could
276 * be due to a failure, or due to the peer closing it gracefully.
e9e3e3ab
GRG
277 */
278
279/* GSocket_SetCallback:
280 * Enables the callbacks specified by 'flags'. Note that 'flags'
281 * may be a combination of flags OR'ed toghether, so the same
282 * callback function can be made to accept different events.
283 * The callback function must have the following prototype:
284 *
285 * void function(GSocket *socket, GSocketEvent event, char *cdata)
286 */
287void GSocket_SetCallback(GSocket *socket, GSocketEventFlags flags,
39b91eca 288 GSocketCallback fallback, char *cdata);
a324a7bc 289
e9e3e3ab
GRG
290/* GSocket_UnsetCallback:
291 * Disables all callbacks specified by 'flags', which may be a
292 * combination of flags OR'ed toghether.
293 */
294void GSocket_UnsetCallback(GSocket *socket, GSocketEventFlags flags);
a324a7bc 295
9bf10d6b 296
a324a7bc
GL
297/* GAddress */
298
da051b23 299GAddress *GAddress_new(void);
a324a7bc
GL
300GAddress *GAddress_copy(GAddress *address);
301void GAddress_destroy(GAddress *address);
302
303void GAddress_SetFamily(GAddress *address, GAddressType type);
304GAddressType GAddress_GetFamily(GAddress *address);
305
e9e3e3ab
GRG
306/* The use of any of the next functions will set the address family to
307 * the specific one. For example if you use GAddress_INET_SetHostName,
308 * address family will be implicitly set to AF_INET.
309 */
a324a7bc
GL
310
311GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname);
9bf10d6b 312GSocketError GAddress_INET_SetAnyAddress(GAddress *address);
a324a7bc
GL
313GSocketError GAddress_INET_SetHostAddress(GAddress *address,
314 unsigned long hostaddr);
5a96d2f4
GL
315GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port,
316 const char *protocol);
a324a7bc
GL
317GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port);
318
319GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname,
320 size_t sbuf);
321unsigned long GAddress_INET_GetHostAddress(GAddress *address);
322unsigned short GAddress_INET_GetPort(GAddress *address);
323
324/* TODO: Define specific parts (INET6, UNIX) */
325
326GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path);
327GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf);
328
a324a7bc 329#ifdef __cplusplus
ba14d986 330}
d422d01e
RR
331#endif /* __cplusplus */
332
333
0ce742cf 334#endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
a324a7bc 335
e9e3e3ab 336#endif /* __GSOCKET_H */