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