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 #define wxUSE_GSOCKET_CPLUSPLUS 1
16 #if !defined(__cplusplus) && defined(wxUSE_GSOCKET_CPLUSPLUS)
17 #error "You need to compile this file (probably a GUI gsock peice) as C++"
20 #ifndef __GSOCKET_STANDALONE__
22 #include "wx/platform.h"
24 #include "wx/dlimpexp.h" /* for WXDLLIMPEXP_NET */
28 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
33 Including sys/types.h under cygwin results in the warnings about "fd_set
34 having been defined in sys/types.h" when winsock.h is included later and
35 doesn't seem to be necessary anyhow. It's not needed under Mac neither.
37 #if !defined(__WXMAC__) && !defined(__CYGWIN__) && !defined(__WXWINCE__)
38 #include <sys/types.h>
45 #ifdef wxUSE_GSOCKET_CPLUSPLUS
53 #ifndef wxUSE_GSOCKET_CPLUSPLUS
54 typedef struct _GSocket GSocket
;
56 typedef struct _GAddress GAddress
;
84 /* See below for an explanation on how events work.
95 GSOCK_INPUT_FLAG
= 1 << GSOCK_INPUT
,
96 GSOCK_OUTPUT_FLAG
= 1 << GSOCK_OUTPUT
,
97 GSOCK_CONNECTION_FLAG
= 1 << GSOCK_CONNECTION
,
98 GSOCK_LOST_FLAG
= 1 << GSOCK_LOST
101 typedef int GSocketEventFlags
;
103 typedef void (*GSocketCallback
)(GSocket
*socket
, GSocketEvent event
,
107 /* Functions tables for internal use by GSocket code: */
109 #if !defined(__WINDOWS__) && !defined(wxUSE_GSOCKET_CPLUSPLUS)
110 struct GSocketBaseFunctionsTable
112 void (*Detected_Read
)(GSocket
*socket
);
113 void (*Detected_Write
)(GSocket
*socket
);
117 #if defined(wxUSE_GSOCKET_CPLUSPLUS)
118 /* Actually this is a misnomer now, but reusing this name means I don't
119 have to ifdef app traits or common socket code */
120 class GSocketGUIFunctionsTable
123 virtual bool OnInit() = 0;
124 virtual void OnExit() = 0;
125 virtual bool CanUseEventLoop() = 0;
126 virtual bool Init_Socket(GSocket
*socket
) = 0;
127 virtual void Destroy_Socket(GSocket
*socket
) = 0;
129 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
) = 0;
130 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
) = 0;
132 virtual void Enable_Events(GSocket
*socket
) = 0;
133 virtual void Disable_Events(GSocket
*socket
) = 0;
137 struct GSocketGUIFunctionsTable
139 int (*GUI_Init
)(void);
140 void (*GUI_Cleanup
)(void);
141 int (*GUI_Init_Socket
)(GSocket
*socket
);
142 void (*GUI_Destroy_Socket
)(GSocket
*socket
);
144 void (*Install_Callback
)(GSocket
*socket
, GSocketEvent event
);
145 void (*Uninstall_Callback
)(GSocket
*socket
, GSocketEvent event
);
147 void (*Enable_Events
)(GSocket
*socket
);
148 void (*Disable_Events
)(GSocket
*socket
);
150 #endif /* defined(wxUSE_GSOCKET_CPLUSPLUS) */
153 /* Global initializers */
155 /* Sets GUI functions callbacks. Must be called *before* GSocket_Init
156 if the app uses async sockets. */
157 void GSocket_SetGUIFunctions(struct GSocketGUIFunctionsTable
*guifunc
);
159 /* GSocket_Init() must be called at the beginning */
160 int GSocket_Init(void);
162 /* GSocket_Cleanup() must be called at the end */
163 void GSocket_Cleanup(void);
166 /* Constructors / Destructors */
168 GSocket
*GSocket_new(void);
169 #if !defined(wxUSE_GSOCKET_CPLUSPLUS)
170 void GSocket_destroy(GSocket
*socket
);
174 #ifndef wxUSE_GSOCKET_CPLUSPLUS
177 * Disallow further read/write operations on this socket, close
178 * the fd and disable all callbacks.
180 void GSocket_Shutdown(GSocket
*socket
);
182 /* Address handling */
188 * Set or get the local or peer address for this socket. The 'set'
189 * functions return GSOCK_NOERROR on success, an error code otherwise.
190 * The 'get' functions return a pointer to a GAddress object on success,
191 * or NULL otherwise, in which case they set the error code of the
192 * corresponding GSocket.
195 * GSOCK_INVSOCK - the socket is not valid.
196 * GSOCK_INVADDR - the address is not valid.
198 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
);
199 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
);
200 GAddress
*GSocket_GetLocal(GSocket
*socket
);
201 GAddress
*GSocket_GetPeer(GSocket
*socket
);
203 /* Server specific parts */
205 /* GSocket_SetServer:
206 * Sets up this socket as a server. The local address must have been
207 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
208 * Returns GSOCK_NOERROR on success, one of the following otherwise:
211 * GSOCK_INVSOCK - the socket is in use.
212 * GSOCK_INVADDR - the local address has not been set.
213 * GSOCK_IOERR - low-level error.
215 GSocketError
GSocket_SetServer(GSocket
*socket
);
217 /* GSocket_WaitConnection:
218 * Waits for an incoming client connection. Returns a pointer to
219 * a GSocket object, or NULL if there was an error, in which case
220 * the last error field will be updated for the calling GSocket.
222 * Error codes (set in the calling GSocket)
223 * GSOCK_INVSOCK - the socket is not valid or not a server.
224 * GSOCK_TIMEDOUT - timeout, no incoming connections.
225 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
226 * GSOCK_MEMERR - couldn't allocate memory.
227 * GSOCK_IOERR - low-level error.
229 GSocket
*GSocket_WaitConnection(GSocket
*socket
);
232 /* Client specific parts */
235 * For stream (connection oriented) sockets, GSocket_Connect() tries
236 * to establish a client connection to a server using the peer address
237 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
238 * connection has been succesfully established, or one of the error
239 * codes listed below. Note that for nonblocking sockets, a return
240 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
241 * request can be completed later; you should use GSocket_Select()
242 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
243 * corresponding asynchronous events.
245 * For datagram (non connection oriented) sockets, GSocket_Connect()
246 * just sets the peer address established with GSocket_SetPeer() as
247 * default destination.
250 * GSOCK_INVSOCK - the socket is in use or not valid.
251 * GSOCK_INVADDR - the peer address has not been established.
252 * GSOCK_TIMEDOUT - timeout, the connection failed.
253 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
254 * GSOCK_MEMERR - couldn't allocate memory.
255 * GSOCK_IOERR - low-level error.
257 GSocketError
GSocket_Connect(GSocket
*socket
, GSocketStream stream
);
259 /* GSocket_SetReusable:
260 * Simply sets the m_resuable flag on the socket. GSocket_SetServer will
261 * make the appropriate setsockopt() call.
262 * Implemented as a GSocket function because clients (ie, wxSocketServer)
263 * don't have access to the GSocket struct information.
264 * Returns TRUE if the flag was set correctly, FALSE if an error occured
265 * (ie, if the parameter was NULL)
267 int GSocket_SetReusable(GSocket
*socket
);
269 /* Datagram sockets */
271 /* GSocket_SetNonOriented:
272 * Sets up this socket as a non-connection oriented (datagram) socket.
273 * Before using this function, the local address must have been set
274 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
275 * on success, or one of the following otherwise.
278 * GSOCK_INVSOCK - the socket is in use.
279 * GSOCK_INVADDR - the local address has not been set.
280 * GSOCK_IOERR - low-level error.
282 GSocketError
GSocket_SetNonOriented(GSocket
*socket
);
287 /* Like recv(), send(), ... */
289 /* For datagram sockets, the incoming / outgoing addresses
290 * are stored as / read from the 'peer' address field.
292 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
);
293 int GSocket_Write(GSocket
*socket
, const char *buffer
,
297 * Polls the socket to determine its status. This function will
298 * check for the events specified in the 'flags' parameter, and
299 * it will return a mask indicating which operations can be
300 * performed. This function won't block, regardless of the
301 * mode (blocking | nonblocking) of the socket.
303 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
);
305 GSocketError
GSocket_GetSockOpt(GSocket
*socket
, int level
, int optname
,
306 void *optval
, int *optlen
);
308 GSocketError
GSocket_SetSockOpt(GSocket
*socket
, int level
, int optname
,
309 const void *optval
, int optlen
);
313 /* GSocket_SetNonBlocking:
314 * Sets the socket to non-blocking mode. All IO calls will return
317 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
);
319 /* GSocket_SetTimeout:
320 * Sets the timeout for blocking calls. Time is expressed in
323 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
);
326 * Returns the last error occured for this socket. Note that successful
327 * operations do not clear this back to GSOCK_NOERROR, so use it only
330 GSocketError WXDLLIMPEXP_NET
GSocket_GetError(GSocket
*socket
);
335 * There is data to be read in the input buffer. If, after a read
336 * operation, there is still data available, the callback function will
339 * The socket is available for writing. That is, the next write call
340 * won't block. This event is generated only once, when the connection is
341 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
342 * when the output buffer empties again. This means that the app should
343 * assume that it can write since the first OUTPUT event, and no more
344 * OUTPUT events will be generated unless an error occurs.
346 * Connection succesfully established, for client sockets, or incoming
347 * client connection, for server sockets. Wait for this event (also watch
348 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
350 * The connection is lost (or a connection request failed); this could
351 * be due to a failure, or due to the peer closing it gracefully.
354 /* GSocket_SetCallback:
355 * Enables the callbacks specified by 'flags'. Note that 'flags'
356 * may be a combination of flags OR'ed toghether, so the same
357 * callback function can be made to accept different events.
358 * The callback function must have the following prototype:
360 * void function(GSocket *socket, GSocketEvent event, char *cdata)
362 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
363 GSocketCallback fallback
, char *cdata
);
365 /* GSocket_UnsetCallback:
366 * Disables all callbacks specified by 'flags', which may be a
367 * combination of flags OR'ed toghether.
369 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
);
371 #endif /* ndef wxUSE_GSOCKET_CPLUSPLUS */
376 GAddress
*GAddress_new(void);
377 GAddress
*GAddress_copy(GAddress
*address
);
378 void GAddress_destroy(GAddress
*address
);
380 void GAddress_SetFamily(GAddress
*address
, GAddressType type
);
381 GAddressType
GAddress_GetFamily(GAddress
*address
);
383 /* The use of any of the next functions will set the address family to
384 * the specific one. For example if you use GAddress_INET_SetHostName,
385 * address family will be implicitly set to AF_INET.
388 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
);
389 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
);
390 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
391 unsigned long hostaddr
);
392 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
393 const char *protocol
);
394 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
);
396 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
,
398 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
);
399 unsigned short GAddress_INET_GetPort(GAddress
*address
);
401 /* TODO: Define specific parts (INET6, UNIX) */
403 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
);
404 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
);
408 #endif /* __cplusplus */
410 #ifdef wxUSE_GSOCKET_CPLUSPLUS
411 # if defined(__WINDOWS__)
412 # include "wx/msw/gsockmsw.h"
413 # elif defined(__WXMAC__) && !defined(__DARWIN__)
414 # include "wx/mac/gsockmac.h"
416 # include "wx/unix/gsockunx.h"
420 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
422 #endif /* __GSOCKET_H */