1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket)
4 * Author: Guilhem Lavaux
5 * Guillermo Rodriguez Garcia <guille@iies.es> (maintainer)
6 * Purpose: GSocket include file (system independent)
8 * -------------------------------------------------------------------------
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++"
22 #ifndef __GSOCKET_STANDALONE__
24 #include "wx/platform.h"
26 #include "wx/dlimpexp.h" /* for WXDLLIMPEXP_NET */
30 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
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.
39 #if !defined(__WXMAC__) && !defined(__CYGWIN__) && !defined(__WXWINCE__)
40 #include <sys/types.h>
47 #ifdef wxUSE_GSOCKET_CPLUSPLUS
48 typedef class GSocketBSD GSocket
;
55 #ifndef wxUSE_GSOCKET_CPLUSPLUS
56 typedef struct _GSocket GSocket
;
58 typedef struct _GAddress GAddress
;
86 /* See below for an explanation on how events work.
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
103 typedef int GSocketEventFlags
;
105 typedef void (*GSocketCallback
)(GSocket
*socket
, GSocketEvent event
,
109 /* Functions tables for internal use by GSocket code: */
112 struct GSocketBaseFunctionsTable
114 void (*Detected_Read
)(GSocket
*socket
);
115 void (*Detected_Write
)(GSocket
*socket
);
119 struct GSocketGUIFunctionsTable
121 int (*GUI_Init
)(void);
122 void (*GUI_Cleanup
)(void);
123 int (*GUI_Init_Socket
)(GSocket
*socket
);
124 void (*GUI_Destroy_Socket
)(GSocket
*socket
);
126 void (*Install_Callback
)(GSocket
*socket
, GSocketEvent event
);
127 void (*Uninstall_Callback
)(GSocket
*socket
, GSocketEvent event
);
129 void (*Enable_Events
)(GSocket
*socket
);
130 void (*Disable_Events
)(GSocket
*socket
);
134 /* Global initializers */
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
);
140 /* GSocket_Init() must be called at the beginning */
141 int GSocket_Init(void);
143 /* GSocket_Cleanup() must be called at the end */
144 void GSocket_Cleanup(void);
147 /* Constructors / Destructors */
149 GSocket
*GSocket_new(void);
150 void GSocket_destroy(GSocket
*socket
);
153 #ifndef wxUSE_GSOCKET_CPLUSPLUS
156 * Disallow further read/write operations on this socket, close
157 * the fd and disable all callbacks.
159 void GSocket_Shutdown(GSocket
*socket
);
161 /* Address handling */
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.
174 * GSOCK_INVSOCK - the socket is not valid.
175 * GSOCK_INVADDR - the address is not valid.
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
);
182 /* Server specific parts */
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:
190 * GSOCK_INVSOCK - the socket is in use.
191 * GSOCK_INVADDR - the local address has not been set.
192 * GSOCK_IOERR - low-level error.
194 GSocketError
GSocket_SetServer(GSocket
*socket
);
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.
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.
208 GSocket
*GSocket_WaitConnection(GSocket
*socket
);
211 /* Client specific parts */
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.
224 * For datagram (non connection oriented) sockets, GSocket_Connect()
225 * just sets the peer address established with GSocket_SetPeer() as
226 * default destination.
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.
236 GSocketError
GSocket_Connect(GSocket
*socket
, GSocketStream stream
);
239 /* Datagram sockets */
241 /* GSocket_SetNonOriented:
242 * Sets up this socket as a non-connection oriented (datagram) socket.
243 * Before using this function, the local address must have been set
244 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
245 * on success, or one of the following otherwise.
248 * GSOCK_INVSOCK - the socket is in use.
249 * GSOCK_INVADDR - the local address has not been set.
250 * GSOCK_IOERR - low-level error.
252 GSocketError
GSocket_SetNonOriented(GSocket
*socket
);
257 /* Like recv(), send(), ... */
259 /* For datagram sockets, the incoming / outgoing addresses
260 * are stored as / read from the 'peer' address field.
262 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
);
263 int GSocket_Write(GSocket
*socket
, const char *buffer
,
267 * Polls the socket to determine its status. This function will
268 * check for the events specified in the 'flags' parameter, and
269 * it will return a mask indicating which operations can be
270 * performed. This function won't block, regardless of the
271 * mode (blocking | nonblocking) of the socket.
273 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
);
275 GSocketError
GSocket_GetSockOpt(GSocket
*socket
, int level
, int optname
,
276 void *optval
, int *optlen
);
278 GSocketError
GSocket_SetSockOpt(GSocket
*socket
, int level
, int optname
,
279 const void *optval
, int optlen
);
280 GSocketError
GSocket_SetReuseAddr(GSocket
*socket
);
282 void GSocket_Streamed(GSocket
*socket
);
283 void GSocket_Unstreamed(GSocket
*socket
);
287 /* GSocket_SetNonBlocking:
288 * Sets the socket to non-blocking mode. All IO calls will return
291 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
);
293 /* GSocket_SetTimeout:
294 * Sets the timeout for blocking calls. Time is expressed in
297 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
);
299 #endif /* ndef wxUSE_GSOCKET_CPLUSPLUS */
302 * Returns the last error occured for this socket. Note that successful
303 * operations do not clear this back to GSOCK_NOERROR, so use it only
306 GSocketError WXDLLIMPEXP_NET
GSocket_GetError(GSocket
*socket
);
308 #ifndef wxUSE_GSOCKET_CPLUSPLUS
313 * There is data to be read in the input buffer. If, after a read
314 * operation, there is still data available, the callback function will
317 * The socket is available for writing. That is, the next write call
318 * won't block. This event is generated only once, when the connection is
319 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
320 * when the output buffer empties again. This means that the app should
321 * assume that it can write since the first OUTPUT event, and no more
322 * OUTPUT events will be generated unless an error occurs.
324 * Connection succesfully established, for client sockets, or incoming
325 * client connection, for server sockets. Wait for this event (also watch
326 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
328 * The connection is lost (or a connection request failed); this could
329 * be due to a failure, or due to the peer closing it gracefully.
332 /* GSocket_SetCallback:
333 * Enables the callbacks specified by 'flags'. Note that 'flags'
334 * may be a combination of flags OR'ed toghether, so the same
335 * callback function can be made to accept different events.
336 * The callback function must have the following prototype:
338 * void function(GSocket *socket, GSocketEvent event, char *cdata)
340 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
341 GSocketCallback fallback
, char *cdata
);
343 /* GSocket_UnsetCallback:
344 * Disables all callbacks specified by 'flags', which may be a
345 * combination of flags OR'ed toghether.
347 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
);
349 #endif /* ndef wxUSE_GSOCKET_CPLUSPLUS */
354 GAddress
*GAddress_new(void);
355 GAddress
*GAddress_copy(GAddress
*address
);
356 void GAddress_destroy(GAddress
*address
);
358 void GAddress_SetFamily(GAddress
*address
, GAddressType type
);
359 GAddressType
GAddress_GetFamily(GAddress
*address
);
361 /* The use of any of the next functions will set the address family to
362 * the specific one. For example if you use GAddress_INET_SetHostName,
363 * address family will be implicitly set to AF_INET.
366 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
);
367 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
);
368 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
369 unsigned long hostaddr
);
370 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
371 const char *protocol
);
372 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
);
374 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
,
376 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
);
377 unsigned short GAddress_INET_GetPort(GAddress
*address
);
379 /* TODO: Define specific parts (INET6, UNIX) */
381 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
);
382 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
);
386 #endif /* __cplusplus */
388 #ifdef wxUSE_GSOCKET_CPLUSPLUS
389 #include "wx/unix/gsockunx.h"
392 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
394 #endif /* __GSOCKET_H */