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