]> git.saurik.com Git - wxWidgets.git/blob - include/wx/gsocket.h
7190696fd081ba9b4b623d10e0005f51853533fe
[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 "wx/setup.h"
12
13 #if wxUSE_SOCKETS
14
15 #include <sys/types.h>
16
17 #if !defined(__cplusplus)
18 typedef int bool;
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,
51 GSOCK_INVPORT,
52 GSOCK_TRYAGAIN,
53 GSOCK_MEMERR
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,
68 GSOCK_LOST_FLAG = 1 << GSOCK_LOST
69 };
70
71 typedef int GSocketEventFlags;
72
73 typedef void (*GSocketCallback)(GSocket *socket, GSocketEvent event,
74 char *cdata);
75
76 #ifdef __cplusplus
77 extern "C" {
78 #endif
79
80 /* Global initialisers */
81
82 /* GSocket_Init() must be called at the beginning */
83 bool GSocket_Init();
84 /* GSocket_Cleanup() must be called at the ending */
85 void GSocket_Cleanup();
86
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 /*
109 GSocket_SetServer() setups the socket as a server. It uses the "Local" field
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(), ... */
132 /*
133 NOTE: In case we read from a non-oriented connection, the incoming (outgoing)
134 connection address is stored in the "Local" ("Peer") field.
135 */
136 int GSocket_Read(GSocket *socket, char *buffer, int size);
137 int GSocket_Write(GSocket *socket, const char *buffer,
138 int size);
139 bool GSocket_DataAvailable(GSocket *socket);
140
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);
148
149 /*
150 GSocket_SetBlocking() puts the socket in non-blocking mode. This is useful
151 if we don't want to wait.
152 */
153 void GSocket_SetNonBlocking(GSocket *socket, bool non_block);
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
163 /*
164 Only one fallback is possible for each event (INPUT, OUTPUT, CONNECTION, LOST)
165 INPUT: The function is called when there is at least a byte in the
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
171 Server socket -> a client request a connection
172 LOST: the connection is lost
173
174 SetCallback accepts a combination of these flags so a same callback can
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 */
182 void GSocket_SetCallback(GSocket *socket, GSocketEventFlags event,
183 GSocketCallback fallback, char *cdata);
184
185 /*
186 UnsetCallback will disables all fallbacks specified by "event".
187 NOTE: event may be a combination of flags
188 */
189 void GSocket_UnsetCallback(GSocket *socket, GSocketEventFlags event);
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
200 /*
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);
209 GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port,
210 const char *protocol);
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 };
235 #endif /* __cplusplus */
236
237
238 #endif
239 /* wxUSE_SOCKETS */
240
241 #endif
242 /* __GSOCKET_H */