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