]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/gsocket.h
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( WXDLLEXPORT )
26 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
30 #include <sys/types.h>
37 typedef struct _GSocket GSocket
;
38 typedef struct _GAddress GAddress
;
65 /* See below for an explanation on how events work.
76 GSOCK_INPUT_FLAG
= 1 << GSOCK_INPUT
,
77 GSOCK_OUTPUT_FLAG
= 1 << GSOCK_OUTPUT
,
78 GSOCK_CONNECTION_FLAG
= 1 << GSOCK_CONNECTION
,
79 GSOCK_LOST_FLAG
= 1 << GSOCK_LOST
82 typedef int GSocketEventFlags
;
84 typedef void (*GSocketCallback
)(GSocket
*socket
, GSocketEvent event
,
88 /* Global initializers */
90 /* GSocket_Init() must be called at the beginning */
91 int GSocket_Init(void);
93 /* GSocket_Cleanup() must be called at the end */
94 void GSocket_Cleanup(void);
97 /* Constructors / Destructors */
99 GSocket
*GSocket_new(void);
100 void GSocket_destroy(GSocket
*socket
);
105 * Disallow further read/write operations on this socket, close
106 * the fd and disable all callbacks.
108 void GSocket_Shutdown(GSocket
*socket
);
110 /* Address handling */
116 * Set or get the local or peer address for this socket. The 'set'
117 * functions return GSOCK_NOERROR on success, an error code otherwise.
118 * The 'get' functions return a pointer to a GAddress object on success,
119 * or NULL otherwise, in which case they set the error code of the
120 * corresponding GSocket.
123 * GSOCK_INVSOCK - the socket is not valid.
124 * GSOCK_INVADDR - the address is not valid.
126 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
);
127 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
);
128 GAddress
*GSocket_GetLocal(GSocket
*socket
);
129 GAddress
*GSocket_GetPeer(GSocket
*socket
);
131 /* Server specific parts */
133 /* GSocket_SetServer:
134 * Sets up this socket as a server. The local address must have been
135 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
136 * Returns GSOCK_NOERROR on success, one of the following otherwise:
139 * GSOCK_INVSOCK - the socket is in use.
140 * GSOCK_INVADDR - the local address has not been set.
141 * GSOCK_IOERR - low-level error.
143 GSocketError
GSocket_SetServer(GSocket
*socket
);
145 /* GSocket_WaitConnection:
146 * Waits for an incoming client connection. Returns a pointer to
147 * a GSocket object, or NULL if there was an error, in which case
148 * the last error field will be updated for the calling GSocket.
150 * Error codes (set in the calling GSocket)
151 * GSOCK_INVSOCK - the socket is not valid or not a server.
152 * GSOCK_TIMEDOUT - timeout, no incoming connections.
153 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
154 * GSOCK_MEMERR - couldn't allocate memory.
155 * GSOCK_IOERR - low-level error.
157 GSocket
*GSocket_WaitConnection(GSocket
*socket
);
160 /* Client specific parts */
163 * For stream (connection oriented) sockets, GSocket_Connect() tries
164 * to establish a client connection to a server using the peer address
165 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
166 * connection has been succesfully established, or one of the error
167 * codes listed below. Note that for nonblocking sockets, a return
168 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
169 * request can be completed later; you should use GSocket_Select()
170 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
171 * corresponding asynchronous events.
173 * For datagram (non connection oriented) sockets, GSocket_Connect()
174 * just sets the peer address established with GSocket_SetPeer() as
175 * default destination.
178 * GSOCK_INVSOCK - the socket is in use or not valid.
179 * GSOCK_INVADDR - the peer address has not been established.
180 * GSOCK_TIMEDOUT - timeout, the connection failed.
181 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
182 * GSOCK_MEMERR - couldn't allocate memory.
183 * GSOCK_IOERR - low-level error.
185 GSocketError
GSocket_Connect(GSocket
*socket
, GSocketStream stream
);
188 /* Datagram sockets */
190 /* GSocket_SetNonOriented:
191 * Sets up this socket as a non-connection oriented (datagram) socket.
192 * Before using this function, the local address must have been set
193 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
194 * on success, or one of the following otherwise.
197 * GSOCK_INVSOCK - the socket is in use.
198 * GSOCK_INVADDR - the local address has not been set.
199 * GSOCK_IOERR - low-level error.
201 GSocketError
GSocket_SetNonOriented(GSocket
*socket
);
206 /* Like recv(), send(), ... */
208 /* For datagram sockets, the incoming / outgoing addresses
209 * are stored as / read from the 'peer' address field.
211 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
);
212 int GSocket_Write(GSocket
*socket
, const char *buffer
,
216 * Polls the socket to determine its status. This function will
217 * check for the events specified in the 'flags' parameter, and
218 * it will return a mask indicating which operations can be
219 * performed. This function won't block, regardless of the
220 * mode (blocking | nonblocking) of the socket.
222 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
);
227 /* GSocket_SetNonBlocking:
228 * Sets the socket to non-blocking mode. All IO calls will return
231 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
);
233 /* GSocket_SetTimeout:
234 * Sets the timeout for blocking calls. Time is expressed in
237 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
);
240 * Returns the last error occured for this socket. Note that successful
241 * operations do not clear this back to GSOCK_NOERROR, so use it only
244 GSocketError WXDLLEXPORT
GSocket_GetError(GSocket
*socket
);
250 * There is data to be read in the input buffer. If, after a read
251 * operation, there is still data available, the callback function will
254 * The socket is available for writing. That is, the next write call
255 * won't block. This event is generated only once, when the connection is
256 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
257 * when the output buffer empties again. This means that the app should
258 * assume that it can write since the first OUTPUT event, and no more
259 * OUTPUT events will be generated unless an error occurs.
261 * Connection succesfully established, for client sockets, or incoming
262 * client connection, for server sockets. Wait for this event (also watch
263 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
265 * The connection is lost (or a connection request failed); this could
266 * be due to a failure, or due to the peer closing it gracefully.
269 /* GSocket_SetCallback:
270 * Enables the callbacks specified by 'flags'. Note that 'flags'
271 * may be a combination of flags OR'ed toghether, so the same
272 * callback function can be made to accept different events.
273 * The callback function must have the following prototype:
275 * void function(GSocket *socket, GSocketEvent event, char *cdata)
277 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
278 GSocketCallback fallback
, char *cdata
);
280 /* GSocket_UnsetCallback:
281 * Disables all callbacks specified by 'flags', which may be a
282 * combination of flags OR'ed toghether.
284 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
);
289 GAddress
*GAddress_new(void);
290 GAddress
*GAddress_copy(GAddress
*address
);
291 void GAddress_destroy(GAddress
*address
);
293 void GAddress_SetFamily(GAddress
*address
, GAddressType type
);
294 GAddressType
GAddress_GetFamily(GAddress
*address
);
296 /* The use of any of the next functions will set the address family to
297 * the specific one. For example if you use GAddress_INET_SetHostName,
298 * address family will be implicitly set to AF_INET.
301 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
);
302 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
);
303 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
304 unsigned long hostaddr
);
305 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
306 const char *protocol
);
307 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
);
309 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
,
311 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
);
312 unsigned short GAddress_INET_GetPort(GAddress
*address
);
314 /* TODO: Define specific parts (INET6, UNIX) */
316 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
);
317 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
);
321 #endif /* __cplusplus */
324 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
326 #endif /* __GSOCKET_H */