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