]> git.saurik.com Git - wxWidgets.git/blame - include/wx/gsocket.h
made deprecated erase() overload inline
[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
2804f77d 7 * (c) 2007 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
98781fa3 23#include <stddef.h>
58071ea0
VZ
24
25/*
26 Including sys/types.h under cygwin results in the warnings about "fd_set
27 having been defined in sys/types.h" when winsock.h is included later and
28 doesn't seem to be necessary anyhow. It's not needed under Mac neither.
29 */
882dfc67 30#if !defined(__WXMAC__) && !defined(__CYGWIN__) && !defined(__WXWINCE__)
a324a7bc 31#include <sys/types.h>
a02bb143 32#endif
a324a7bc 33
882dfc67
JS
34#ifdef __WXWINCE__
35#include <stdlib.h>
36#endif
37
a324a7bc
GL
38typedef struct _GAddress GAddress;
39
2804f77d
VZ
40enum GAddressType
41{
a324a7bc
GL
42 GSOCK_NOFAMILY = 0,
43 GSOCK_INET,
44 GSOCK_INET6,
45 GSOCK_UNIX
2804f77d 46};
a324a7bc 47
2804f77d
VZ
48enum GSocketStream
49{
a324a7bc
GL
50 GSOCK_STREAMED,
51 GSOCK_UNSTREAMED
2804f77d 52};
a324a7bc 53
2804f77d
VZ
54enum GSocketError
55{
a324a7bc
GL
56 GSOCK_NOERROR = 0,
57 GSOCK_INVOP,
58 GSOCK_IOERR,
59 GSOCK_INVADDR,
60 GSOCK_INVSOCK,
61 GSOCK_NOHOST,
f439844b 62 GSOCK_INVPORT,
98781fa3 63 GSOCK_WOULDBLOCK,
aa6d9706 64 GSOCK_TIMEDOUT,
bfa7bf7d 65 GSOCK_MEMERR,
98bdbbe3 66 GSOCK_OPTERR
2804f77d 67};
a324a7bc 68
9bf10d6b
GRG
69/* See below for an explanation on how events work.
70 */
2804f77d
VZ
71enum GSocketEvent
72{
a324a7bc
GL
73 GSOCK_INPUT = 0,
74 GSOCK_OUTPUT = 1,
75 GSOCK_CONNECTION = 2,
76 GSOCK_LOST = 3,
77 GSOCK_MAX_EVENT = 4
2804f77d 78};
a324a7bc 79
2804f77d
VZ
80enum
81{
a324a7bc
GL
82 GSOCK_INPUT_FLAG = 1 << GSOCK_INPUT,
83 GSOCK_OUTPUT_FLAG = 1 << GSOCK_OUTPUT,
84 GSOCK_CONNECTION_FLAG = 1 << GSOCK_CONNECTION,
31528cd3 85 GSOCK_LOST_FLAG = 1 << GSOCK_LOST
a324a7bc
GL
86};
87
88typedef int GSocketEventFlags;
89
2804f77d
VZ
90class GSocket;
91
39b91eca 92typedef void (*GSocketCallback)(GSocket *socket, GSocketEvent event,
31528cd3 93 char *cdata);
a324a7bc 94
2804f77d
VZ
95/*
96 Class providing hooks abstracting the differences between console and GUI
97 applications for socket code.
98
99 We also have different implementations of this class for different platforms
100 allowing us to keep more things in the common code but the main reason for
101 its existence is that we want the same socket code work differently
102 depending on whether it's used from a console or a GUI program. This is
103 achieved by implementing the virtual methods of this class differently in
104 the objects returned by wxConsoleAppTraits::GetSocketFunctionsTable() and
105 the same method in wxGUIAppTraits.
106 */
107class GSocketManager
4b4d23c7
DE
108{
109public:
2804f77d
VZ
110 // set the manager to use, we don't take ownership of it
111 //
112 // this should be called before GSocket_Init(), i.e. before the first
113 // wxSocket object is created, otherwise the manager returned by
114 // wxAppTraits::GetSocketManager() will be used
115 static void Set(GSocketManager *manager);
116
117 // return the manager to use
118 //
119 // this initializes the manager at first use
120 static GSocketManager *Get()
121 {
122 if ( !ms_manager )
123 Init();
124
125 return ms_manager;
126 }
127
128 // called before the first wxSocket is created and should do the
129 // initializations needed in order to use the network
130 //
131 // return true if initialized successfully
4b4d23c7 132 virtual bool OnInit() = 0;
2804f77d
VZ
133
134 // undo the initializations of OnInit()
4b4d23c7 135 virtual void OnExit() = 0;
2804f77d
VZ
136
137
138 // do manager-specific socket initializations (and undo it): this is called
139 // in the beginning/end of the socket initialization/destruction
4b4d23c7
DE
140 virtual bool Init_Socket(GSocket *socket) = 0;
141 virtual void Destroy_Socket(GSocket *socket) = 0;
2804f77d 142
4b4d23c7
DE
143 virtual void Install_Callback(GSocket *socket, GSocketEvent event) = 0;
144 virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event) = 0;
2804f77d 145
4b4d23c7
DE
146 virtual void Enable_Events(GSocket *socket) = 0;
147 virtual void Disable_Events(GSocket *socket) = 0;
2804f77d
VZ
148
149 virtual ~GSocketManager() { }
150
151private:
152 // get the manager to use if we don't have it yet
153 static void Init();
154
155 static GSocketManager *ms_manager;
4b4d23c7
DE
156};
157
2804f77d
VZ
158#if defined(__WINDOWS__)
159 #include "wx/msw/gsockmsw.h"
160#else
161 #include "wx/unix/gsockunx.h"
162#endif
38bb138f 163
a58d5df4 164
2804f77d 165/* Global initializers */
38bb138f 166
2804f77d
VZ
167/* GSocket_Init() must be called at the beginning (but after calling
168 * GSocketManager::Set() if a custom manager should be used) */
169bool GSocket_Init();
9bf10d6b
GRG
170
171/* GSocket_Cleanup() must be called at the end */
2804f77d 172void GSocket_Cleanup();
a58d5df4 173
9bf10d6b 174
a324a7bc
GL
175/* Constructors / Destructors */
176
2804f77d 177GSocket *GSocket_new();
09e6e5ec 178
9bf10d6b 179
a324a7bc
GL
180/* GAddress */
181
2804f77d 182GAddress *GAddress_new();
a324a7bc
GL
183GAddress *GAddress_copy(GAddress *address);
184void GAddress_destroy(GAddress *address);
185
186void GAddress_SetFamily(GAddress *address, GAddressType type);
187GAddressType GAddress_GetFamily(GAddress *address);
188
e9e3e3ab
GRG
189/* The use of any of the next functions will set the address family to
190 * the specific one. For example if you use GAddress_INET_SetHostName,
191 * address family will be implicitly set to AF_INET.
192 */
a324a7bc
GL
193
194GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname);
60edcf45 195GSocketError GAddress_INET_SetBroadcastAddress(GAddress *address);
9bf10d6b 196GSocketError GAddress_INET_SetAnyAddress(GAddress *address);
a324a7bc
GL
197GSocketError GAddress_INET_SetHostAddress(GAddress *address,
198 unsigned long hostaddr);
5a96d2f4
GL
199GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port,
200 const char *protocol);
a324a7bc
GL
201GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port);
202
203GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname,
204 size_t sbuf);
205unsigned long GAddress_INET_GetHostAddress(GAddress *address);
206unsigned short GAddress_INET_GetPort(GAddress *address);
207
8575ff50
VZ
208#if wxUSE_IPV6
209
210GSocketError GAddress_INET6_SetHostName(GAddress *address, const char *hostname);
211GSocketError GAddress_INET6_SetAnyAddress(GAddress *address);
212GSocketError GAddress_INET6_SetHostAddress(GAddress *address,
213 struct in6_addr hostaddr);
214GSocketError GAddress_INET6_SetPortName(GAddress *address, const char *port,
215 const char *protocol);
216GSocketError GAddress_INET6_SetPort(GAddress *address, unsigned short port);
217
218GSocketError GAddress_INET6_GetHostName(GAddress *address, char *hostname,
219 size_t sbuf);
220GSocketError GAddress_INET6_GetHostAddress(GAddress *address,struct in6_addr *hostaddr);
221unsigned short GAddress_INET6_GetPort(GAddress *address);
222
223#endif // wxUSE_IPV6
224
225/* TODO: Define specific parts (UNIX) */
a324a7bc
GL
226
227GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path);
228GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf);
229
2804f77d 230#endif /* wxUSE_SOCKETS */
a324a7bc 231
2804f77d 232#endif /* _WX_GSOCKET_H_ */