]> git.saurik.com Git - wxWidgets.git/blob - include/wx/gsocket.h
ecc65f37f636d7f86114a8f57fbae5474b8d1039
[wxWidgets.git] / include / wx / gsocket.h
1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket)
3 * Name: gsocket.h
4 * Purpose: GSocket include file (system independent)
5 * CVSID: $Id$
6 * -------------------------------------------------------------------------
7 */
8 #ifndef __GSOCKET_H
9 #define __GSOCKET_H
10
11 #include "wx/setup.h"
12
13 #if wxUSE_SOCKETS
14
15 #include <stddef.h>
16 #include <sys/types.h>
17
18 #if !defined(__cplusplus)
19 typedef int bool;
20 #endif
21
22 #ifndef TRUE
23 #define TRUE 1
24 #endif
25
26 #ifndef FALSE
27 #define FALSE 0
28 #endif
29
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 typedef struct _GSocket GSocket;
36 typedef struct _GAddress GAddress;
37
38 typedef enum {
39 GSOCK_NOFAMILY = 0,
40 GSOCK_INET,
41 GSOCK_INET6,
42 GSOCK_UNIX
43 } GAddressType;
44
45 typedef enum {
46 GSOCK_STREAMED,
47 GSOCK_UNSTREAMED
48 } GSocketStream;
49
50 typedef enum {
51 GSOCK_NOERROR = 0,
52 GSOCK_INVOP,
53 GSOCK_IOERR,
54 GSOCK_INVADDR,
55 GSOCK_INVSOCK,
56 GSOCK_NOHOST,
57 GSOCK_INVPORT,
58 GSOCK_WOULDBLOCK,
59 GSOCK_TIMEDOUT,
60 GSOCK_MEMERR
61 } GSocketError;
62
63 typedef enum {
64 GSOCK_INPUT = 0,
65 GSOCK_OUTPUT = 1,
66 GSOCK_CONNECTION = 2,
67 GSOCK_LOST = 3,
68 GSOCK_MAX_EVENT = 4
69 } GSocketEvent;
70
71 enum {
72 GSOCK_INPUT_FLAG = 1 << GSOCK_INPUT,
73 GSOCK_OUTPUT_FLAG = 1 << GSOCK_OUTPUT,
74 GSOCK_CONNECTION_FLAG = 1 << GSOCK_CONNECTION,
75 GSOCK_LOST_FLAG = 1 << GSOCK_LOST
76 };
77
78 typedef int GSocketEventFlags;
79
80 typedef void (*GSocketCallback)(GSocket *socket, GSocketEvent event,
81 char *cdata);
82
83
84 /* Global initializers */
85
86 /* GSocket_Init() must be called at the beginning */
87 bool GSocket_Init();
88 /* GSocket_Cleanup() must be called at the ending */
89 void GSocket_Cleanup();
90
91 /* Constructors / Destructors */
92
93 GSocket *GSocket_new();
94 void GSocket_destroy(GSocket *socket);
95
96 /* This will disable all further IO calls to this socket */
97 void GSocket_Shutdown(GSocket *socket);
98
99 /* Address handling */
100
101 GSocketError GSocket_SetLocal(GSocket *socket, GAddress *address);
102 GSocketError GSocket_SetPeer(GSocket *socket, GAddress *address);
103 GAddress *GSocket_GetLocal(GSocket *socket);
104 GAddress *GSocket_GetPeer(GSocket *socket);
105
106 /* Non-oriented connections handlers */
107
108 GSocketError GSocket_SetNonOriented(GSocket *socket);
109
110 /* Server specific parts */
111
112 /* GSocket_SetServer:
113 * Sets up the socket as a server. It uses the "Local" field of GSocket.
114 * "Local" must be set by GSocket_SetLocal() before GSocket_SetServer()
115 * is called. Possible error codes are: GSOCK_INVSOCK if socket has not
116 * been initialized, GSOCK_INVADDR if the local address has not been
117 * defined and GSOCK_IOERR for other internal errors.
118 */
119 GSocketError GSocket_SetServer(GSocket *socket);
120
121 /* GSocket_WaitConnection:
122 * Waits for an incoming client connection.
123 */
124 GSocket *GSocket_WaitConnection(GSocket *socket);
125
126 /* Client specific parts */
127
128 /* GSocket_Connect:
129 * Establishes a client connection to a server using the "Peer"
130 * field of GSocket. "Peer" must be set by GSocket_SetPeer() before
131 * GSocket_Connect() is called. Possible error codes are GSOCK_INVSOCK,
132 * GSOCK_INVADDR, GSOCK_TIMEDOUT, GSOCK_WOULDBLOCK and GSOCK_IOERR.
133 * If a socket is nonblocking and Connect() returns GSOCK_WOULDBLOCK,
134 * the connection request can be completed later. Use GSocket_Select()
135 * to check it, or wait for a GSOCK_CONNECTION event.
136 */
137 GSocketError GSocket_Connect(GSocket *socket, GSocketStream stream);
138
139 /* Generic IO */
140
141 /* Like recv(), send(), ... */
142
143 /* NOTE: In case we read from a non-oriented connection, the incoming
144 * (outgoing) connection address is stored in the "Local" ("Peer")
145 * field.
146 */
147 int GSocket_Read(GSocket *socket, char *buffer, int size);
148 int GSocket_Write(GSocket *socket, const char *buffer,
149 int size);
150
151 /* GSocket_Select:
152 * Polls the socket to determine its status. This function will
153 * check for the events specified in the 'flags' parameter, and
154 * it will return a mask indicating which operations can be
155 * performed. This function won't block, regardless of the
156 * mode (blocking|nonblocking) of the socket.
157 */
158 GSocketEventFlags GSocket_Select(GSocket *socket, GSocketEventFlags flags);
159
160 /* Flags/Parameters */
161
162 /* GSocket_SetTimeout:
163 * Sets the timeout for blocking calls. Time is
164 * expressed in milliseconds.
165 */
166 void GSocket_SetTimeout(GSocket *socket, unsigned long millisec);
167
168 /* GSocket_SetNonBlocking:
169 * Sets the socket to non-blocking mode. This is useful if
170 * we don't want to wait.
171 */
172 void GSocket_SetNonBlocking(GSocket *socket, bool non_block);
173
174 /* GSocket_GetError:
175 * Returns the last error occured for this socket.
176 */
177 GSocketError GSocket_GetError(GSocket *socket);
178
179 /* Callbacks */
180
181 /* Only one callback is possible for each event (INPUT, OUTPUT, CONNECTION
182 * and LOST). The callbacks are called in the following situations:
183 *
184 * INPUT: There is at least one byte in the input buffer
185 * OUTPUT: The system is sure that the next write call will not block
186 * CONNECTION: Two cases are possible:
187 * Client socket -> the connection is established
188 * Server socket -> a client requests a connection
189 * LOST: The connection is lost
190 *
191 * An event is generated only once and its state is reseted when the
192 * relative IO call is requested.
193 * For example: INPUT -> GSocket_Read()
194 * CONNECTION -> GSocket_Accept()
195 */
196
197 /* GSocket_SetCallback:
198 * Enables the callbacks specified by 'flags'. Note that 'flags'
199 * may be a combination of flags OR'ed toghether, so the same
200 * callback function can be made to accept different events.
201 * The callback function must have the following prototype:
202 *
203 * void function(GSocket *socket, GSocketEvent event, char *cdata)
204 */
205 void GSocket_SetCallback(GSocket *socket, GSocketEventFlags flags,
206 GSocketCallback fallback, char *cdata);
207
208 /* GSocket_UnsetCallback:
209 * Disables all callbacks specified by 'flags', which may be a
210 * combination of flags OR'ed toghether.
211 */
212 void GSocket_UnsetCallback(GSocket *socket, GSocketEventFlags flags);
213
214 /* GAddress */
215
216 GAddress *GAddress_new();
217 GAddress *GAddress_copy(GAddress *address);
218 void GAddress_destroy(GAddress *address);
219
220 void GAddress_SetFamily(GAddress *address, GAddressType type);
221 GAddressType GAddress_GetFamily(GAddress *address);
222
223 /* The use of any of the next functions will set the address family to
224 * the specific one. For example if you use GAddress_INET_SetHostName,
225 * address family will be implicitly set to AF_INET.
226 */
227
228 GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname);
229 GSocketError GAddress_INET_SetHostAddress(GAddress *address,
230 unsigned long hostaddr);
231 GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port,
232 const char *protocol);
233 GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port);
234
235 GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname,
236 size_t sbuf);
237 unsigned long GAddress_INET_GetHostAddress(GAddress *address);
238 unsigned short GAddress_INET_GetPort(GAddress *address);
239
240 /* TODO: Define specific parts (INET6, UNIX) */
241
242 GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path);
243 GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf);
244
245 #ifdef __cplusplus
246 };
247 #endif /* __cplusplus */
248
249
250 #endif /* wxUSE_SOCKETS */
251
252 #endif /* __GSOCKET_H */