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
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: */
111 #if !defined(__WINDOWS__) && !defined(wxUSE_GSOCKET_CPLUSPLUS)
112 struct GSocketBaseFunctionsTable
114 void (*Detected_Read
)(GSocket
*socket
);
115 void (*Detected_Write
)(GSocket
*socket
);
119 #if defined(wxUSE_GSOCKET_CPLUSPLUS)
120 /* Actually this is a misnomer now, but reusing this name means I don't
121 have to ifdef app traits or common socket code */
122 class GSocketGUIFunctionsTable
125 virtual bool OnInit() = 0;
126 virtual void OnExit() = 0;
127 virtual bool CanUseEventLoop() = 0;
128 virtual bool Init_Socket(GSocket
*socket
) = 0;
129 virtual void Destroy_Socket(GSocket
*socket
) = 0;
131 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
) = 0;
132 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
) = 0;
134 virtual void Enable_Events(GSocket
*socket
) = 0;
135 virtual void Disable_Events(GSocket
*socket
) = 0;
139 struct GSocketGUIFunctionsTable
141 int (*GUI_Init
)(void);
142 void (*GUI_Cleanup
)(void);
143 int (*GUI_Init_Socket
)(GSocket
*socket
);
144 void (*GUI_Destroy_Socket
)(GSocket
*socket
);
146 void (*Install_Callback
)(GSocket
*socket
, GSocketEvent event
);
147 void (*Uninstall_Callback
)(GSocket
*socket
, GSocketEvent event
);
149 void (*Enable_Events
)(GSocket
*socket
);
150 void (*Disable_Events
)(GSocket
*socket
);
152 #endif /* defined(wxUSE_GSOCKET_CPLUSPLUS) */
155 /* Global initializers */
157 /* Sets GUI functions callbacks. Must be called *before* GSocket_Init
158 if the app uses async sockets. */
159 void GSocket_SetGUIFunctions(struct GSocketGUIFunctionsTable
*guifunc
);
161 /* GSocket_Init() must be called at the beginning */
162 int GSocket_Init(void);
164 /* GSocket_Cleanup() must be called at the end */
165 void GSocket_Cleanup(void);
168 /* Constructors / Destructors */
170 GSocket
*GSocket_new(void);
171 #if !defined(wxUSE_GSOCKET_CPLUSPLUS)
172 void GSocket_destroy(GSocket
*socket
);
176 #ifndef wxUSE_GSOCKET_CPLUSPLUS
179 * Disallow further read/write operations on this socket, close
180 * the fd and disable all callbacks.
182 void GSocket_Shutdown(GSocket
*socket
);
184 /* Address handling */
190 * Set or get the local or peer address for this socket. The 'set'
191 * functions return GSOCK_NOERROR on success, an error code otherwise.
192 * The 'get' functions return a pointer to a GAddress object on success,
193 * or NULL otherwise, in which case they set the error code of the
194 * corresponding GSocket.
197 * GSOCK_INVSOCK - the socket is not valid.
198 * GSOCK_INVADDR - the address is not valid.
200 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
);
201 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
);
202 GAddress
*GSocket_GetLocal(GSocket
*socket
);
203 GAddress
*GSocket_GetPeer(GSocket
*socket
);
205 /* Server specific parts */
207 /* GSocket_SetServer:
208 * Sets up this socket as a server. The local address must have been
209 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
210 * Returns GSOCK_NOERROR on success, one of the following otherwise:
213 * GSOCK_INVSOCK - the socket is in use.
214 * GSOCK_INVADDR - the local address has not been set.
215 * GSOCK_IOERR - low-level error.
217 GSocketError
GSocket_SetServer(GSocket
*socket
);
219 /* GSocket_WaitConnection:
220 * Waits for an incoming client connection. Returns a pointer to
221 * a GSocket object, or NULL if there was an error, in which case
222 * the last error field will be updated for the calling GSocket.
224 * Error codes (set in the calling GSocket)
225 * GSOCK_INVSOCK - the socket is not valid or not a server.
226 * GSOCK_TIMEDOUT - timeout, no incoming connections.
227 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
228 * GSOCK_MEMERR - couldn't allocate memory.
229 * GSOCK_IOERR - low-level error.
231 GSocket
*GSocket_WaitConnection(GSocket
*socket
);
234 /* Client specific parts */
237 * For stream (connection oriented) sockets, GSocket_Connect() tries
238 * to establish a client connection to a server using the peer address
239 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
240 * connection has been succesfully established, or one of the error
241 * codes listed below. Note that for nonblocking sockets, a return
242 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
243 * request can be completed later; you should use GSocket_Select()
244 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
245 * corresponding asynchronous events.
247 * For datagram (non connection oriented) sockets, GSocket_Connect()
248 * just sets the peer address established with GSocket_SetPeer() as
249 * default destination.
252 * GSOCK_INVSOCK - the socket is in use or not valid.
253 * GSOCK_INVADDR - the peer address has not been established.
254 * GSOCK_TIMEDOUT - timeout, the connection failed.
255 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
256 * GSOCK_MEMERR - couldn't allocate memory.
257 * GSOCK_IOERR - low-level error.
259 GSocketError
GSocket_Connect(GSocket
*socket
, GSocketStream stream
);
261 /* GSocket_SetReusable:
262 * Simply sets the m_resuable flag on the socket. GSocket_SetServer will
263 * make the appropriate setsockopt() call.
264 * Implemented as a GSocket function because clients (ie, wxSocketServer)
265 * don't have access to the GSocket struct information.
266 * Returns TRUE if the flag was set correctly, FALSE if an error occured
267 * (ie, if the parameter was NULL)
269 int GSocket_SetReusable(GSocket
*socket
);
271 /* Datagram sockets */
273 /* GSocket_SetNonOriented:
274 * Sets up this socket as a non-connection oriented (datagram) socket.
275 * Before using this function, the local address must have been set
276 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
277 * on success, or one of the following otherwise.
280 * GSOCK_INVSOCK - the socket is in use.
281 * GSOCK_INVADDR - the local address has not been set.
282 * GSOCK_IOERR - low-level error.
284 GSocketError
GSocket_SetNonOriented(GSocket
*socket
);
289 /* Like recv(), send(), ... */
291 /* For datagram sockets, the incoming / outgoing addresses
292 * are stored as / read from the 'peer' address field.
294 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
);
295 int GSocket_Write(GSocket
*socket
, const char *buffer
,
299 * Polls the socket to determine its status. This function will
300 * check for the events specified in the 'flags' parameter, and
301 * it will return a mask indicating which operations can be
302 * performed. This function won't block, regardless of the
303 * mode (blocking | nonblocking) of the socket.
305 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
);
307 GSocketError
GSocket_GetSockOpt(GSocket
*socket
, int level
, int optname
,
308 void *optval
, int *optlen
);
310 GSocketError
GSocket_SetSockOpt(GSocket
*socket
, int level
, int optname
,
311 const void *optval
, int optlen
);
315 /* GSocket_SetNonBlocking:
316 * Sets the socket to non-blocking mode. All IO calls will return
319 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
);
321 /* GSocket_SetTimeout:
322 * Sets the timeout for blocking calls. Time is expressed in
325 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
);
328 * Returns the last error occured for this socket. Note that successful
329 * operations do not clear this back to GSOCK_NOERROR, so use it only
332 GSocketError WXDLLIMPEXP_NET
GSocket_GetError(GSocket
*socket
);
337 * There is data to be read in the input buffer. If, after a read
338 * operation, there is still data available, the callback function will
341 * The socket is available for writing. That is, the next write call
342 * won't block. This event is generated only once, when the connection is
343 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
344 * when the output buffer empties again. This means that the app should
345 * assume that it can write since the first OUTPUT event, and no more
346 * OUTPUT events will be generated unless an error occurs.
348 * Connection succesfully established, for client sockets, or incoming
349 * client connection, for server sockets. Wait for this event (also watch
350 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
352 * The connection is lost (or a connection request failed); this could
353 * be due to a failure, or due to the peer closing it gracefully.
356 /* GSocket_SetCallback:
357 * Enables the callbacks specified by 'flags'. Note that 'flags'
358 * may be a combination of flags OR'ed toghether, so the same
359 * callback function can be made to accept different events.
360 * The callback function must have the following prototype:
362 * void function(GSocket *socket, GSocketEvent event, char *cdata)
364 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
365 GSocketCallback fallback
, char *cdata
);
367 /* GSocket_UnsetCallback:
368 * Disables all callbacks specified by 'flags', which may be a
369 * combination of flags OR'ed toghether.
371 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
);
373 #endif /* ndef wxUSE_GSOCKET_CPLUSPLUS */
378 GAddress
*GAddress_new(void);
379 GAddress
*GAddress_copy(GAddress
*address
);
380 void GAddress_destroy(GAddress
*address
);
382 void GAddress_SetFamily(GAddress
*address
, GAddressType type
);
383 GAddressType
GAddress_GetFamily(GAddress
*address
);
385 /* The use of any of the next functions will set the address family to
386 * the specific one. For example if you use GAddress_INET_SetHostName,
387 * address family will be implicitly set to AF_INET.
390 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
);
391 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
);
392 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
393 unsigned long hostaddr
);
394 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
395 const char *protocol
);
396 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
);
398 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
,
400 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
);
401 unsigned short GAddress_INET_GetPort(GAddress
*address
);
403 /* TODO: Define specific parts (INET6, UNIX) */
405 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
);
406 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
);
410 #endif /* __cplusplus */
412 #ifdef wxUSE_GSOCKET_CPLUSPLUS
414 # include "wx/msw/gsockmsw.h"
416 # include "wx/unix/gsockunx.h"
420 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
422 #endif /* __GSOCKET_H */