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