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