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 * -------------------------------------------------------------------------
15 /* #define wxUSE_GSOCKET_CPLUSPLUS 1 */
16 #undef wxUSE_GSOCKET_CPLUSPLUS
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. */
20 #undef wxUSE_GSOCKET_CPLUSPLUS
22 #if !defined(__cplusplus) && defined(wxUSE_GSOCKET_CPLUSPLUS)
23 #error "You need to compile this file (probably a GUI gsock peice) as C++"
26 #ifndef __GSOCKET_STANDALONE__
28 #include "wx/platform.h"
30 #include "wx/dlimpexp.h" /* for WXDLLIMPEXP_NET */
34 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
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.
43 #if !defined(__WXMAC__) && !defined(__CYGWIN__) && !defined(__WXWINCE__)
44 #include <sys/types.h>
51 #ifdef wxUSE_GSOCKET_CPLUSPLUS
55 typedef class GSocketBSD GSocket
;
63 #ifndef wxUSE_GSOCKET_CPLUSPLUS
64 typedef struct _GSocket GSocket
;
66 typedef struct _GAddress GAddress
;
94 /* See below for an explanation on how events work.
105 GSOCK_INPUT_FLAG
= 1 << GSOCK_INPUT
,
106 GSOCK_OUTPUT_FLAG
= 1 << GSOCK_OUTPUT
,
107 GSOCK_CONNECTION_FLAG
= 1 << GSOCK_CONNECTION
,
108 GSOCK_LOST_FLAG
= 1 << GSOCK_LOST
111 typedef int GSocketEventFlags
;
113 typedef void (*GSocketCallback
)(GSocket
*socket
, GSocketEvent event
,
117 /* Functions tables for internal use by GSocket code: */
120 struct GSocketBaseFunctionsTable
122 void (*Detected_Read
)(GSocket
*socket
);
123 void (*Detected_Write
)(GSocket
*socket
);
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
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;
139 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
) = 0;
140 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
) = 0;
142 virtual void Enable_Events(GSocket
*socket
) = 0;
143 virtual void Disable_Events(GSocket
*socket
) = 0;
147 struct GSocketGUIFunctionsTable
149 int (*GUI_Init
)(void);
150 void (*GUI_Cleanup
)(void);
151 int (*GUI_Init_Socket
)(GSocket
*socket
);
152 void (*GUI_Destroy_Socket
)(GSocket
*socket
);
154 void (*Install_Callback
)(GSocket
*socket
, GSocketEvent event
);
155 void (*Uninstall_Callback
)(GSocket
*socket
, GSocketEvent event
);
157 void (*Enable_Events
)(GSocket
*socket
);
158 void (*Disable_Events
)(GSocket
*socket
);
160 #endif /* defined(__WINDOWS__) && defined(wxUSE_GSOCKET_CPLUSPLUS) */
163 /* Global initializers */
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
);
169 /* GSocket_Init() must be called at the beginning */
170 int GSocket_Init(void);
172 /* GSocket_Cleanup() must be called at the end */
173 void GSocket_Cleanup(void);
176 /* Constructors / Destructors */
178 GSocket
*GSocket_new(void);
179 #if !defined(__WINDOWS__) || !defined(wxUSE_GSOCKET_CPLUSPLUS)
180 void GSocket_destroy(GSocket
*socket
);
184 #ifndef wxUSE_GSOCKET_CPLUSPLUS
187 * Disallow further read/write operations on this socket, close
188 * the fd and disable all callbacks.
190 void GSocket_Shutdown(GSocket
*socket
);
192 /* Address handling */
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.
205 * GSOCK_INVSOCK - the socket is not valid.
206 * GSOCK_INVADDR - the address is not valid.
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
);
213 /* Server specific parts */
215 /* GSocket_SetServer:
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:
221 * GSOCK_INVSOCK - the socket is in use.
222 * GSOCK_INVADDR - the local address has not been set.
223 * GSOCK_IOERR - low-level error.
225 GSocketError
GSocket_SetServer(GSocket
*socket
);
227 /* GSocket_WaitConnection:
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.
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.
237 * GSOCK_IOERR - low-level error.
239 GSocket
*GSocket_WaitConnection(GSocket
*socket
);
242 /* Client specific parts */
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.
255 * For datagram (non connection oriented) sockets, GSocket_Connect()
256 * just sets the peer address established with GSocket_SetPeer() as
257 * default destination.
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.
265 * GSOCK_IOERR - low-level error.
267 GSocketError
GSocket_Connect(GSocket
*socket
, GSocketStream stream
);
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)
277 int GSocket_SetReusable(GSocket
*socket
);
279 /* Datagram sockets */
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.
288 * GSOCK_INVSOCK - the socket is in use.
289 * GSOCK_INVADDR - the local address has not been set.
290 * GSOCK_IOERR - low-level error.
292 GSocketError
GSocket_SetNonOriented(GSocket
*socket
);
297 /* Like recv(), send(), ... */
299 /* For datagram sockets, the incoming / outgoing addresses
300 * are stored as / read from the 'peer' address field.
302 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
);
303 int GSocket_Write(GSocket
*socket
, const char *buffer
,
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
311 * mode (blocking | nonblocking) of the socket.
313 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
);
315 GSocketError
GSocket_GetSockOpt(GSocket
*socket
, int level
, int optname
,
316 void *optval
, int *optlen
);
318 GSocketError
GSocket_SetSockOpt(GSocket
*socket
, int level
, int optname
,
319 const void *optval
, int optlen
);
323 /* GSocket_SetNonBlocking:
324 * Sets the socket to non-blocking mode. All IO calls will return
327 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
);
329 /* GSocket_SetTimeout:
330 * Sets the timeout for blocking calls. Time is expressed in
333 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
);
335 #endif /* ndef wxUSE_GSOCKET_CPLUSPLUS */
338 * Returns the last error occured for this socket. Note that successful
339 * operations do not clear this back to GSOCK_NOERROR, so use it only
342 GSocketError WXDLLIMPEXP_NET
GSocket_GetError(GSocket
*socket
);
344 #ifndef wxUSE_GSOCKET_CPLUSPLUS
349 * There is data to be read in the input buffer. If, after a read
350 * operation, there is still data available, the callback function will
353 * The socket is available for writing. That is, the next write call
354 * won't block. This event is generated only once, when the connection is
355 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
356 * when the output buffer empties again. This means that the app should
357 * assume that it can write since the first OUTPUT event, and no more
358 * OUTPUT events will be generated unless an error occurs.
360 * Connection succesfully established, for client sockets, or incoming
361 * client connection, for server sockets. Wait for this event (also watch
362 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
364 * The connection is lost (or a connection request failed); this could
365 * be due to a failure, or due to the peer closing it gracefully.
368 /* GSocket_SetCallback:
369 * Enables the callbacks specified by 'flags'. Note that 'flags'
370 * may be a combination of flags OR'ed toghether, so the same
371 * callback function can be made to accept different events.
372 * The callback function must have the following prototype:
374 * void function(GSocket *socket, GSocketEvent event, char *cdata)
376 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
377 GSocketCallback fallback
, char *cdata
);
379 /* GSocket_UnsetCallback:
380 * Disables all callbacks specified by 'flags', which may be a
381 * combination of flags OR'ed toghether.
383 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
);
385 #endif /* ndef wxUSE_GSOCKET_CPLUSPLUS */
390 GAddress
*GAddress_new(void);
391 GAddress
*GAddress_copy(GAddress
*address
);
392 void GAddress_destroy(GAddress
*address
);
394 void GAddress_SetFamily(GAddress
*address
, GAddressType type
);
395 GAddressType
GAddress_GetFamily(GAddress
*address
);
397 /* The use of any of the next functions will set the address family to
398 * the specific one. For example if you use GAddress_INET_SetHostName,
399 * address family will be implicitly set to AF_INET.
402 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
);
403 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
);
404 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
405 unsigned long hostaddr
);
406 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
407 const char *protocol
);
408 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
);
410 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
,
412 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
);
413 unsigned short GAddress_INET_GetPort(GAddress
*address
);
415 /* TODO: Define specific parts (INET6, UNIX) */
417 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
);
418 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
);
422 #endif /* __cplusplus */
424 #ifdef wxUSE_GSOCKET_CPLUSPLUS
426 # include "wx/msw/gsockmsw.h"
428 # include "wx/unix/gsockunx.h"
432 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
434 #endif /* __GSOCKET_H */