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