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