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