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