]>
Commit | Line | Data |
---|---|---|
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 | |
98781fa3 | 23 | #include <stddef.h> |
58071ea0 VZ |
24 | |
25 | /* | |
d80170bd | 26 | Including sys/types.h under Cygwin results in the warnings about "fd_set |
58071ea0 VZ |
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 | ||
2804f77d VZ |
38 | enum GAddressType |
39 | { | |
a324a7bc GL |
40 | GSOCK_NOFAMILY = 0, |
41 | GSOCK_INET, | |
42 | GSOCK_INET6, | |
43 | GSOCK_UNIX | |
2804f77d | 44 | }; |
a324a7bc | 45 | |
2804f77d VZ |
46 | enum GSocketStream |
47 | { | |
a324a7bc GL |
48 | GSOCK_STREAMED, |
49 | GSOCK_UNSTREAMED | |
2804f77d | 50 | }; |
a324a7bc | 51 | |
2804f77d VZ |
52 | enum GSocketError |
53 | { | |
a324a7bc GL |
54 | GSOCK_NOERROR = 0, |
55 | GSOCK_INVOP, | |
56 | GSOCK_IOERR, | |
57 | GSOCK_INVADDR, | |
58 | GSOCK_INVSOCK, | |
59 | GSOCK_NOHOST, | |
f439844b | 60 | GSOCK_INVPORT, |
98781fa3 | 61 | GSOCK_WOULDBLOCK, |
aa6d9706 | 62 | GSOCK_TIMEDOUT, |
bfa7bf7d | 63 | GSOCK_MEMERR, |
98bdbbe3 | 64 | GSOCK_OPTERR |
2804f77d | 65 | }; |
a324a7bc | 66 | |
9bf10d6b GRG |
67 | /* See below for an explanation on how events work. |
68 | */ | |
2804f77d VZ |
69 | enum GSocketEvent |
70 | { | |
a324a7bc GL |
71 | GSOCK_INPUT = 0, |
72 | GSOCK_OUTPUT = 1, | |
73 | GSOCK_CONNECTION = 2, | |
74 | GSOCK_LOST = 3, | |
75 | GSOCK_MAX_EVENT = 4 | |
2804f77d | 76 | }; |
a324a7bc | 77 | |
2804f77d VZ |
78 | enum |
79 | { | |
a324a7bc GL |
80 | GSOCK_INPUT_FLAG = 1 << GSOCK_INPUT, |
81 | GSOCK_OUTPUT_FLAG = 1 << GSOCK_OUTPUT, | |
82 | GSOCK_CONNECTION_FLAG = 1 << GSOCK_CONNECTION, | |
31528cd3 | 83 | GSOCK_LOST_FLAG = 1 << GSOCK_LOST |
a324a7bc GL |
84 | }; |
85 | ||
86 | typedef int GSocketEventFlags; | |
87 | ||
d80170bd | 88 | struct GAddress; |
2804f77d VZ |
89 | class GSocket; |
90 | ||
39b91eca | 91 | typedef void (*GSocketCallback)(GSocket *socket, GSocketEvent event, |
31528cd3 | 92 | char *cdata); |
a324a7bc | 93 | |
2804f77d VZ |
94 | /* |
95 | Class providing hooks abstracting the differences between console and GUI | |
96 | applications for socket code. | |
97 | ||
98 | We also have different implementations of this class for different platforms | |
99 | allowing us to keep more things in the common code but the main reason for | |
100 | its existence is that we want the same socket code work differently | |
101 | depending on whether it's used from a console or a GUI program. This is | |
102 | achieved by implementing the virtual methods of this class differently in | |
103 | the objects returned by wxConsoleAppTraits::GetSocketFunctionsTable() and | |
104 | the same method in wxGUIAppTraits. | |
105 | */ | |
106 | class GSocketManager | |
4b4d23c7 DE |
107 | { |
108 | public: | |
2804f77d VZ |
109 | // set the manager to use, we don't take ownership of it |
110 | // | |
111 | // this should be called before GSocket_Init(), i.e. before the first | |
112 | // wxSocket object is created, otherwise the manager returned by | |
113 | // wxAppTraits::GetSocketManager() will be used | |
114 | static void Set(GSocketManager *manager); | |
115 | ||
116 | // return the manager to use | |
117 | // | |
118 | // this initializes the manager at first use | |
119 | static GSocketManager *Get() | |
120 | { | |
121 | if ( !ms_manager ) | |
122 | Init(); | |
123 | ||
124 | return ms_manager; | |
125 | } | |
126 | ||
127 | // called before the first wxSocket is created and should do the | |
128 | // initializations needed in order to use the network | |
129 | // | |
130 | // return true if initialized successfully | |
4b4d23c7 | 131 | virtual bool OnInit() = 0; |
2804f77d VZ |
132 | |
133 | // undo the initializations of OnInit() | |
4b4d23c7 | 134 | virtual void OnExit() = 0; |
2804f77d VZ |
135 | |
136 | ||
137 | // do manager-specific socket initializations (and undo it): this is called | |
138 | // in the beginning/end of the socket initialization/destruction | |
4b4d23c7 DE |
139 | virtual bool Init_Socket(GSocket *socket) = 0; |
140 | virtual void Destroy_Socket(GSocket *socket) = 0; | |
2804f77d | 141 | |
4b4d23c7 DE |
142 | virtual void Install_Callback(GSocket *socket, GSocketEvent event) = 0; |
143 | virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event) = 0; | |
2804f77d | 144 | |
4b4d23c7 DE |
145 | virtual void Enable_Events(GSocket *socket) = 0; |
146 | virtual void Disable_Events(GSocket *socket) = 0; | |
2804f77d VZ |
147 | |
148 | virtual ~GSocketManager() { } | |
149 | ||
150 | private: | |
151 | // get the manager to use if we don't have it yet | |
152 | static void Init(); | |
153 | ||
154 | static GSocketManager *ms_manager; | |
4b4d23c7 DE |
155 | }; |
156 | ||
f0db5d75 VZ |
157 | /* |
158 | Base class providing functionality common to BSD and Winsock sockets. | |
159 | ||
160 | TODO: merge this in wxSocket itself, there is no reason to maintain the | |
161 | separation between wxSocket and GSocket. | |
162 | */ | |
163 | class GSocketBase | |
164 | { | |
165 | public: | |
eb97543d VZ |
166 | // static factory function |
167 | static GSocket *Create(); | |
168 | ||
169 | virtual ~GSocketBase(); | |
170 | ||
f0db5d75 VZ |
171 | GSocketEventFlags Select(GSocketEventFlags flags); |
172 | ||
eb97543d VZ |
173 | virtual void Close() = 0; |
174 | virtual void Shutdown(); | |
175 | ||
ff55f837 VZ |
176 | // this is officially SOCKET (unsigned int) under Windows but we don't want |
177 | // to include winsock.h which defines SOCKET from here so just use int | |
178 | // under all platforms | |
f0db5d75 | 179 | int m_fd; |
f0db5d75 | 180 | |
f0db5d75 VZ |
181 | int m_initialRecvBufferSize; |
182 | int m_initialSendBufferSize; | |
183 | ||
184 | GAddress *m_local; | |
185 | GAddress *m_peer; | |
186 | GSocketError m_error; | |
187 | ||
188 | bool m_non_blocking; | |
189 | bool m_server; | |
190 | bool m_stream; | |
191 | bool m_establishing; | |
192 | bool m_reusable; | |
193 | bool m_broadcast; | |
194 | bool m_dobind; | |
195 | ||
196 | #ifdef __WINDOWS__ | |
197 | struct timeval m_timeout; | |
198 | #else | |
199 | unsigned long m_timeout; | |
200 | #endif | |
201 | ||
202 | GSocketEventFlags m_detected; | |
203 | GSocketCallback m_cbacks[GSOCK_MAX_EVENT]; | |
204 | char *m_data[GSOCK_MAX_EVENT]; | |
eb97543d VZ |
205 | |
206 | protected: | |
207 | GSocketBase(); | |
f0db5d75 VZ |
208 | }; |
209 | ||
2804f77d VZ |
210 | #if defined(__WINDOWS__) |
211 | #include "wx/msw/gsockmsw.h" | |
212 | #else | |
213 | #include "wx/unix/gsockunx.h" | |
214 | #endif | |
38bb138f | 215 | |
2804f77d | 216 | /* Global initializers */ |
38bb138f | 217 | |
2804f77d VZ |
218 | /* GSocket_Init() must be called at the beginning (but after calling |
219 | * GSocketManager::Set() if a custom manager should be used) */ | |
220 | bool GSocket_Init(); | |
9bf10d6b GRG |
221 | |
222 | /* GSocket_Cleanup() must be called at the end */ | |
2804f77d | 223 | void GSocket_Cleanup(); |
a58d5df4 | 224 | |
9bf10d6b | 225 | |
a324a7bc GL |
226 | /* GAddress */ |
227 | ||
d80170bd VZ |
228 | // Represents a socket endpoint, i.e. -- in spite of its name -- not an address |
229 | // but an (address, port) pair | |
230 | struct GAddress | |
231 | { | |
232 | struct sockaddr *m_addr; | |
233 | size_t m_len; | |
234 | ||
235 | GAddressType m_family; | |
236 | int m_realfamily; | |
237 | ||
238 | GSocketError m_error; | |
239 | }; | |
240 | ||
2804f77d | 241 | GAddress *GAddress_new(); |
a324a7bc GL |
242 | GAddress *GAddress_copy(GAddress *address); |
243 | void GAddress_destroy(GAddress *address); | |
244 | ||
245 | void GAddress_SetFamily(GAddress *address, GAddressType type); | |
246 | GAddressType GAddress_GetFamily(GAddress *address); | |
247 | ||
e9e3e3ab GRG |
248 | /* The use of any of the next functions will set the address family to |
249 | * the specific one. For example if you use GAddress_INET_SetHostName, | |
250 | * address family will be implicitly set to AF_INET. | |
251 | */ | |
a324a7bc GL |
252 | |
253 | GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname); | |
60edcf45 | 254 | GSocketError GAddress_INET_SetBroadcastAddress(GAddress *address); |
9bf10d6b | 255 | GSocketError GAddress_INET_SetAnyAddress(GAddress *address); |
a324a7bc GL |
256 | GSocketError GAddress_INET_SetHostAddress(GAddress *address, |
257 | unsigned long hostaddr); | |
5a96d2f4 GL |
258 | GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port, |
259 | const char *protocol); | |
a324a7bc GL |
260 | GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port); |
261 | ||
262 | GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, | |
263 | size_t sbuf); | |
264 | unsigned long GAddress_INET_GetHostAddress(GAddress *address); | |
265 | unsigned short GAddress_INET_GetPort(GAddress *address); | |
266 | ||
d80170bd VZ |
267 | GSocketError _GAddress_translate_from(GAddress *address, |
268 | struct sockaddr *addr, int len); | |
269 | GSocketError _GAddress_translate_to (GAddress *address, | |
270 | struct sockaddr **addr, int *len); | |
271 | GSocketError _GAddress_Init_INET(GAddress *address); | |
272 | ||
8575ff50 VZ |
273 | #if wxUSE_IPV6 |
274 | ||
275 | GSocketError GAddress_INET6_SetHostName(GAddress *address, const char *hostname); | |
276 | GSocketError GAddress_INET6_SetAnyAddress(GAddress *address); | |
277 | GSocketError GAddress_INET6_SetHostAddress(GAddress *address, | |
278 | struct in6_addr hostaddr); | |
279 | GSocketError GAddress_INET6_SetPortName(GAddress *address, const char *port, | |
280 | const char *protocol); | |
281 | GSocketError GAddress_INET6_SetPort(GAddress *address, unsigned short port); | |
282 | ||
283 | GSocketError GAddress_INET6_GetHostName(GAddress *address, char *hostname, | |
284 | size_t sbuf); | |
285 | GSocketError GAddress_INET6_GetHostAddress(GAddress *address,struct in6_addr *hostaddr); | |
286 | unsigned short GAddress_INET6_GetPort(GAddress *address); | |
287 | ||
288 | #endif // wxUSE_IPV6 | |
289 | ||
d80170bd VZ |
290 | // these functions are available under all platforms but only implemented under |
291 | // Unix ones, elsewhere they just return GSOCK_INVADDR | |
292 | GSocketError _GAddress_Init_UNIX(GAddress *address); | |
a324a7bc GL |
293 | GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path); |
294 | GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf); | |
295 | ||
2804f77d | 296 | #endif /* wxUSE_SOCKETS */ |
a324a7bc | 297 | |
2804f77d | 298 | #endif /* _WX_GSOCKET_H_ */ |