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