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