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__
17 #include "wx/dlimpexp.h" // for WXDLLIMPEXP_NET
21 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
26 Including sys/types.h under cygwin results in the warnings about "fd_set
27 having been defined in sys/types.h" when winsock.h is included later and
28 doesn't seem to be necessary anyhow. It's not needed under Mac neither.
30 #if !defined(__WXMAC__) && !defined(__CYGWIN__) && !defined(__WXWINCE__)
31 #include <sys/types.h>
42 typedef struct _GSocket GSocket
;
43 typedef struct _GAddress GAddress
;
70 /* See below for an explanation on how events work.
81 GSOCK_INPUT_FLAG
= 1 << GSOCK_INPUT
,
82 GSOCK_OUTPUT_FLAG
= 1 << GSOCK_OUTPUT
,
83 GSOCK_CONNECTION_FLAG
= 1 << GSOCK_CONNECTION
,
84 GSOCK_LOST_FLAG
= 1 << GSOCK_LOST
87 typedef int GSocketEventFlags
;
89 typedef void (*GSocketCallback
)(GSocket
*socket
, GSocketEvent event
,
93 /* Functions tables for internal use by GSocket code: */
96 struct GSocketBaseFunctionsTable
98 void (*Detected_Read
)(GSocket
*socket
);
99 void (*Detected_Write
)(GSocket
*socket
);
103 struct GSocketGUIFunctionsTable
105 int (*GUI_Init
)(void);
106 void (*GUI_Cleanup
)(void);
107 int (*GUI_Init_Socket
)(GSocket
*socket
);
108 void (*GUI_Destroy_Socket
)(GSocket
*socket
);
110 void (*Install_Callback
)(GSocket
*socket
, GSocketEvent event
);
111 void (*Uninstall_Callback
)(GSocket
*socket
, GSocketEvent event
);
113 void (*Enable_Events
)(GSocket
*socket
);
114 void (*Disable_Events
)(GSocket
*socket
);
118 /* Global initializers */
120 /* Sets GUI functions callbacks. Must be called *before* GSocket_Init
121 if the app uses async sockets. */
122 void GSocket_SetGUIFunctions(struct GSocketGUIFunctionsTable
*guifunc
);
124 /* GSocket_Init() must be called at the beginning */
125 int GSocket_Init(void);
127 /* GSocket_Cleanup() must be called at the end */
128 void GSocket_Cleanup(void);
131 /* Constructors / Destructors */
133 GSocket
*GSocket_new(void);
134 void GSocket_destroy(GSocket
*socket
);
139 * Disallow further read/write operations on this socket, close
140 * the fd and disable all callbacks.
142 void GSocket_Shutdown(GSocket
*socket
);
144 /* Address handling */
150 * Set or get the local or peer address for this socket. The 'set'
151 * functions return GSOCK_NOERROR on success, an error code otherwise.
152 * The 'get' functions return a pointer to a GAddress object on success,
153 * or NULL otherwise, in which case they set the error code of the
154 * corresponding GSocket.
157 * GSOCK_INVSOCK - the socket is not valid.
158 * GSOCK_INVADDR - the address is not valid.
160 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
);
161 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
);
162 GAddress
*GSocket_GetLocal(GSocket
*socket
);
163 GAddress
*GSocket_GetPeer(GSocket
*socket
);
165 /* Server specific parts */
167 /* GSocket_SetServer:
168 * Sets up this socket as a server. The local address must have been
169 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
170 * Returns GSOCK_NOERROR on success, one of the following otherwise:
173 * GSOCK_INVSOCK - the socket is in use.
174 * GSOCK_INVADDR - the local address has not been set.
175 * GSOCK_IOERR - low-level error.
177 GSocketError
GSocket_SetServer(GSocket
*socket
);
179 /* GSocket_WaitConnection:
180 * Waits for an incoming client connection. Returns a pointer to
181 * a GSocket object, or NULL if there was an error, in which case
182 * the last error field will be updated for the calling GSocket.
184 * Error codes (set in the calling GSocket)
185 * GSOCK_INVSOCK - the socket is not valid or not a server.
186 * GSOCK_TIMEDOUT - timeout, no incoming connections.
187 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
188 * GSOCK_MEMERR - couldn't allocate memory.
189 * GSOCK_IOERR - low-level error.
191 GSocket
*GSocket_WaitConnection(GSocket
*socket
);
194 /* Client specific parts */
197 * For stream (connection oriented) sockets, GSocket_Connect() tries
198 * to establish a client connection to a server using the peer address
199 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
200 * connection has been succesfully established, or one of the error
201 * codes listed below. Note that for nonblocking sockets, a return
202 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
203 * request can be completed later; you should use GSocket_Select()
204 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
205 * corresponding asynchronous events.
207 * For datagram (non connection oriented) sockets, GSocket_Connect()
208 * just sets the peer address established with GSocket_SetPeer() as
209 * default destination.
212 * GSOCK_INVSOCK - the socket is in use or not valid.
213 * GSOCK_INVADDR - the peer address has not been established.
214 * GSOCK_TIMEDOUT - timeout, the connection failed.
215 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
216 * GSOCK_MEMERR - couldn't allocate memory.
217 * GSOCK_IOERR - low-level error.
219 GSocketError
GSocket_Connect(GSocket
*socket
, GSocketStream stream
);
222 /* Datagram sockets */
224 /* GSocket_SetNonOriented:
225 * Sets up this socket as a non-connection oriented (datagram) socket.
226 * Before using this function, the local address must have been set
227 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
228 * on success, or one of the following otherwise.
231 * GSOCK_INVSOCK - the socket is in use.
232 * GSOCK_INVADDR - the local address has not been set.
233 * GSOCK_IOERR - low-level error.
235 GSocketError
GSocket_SetNonOriented(GSocket
*socket
);
240 /* Like recv(), send(), ... */
242 /* For datagram sockets, the incoming / outgoing addresses
243 * are stored as / read from the 'peer' address field.
245 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
);
246 int GSocket_Write(GSocket
*socket
, const char *buffer
,
250 * Polls the socket to determine its status. This function will
251 * check for the events specified in the 'flags' parameter, and
252 * it will return a mask indicating which operations can be
253 * performed. This function won't block, regardless of the
254 * mode (blocking | nonblocking) of the socket.
256 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
);
261 /* GSocket_SetNonBlocking:
262 * Sets the socket to non-blocking mode. All IO calls will return
265 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
);
267 /* GSocket_SetTimeout:
268 * Sets the timeout for blocking calls. Time is expressed in
271 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
);
274 * Returns the last error occured for this socket. Note that successful
275 * operations do not clear this back to GSOCK_NOERROR, so use it only
278 GSocketError WXDLLIMPEXP_NET
GSocket_GetError(GSocket
*socket
);
284 * There is data to be read in the input buffer. If, after a read
285 * operation, there is still data available, the callback function will
288 * The socket is available for writing. That is, the next write call
289 * won't block. This event is generated only once, when the connection is
290 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
291 * when the output buffer empties again. This means that the app should
292 * assume that it can write since the first OUTPUT event, and no more
293 * OUTPUT events will be generated unless an error occurs.
295 * Connection succesfully established, for client sockets, or incoming
296 * client connection, for server sockets. Wait for this event (also watch
297 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
299 * The connection is lost (or a connection request failed); this could
300 * be due to a failure, or due to the peer closing it gracefully.
303 /* GSocket_SetCallback:
304 * Enables the callbacks specified by 'flags'. Note that 'flags'
305 * may be a combination of flags OR'ed toghether, so the same
306 * callback function can be made to accept different events.
307 * The callback function must have the following prototype:
309 * void function(GSocket *socket, GSocketEvent event, char *cdata)
311 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
312 GSocketCallback fallback
, char *cdata
);
314 /* GSocket_UnsetCallback:
315 * Disables all callbacks specified by 'flags', which may be a
316 * combination of flags OR'ed toghether.
318 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
);
323 GAddress
*GAddress_new(void);
324 GAddress
*GAddress_copy(GAddress
*address
);
325 void GAddress_destroy(GAddress
*address
);
327 void GAddress_SetFamily(GAddress
*address
, GAddressType type
);
328 GAddressType
GAddress_GetFamily(GAddress
*address
);
330 /* The use of any of the next functions will set the address family to
331 * the specific one. For example if you use GAddress_INET_SetHostName,
332 * address family will be implicitly set to AF_INET.
335 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
);
336 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
);
337 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
338 unsigned long hostaddr
);
339 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
340 const char *protocol
);
341 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
);
343 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
,
345 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
);
346 unsigned short GAddress_INET_GetPort(GAddress
*address
);
348 /* TODO: Define specific parts (INET6, UNIX) */
350 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
);
351 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
);
355 #endif /* __cplusplus */
358 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
360 #endif /* __GSOCKET_H */