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 /* kludge for GTK.. gsockgtk.c craps out miserably if we include
18 defs.h ... no idea how other files get away with it.. */
20 #if !defined( __WXMSW__ ) && !defined( WXDLLIMPEXP_NET )
21 #define WXDLLIMPEXP_NET
26 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
31 Including sys/types.h under cygwin results in the warnings about "fd_set
32 having been defined in sys/types.h" when winsock.h is included later and
33 doesn't seem to be necessary anyhow. It's not needed under Mac neither.
35 #if !defined(__WXMAC__) && !defined(__CYGWIN__) && !defined(__WXWINCE__)
36 #include <sys/types.h>
47 typedef struct _GSocket GSocket
;
48 typedef struct _GAddress GAddress
;
75 /* See below for an explanation on how events work.
86 GSOCK_INPUT_FLAG
= 1 << GSOCK_INPUT
,
87 GSOCK_OUTPUT_FLAG
= 1 << GSOCK_OUTPUT
,
88 GSOCK_CONNECTION_FLAG
= 1 << GSOCK_CONNECTION
,
89 GSOCK_LOST_FLAG
= 1 << GSOCK_LOST
92 typedef int GSocketEventFlags
;
94 typedef void (*GSocketCallback
)(GSocket
*socket
, GSocketEvent event
,
98 /* Global initializers */
100 /* GSocket_Init() must be called at the beginning */
101 int GSocket_Init(void);
103 /* GSocket_Cleanup() must be called at the end */
104 void GSocket_Cleanup(void);
107 /* Constructors / Destructors */
109 GSocket
*GSocket_new(void);
110 void GSocket_destroy(GSocket
*socket
);
115 * Disallow further read/write operations on this socket, close
116 * the fd and disable all callbacks.
118 void GSocket_Shutdown(GSocket
*socket
);
120 /* Address handling */
126 * Set or get the local or peer address for this socket. The 'set'
127 * functions return GSOCK_NOERROR on success, an error code otherwise.
128 * The 'get' functions return a pointer to a GAddress object on success,
129 * or NULL otherwise, in which case they set the error code of the
130 * corresponding GSocket.
133 * GSOCK_INVSOCK - the socket is not valid.
134 * GSOCK_INVADDR - the address is not valid.
136 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
);
137 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
);
138 GAddress
*GSocket_GetLocal(GSocket
*socket
);
139 GAddress
*GSocket_GetPeer(GSocket
*socket
);
141 /* Server specific parts */
143 /* GSocket_SetServer:
144 * Sets up this socket as a server. The local address must have been
145 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
146 * Returns GSOCK_NOERROR on success, one of the following otherwise:
149 * GSOCK_INVSOCK - the socket is in use.
150 * GSOCK_INVADDR - the local address has not been set.
151 * GSOCK_IOERR - low-level error.
153 GSocketError
GSocket_SetServer(GSocket
*socket
);
155 /* GSocket_WaitConnection:
156 * Waits for an incoming client connection. Returns a pointer to
157 * a GSocket object, or NULL if there was an error, in which case
158 * the last error field will be updated for the calling GSocket.
160 * Error codes (set in the calling GSocket)
161 * GSOCK_INVSOCK - the socket is not valid or not a server.
162 * GSOCK_TIMEDOUT - timeout, no incoming connections.
163 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
164 * GSOCK_MEMERR - couldn't allocate memory.
165 * GSOCK_IOERR - low-level error.
167 GSocket
*GSocket_WaitConnection(GSocket
*socket
);
170 /* Client specific parts */
173 * For stream (connection oriented) sockets, GSocket_Connect() tries
174 * to establish a client connection to a server using the peer address
175 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
176 * connection has been succesfully established, or one of the error
177 * codes listed below. Note that for nonblocking sockets, a return
178 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
179 * request can be completed later; you should use GSocket_Select()
180 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
181 * corresponding asynchronous events.
183 * For datagram (non connection oriented) sockets, GSocket_Connect()
184 * just sets the peer address established with GSocket_SetPeer() as
185 * default destination.
188 * GSOCK_INVSOCK - the socket is in use or not valid.
189 * GSOCK_INVADDR - the peer address has not been established.
190 * GSOCK_TIMEDOUT - timeout, the connection failed.
191 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
192 * GSOCK_MEMERR - couldn't allocate memory.
193 * GSOCK_IOERR - low-level error.
195 GSocketError
GSocket_Connect(GSocket
*socket
, GSocketStream stream
);
198 /* Datagram sockets */
200 /* GSocket_SetNonOriented:
201 * Sets up this socket as a non-connection oriented (datagram) socket.
202 * Before using this function, the local address must have been set
203 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
204 * on success, or one of the following otherwise.
207 * GSOCK_INVSOCK - the socket is in use.
208 * GSOCK_INVADDR - the local address has not been set.
209 * GSOCK_IOERR - low-level error.
211 GSocketError
GSocket_SetNonOriented(GSocket
*socket
);
216 /* Like recv(), send(), ... */
218 /* For datagram sockets, the incoming / outgoing addresses
219 * are stored as / read from the 'peer' address field.
221 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
);
222 int GSocket_Write(GSocket
*socket
, const char *buffer
,
226 * Polls the socket to determine its status. This function will
227 * check for the events specified in the 'flags' parameter, and
228 * it will return a mask indicating which operations can be
229 * performed. This function won't block, regardless of the
230 * mode (blocking | nonblocking) of the socket.
232 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
);
237 /* GSocket_SetNonBlocking:
238 * Sets the socket to non-blocking mode. All IO calls will return
241 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
);
243 /* GSocket_SetTimeout:
244 * Sets the timeout for blocking calls. Time is expressed in
247 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
);
250 * Returns the last error occured for this socket. Note that successful
251 * operations do not clear this back to GSOCK_NOERROR, so use it only
254 GSocketError WXDLLIMPEXP_NET
GSocket_GetError(GSocket
*socket
);
260 * There is data to be read in the input buffer. If, after a read
261 * operation, there is still data available, the callback function will
264 * The socket is available for writing. That is, the next write call
265 * won't block. This event is generated only once, when the connection is
266 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
267 * when the output buffer empties again. This means that the app should
268 * assume that it can write since the first OUTPUT event, and no more
269 * OUTPUT events will be generated unless an error occurs.
271 * Connection succesfully established, for client sockets, or incoming
272 * client connection, for server sockets. Wait for this event (also watch
273 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
275 * The connection is lost (or a connection request failed); this could
276 * be due to a failure, or due to the peer closing it gracefully.
279 /* GSocket_SetCallback:
280 * Enables the callbacks specified by 'flags'. Note that 'flags'
281 * may be a combination of flags OR'ed toghether, so the same
282 * callback function can be made to accept different events.
283 * The callback function must have the following prototype:
285 * void function(GSocket *socket, GSocketEvent event, char *cdata)
287 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
288 GSocketCallback fallback
, char *cdata
);
290 /* GSocket_UnsetCallback:
291 * Disables all callbacks specified by 'flags', which may be a
292 * combination of flags OR'ed toghether.
294 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
);
299 GAddress
*GAddress_new(void);
300 GAddress
*GAddress_copy(GAddress
*address
);
301 void GAddress_destroy(GAddress
*address
);
303 void GAddress_SetFamily(GAddress
*address
, GAddressType type
);
304 GAddressType
GAddress_GetFamily(GAddress
*address
);
306 /* The use of any of the next functions will set the address family to
307 * the specific one. For example if you use GAddress_INET_SetHostName,
308 * address family will be implicitly set to AF_INET.
311 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
);
312 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
);
313 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
314 unsigned long hostaddr
);
315 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
316 const char *protocol
);
317 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
);
319 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
,
321 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
);
322 unsigned short GAddress_INET_GetPort(GAddress
*address
);
324 /* TODO: Define specific parts (INET6, UNIX) */
326 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
);
327 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
);
331 #endif /* __cplusplus */
334 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
336 #endif /* __GSOCKET_H */