]> git.saurik.com Git - wxWidgets.git/blob - include/wx/gsocket.h
there is no std::wostream under Palm (part of ticket 4700)
[wxWidgets.git] / include / wx / gsocket.h
1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket)
3 * Name: gsocket.h
4 * Author: Guilhem Lavaux
5 * Guillermo Rodriguez Garcia <guille@iies.es>
6 * Copyright: (c) Guilhem Lavaux
7 * (c) 2007 Vadim Zeitlin <vadim@wxwidgets.org>
8 * Licence: wxWindows Licence
9 * Purpose: GSocket include file (system independent)
10 * CVSID: $Id$
11 * -------------------------------------------------------------------------
12 */
13
14 #ifndef _WX_GSOCKET_H_
15 #define _WX_GSOCKET_H_
16
17 #include "wx/defs.h"
18
19 #if wxUSE_SOCKETS
20
21 #include "wx/dlimpexp.h" /* for WXDLLIMPEXP_NET */
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 typedef struct _GAddress GAddress;
39
40 enum GAddressType
41 {
42 GSOCK_NOFAMILY = 0,
43 GSOCK_INET,
44 GSOCK_INET6,
45 GSOCK_UNIX
46 };
47
48 enum GSocketStream
49 {
50 GSOCK_STREAMED,
51 GSOCK_UNSTREAMED
52 };
53
54 enum GSocketError
55 {
56 GSOCK_NOERROR = 0,
57 GSOCK_INVOP,
58 GSOCK_IOERR,
59 GSOCK_INVADDR,
60 GSOCK_INVSOCK,
61 GSOCK_NOHOST,
62 GSOCK_INVPORT,
63 GSOCK_WOULDBLOCK,
64 GSOCK_TIMEDOUT,
65 GSOCK_MEMERR,
66 GSOCK_OPTERR
67 };
68
69 /* See below for an explanation on how events work.
70 */
71 enum GSocketEvent
72 {
73 GSOCK_INPUT = 0,
74 GSOCK_OUTPUT = 1,
75 GSOCK_CONNECTION = 2,
76 GSOCK_LOST = 3,
77 GSOCK_MAX_EVENT = 4
78 };
79
80 enum
81 {
82 GSOCK_INPUT_FLAG = 1 << GSOCK_INPUT,
83 GSOCK_OUTPUT_FLAG = 1 << GSOCK_OUTPUT,
84 GSOCK_CONNECTION_FLAG = 1 << GSOCK_CONNECTION,
85 GSOCK_LOST_FLAG = 1 << GSOCK_LOST
86 };
87
88 typedef int GSocketEventFlags;
89
90 class GSocket;
91
92 typedef void (*GSocketCallback)(GSocket *socket, GSocketEvent event,
93 char *cdata);
94
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 */
107 class GSocketManager
108 {
109 public:
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
132 virtual bool OnInit() = 0;
133
134 // undo the initializations of OnInit()
135 virtual void OnExit() = 0;
136
137
138 // do manager-specific socket initializations (and undo it): this is called
139 // in the beginning/end of the socket initialization/destruction
140 virtual bool Init_Socket(GSocket *socket) = 0;
141 virtual void Destroy_Socket(GSocket *socket) = 0;
142
143 virtual void Install_Callback(GSocket *socket, GSocketEvent event) = 0;
144 virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event) = 0;
145
146 virtual void Enable_Events(GSocket *socket) = 0;
147 virtual void Disable_Events(GSocket *socket) = 0;
148
149 virtual ~GSocketManager() { }
150
151 private:
152 // get the manager to use if we don't have it yet
153 static void Init();
154
155 static GSocketManager *ms_manager;
156 };
157
158 #if defined(__WINDOWS__)
159 #include "wx/msw/gsockmsw.h"
160 #else
161 #include "wx/unix/gsockunx.h"
162 #endif
163
164
165 /* Global initializers */
166
167 /* GSocket_Init() must be called at the beginning (but after calling
168 * GSocketManager::Set() if a custom manager should be used) */
169 bool GSocket_Init();
170
171 /* GSocket_Cleanup() must be called at the end */
172 void GSocket_Cleanup();
173
174
175 /* Constructors / Destructors */
176
177 GSocket *GSocket_new();
178
179
180 /* GAddress */
181
182 GAddress *GAddress_new();
183 GAddress *GAddress_copy(GAddress *address);
184 void GAddress_destroy(GAddress *address);
185
186 void GAddress_SetFamily(GAddress *address, GAddressType type);
187 GAddressType GAddress_GetFamily(GAddress *address);
188
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 */
193
194 GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname);
195 GSocketError GAddress_INET_SetBroadcastAddress(GAddress *address);
196 GSocketError GAddress_INET_SetAnyAddress(GAddress *address);
197 GSocketError GAddress_INET_SetHostAddress(GAddress *address,
198 unsigned long hostaddr);
199 GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port,
200 const char *protocol);
201 GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port);
202
203 GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname,
204 size_t sbuf);
205 unsigned long GAddress_INET_GetHostAddress(GAddress *address);
206 unsigned short GAddress_INET_GetPort(GAddress *address);
207
208 #if wxUSE_IPV6
209
210 GSocketError GAddress_INET6_SetHostName(GAddress *address, const char *hostname);
211 GSocketError GAddress_INET6_SetAnyAddress(GAddress *address);
212 GSocketError GAddress_INET6_SetHostAddress(GAddress *address,
213 struct in6_addr hostaddr);
214 GSocketError GAddress_INET6_SetPortName(GAddress *address, const char *port,
215 const char *protocol);
216 GSocketError GAddress_INET6_SetPort(GAddress *address, unsigned short port);
217
218 GSocketError GAddress_INET6_GetHostName(GAddress *address, char *hostname,
219 size_t sbuf);
220 GSocketError GAddress_INET6_GetHostAddress(GAddress *address,struct in6_addr *hostaddr);
221 unsigned short GAddress_INET6_GetPort(GAddress *address);
222
223 #endif // wxUSE_IPV6
224
225 /* TODO: Define specific parts (UNIX) */
226
227 GSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path);
228 GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf);
229
230 #endif /* wxUSE_SOCKETS */
231
232 #endif /* _WX_GSOCKET_H_ */