]> git.saurik.com Git - wxWidgets.git/blob - include/wx/gsocket.h
fixed somebody's poorly done StreamSize-->GetSize transition
[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 <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 GSOCK_TRYAGAIN,
51 GSOCK_MEMERR
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,
66 GSOCK_LOST_FLAG = 1 << GSOCK_LOST
67 };
68
69 typedef int GSocketEventFlags;
70
71 typedef void (*GSocketCallback)(GSocket *socket, GSocketEvent event,
72 char *cdata);
73
74 #ifdef __cplusplus
75 extern "C" {
76 #endif
77
78 /* Global initialisers */
79
80 /* GSocket_Init() must be called at the beginning */
81 bool GSocket_Init();
82 /* GSocket_Cleanup() must be called at the ending */
83 void GSocket_Cleanup();
84
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 /*
107 GSocket_SetServer() setups the socket as a server. It uses the "Local" field
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(), ... */
130 /*
131 NOTE: In case we read from a non-oriented connection, the incoming (outgoing)
132 connection address is stored in the "Local" ("Peer") field.
133 */
134 int GSocket_Read(GSocket *socket, char *buffer, int size);
135 int GSocket_Write(GSocket *socket, const char *buffer,
136 int size);
137 bool GSocket_DataAvailable(GSocket *socket);
138
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);
146
147 /*
148 GSocket_SetBlocking() puts the socket in non-blocking mode. This is useful
149 if we don't want to wait.
150 */
151 void GSocket_SetNonBlocking(GSocket *socket, bool non_block);
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
161 /*
162 Only one fallback is possible for each event (INPUT, OUTPUT, CONNECTION, LOST)
163 INPUT: The function is called when there is at least a byte in the
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
169 Server socket -> a client request a connection
170 LOST: the connection is lost
171
172 SetCallback accepts a combination of these flags so a same callback can
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 */
180 void GSocket_SetCallback(GSocket *socket, GSocketEventFlags event,
181 GSocketCallback fallback, char *cdata);
182
183 /*
184 UnsetCallback will disables all fallbacks specified by "event".
185 NOTE: event may be a combination of flags
186 */
187 void GSocket_UnsetCallback(GSocket *socket, GSocketEventFlags event);
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
198 /*
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);
207 GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port,
208 const char *protocol);
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 */