]> git.saurik.com Git - wxWidgets.git/blob - include/wx/gsocket.h
e7d362e24720051d926e4a424c3b1304316782a1
[wxWidgets.git] / include / wx / gsocket.h
1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket)
3 * Name: gsocket.h
4 * Author: Guilhem Lavaux
5 * Guillermo Rodriguez Garcia <guille@iies.es>
6 * Copyright: (c) Guilhem Lavaux
7 * (c) 2007,2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 * Licence: wxWindows Licence
9 * Purpose: GSocket include file (system independent)
10 * CVSID: $Id$
11 * -------------------------------------------------------------------------
12 */
13
14 #ifndef _WX_GSOCKET_H_
15 #define _WX_GSOCKET_H_
16
17 #include "wx/defs.h"
18
19 #if wxUSE_SOCKETS
20
21 #include "wx/dlimpexp.h" /* for WXDLLIMPEXP_NET */
22
23 class WXDLLIMPEXP_FWD_NET wxSocketBase;
24
25 #include <stddef.h>
26
27 /*
28 Including sys/types.h under Cygwin results in the warnings about "fd_set
29 having been defined in sys/types.h" when winsock.h is included later and
30 doesn't seem to be necessary anyhow. It's not needed under Mac neither.
31 */
32 #if !defined(__WXMAC__) && !defined(__CYGWIN__) && !defined(__WXWINCE__)
33 #include <sys/types.h>
34 #endif
35
36 #ifdef __WXWINCE__
37 #include <stdlib.h>
38 #endif
39
40 enum GAddressType
41 {
42 GSOCK_NOFAMILY = 0,
43 GSOCK_INET,
44 GSOCK_INET6,
45 GSOCK_UNIX
46 };
47
48 enum GSocketStream
49 {
50 GSOCK_STREAMED,
51 GSOCK_UNSTREAMED
52 };
53
54 enum GSocketError
55 {
56 GSOCK_NOERROR = 0,
57 GSOCK_INVOP,
58 GSOCK_IOERR,
59 GSOCK_INVADDR,
60 GSOCK_INVSOCK,
61 GSOCK_NOHOST,
62 GSOCK_INVPORT,
63 GSOCK_WOULDBLOCK,
64 GSOCK_TIMEDOUT,
65 GSOCK_MEMERR,
66 GSOCK_OPTERR
67 };
68
69 /* See below for an explanation on how events work.
70 */
71 enum GSocketEvent
72 {
73 GSOCK_INPUT = 0,
74 GSOCK_OUTPUT = 1,
75 GSOCK_CONNECTION = 2,
76 GSOCK_LOST = 3,
77 GSOCK_MAX_EVENT = 4
78 };
79
80 enum
81 {
82 GSOCK_INPUT_FLAG = 1 << GSOCK_INPUT,
83 GSOCK_OUTPUT_FLAG = 1 << GSOCK_OUTPUT,
84 GSOCK_CONNECTION_FLAG = 1 << GSOCK_CONNECTION,
85 GSOCK_LOST_FLAG = 1 << GSOCK_LOST
86 };
87
88 typedef int GSocketEventFlags;
89
90 struct GAddress;
91 class GSocket;
92
93 typedef void (*GSocketCallback)(GSocket *socket, GSocketEvent event,
94 char *cdata);
95
96 /*
97 Class providing hooks abstracting the differences between console and GUI
98 applications for socket code.
99
100 We also have different implementations of this class for different platforms
101 allowing us to keep more things in the common code but the main reason for
102 its existence is that we want the same socket code work differently
103 depending on whether it's used from a console or a GUI program. This is
104 achieved by implementing the virtual methods of this class differently in
105 the objects returned by wxConsoleAppTraits::GetSocketFunctionsTable() and
106 the same method in wxGUIAppTraits.
107 */
108 class GSocketManager
109 {
110 public:
111 // set the manager to use, we don't take ownership of it
112 //
113 // this should be called before GSocket_Init(), i.e. before the first
114 // wxSocket object is created, otherwise the manager returned by
115 // wxAppTraits::GetSocketManager() will be used
116 static void Set(GSocketManager *manager);
117
118 // return the manager to use
119 //
120 // this initializes the manager at first use
121 static GSocketManager *Get()
122 {
123 if ( !ms_manager )
124 Init();
125
126 return ms_manager;
127 }
128
129 // called before the first wxSocket is created and should do the
130 // initializations needed in order to use the network
131 //
132 // return true if initialized successfully
133 virtual bool OnInit() = 0;
134
135 // undo the initializations of OnInit()
136 virtual void OnExit() = 0;
137
138
139 // do manager-specific socket initializations (and undo it): this is called
140 // in the beginning/end of the socket initialization/destruction
141 virtual bool Init_Socket(GSocket *socket) = 0;
142 virtual void Destroy_Socket(GSocket *socket) = 0;
143
144 virtual void Install_Callback(GSocket *socket, GSocketEvent event) = 0;
145 virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event) = 0;
146
147 virtual void Enable_Events(GSocket *socket) = 0;
148 virtual void Disable_Events(GSocket *socket) = 0;
149
150 virtual ~GSocketManager() { }
151
152 private:
153 // get the manager to use if we don't have it yet
154 static void Init();
155
156 static GSocketManager *ms_manager;
157 };
158
159 /*
160 Base class providing functionality common to BSD and Winsock sockets.
161
162 TODO: merge this in wxSocket itself, there is no reason to maintain the
163 separation between wxSocket and GSocket.
164 */
165 class GSocketBase
166 {
167 public:
168 // static factory function: creates the low-level socket associated with
169 // the given wxSocket (and inherits its attributes such as timeout)
170 static GSocket *Create(wxSocketBase& wxsocket);
171
172 virtual ~GSocketBase();
173
174 void SetTimeout(unsigned long millisec);
175
176 GSocketError SetLocal(GAddress *address);
177 GSocketError SetPeer(GAddress *address);
178 GAddress *GetLocal();
179 GAddress *GetPeer();
180
181 GSocketEventFlags Select(GSocketEventFlags flags);
182
183 virtual GSocket *WaitConnection(wxSocketBase& wxsocket) = 0;
184
185 virtual void Close() = 0;
186 virtual void Shutdown();
187
188 void SetInitialSocketBuffers(int recv, int send)
189 {
190 m_initialRecvBufferSize = recv;
191 m_initialSendBufferSize = send;
192 }
193
194 // notify m_wxsocket about the given socket event by calling its (inaptly
195 // named) OnRequest() method
196 void NotifyOnStateChange(GSocketEvent event);
197
198 // this is officially SOCKET (unsigned int) under Windows but we don't want
199 // to include winsock.h which defines SOCKET from here so just use int
200 // under all platforms
201 int m_fd;
202
203 int m_initialRecvBufferSize;
204 int m_initialSendBufferSize;
205
206 GAddress *m_local;
207 GAddress *m_peer;
208 GSocketError m_error;
209
210 bool m_non_blocking;
211 bool m_server;
212 bool m_stream;
213 bool m_establishing;
214 bool m_reusable;
215 bool m_broadcast;
216 bool m_dobind;
217
218 #ifdef __WINDOWS__
219 struct timeval m_timeout;
220 #else
221 unsigned long m_timeout;
222 #endif
223
224 GSocketEventFlags m_detected;
225
226 protected:
227 GSocketBase(wxSocketBase& wxsocket);
228
229 private:
230 // set in ctor and never changed except that it's reset to NULL when the
231 // socket is shut down
232 wxSocketBase *m_wxsocket;
233
234 DECLARE_NO_COPY_CLASS(GSocketBase)
235 };
236
237 #if defined(__WINDOWS__)
238 #include "wx/msw/gsockmsw.h"
239 #else
240 #include "wx/unix/gsockunx.h"
241 #endif
242
243 /* Global initializers */
244
245 /* GSocket_Init() must be called at the beginning (but after calling
246 * GSocketManager::Set() if a custom manager should be used) */
247 bool GSocket_Init();
248
249 /* GSocket_Cleanup() must be called at the end */
250 void GSocket_Cleanup();
251
252
253 /* GAddress */
254
255 // Represents a socket endpoint, i.e. -- in spite of its name -- not an address
256 // but an (address, port) pair
257 struct GAddress
258 {
259 struct sockaddr *m_addr;
260 size_t m_len;
261
262 GAddressType m_family;
263 int m_realfamily;
264
265 GSocketError m_error;
266 };
267
268 GAddress *GAddress_new();
269 GAddress *GAddress_copy(GAddress *address);
270 void GAddress_destroy(GAddress *address);
271
272 void GAddress_SetFamily(GAddress *address, GAddressType type);
273 GAddressType GAddress_GetFamily(GAddress *address);
274
275 /* The use of any of the next functions will set the address family to
276 * the specific one. For example if you use GAddress_INET_SetHostName,
277 * address family will be implicitly set to AF_INET.
278 */
279
280 GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname);
281 GSocketError GAddress_INET_SetBroadcastAddress(GAddress *address);
282 GSocketError GAddress_INET_SetAnyAddress(GAddress *address);
283 GSocketError GAddress_INET_SetHostAddress(GAddress *address,
284 unsigned long hostaddr);
285 GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port,
286 const char *protocol);
287 GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port);
288
289 GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname,
290 size_t sbuf);
291 unsigned long GAddress_INET_GetHostAddress(GAddress *address);
292 unsigned short GAddress_INET_GetPort(GAddress *address);
293
294 GSocketError _GAddress_translate_from(GAddress *address,
295 struct sockaddr *addr, int len);
296 GSocketError _GAddress_translate_to (GAddress *address,
297 struct sockaddr **addr, int *len);
298 GSocketError _GAddress_Init_INET(GAddress *address);
299
300 #if wxUSE_IPV6
301
302 GSocketError GAddress_INET6_SetHostName(GAddress *address, const char *hostname);
303 GSocketError GAddress_INET6_SetAnyAddress(GAddress *address);
304 GSocketError GAddress_INET6_SetHostAddress(GAddress *address,
305 struct in6_addr hostaddr);
306 GSocketError GAddress_INET6_SetPortName(GAddress *address, const char *port,
307 const char *protocol);
308 GSocketError GAddress_INET6_SetPort(GAddress *address, unsigned short port);
309
310 GSocketError GAddress_INET6_GetHostName(GAddress *address, char *hostname,
311 size_t sbuf);
312 GSocketError GAddress_INET6_GetHostAddress(GAddress *address,struct in6_addr *hostaddr);
313 unsigned short GAddress_INET6_GetPort(GAddress *address);
314
315 #endif // wxUSE_IPV6
316
317 // these functions are available under all platforms but only implemented under
318 // Unix ones, elsewhere they just return GSOCK_INVADDR
319 GSocketError _GAddress_Init_UNIX(GAddress *address);
320 GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path);
321 GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf);
322
323 #endif /* wxUSE_SOCKETS */
324
325 #endif /* _WX_GSOCKET_H_ */