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