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 // FIXME: OS/2 needs to move to modern C++ GSocket
16 #undef wxUSE_GSOCKET_CPLUSPLUS
18 #define wxUSE_GSOCKET_CPLUSPLUS 1
21 #if !defined(__cplusplus) && defined(wxUSE_GSOCKET_CPLUSPLUS)
22 #error "You need to compile this file (probably a GUI gsock peice) as C++"
25 #ifndef __GSOCKET_STANDALONE__
27 #include "wx/platform.h"
29 #include "wx/dlimpexp.h" /* for WXDLLIMPEXP_NET */
33 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
38 Including sys/types.h under cygwin results in the warnings about "fd_set
39 having been defined in sys/types.h" when winsock.h is included later and
40 doesn't seem to be necessary anyhow. It's not needed under Mac neither.
42 #if !defined(__WXMAC__) && !defined(__CYGWIN__) && !defined(__WXWINCE__)
43 #include <sys/types.h>
50 #ifdef wxUSE_GSOCKET_CPLUSPLUS
58 #ifndef wxUSE_GSOCKET_CPLUSPLUS
59 typedef struct _GSocket GSocket
;
61 typedef struct _GAddress GAddress
;
89 /* See below for an explanation on how events work.
100 GSOCK_INPUT_FLAG
= 1 << GSOCK_INPUT
,
101 GSOCK_OUTPUT_FLAG
= 1 << GSOCK_OUTPUT
,
102 GSOCK_CONNECTION_FLAG
= 1 << GSOCK_CONNECTION
,
103 GSOCK_LOST_FLAG
= 1 << GSOCK_LOST
106 typedef int GSocketEventFlags
;
108 typedef void (*GSocketCallback
)(GSocket
*socket
, GSocketEvent event
,
112 /* Functions tables for internal use by GSocket code: */
114 #if !defined(__WINDOWS__) && !defined(wxUSE_GSOCKET_CPLUSPLUS)
115 struct GSocketBaseFunctionsTable
117 void (*Detected_Read
)(GSocket
*socket
);
118 void (*Detected_Write
)(GSocket
*socket
);
122 #if defined(wxUSE_GSOCKET_CPLUSPLUS)
123 /* Actually this is a misnomer now, but reusing this name means I don't
124 have to ifdef app traits or common socket code */
125 class GSocketGUIFunctionsTable
128 virtual bool OnInit() = 0;
129 virtual void OnExit() = 0;
130 virtual bool CanUseEventLoop() = 0;
131 virtual bool Init_Socket(GSocket
*socket
) = 0;
132 virtual void Destroy_Socket(GSocket
*socket
) = 0;
134 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
) = 0;
135 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
) = 0;
137 virtual void Enable_Events(GSocket
*socket
) = 0;
138 virtual void Disable_Events(GSocket
*socket
) = 0;
142 struct GSocketGUIFunctionsTable
144 int (*GUI_Init
)(void);
145 void (*GUI_Cleanup
)(void);
146 int (*GUI_Init_Socket
)(GSocket
*socket
);
147 void (*GUI_Destroy_Socket
)(GSocket
*socket
);
149 void (*Install_Callback
)(GSocket
*socket
, GSocketEvent event
);
150 void (*Uninstall_Callback
)(GSocket
*socket
, GSocketEvent event
);
152 void (*Enable_Events
)(GSocket
*socket
);
153 void (*Disable_Events
)(GSocket
*socket
);
155 #endif /* defined(wxUSE_GSOCKET_CPLUSPLUS) */
158 /* Global initializers */
160 /* Sets GUI functions callbacks. Must be called *before* GSocket_Init
161 if the app uses async sockets. */
162 void GSocket_SetGUIFunctions(struct GSocketGUIFunctionsTable
*guifunc
);
164 /* GSocket_Init() must be called at the beginning */
165 int GSocket_Init(void);
167 /* GSocket_Cleanup() must be called at the end */
168 void GSocket_Cleanup(void);
171 /* Constructors / Destructors */
173 GSocket
*GSocket_new(void);
174 #if !defined(wxUSE_GSOCKET_CPLUSPLUS)
175 void GSocket_destroy(GSocket
*socket
);
179 #ifndef wxUSE_GSOCKET_CPLUSPLUS
182 * Disallow further read/write operations on this socket, close
183 * the fd and disable all callbacks.
185 void GSocket_Shutdown(GSocket
*socket
);
187 /* Address handling */
193 * Set or get the local or peer address for this socket. The 'set'
194 * functions return GSOCK_NOERROR on success, an error code otherwise.
195 * The 'get' functions return a pointer to a GAddress object on success,
196 * or NULL otherwise, in which case they set the error code of the
197 * corresponding GSocket.
200 * GSOCK_INVSOCK - the socket is not valid.
201 * GSOCK_INVADDR - the address is not valid.
203 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
);
204 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
);
205 GAddress
*GSocket_GetLocal(GSocket
*socket
);
206 GAddress
*GSocket_GetPeer(GSocket
*socket
);
208 /* Server specific parts */
210 /* GSocket_SetServer:
211 * Sets up this socket as a server. The local address must have been
212 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
213 * Returns GSOCK_NOERROR on success, one of the following otherwise:
216 * GSOCK_INVSOCK - the socket is in use.
217 * GSOCK_INVADDR - the local address has not been set.
218 * GSOCK_IOERR - low-level error.
220 GSocketError
GSocket_SetServer(GSocket
*socket
);
222 /* GSocket_WaitConnection:
223 * Waits for an incoming client connection. Returns a pointer to
224 * a GSocket object, or NULL if there was an error, in which case
225 * the last error field will be updated for the calling GSocket.
227 * Error codes (set in the calling GSocket)
228 * GSOCK_INVSOCK - the socket is not valid or not a server.
229 * GSOCK_TIMEDOUT - timeout, no incoming connections.
230 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
231 * GSOCK_MEMERR - couldn't allocate memory.
232 * GSOCK_IOERR - low-level error.
234 GSocket
*GSocket_WaitConnection(GSocket
*socket
);
237 /* Client specific parts */
240 * For stream (connection oriented) sockets, GSocket_Connect() tries
241 * to establish a client connection to a server using the peer address
242 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
243 * connection has been succesfully established, or one of the error
244 * codes listed below. Note that for nonblocking sockets, a return
245 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
246 * request can be completed later; you should use GSocket_Select()
247 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
248 * corresponding asynchronous events.
250 * For datagram (non connection oriented) sockets, GSocket_Connect()
251 * just sets the peer address established with GSocket_SetPeer() as
252 * default destination.
255 * GSOCK_INVSOCK - the socket is in use or not valid.
256 * GSOCK_INVADDR - the peer address has not been established.
257 * GSOCK_TIMEDOUT - timeout, the connection failed.
258 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
259 * GSOCK_MEMERR - couldn't allocate memory.
260 * GSOCK_IOERR - low-level error.
262 GSocketError
GSocket_Connect(GSocket
*socket
, GSocketStream stream
);
264 /* GSocket_SetReusable:
265 * Simply sets the m_resuable flag on the socket. GSocket_SetServer will
266 * make the appropriate setsockopt() call.
267 * Implemented as a GSocket function because clients (ie, wxSocketServer)
268 * don't have access to the GSocket struct information.
269 * Returns TRUE if the flag was set correctly, FALSE if an error occured
270 * (ie, if the parameter was NULL)
272 int GSocket_SetReusable(GSocket
*socket
);
274 /* Datagram sockets */
276 /* GSocket_SetNonOriented:
277 * Sets up this socket as a non-connection oriented (datagram) socket.
278 * Before using this function, the local address must have been set
279 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
280 * on success, or one of the following otherwise.
283 * GSOCK_INVSOCK - the socket is in use.
284 * GSOCK_INVADDR - the local address has not been set.
285 * GSOCK_IOERR - low-level error.
287 GSocketError
GSocket_SetNonOriented(GSocket
*socket
);
292 /* Like recv(), send(), ... */
294 /* For datagram sockets, the incoming / outgoing addresses
295 * are stored as / read from the 'peer' address field.
297 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
);
298 int GSocket_Write(GSocket
*socket
, const char *buffer
,
302 * Polls the socket to determine its status. This function will
303 * check for the events specified in the 'flags' parameter, and
304 * it will return a mask indicating which operations can be
305 * performed. This function won't block, regardless of the
306 * mode (blocking | nonblocking) of the socket.
308 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
);
310 GSocketError
GSocket_GetSockOpt(GSocket
*socket
, int level
, int optname
,
311 void *optval
, int *optlen
);
313 GSocketError
GSocket_SetSockOpt(GSocket
*socket
, int level
, int optname
,
314 const void *optval
, int optlen
);
318 /* GSocket_SetNonBlocking:
319 * Sets the socket to non-blocking mode. All IO calls will return
322 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
);
324 /* GSocket_SetTimeout:
325 * Sets the timeout for blocking calls. Time is expressed in
328 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
);
331 * Returns the last error occured for this socket. Note that successful
332 * operations do not clear this back to GSOCK_NOERROR, so use it only
335 GSocketError WXDLLIMPEXP_NET
GSocket_GetError(GSocket
*socket
);
340 * There is data to be read in the input buffer. If, after a read
341 * operation, there is still data available, the callback function will
344 * The socket is available for writing. That is, the next write call
345 * won't block. This event is generated only once, when the connection is
346 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
347 * when the output buffer empties again. This means that the app should
348 * assume that it can write since the first OUTPUT event, and no more
349 * OUTPUT events will be generated unless an error occurs.
351 * Connection succesfully established, for client sockets, or incoming
352 * client connection, for server sockets. Wait for this event (also watch
353 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
355 * The connection is lost (or a connection request failed); this could
356 * be due to a failure, or due to the peer closing it gracefully.
359 /* GSocket_SetCallback:
360 * Enables the callbacks specified by 'flags'. Note that 'flags'
361 * may be a combination of flags OR'ed toghether, so the same
362 * callback function can be made to accept different events.
363 * The callback function must have the following prototype:
365 * void function(GSocket *socket, GSocketEvent event, char *cdata)
367 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
368 GSocketCallback fallback
, char *cdata
);
370 /* GSocket_UnsetCallback:
371 * Disables all callbacks specified by 'flags', which may be a
372 * combination of flags OR'ed toghether.
374 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
);
376 #endif /* ndef wxUSE_GSOCKET_CPLUSPLUS */
381 GAddress
*GAddress_new(void);
382 GAddress
*GAddress_copy(GAddress
*address
);
383 void GAddress_destroy(GAddress
*address
);
385 void GAddress_SetFamily(GAddress
*address
, GAddressType type
);
386 GAddressType
GAddress_GetFamily(GAddress
*address
);
388 /* The use of any of the next functions will set the address family to
389 * the specific one. For example if you use GAddress_INET_SetHostName,
390 * address family will be implicitly set to AF_INET.
393 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
);
394 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
);
395 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
396 unsigned long hostaddr
);
397 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
398 const char *protocol
);
399 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
);
401 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
,
403 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
);
404 unsigned short GAddress_INET_GetPort(GAddress
*address
);
406 /* TODO: Define specific parts (INET6, UNIX) */
408 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
);
409 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
);
413 #endif /* __cplusplus */
415 #ifdef wxUSE_GSOCKET_CPLUSPLUS
417 # include "wx/msw/gsockmsw.h"
419 # include "wx/unix/gsockunx.h"
423 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
425 #endif /* __GSOCKET_H */