]>
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 | ||
a324a7bc GL |
15 | #include <sys/types.h> |
16 | ||
17 | #if !defined(__cplusplus) | |
a324a7bc | 18 | typedef int bool; |
a324a7bc GL |
19 | #endif |
20 | ||
21 | #ifndef TRUE | |
22 | #define TRUE 1 | |
23 | #endif | |
24 | ||
25 | #ifndef FALSE | |
26 | #define FALSE 0 | |
27 | #endif | |
28 | ||
29 | typedef struct _GSocket GSocket; | |
30 | typedef struct _GAddress GAddress; | |
31 | ||
32 | typedef enum { | |
33 | GSOCK_NOFAMILY = 0, | |
34 | GSOCK_INET, | |
35 | GSOCK_INET6, | |
36 | GSOCK_UNIX | |
37 | } GAddressType; | |
38 | ||
39 | typedef enum { | |
40 | GSOCK_STREAMED, | |
41 | GSOCK_UNSTREAMED | |
42 | } GSocketStream; | |
43 | ||
44 | typedef enum { | |
45 | GSOCK_NOERROR = 0, | |
46 | GSOCK_INVOP, | |
47 | GSOCK_IOERR, | |
48 | GSOCK_INVADDR, | |
49 | GSOCK_INVSOCK, | |
50 | GSOCK_NOHOST, | |
f439844b | 51 | GSOCK_INVPORT, |
e00f35bb GL |
52 | GSOCK_TRYAGAIN, |
53 | GSOCK_MEMERR | |
a324a7bc GL |
54 | } GSocketError; |
55 | ||
56 | typedef enum { | |
57 | GSOCK_INPUT = 0, | |
58 | GSOCK_OUTPUT = 1, | |
59 | GSOCK_CONNECTION = 2, | |
60 | GSOCK_LOST = 3, | |
61 | GSOCK_MAX_EVENT = 4 | |
62 | } GSocketEvent; | |
63 | ||
64 | enum { | |
65 | GSOCK_INPUT_FLAG = 1 << GSOCK_INPUT, | |
66 | GSOCK_OUTPUT_FLAG = 1 << GSOCK_OUTPUT, | |
67 | GSOCK_CONNECTION_FLAG = 1 << GSOCK_CONNECTION, | |
31528cd3 | 68 | GSOCK_LOST_FLAG = 1 << GSOCK_LOST |
a324a7bc GL |
69 | }; |
70 | ||
71 | typedef int GSocketEventFlags; | |
72 | ||
39b91eca | 73 | typedef void (*GSocketCallback)(GSocket *socket, GSocketEvent event, |
31528cd3 | 74 | char *cdata); |
a324a7bc GL |
75 | |
76 | #ifdef __cplusplus | |
77 | extern "C" { | |
78 | #endif | |
79 | ||
a58d5df4 GL |
80 | /* Global initialisers */ |
81 | ||
82 | /* GSocket_Init() must be called at the beginning */ | |
31989b0b | 83 | bool GSocket_Init(); |
a58d5df4 GL |
84 | /* GSocket_Cleanup() must be called at the ending */ |
85 | void GSocket_Cleanup(); | |
86 | ||
a324a7bc GL |
87 | /* Constructors / Destructors */ |
88 | ||
89 | GSocket *GSocket_new(); | |
90 | void GSocket_destroy(GSocket *socket); | |
91 | ||
92 | /* This will disable all IO calls to this socket but errors are still available */ | |
93 | void GSocket_Shutdown(GSocket *socket); | |
94 | ||
95 | /* Address handling */ | |
96 | ||
97 | GSocketError GSocket_SetLocal(GSocket *socket, GAddress *address); | |
98 | GSocketError GSocket_SetPeer(GSocket *socket, GAddress *address); | |
99 | GAddress *GSocket_GetLocal(GSocket *socket); | |
100 | GAddress *GSocket_GetPeer(GSocket *socket); | |
101 | ||
102 | /* Non-oriented connections handlers */ | |
103 | ||
104 | GSocketError GSocket_SetNonOriented(GSocket *socket); | |
105 | ||
106 | /* Server specific parts */ | |
107 | ||
108 | /* | |
7e1e0960 | 109 | GSocket_SetServer() setups the socket as a server. It uses the "Local" field |
a324a7bc GL |
110 | of GSocket. "Local" must be set by GSocket_SetLocal() before |
111 | GSocket_SetServer() is called. In the other case, it returns GSOCK_INVADDR. | |
112 | */ | |
113 | GSocketError GSocket_SetServer(GSocket *socket); | |
114 | ||
115 | /* | |
116 | GSocket_WaitConnection() waits for an incoming client connection. | |
117 | */ | |
118 | GSocket *GSocket_WaitConnection(GSocket *socket); | |
119 | ||
120 | /* Client specific parts */ | |
121 | ||
122 | /* | |
123 | GSocket_Connect() establishes a client connection to a server using the "Peer" | |
124 | field of GSocket. "Peer" must be set by GSocket_SetPeer() before | |
125 | GSocket_Connect() is called. In the other case, it returns GSOCK_INVADDR. | |
126 | */ | |
127 | GSocketError GSocket_Connect(GSocket *socket, GSocketStream stream); | |
128 | ||
129 | /* Generic IO */ | |
130 | ||
131 | /* Like recv(), send(), ... */ | |
31528cd3 | 132 | /* |
a324a7bc | 133 | NOTE: In case we read from a non-oriented connection, the incoming (outgoing) |
31528cd3 | 134 | connection address is stored in the "Local" ("Peer") field. |
a324a7bc GL |
135 | */ |
136 | int GSocket_Read(GSocket *socket, char *buffer, int size); | |
137 | int GSocket_Write(GSocket *socket, const char *buffer, | |
31528cd3 | 138 | int size); |
a324a7bc GL |
139 | bool GSocket_DataAvailable(GSocket *socket); |
140 | ||
39b91eca GL |
141 | /* Flags/Parameters */ |
142 | ||
143 | /* | |
144 | GSocket_SetTimeout() sets the timeout for reading and writing IO call. Time | |
145 | is expressed in milliseconds. | |
146 | */ | |
147 | void GSocket_SetTimeout(GSocket *socket, unsigned long millisec); | |
a324a7bc GL |
148 | |
149 | /* | |
150 | GSocket_SetBlocking() puts the socket in non-blocking mode. This is useful | |
151 | if we don't want to wait. | |
152 | */ | |
39b91eca | 153 | void GSocket_SetNonBlocking(GSocket *socket, bool non_block); |
a324a7bc GL |
154 | |
155 | /* | |
156 | GSocket_GetError() returns the last error occured on the socket stream. | |
157 | */ | |
158 | ||
159 | GSocketError GSocket_GetError(GSocket *socket); | |
160 | ||
161 | /* Callbacks */ | |
162 | ||
31528cd3 | 163 | /* |
a324a7bc | 164 | Only one fallback is possible for each event (INPUT, OUTPUT, CONNECTION, LOST) |
31528cd3 | 165 | INPUT: The function is called when there is at least a byte in the |
a324a7bc GL |
166 | input buffer |
167 | OUTPUT: The function is called when the system is sure the next write call | |
168 | will not block | |
169 | CONNECTION: Two cases is possible: | |
170 | Client socket -> the connection is established | |
31528cd3 | 171 | Server socket -> a client request a connection |
a324a7bc GL |
172 | LOST: the connection is lost |
173 | ||
39b91eca | 174 | SetCallback accepts a combination of these flags so a same callback can |
a324a7bc GL |
175 | receive different events. |
176 | ||
177 | An event is generated only once and its state is reseted when the relative | |
178 | IO call is requested. | |
179 | For example: INPUT -> GSocket_Read() | |
180 | CONNECTION -> GSocket_Accept() | |
181 | */ | |
39b91eca GL |
182 | void GSocket_SetCallback(GSocket *socket, GSocketEventFlags event, |
183 | GSocketCallback fallback, char *cdata); | |
a324a7bc GL |
184 | |
185 | /* | |
39b91eca | 186 | UnsetCallback will disables all fallbacks specified by "event". |
a324a7bc GL |
187 | NOTE: event may be a combination of flags |
188 | */ | |
39b91eca | 189 | void GSocket_UnsetCallback(GSocket *socket, GSocketEventFlags event); |
a324a7bc GL |
190 | |
191 | /* GAddress */ | |
192 | ||
193 | GAddress *GAddress_new(); | |
194 | GAddress *GAddress_copy(GAddress *address); | |
195 | void GAddress_destroy(GAddress *address); | |
196 | ||
197 | void GAddress_SetFamily(GAddress *address, GAddressType type); | |
198 | GAddressType GAddress_GetFamily(GAddress *address); | |
199 | ||
31528cd3 | 200 | /* |
a324a7bc GL |
201 | The use of any of the next functions will set the address family to the adapted |
202 | one. For example if you use GAddress_INET_SetHostName, address family will be AF_INET | |
203 | implicitely | |
204 | */ | |
205 | ||
206 | GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname); | |
207 | GSocketError GAddress_INET_SetHostAddress(GAddress *address, | |
208 | unsigned long hostaddr); | |
5a96d2f4 GL |
209 | GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port, |
210 | const char *protocol); | |
a324a7bc GL |
211 | GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port); |
212 | ||
213 | GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, | |
214 | size_t sbuf); | |
215 | unsigned long GAddress_INET_GetHostAddress(GAddress *address); | |
216 | unsigned short GAddress_INET_GetPort(GAddress *address); | |
217 | ||
218 | /* TODO: Define specific parts (INET6, UNIX) */ | |
219 | ||
220 | GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path); | |
221 | GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf); | |
222 | ||
223 | /* | |
224 | * System specific functions | |
225 | */ | |
226 | ||
227 | /* On systems needing an event id */ | |
228 | void GSocket_SetEventID(GSocket *socket, unsigned long evt_id); | |
229 | ||
230 | /* On systems which don't have background refresh */ | |
231 | void GSocket_DoEvent(unsigned long evt_id); | |
232 | ||
233 | #ifdef __cplusplus | |
234 | }; | |
d422d01e RR |
235 | #endif /* __cplusplus */ |
236 | ||
237 | ||
a324a7bc | 238 | #endif |
d422d01e | 239 | /* wxUSE_SOCKETS */ |
a324a7bc GL |
240 | |
241 | #endif | |
d422d01e | 242 | /* __GSOCKET_H */ |