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