]>
Commit | Line | Data |
---|---|---|
a324a7bc GL |
1 | /* ------------------------------------------------------------------------- |
2 | * Project: GSocket (Generic Socket) | |
3 | * Name: gsocket.h | |
4 | * Purpose: GSocket include file (system independent) | |
5 | * CVSID: $Id$ | |
a324a7bc GL |
6 | * ------------------------------------------------------------------------- |
7 | */ | |
8 | #ifndef __GSOCKET_H | |
9 | #define __GSOCKET_H | |
10 | ||
d422d01e RR |
11 | #include "wx/setup.h" |
12 | ||
13 | #if wxUSE_SOCKETS | |
14 | ||
98781fa3 | 15 | #include <stddef.h> |
a324a7bc GL |
16 | #include <sys/types.h> |
17 | ||
18 | #if !defined(__cplusplus) | |
a324a7bc | 19 | typedef int bool; |
a324a7bc GL |
20 | #endif |
21 | ||
22 | #ifndef TRUE | |
23 | #define TRUE 1 | |
24 | #endif | |
25 | ||
26 | #ifndef FALSE | |
27 | #define FALSE 0 | |
28 | #endif | |
29 | ||
e9e3e3ab GRG |
30 | |
31 | #ifdef __cplusplus | |
32 | extern "C" { | |
33 | #endif | |
34 | ||
a324a7bc GL |
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, | |
f439844b | 57 | GSOCK_INVPORT, |
98781fa3 | 58 | GSOCK_WOULDBLOCK, |
aa6d9706 | 59 | GSOCK_TIMEDOUT, |
e00f35bb | 60 | GSOCK_MEMERR |
a324a7bc GL |
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, | |
31528cd3 | 75 | GSOCK_LOST_FLAG = 1 << GSOCK_LOST |
a324a7bc GL |
76 | }; |
77 | ||
78 | typedef int GSocketEventFlags; | |
79 | ||
39b91eca | 80 | typedef void (*GSocketCallback)(GSocket *socket, GSocketEvent event, |
31528cd3 | 81 | char *cdata); |
a324a7bc | 82 | |
a324a7bc | 83 | |
e9e3e3ab | 84 | /* Global initializers */ |
a58d5df4 GL |
85 | |
86 | /* GSocket_Init() must be called at the beginning */ | |
31989b0b | 87 | bool GSocket_Init(); |
a58d5df4 GL |
88 | /* GSocket_Cleanup() must be called at the ending */ |
89 | void GSocket_Cleanup(); | |
90 | ||
a324a7bc GL |
91 | /* Constructors / Destructors */ |
92 | ||
93 | GSocket *GSocket_new(); | |
94 | void GSocket_destroy(GSocket *socket); | |
95 | ||
e9e3e3ab | 96 | /* This will disable all further IO calls to this socket */ |
a324a7bc GL |
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 | ||
e9e3e3ab GRG |
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 | */ | |
a324a7bc GL |
119 | GSocketError GSocket_SetServer(GSocket *socket); |
120 | ||
e9e3e3ab GRG |
121 | /* GSocket_WaitConnection: |
122 | * Waits for an incoming client connection. | |
123 | */ | |
a324a7bc GL |
124 | GSocket *GSocket_WaitConnection(GSocket *socket); |
125 | ||
126 | /* Client specific parts */ | |
127 | ||
e9e3e3ab GRG |
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 | */ | |
a324a7bc GL |
137 | GSocketError GSocket_Connect(GSocket *socket, GSocketStream stream); |
138 | ||
139 | /* Generic IO */ | |
140 | ||
141 | /* Like recv(), send(), ... */ | |
e9e3e3ab GRG |
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 | */ | |
a324a7bc GL |
147 | int GSocket_Read(GSocket *socket, char *buffer, int size); |
148 | int GSocket_Write(GSocket *socket, const char *buffer, | |
31528cd3 | 149 | int size); |
e9e3e3ab GRG |
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); | |
a324a7bc | 159 | |
39b91eca GL |
160 | /* Flags/Parameters */ |
161 | ||
e9e3e3ab GRG |
162 | /* GSocket_SetTimeout: |
163 | * Sets the timeout for blocking calls. Time is | |
164 | * expressed in milliseconds. | |
39b91eca GL |
165 | */ |
166 | void GSocket_SetTimeout(GSocket *socket, unsigned long millisec); | |
a324a7bc | 167 | |
e9e3e3ab GRG |
168 | /* GSocket_SetNonBlocking: |
169 | * Sets the socket to non-blocking mode. This is useful if | |
170 | * we don't want to wait. | |
171 | */ | |
39b91eca | 172 | void GSocket_SetNonBlocking(GSocket *socket, bool non_block); |
a324a7bc | 173 | |
e9e3e3ab GRG |
174 | /* GSocket_GetError: |
175 | * Returns the last error occured for this socket. | |
176 | */ | |
a324a7bc GL |
177 | GSocketError GSocket_GetError(GSocket *socket); |
178 | ||
179 | /* Callbacks */ | |
180 | ||
e9e3e3ab GRG |
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, | |
39b91eca | 206 | GSocketCallback fallback, char *cdata); |
a324a7bc | 207 | |
e9e3e3ab GRG |
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); | |
a324a7bc GL |
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 | ||
e9e3e3ab GRG |
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 | */ | |
a324a7bc GL |
227 | |
228 | GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname); | |
229 | GSocketError GAddress_INET_SetHostAddress(GAddress *address, | |
230 | unsigned long hostaddr); | |
5a96d2f4 GL |
231 | GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port, |
232 | const char *protocol); | |
a324a7bc GL |
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 | ||
a324a7bc GL |
245 | #ifdef __cplusplus |
246 | }; | |
d422d01e RR |
247 | #endif /* __cplusplus */ |
248 | ||
249 | ||
e9e3e3ab | 250 | #endif /* wxUSE_SOCKETS */ |
a324a7bc | 251 | |
e9e3e3ab | 252 | #endif /* __GSOCKET_H */ |