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