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 #ifndef __GSOCKET_STANDALONE__
16 #include "wx/platform.h"
18 #include "wx/dlimpexp.h" // for WXDLLIMPEXP_NET
22 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
27 Including sys/types.h under cygwin results in the warnings about "fd_set
28 having been defined in sys/types.h" when winsock.h is included later and
29 doesn't seem to be necessary anyhow. It's not needed under Mac neither.
31 #if !defined(__WXMAC__) && !defined(__CYGWIN__) && !defined(__WXWINCE__)
32 #include <sys/types.h>
43 typedef struct _GSocket GSocket
;
44 typedef struct _GAddress GAddress
;
71 /* See below for an explanation on how events work.
82 GSOCK_INPUT_FLAG
= 1 << GSOCK_INPUT
,
83 GSOCK_OUTPUT_FLAG
= 1 << GSOCK_OUTPUT
,
84 GSOCK_CONNECTION_FLAG
= 1 << GSOCK_CONNECTION
,
85 GSOCK_LOST_FLAG
= 1 << GSOCK_LOST
88 typedef int GSocketEventFlags
;
90 typedef void (*GSocketCallback
)(GSocket
*socket
, GSocketEvent event
,
94 /* Functions tables for internal use by GSocket code: */
97 struct GSocketBaseFunctionsTable
99 void (*Detected_Read
)(GSocket
*socket
);
100 void (*Detected_Write
)(GSocket
*socket
);
104 struct GSocketGUIFunctionsTable
106 int (*GUI_Init
)(void);
107 void (*GUI_Cleanup
)(void);
108 int (*GUI_Init_Socket
)(GSocket
*socket
);
109 void (*GUI_Destroy_Socket
)(GSocket
*socket
);
111 void (*Install_Callback
)(GSocket
*socket
, GSocketEvent event
);
112 void (*Uninstall_Callback
)(GSocket
*socket
, GSocketEvent event
);
114 void (*Enable_Events
)(GSocket
*socket
);
115 void (*Disable_Events
)(GSocket
*socket
);
119 /* Global initializers */
121 /* Sets GUI functions callbacks. Must be called *before* GSocket_Init
122 if the app uses async sockets. */
123 void GSocket_SetGUIFunctions(struct GSocketGUIFunctionsTable
*guifunc
);
125 /* GSocket_Init() must be called at the beginning */
126 int GSocket_Init(void);
128 /* GSocket_Cleanup() must be called at the end */
129 void GSocket_Cleanup(void);
132 /* Constructors / Destructors */
134 GSocket
*GSocket_new(void);
135 void GSocket_destroy(GSocket
*socket
);
140 * Disallow further read/write operations on this socket, close
141 * the fd and disable all callbacks.
143 void GSocket_Shutdown(GSocket
*socket
);
145 /* Address handling */
151 * Set or get the local or peer address for this socket. The 'set'
152 * functions return GSOCK_NOERROR on success, an error code otherwise.
153 * The 'get' functions return a pointer to a GAddress object on success,
154 * or NULL otherwise, in which case they set the error code of the
155 * corresponding GSocket.
158 * GSOCK_INVSOCK - the socket is not valid.
159 * GSOCK_INVADDR - the address is not valid.
161 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
);
162 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
);
163 GAddress
*GSocket_GetLocal(GSocket
*socket
);
164 GAddress
*GSocket_GetPeer(GSocket
*socket
);
166 /* Server specific parts */
168 /* GSocket_SetServer:
169 * Sets up this socket as a server. The local address must have been
170 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
171 * Returns GSOCK_NOERROR on success, one of the following otherwise:
174 * GSOCK_INVSOCK - the socket is in use.
175 * GSOCK_INVADDR - the local address has not been set.
176 * GSOCK_IOERR - low-level error.
178 GSocketError
GSocket_SetServer(GSocket
*socket
);
180 /* GSocket_WaitConnection:
181 * Waits for an incoming client connection. Returns a pointer to
182 * a GSocket object, or NULL if there was an error, in which case
183 * the last error field will be updated for the calling GSocket.
185 * Error codes (set in the calling GSocket)
186 * GSOCK_INVSOCK - the socket is not valid or not a server.
187 * GSOCK_TIMEDOUT - timeout, no incoming connections.
188 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
189 * GSOCK_MEMERR - couldn't allocate memory.
190 * GSOCK_IOERR - low-level error.
192 GSocket
*GSocket_WaitConnection(GSocket
*socket
);
195 /* Client specific parts */
198 * For stream (connection oriented) sockets, GSocket_Connect() tries
199 * to establish a client connection to a server using the peer address
200 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
201 * connection has been succesfully established, or one of the error
202 * codes listed below. Note that for nonblocking sockets, a return
203 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
204 * request can be completed later; you should use GSocket_Select()
205 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
206 * corresponding asynchronous events.
208 * For datagram (non connection oriented) sockets, GSocket_Connect()
209 * just sets the peer address established with GSocket_SetPeer() as
210 * default destination.
213 * GSOCK_INVSOCK - the socket is in use or not valid.
214 * GSOCK_INVADDR - the peer address has not been established.
215 * GSOCK_TIMEDOUT - timeout, the connection failed.
216 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
217 * GSOCK_MEMERR - couldn't allocate memory.
218 * GSOCK_IOERR - low-level error.
220 GSocketError
GSocket_Connect(GSocket
*socket
, GSocketStream stream
);
223 /* Datagram sockets */
225 /* GSocket_SetNonOriented:
226 * Sets up this socket as a non-connection oriented (datagram) socket.
227 * Before using this function, the local address must have been set
228 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
229 * on success, or one of the following otherwise.
232 * GSOCK_INVSOCK - the socket is in use.
233 * GSOCK_INVADDR - the local address has not been set.
234 * GSOCK_IOERR - low-level error.
236 GSocketError
GSocket_SetNonOriented(GSocket
*socket
);
241 /* Like recv(), send(), ... */
243 /* For datagram sockets, the incoming / outgoing addresses
244 * are stored as / read from the 'peer' address field.
246 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
);
247 int GSocket_Write(GSocket
*socket
, const char *buffer
,
251 * Polls the socket to determine its status. This function will
252 * check for the events specified in the 'flags' parameter, and
253 * it will return a mask indicating which operations can be
254 * performed. This function won't block, regardless of the
255 * mode (blocking | nonblocking) of the socket.
257 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
);
262 /* GSocket_SetNonBlocking:
263 * Sets the socket to non-blocking mode. All IO calls will return
266 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
);
268 /* GSocket_SetTimeout:
269 * Sets the timeout for blocking calls. Time is expressed in
272 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
);
275 * Returns the last error occured for this socket. Note that successful
276 * operations do not clear this back to GSOCK_NOERROR, so use it only
279 GSocketError WXDLLIMPEXP_NET
GSocket_GetError(GSocket
*socket
);
285 * There is data to be read in the input buffer. If, after a read
286 * operation, there is still data available, the callback function will
289 * The socket is available for writing. That is, the next write call
290 * won't block. This event is generated only once, when the connection is
291 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
292 * when the output buffer empties again. This means that the app should
293 * assume that it can write since the first OUTPUT event, and no more
294 * OUTPUT events will be generated unless an error occurs.
296 * Connection succesfully established, for client sockets, or incoming
297 * client connection, for server sockets. Wait for this event (also watch
298 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
300 * The connection is lost (or a connection request failed); this could
301 * be due to a failure, or due to the peer closing it gracefully.
304 /* GSocket_SetCallback:
305 * Enables the callbacks specified by 'flags'. Note that 'flags'
306 * may be a combination of flags OR'ed toghether, so the same
307 * callback function can be made to accept different events.
308 * The callback function must have the following prototype:
310 * void function(GSocket *socket, GSocketEvent event, char *cdata)
312 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
313 GSocketCallback fallback
, char *cdata
);
315 /* GSocket_UnsetCallback:
316 * Disables all callbacks specified by 'flags', which may be a
317 * combination of flags OR'ed toghether.
319 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
);
324 GAddress
*GAddress_new(void);
325 GAddress
*GAddress_copy(GAddress
*address
);
326 void GAddress_destroy(GAddress
*address
);
328 void GAddress_SetFamily(GAddress
*address
, GAddressType type
);
329 GAddressType
GAddress_GetFamily(GAddress
*address
);
331 /* The use of any of the next functions will set the address family to
332 * the specific one. For example if you use GAddress_INET_SetHostName,
333 * address family will be implicitly set to AF_INET.
336 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
);
337 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
);
338 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
339 unsigned long hostaddr
);
340 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
341 const char *protocol
);
342 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
);
344 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
,
346 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
);
347 unsigned short GAddress_INET_GetPort(GAddress
*address
);
349 /* TODO: Define specific parts (INET6, UNIX) */
351 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
);
352 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
);
356 #endif /* __cplusplus */
359 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
361 #endif /* __GSOCKET_H */