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