]>
Commit | Line | Data |
---|---|---|
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 | typedef class GSocketBSD 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 | #ifndef __WINDOWS__ | |
112 | struct GSocketBaseFunctionsTable | |
113 | { | |
114 | void (*Detected_Read)(GSocket *socket); | |
115 | void (*Detected_Write)(GSocket *socket); | |
116 | }; | |
117 | #endif | |
118 | ||
119 | struct GSocketGUIFunctionsTable | |
120 | { | |
121 | int (*GUI_Init)(void); | |
122 | void (*GUI_Cleanup)(void); | |
123 | int (*GUI_Init_Socket)(GSocket *socket); | |
124 | void (*GUI_Destroy_Socket)(GSocket *socket); | |
125 | #ifndef __WINDOWS__ | |
126 | void (*Install_Callback)(GSocket *socket, GSocketEvent event); | |
127 | void (*Uninstall_Callback)(GSocket *socket, GSocketEvent event); | |
128 | #endif | |
129 | void (*Enable_Events)(GSocket *socket); | |
130 | void (*Disable_Events)(GSocket *socket); | |
131 | }; | |
132 | ||
133 | ||
134 | /* Global initializers */ | |
135 | ||
136 | /* Sets GUI functions callbacks. Must be called *before* GSocket_Init | |
137 | if the app uses async sockets. */ | |
138 | void GSocket_SetGUIFunctions(struct GSocketGUIFunctionsTable *guifunc); | |
139 | ||
140 | /* GSocket_Init() must be called at the beginning */ | |
141 | int GSocket_Init(void); | |
142 | ||
143 | /* GSocket_Cleanup() must be called at the end */ | |
144 | void GSocket_Cleanup(void); | |
145 | ||
146 | ||
147 | /* Constructors / Destructors */ | |
148 | ||
149 | GSocket *GSocket_new(void); | |
150 | void GSocket_destroy(GSocket *socket); | |
151 | ||
152 | ||
153 | #ifndef wxUSE_GSOCKET_CPLUSPLUS | |
154 | ||
155 | /* GSocket_Shutdown: | |
156 | * Disallow further read/write operations on this socket, close | |
157 | * the fd and disable all callbacks. | |
158 | */ | |
159 | void GSocket_Shutdown(GSocket *socket); | |
160 | ||
161 | /* Address handling */ | |
162 | ||
163 | /* GSocket_SetLocal: | |
164 | * GSocket_GetLocal: | |
165 | * GSocket_SetPeer: | |
166 | * GSocket_GetPeer: | |
167 | * Set or get the local or peer address for this socket. The 'set' | |
168 | * functions return GSOCK_NOERROR on success, an error code otherwise. | |
169 | * The 'get' functions return a pointer to a GAddress object on success, | |
170 | * or NULL otherwise, in which case they set the error code of the | |
171 | * corresponding GSocket. | |
172 | * | |
173 | * Error codes: | |
174 | * GSOCK_INVSOCK - the socket is not valid. | |
175 | * GSOCK_INVADDR - the address is not valid. | |
176 | */ | |
177 | GSocketError GSocket_SetLocal(GSocket *socket, GAddress *address); | |
178 | GSocketError GSocket_SetPeer(GSocket *socket, GAddress *address); | |
179 | GAddress *GSocket_GetLocal(GSocket *socket); | |
180 | GAddress *GSocket_GetPeer(GSocket *socket); | |
181 | ||
182 | /* Server specific parts */ | |
183 | ||
184 | /* GSocket_SetServer: | |
185 | * Sets up this socket as a server. The local address must have been | |
186 | * set with GSocket_SetLocal() before GSocket_SetServer() is called. | |
187 | * Returns GSOCK_NOERROR on success, one of the following otherwise: | |
188 | * | |
189 | * Error codes: | |
190 | * GSOCK_INVSOCK - the socket is in use. | |
191 | * GSOCK_INVADDR - the local address has not been set. | |
192 | * GSOCK_IOERR - low-level error. | |
193 | */ | |
194 | GSocketError GSocket_SetServer(GSocket *socket); | |
195 | ||
196 | /* GSocket_WaitConnection: | |
197 | * Waits for an incoming client connection. Returns a pointer to | |
198 | * a GSocket object, or NULL if there was an error, in which case | |
199 | * the last error field will be updated for the calling GSocket. | |
200 | * | |
201 | * Error codes (set in the calling GSocket) | |
202 | * GSOCK_INVSOCK - the socket is not valid or not a server. | |
203 | * GSOCK_TIMEDOUT - timeout, no incoming connections. | |
204 | * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking. | |
205 | * GSOCK_MEMERR - couldn't allocate memory. | |
206 | * GSOCK_IOERR - low-level error. | |
207 | */ | |
208 | GSocket *GSocket_WaitConnection(GSocket *socket); | |
209 | ||
210 | ||
211 | /* Client specific parts */ | |
212 | ||
213 | /* GSocket_Connect: | |
214 | * For stream (connection oriented) sockets, GSocket_Connect() tries | |
215 | * to establish a client connection to a server using the peer address | |
216 | * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the | |
217 | * connection has been succesfully established, or one of the error | |
218 | * codes listed below. Note that for nonblocking sockets, a return | |
219 | * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection | |
220 | * request can be completed later; you should use GSocket_Select() | |
221 | * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the | |
222 | * corresponding asynchronous events. | |
223 | * | |
224 | * For datagram (non connection oriented) sockets, GSocket_Connect() | |
225 | * just sets the peer address established with GSocket_SetPeer() as | |
226 | * default destination. | |
227 | * | |
228 | * Error codes: | |
229 | * GSOCK_INVSOCK - the socket is in use or not valid. | |
230 | * GSOCK_INVADDR - the peer address has not been established. | |
231 | * GSOCK_TIMEDOUT - timeout, the connection failed. | |
232 | * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only) | |
233 | * GSOCK_MEMERR - couldn't allocate memory. | |
234 | * GSOCK_IOERR - low-level error. | |
235 | */ | |
236 | GSocketError GSocket_Connect(GSocket *socket, GSocketStream stream); | |
237 | ||
238 | /* GSocket_SetReusable: | |
239 | * Simply sets the m_resuable flag on the socket. GSocket_SetServer will | |
240 | * make the appropriate setsockopt() call. | |
241 | * Implemented as a GSocket function because clients (ie, wxSocketServer) | |
242 | * don't have access to the GSocket struct information. | |
243 | * Returns TRUE if the flag was set correctly, FALSE if an error occured | |
244 | * (ie, if the parameter was NULL) | |
245 | */ | |
246 | int GSocket_SetReusable(GSocket *socket); | |
247 | ||
248 | /* Datagram sockets */ | |
249 | ||
250 | /* GSocket_SetNonOriented: | |
251 | * Sets up this socket as a non-connection oriented (datagram) socket. | |
252 | * Before using this function, the local address must have been set | |
253 | * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR | |
254 | * on success, or one of the following otherwise. | |
255 | * | |
256 | * Error codes: | |
257 | * GSOCK_INVSOCK - the socket is in use. | |
258 | * GSOCK_INVADDR - the local address has not been set. | |
259 | * GSOCK_IOERR - low-level error. | |
260 | */ | |
261 | GSocketError GSocket_SetNonOriented(GSocket *socket); | |
262 | ||
263 | ||
264 | /* Generic IO */ | |
265 | ||
266 | /* Like recv(), send(), ... */ | |
267 | ||
268 | /* For datagram sockets, the incoming / outgoing addresses | |
269 | * are stored as / read from the 'peer' address field. | |
270 | */ | |
271 | int GSocket_Read(GSocket *socket, char *buffer, int size); | |
272 | int GSocket_Write(GSocket *socket, const char *buffer, | |
273 | int size); | |
274 | ||
275 | /* GSocket_Select: | |
276 | * Polls the socket to determine its status. This function will | |
277 | * check for the events specified in the 'flags' parameter, and | |
278 | * it will return a mask indicating which operations can be | |
279 | * performed. This function won't block, regardless of the | |
280 | * mode (blocking | nonblocking) of the socket. | |
281 | */ | |
282 | GSocketEventFlags GSocket_Select(GSocket *socket, GSocketEventFlags flags); | |
283 | ||
284 | GSocketError GSocket_GetSockOpt(GSocket *socket, int level, int optname, | |
285 | void *optval, int *optlen); | |
286 | ||
287 | GSocketError GSocket_SetSockOpt(GSocket *socket, int level, int optname, | |
288 | const void *optval, int optlen); | |
289 | ||
290 | /* Attributes */ | |
291 | ||
292 | /* GSocket_SetNonBlocking: | |
293 | * Sets the socket to non-blocking mode. All IO calls will return | |
294 | * immediately. | |
295 | */ | |
296 | void GSocket_SetNonBlocking(GSocket *socket, int non_block); | |
297 | ||
298 | /* GSocket_SetTimeout: | |
299 | * Sets the timeout for blocking calls. Time is expressed in | |
300 | * milliseconds. | |
301 | */ | |
302 | void GSocket_SetTimeout(GSocket *socket, unsigned long millisec); | |
303 | ||
304 | #endif /* ndef wxUSE_GSOCKET_CPLUSPLUS */ | |
305 | ||
306 | /* GSocket_GetError: | |
307 | * Returns the last error occured for this socket. Note that successful | |
308 | * operations do not clear this back to GSOCK_NOERROR, so use it only | |
309 | * after an error. | |
310 | */ | |
311 | GSocketError WXDLLIMPEXP_NET GSocket_GetError(GSocket *socket); | |
312 | ||
313 | #ifndef wxUSE_GSOCKET_CPLUSPLUS | |
314 | ||
315 | /* Callbacks */ | |
316 | ||
317 | /* GSOCK_INPUT: | |
318 | * There is data to be read in the input buffer. If, after a read | |
319 | * operation, there is still data available, the callback function will | |
320 | * be called again. | |
321 | * GSOCK_OUTPUT: | |
322 | * The socket is available for writing. That is, the next write call | |
323 | * won't block. This event is generated only once, when the connection is | |
324 | * first established, and then only if a call failed with GSOCK_WOULDBLOCK, | |
325 | * when the output buffer empties again. This means that the app should | |
326 | * assume that it can write since the first OUTPUT event, and no more | |
327 | * OUTPUT events will be generated unless an error occurs. | |
328 | * GSOCK_CONNECTION: | |
329 | * Connection succesfully established, for client sockets, or incoming | |
330 | * client connection, for server sockets. Wait for this event (also watch | |
331 | * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call. | |
332 | * GSOCK_LOST: | |
333 | * The connection is lost (or a connection request failed); this could | |
334 | * be due to a failure, or due to the peer closing it gracefully. | |
335 | */ | |
336 | ||
337 | /* GSocket_SetCallback: | |
338 | * Enables the callbacks specified by 'flags'. Note that 'flags' | |
339 | * may be a combination of flags OR'ed toghether, so the same | |
340 | * callback function can be made to accept different events. | |
341 | * The callback function must have the following prototype: | |
342 | * | |
343 | * void function(GSocket *socket, GSocketEvent event, char *cdata) | |
344 | */ | |
345 | void GSocket_SetCallback(GSocket *socket, GSocketEventFlags flags, | |
346 | GSocketCallback fallback, char *cdata); | |
347 | ||
348 | /* GSocket_UnsetCallback: | |
349 | * Disables all callbacks specified by 'flags', which may be a | |
350 | * combination of flags OR'ed toghether. | |
351 | */ | |
352 | void GSocket_UnsetCallback(GSocket *socket, GSocketEventFlags flags); | |
353 | ||
354 | #endif /* ndef wxUSE_GSOCKET_CPLUSPLUS */ | |
355 | ||
356 | ||
357 | /* GAddress */ | |
358 | ||
359 | GAddress *GAddress_new(void); | |
360 | GAddress *GAddress_copy(GAddress *address); | |
361 | void GAddress_destroy(GAddress *address); | |
362 | ||
363 | void GAddress_SetFamily(GAddress *address, GAddressType type); | |
364 | GAddressType GAddress_GetFamily(GAddress *address); | |
365 | ||
366 | /* The use of any of the next functions will set the address family to | |
367 | * the specific one. For example if you use GAddress_INET_SetHostName, | |
368 | * address family will be implicitly set to AF_INET. | |
369 | */ | |
370 | ||
371 | GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname); | |
372 | GSocketError GAddress_INET_SetAnyAddress(GAddress *address); | |
373 | GSocketError GAddress_INET_SetHostAddress(GAddress *address, | |
374 | unsigned long hostaddr); | |
375 | GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port, | |
376 | const char *protocol); | |
377 | GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port); | |
378 | ||
379 | GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, | |
380 | size_t sbuf); | |
381 | unsigned long GAddress_INET_GetHostAddress(GAddress *address); | |
382 | unsigned short GAddress_INET_GetPort(GAddress *address); | |
383 | ||
384 | /* TODO: Define specific parts (INET6, UNIX) */ | |
385 | ||
386 | GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path); | |
387 | GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf); | |
388 | ||
389 | #ifdef __cplusplus | |
390 | } | |
391 | #endif /* __cplusplus */ | |
392 | ||
393 | #ifdef wxUSE_GSOCKET_CPLUSPLUS | |
394 | #include "wx/unix/gsockunx.h" | |
395 | #endif | |
396 | ||
397 | #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */ | |
398 | ||
399 | #endif /* __GSOCKET_H */ |