]>
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__
18 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
22 #include <sys/types.h>
29 typedef struct _GSocket GSocket
;
30 typedef struct _GAddress GAddress
;
57 /* See below for an explanation on how events work.
68 GSOCK_INPUT_FLAG
= 1 << GSOCK_INPUT
,
69 GSOCK_OUTPUT_FLAG
= 1 << GSOCK_OUTPUT
,
70 GSOCK_CONNECTION_FLAG
= 1 << GSOCK_CONNECTION
,
71 GSOCK_LOST_FLAG
= 1 << GSOCK_LOST
74 typedef int GSocketEventFlags
;
76 typedef void (*GSocketCallback
)(GSocket
*socket
, GSocketEvent event
,
80 /* Global initializers */
82 /* GSocket_Init() must be called at the beginning */
83 int GSocket_Init(void);
85 /* GSocket_Cleanup() must be called at the end */
86 void GSocket_Cleanup(void);
89 /* Constructors / Destructors */
91 GSocket
*GSocket_new(void);
92 void GSocket_destroy(GSocket
*socket
);
97 * Disallow further read/write operations on this socket, close
98 * the fd and disable all callbacks.
100 void GSocket_Shutdown(GSocket
*socket
);
102 /* Address handling */
108 * Set or get the local or peer address for this socket. The 'set'
109 * functions return GSOCK_NOERROR on success, an error code otherwise.
110 * The 'get' functions return a pointer to a GAddress object on success,
111 * or NULL otherwise, in which case they set the error code of the
112 * corresponding GSocket.
115 * GSOCK_INVSOCK - the socket is not valid.
116 * GSOCK_INVADDR - the address is not valid.
118 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
);
119 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
);
120 GAddress
*GSocket_GetLocal(GSocket
*socket
);
121 GAddress
*GSocket_GetPeer(GSocket
*socket
);
123 /* Server specific parts */
125 /* GSocket_SetServer:
126 * Sets up this socket as a server. The local address must have been
127 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
128 * Returns GSOCK_NOERROR on success, one of the following otherwise:
131 * GSOCK_INVSOCK - the socket is in use.
132 * GSOCK_INVADDR - the local address has not been set.
133 * GSOCK_IOERR - low-level error.
135 GSocketError
GSocket_SetServer(GSocket
*socket
);
137 /* GSocket_WaitConnection:
138 * Waits for an incoming client connection. Returns a pointer to
139 * a GSocket object, or NULL if there was an error, in which case
140 * the last error field will be updated for the calling GSocket.
142 * Error codes (set in the calling GSocket)
143 * GSOCK_INVSOCK - the socket is not valid or not a server.
144 * GSOCK_TIMEDOUT - timeout, no incoming connections.
145 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
146 * GSOCK_MEMERR - couldn't allocate memory.
147 * GSOCK_IOERR - low-level error.
149 GSocket
*GSocket_WaitConnection(GSocket
*socket
);
152 /* Client specific parts */
155 * For stream (connection oriented) sockets, GSocket_Connect() tries
156 * to establish a client connection to a server using the peer address
157 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
158 * connection has been succesfully established, or one of the error
159 * codes listed below. Note that for nonblocking sockets, a return
160 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
161 * request can be completed later; you should use GSocket_Select()
162 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
163 * corresponding asynchronous events.
165 * For datagram (non connection oriented) sockets, GSocket_Connect()
166 * just sets the peer address established with GSocket_SetPeer() as
167 * default destination.
170 * GSOCK_INVSOCK - the socket is in use or not valid.
171 * GSOCK_INVADDR - the peer address has not been established.
172 * GSOCK_TIMEDOUT - timeout, the connection failed.
173 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
174 * GSOCK_MEMERR - couldn't allocate memory.
175 * GSOCK_IOERR - low-level error.
177 GSocketError
GSocket_Connect(GSocket
*socket
, GSocketStream stream
);
180 /* Datagram sockets */
182 /* GSocket_SetNonOriented:
183 * Sets up this socket as a non-connection oriented (datagram) socket.
184 * Before using this function, the local address must have been set
185 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
186 * on success, or one of the following otherwise.
189 * GSOCK_INVSOCK - the socket is in use.
190 * GSOCK_INVADDR - the local address has not been set.
191 * GSOCK_IOERR - low-level error.
193 GSocketError
GSocket_SetNonOriented(GSocket
*socket
);
198 /* Like recv(), send(), ... */
200 /* For datagram sockets, the incoming / outgoing addresses
201 * are stored as / read from the 'peer' address field.
203 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
);
204 int GSocket_Write(GSocket
*socket
, const char *buffer
,
208 * Polls the socket to determine its status. This function will
209 * check for the events specified in the 'flags' parameter, and
210 * it will return a mask indicating which operations can be
211 * performed. This function won't block, regardless of the
212 * mode (blocking | nonblocking) of the socket.
214 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
);
219 /* GSocket_SetNonBlocking:
220 * Sets the socket to non-blocking mode. All IO calls will return
223 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
);
225 /* GSocket_SetTimeout:
226 * Sets the timeout for blocking calls. Time is expressed in
229 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
);
232 * Returns the last error occured for this socket. Note that successful
233 * operations do not clear this back to GSOCK_NOERROR, so use it only
236 GSocketError
GSocket_GetError(GSocket
*socket
);
242 * There is data to be read in the input buffer. If, after a read
243 * operation, there is still data available, the callback function will
246 * The socket is available for writing. That is, the next write call
247 * won't block. This event is generated only once, when the connection is
248 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
249 * when the output buffer empties again. This means that the app should
250 * assume that it can write since the first OUTPUT event, and no more
251 * OUTPUT events will be generated unless an error occurs.
253 * Connection succesfully established, for client sockets, or incoming
254 * client connection, for server sockets. Wait for this event (also watch
255 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
257 * The connection is lost (or a connection request failed); this could
258 * be due to a failure, or due to the peer closing it gracefully.
261 /* GSocket_SetCallback:
262 * Enables the callbacks specified by 'flags'. Note that 'flags'
263 * may be a combination of flags OR'ed toghether, so the same
264 * callback function can be made to accept different events.
265 * The callback function must have the following prototype:
267 * void function(GSocket *socket, GSocketEvent event, char *cdata)
269 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
270 GSocketCallback fallback
, char *cdata
);
272 /* GSocket_UnsetCallback:
273 * Disables all callbacks specified by 'flags', which may be a
274 * combination of flags OR'ed toghether.
276 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
);
281 GAddress
*GAddress_new(void);
282 GAddress
*GAddress_copy(GAddress
*address
);
283 void GAddress_destroy(GAddress
*address
);
285 void GAddress_SetFamily(GAddress
*address
, GAddressType type
);
286 GAddressType
GAddress_GetFamily(GAddress
*address
);
288 /* The use of any of the next functions will set the address family to
289 * the specific one. For example if you use GAddress_INET_SetHostName,
290 * address family will be implicitly set to AF_INET.
293 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
);
294 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
);
295 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
296 unsigned long hostaddr
);
297 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
298 const char *protocol
);
299 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
);
301 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
,
303 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
);
304 unsigned short GAddress_INET_GetPort(GAddress
*address
);
306 /* TODO: Define specific parts (INET6, UNIX) */
308 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
);
309 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
);
313 #endif /* __cplusplus */
316 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
318 #endif /* __GSOCKET_H */