]>
Commit | Line | Data |
---|---|---|
a324a7bc GL |
1 | /* ------------------------------------------------------------------------- |
2 | * Project: GSocket (Generic Socket) | |
3 | * Name: gsocket.h | |
33ac7e6f | 4 | * Author: Guilhem Lavaux |
9bf10d6b | 5 | * Guillermo Rodriguez Garcia <guille@iies.es> (maintainer) |
a324a7bc GL |
6 | * Purpose: GSocket include file (system independent) |
7 | * CVSID: $Id$ | |
a324a7bc GL |
8 | * ------------------------------------------------------------------------- |
9 | */ | |
9bf10d6b | 10 | |
a324a7bc GL |
11 | #ifndef __GSOCKET_H |
12 | #define __GSOCKET_H | |
13 | ||
83a9be34 DE |
14 | // FIXME: OS/2 needs to move to modern C++ GSocket |
15 | #if defined(__OS2__) | |
09e6e5ec | 16 | #undef wxUSE_GSOCKET_CPLUSPLUS |
83a9be34 DE |
17 | #else |
18 | #define wxUSE_GSOCKET_CPLUSPLUS 1 | |
19 | #endif | |
20 | ||
09e6e5ec DE |
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 | ||
0ce742cf | 25 | #ifndef __GSOCKET_STANDALONE__ |
d422d01e | 26 | #include "wx/setup.h" |
3a858e75 | 27 | #include "wx/platform.h" |
ab8af15f | 28 | |
b6db2e91 | 29 | #include "wx/dlimpexp.h" /* for WXDLLIMPEXP_NET */ |
ab8af15f | 30 | |
0ce742cf | 31 | #endif |
d422d01e | 32 | |
0ce742cf | 33 | #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) |
d422d01e | 34 | |
98781fa3 | 35 | #include <stddef.h> |
58071ea0 VZ |
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 | */ | |
882dfc67 | 42 | #if !defined(__WXMAC__) && !defined(__CYGWIN__) && !defined(__WXWINCE__) |
a324a7bc | 43 | #include <sys/types.h> |
a02bb143 | 44 | #endif |
a324a7bc | 45 | |
882dfc67 JS |
46 | #ifdef __WXWINCE__ |
47 | #include <stdlib.h> | |
48 | #endif | |
49 | ||
09e6e5ec | 50 | #ifdef wxUSE_GSOCKET_CPLUSPLUS |
4b4d23c7 | 51 | class GSocket; |
b6db2e91 | 52 | #endif |
09e6e5ec | 53 | |
e9e3e3ab GRG |
54 | #ifdef __cplusplus |
55 | extern "C" { | |
56 | #endif | |
57 | ||
09e6e5ec | 58 | #ifndef wxUSE_GSOCKET_CPLUSPLUS |
a324a7bc | 59 | typedef struct _GSocket GSocket; |
b6db2e91 | 60 | #endif |
a324a7bc GL |
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, | |
f439844b | 82 | GSOCK_INVPORT, |
98781fa3 | 83 | GSOCK_WOULDBLOCK, |
aa6d9706 | 84 | GSOCK_TIMEDOUT, |
bfa7bf7d VZ |
85 | GSOCK_MEMERR, |
86 | GSOCK_OPTERR, | |
a324a7bc GL |
87 | } GSocketError; |
88 | ||
9bf10d6b GRG |
89 | /* See below for an explanation on how events work. |
90 | */ | |
a324a7bc GL |
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, | |
31528cd3 | 103 | GSOCK_LOST_FLAG = 1 << GSOCK_LOST |
a324a7bc GL |
104 | }; |
105 | ||
106 | typedef int GSocketEventFlags; | |
107 | ||
39b91eca | 108 | typedef void (*GSocketCallback)(GSocket *socket, GSocketEvent event, |
31528cd3 | 109 | char *cdata); |
a324a7bc | 110 | |
a324a7bc | 111 | |
38bb138f VS |
112 | /* Functions tables for internal use by GSocket code: */ |
113 | ||
ba2a81d7 | 114 | #if !defined(__WINDOWS__) && !defined(wxUSE_GSOCKET_CPLUSPLUS) |
38bb138f VS |
115 | struct GSocketBaseFunctionsTable |
116 | { | |
117 | void (*Detected_Read)(GSocket *socket); | |
118 | void (*Detected_Write)(GSocket *socket); | |
119 | }; | |
120 | #endif | |
121 | ||
ba2a81d7 | 122 | #if defined(wxUSE_GSOCKET_CPLUSPLUS) |
4b4d23c7 DE |
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 | |
38bb138f VS |
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 | }; | |
ba2a81d7 | 155 | #endif /* defined(wxUSE_GSOCKET_CPLUSPLUS) */ |
38bb138f VS |
156 | |
157 | ||
e9e3e3ab | 158 | /* Global initializers */ |
a58d5df4 | 159 | |
38bb138f VS |
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 | ||
a58d5df4 | 164 | /* GSocket_Init() must be called at the beginning */ |
5c9eff30 | 165 | int GSocket_Init(void); |
9bf10d6b GRG |
166 | |
167 | /* GSocket_Cleanup() must be called at the end */ | |
da051b23 | 168 | void GSocket_Cleanup(void); |
a58d5df4 | 169 | |
9bf10d6b | 170 | |
a324a7bc GL |
171 | /* Constructors / Destructors */ |
172 | ||
da051b23 | 173 | GSocket *GSocket_new(void); |
ba2a81d7 | 174 | #if !defined(wxUSE_GSOCKET_CPLUSPLUS) |
a324a7bc | 175 | void GSocket_destroy(GSocket *socket); |
4b4d23c7 | 176 | #endif |
a324a7bc | 177 | |
9bf10d6b | 178 | |
09e6e5ec | 179 | #ifndef wxUSE_GSOCKET_CPLUSPLUS |
9bf10d6b GRG |
180 | |
181 | /* GSocket_Shutdown: | |
182 | * Disallow further read/write operations on this socket, close | |
183 | * the fd and disable all callbacks. | |
184 | */ | |
a324a7bc GL |
185 | void GSocket_Shutdown(GSocket *socket); |
186 | ||
187 | /* Address handling */ | |
188 | ||
9bf10d6b GRG |
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 | */ | |
a324a7bc GL |
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 | ||
a324a7bc GL |
208 | /* Server specific parts */ |
209 | ||
e9e3e3ab | 210 | /* GSocket_SetServer: |
9bf10d6b GRG |
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: | |
33ac7e6f | 214 | * |
9bf10d6b GRG |
215 | * Error codes: |
216 | * GSOCK_INVSOCK - the socket is in use. | |
217 | * GSOCK_INVADDR - the local address has not been set. | |
33ac7e6f | 218 | * GSOCK_IOERR - low-level error. |
e9e3e3ab | 219 | */ |
a324a7bc GL |
220 | GSocketError GSocket_SetServer(GSocket *socket); |
221 | ||
e9e3e3ab | 222 | /* GSocket_WaitConnection: |
9bf10d6b GRG |
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. | |
33ac7e6f | 232 | * GSOCK_IOERR - low-level error. |
e9e3e3ab | 233 | */ |
a324a7bc GL |
234 | GSocket *GSocket_WaitConnection(GSocket *socket); |
235 | ||
9bf10d6b | 236 | |
a324a7bc GL |
237 | /* Client specific parts */ |
238 | ||
e9e3e3ab | 239 | /* GSocket_Connect: |
9bf10d6b GRG |
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. | |
33ac7e6f | 260 | * GSOCK_IOERR - low-level error. |
e9e3e3ab | 261 | */ |
a324a7bc GL |
262 | GSocketError GSocket_Connect(GSocket *socket, GSocketStream stream); |
263 | ||
74c481d1 VZ |
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); | |
9bf10d6b GRG |
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. | |
33ac7e6f | 285 | * GSOCK_IOERR - low-level error. |
9bf10d6b GRG |
286 | */ |
287 | GSocketError GSocket_SetNonOriented(GSocket *socket); | |
288 | ||
289 | ||
a324a7bc GL |
290 | /* Generic IO */ |
291 | ||
292 | /* Like recv(), send(), ... */ | |
e9e3e3ab | 293 | |
9bf10d6b GRG |
294 | /* For datagram sockets, the incoming / outgoing addresses |
295 | * are stored as / read from the 'peer' address field. | |
e9e3e3ab | 296 | */ |
a324a7bc GL |
297 | int GSocket_Read(GSocket *socket, char *buffer, int size); |
298 | int GSocket_Write(GSocket *socket, const char *buffer, | |
31528cd3 | 299 | int size); |
e9e3e3ab GRG |
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 | |
9bf10d6b | 306 | * mode (blocking | nonblocking) of the socket. |
e9e3e3ab GRG |
307 | */ |
308 | GSocketEventFlags GSocket_Select(GSocket *socket, GSocketEventFlags flags); | |
a324a7bc | 309 | |
bfa7bf7d VZ |
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 | ||
9bf10d6b | 316 | /* Attributes */ |
a324a7bc | 317 | |
e9e3e3ab | 318 | /* GSocket_SetNonBlocking: |
9bf10d6b GRG |
319 | * Sets the socket to non-blocking mode. All IO calls will return |
320 | * immediately. | |
e9e3e3ab | 321 | */ |
5c9eff30 | 322 | void GSocket_SetNonBlocking(GSocket *socket, int non_block); |
a324a7bc | 323 | |
9bf10d6b GRG |
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 | ||
e9e3e3ab | 330 | /* GSocket_GetError: |
9bf10d6b GRG |
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. | |
e9e3e3ab | 334 | */ |
7c4728f6 | 335 | GSocketError WXDLLIMPEXP_NET GSocket_GetError(GSocket *socket); |
a324a7bc GL |
336 | |
337 | /* Callbacks */ | |
338 | ||
9bf10d6b GRG |
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: | |
33ac7e6f | 344 | * The socket is available for writing. That is, the next write call |
9bf10d6b GRG |
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. | |
e9e3e3ab GRG |
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, | |
39b91eca | 368 | GSocketCallback fallback, char *cdata); |
a324a7bc | 369 | |
e9e3e3ab GRG |
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); | |
a324a7bc | 375 | |
b6db2e91 | 376 | #endif /* ndef wxUSE_GSOCKET_CPLUSPLUS */ |
09e6e5ec | 377 | |
9bf10d6b | 378 | |
a324a7bc GL |
379 | /* GAddress */ |
380 | ||
da051b23 | 381 | GAddress *GAddress_new(void); |
a324a7bc GL |
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 | ||
e9e3e3ab GRG |
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 | */ | |
a324a7bc GL |
392 | |
393 | GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname); | |
9bf10d6b | 394 | GSocketError GAddress_INET_SetAnyAddress(GAddress *address); |
a324a7bc GL |
395 | GSocketError GAddress_INET_SetHostAddress(GAddress *address, |
396 | unsigned long hostaddr); | |
5a96d2f4 GL |
397 | GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port, |
398 | const char *protocol); | |
a324a7bc GL |
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 | ||
a324a7bc | 411 | #ifdef __cplusplus |
ba14d986 | 412 | } |
d422d01e RR |
413 | #endif /* __cplusplus */ |
414 | ||
09e6e5ec | 415 | #ifdef wxUSE_GSOCKET_CPLUSPLUS |
4b4d23c7 DE |
416 | # ifdef __WINDOWS__ |
417 | # include "wx/msw/gsockmsw.h" | |
418 | # else | |
419 | # include "wx/unix/gsockunx.h" | |
420 | # endif | |
b6db2e91 | 421 | #endif |
d422d01e | 422 | |
0ce742cf | 423 | #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */ |
a324a7bc | 424 | |
e9e3e3ab | 425 | #endif /* __GSOCKET_H */ |