1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket)
4 * Author: Guilhem Lavaux
5 * Guillermo Rodriguez Garcia <guille@iies.es>
6 * Copyright: (c) Guilhem Lavaux
7 * (c) 2007,2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 * Licence: wxWindows Licence
9 * Purpose: GSocket include file (system independent)
11 * -------------------------------------------------------------------------
14 #ifndef _WX_GSOCKET_H_
15 #define _WX_GSOCKET_H_
21 #include "wx/dlimpexp.h" /* for WXDLLIMPEXP_NET */
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.
30 #if !defined(__WXMAC__) && !defined(__CYGWIN__) && !defined(__WXWINCE__)
31 #include <sys/types.h>
67 /* See below for an explanation on how events work.
80 GSOCK_INPUT_FLAG
= 1 << GSOCK_INPUT
,
81 GSOCK_OUTPUT_FLAG
= 1 << GSOCK_OUTPUT
,
82 GSOCK_CONNECTION_FLAG
= 1 << GSOCK_CONNECTION
,
83 GSOCK_LOST_FLAG
= 1 << GSOCK_LOST
86 typedef int GSocketEventFlags
;
91 typedef void (*GSocketCallback
)(GSocket
*socket
, GSocketEvent event
,
95 Class providing hooks abstracting the differences between console and GUI
96 applications for socket code.
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.
109 // set the manager to use, we don't take ownership of it
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
);
116 // return the manager to use
118 // this initializes the manager at first use
119 static GSocketManager
*Get()
127 // called before the first wxSocket is created and should do the
128 // initializations needed in order to use the network
130 // return true if initialized successfully
131 virtual bool OnInit() = 0;
133 // undo the initializations of OnInit()
134 virtual void OnExit() = 0;
137 // do manager-specific socket initializations (and undo it): this is called
138 // in the beginning/end of the socket initialization/destruction
139 virtual bool Init_Socket(GSocket
*socket
) = 0;
140 virtual void Destroy_Socket(GSocket
*socket
) = 0;
142 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
) = 0;
143 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
) = 0;
145 virtual void Enable_Events(GSocket
*socket
) = 0;
146 virtual void Disable_Events(GSocket
*socket
) = 0;
148 virtual ~GSocketManager() { }
151 // get the manager to use if we don't have it yet
154 static GSocketManager
*ms_manager
;
158 Base class providing functionality common to BSD and Winsock sockets.
160 TODO: merge this in wxSocket itself, there is no reason to maintain the
161 separation between wxSocket and GSocket.
166 GSocketEventFlags
Select(GSocketEventFlags flags
);
175 int m_initialRecvBufferSize
;
176 int m_initialSendBufferSize
;
180 GSocketError m_error
;
191 struct timeval m_timeout
;
193 unsigned long m_timeout
;
196 GSocketEventFlags m_detected
;
197 GSocketCallback m_cbacks
[GSOCK_MAX_EVENT
];
198 char *m_data
[GSOCK_MAX_EVENT
];
201 #if defined(__WINDOWS__)
202 #include "wx/msw/gsockmsw.h"
204 #include "wx/unix/gsockunx.h"
208 /* Global initializers */
210 /* GSocket_Init() must be called at the beginning (but after calling
211 * GSocketManager::Set() if a custom manager should be used) */
214 /* GSocket_Cleanup() must be called at the end */
215 void GSocket_Cleanup();
218 /* Constructors / Destructors */
220 GSocket
*GSocket_new();
225 // Represents a socket endpoint, i.e. -- in spite of its name -- not an address
226 // but an (address, port) pair
229 struct sockaddr
*m_addr
;
232 GAddressType m_family
;
235 GSocketError m_error
;
238 GAddress
*GAddress_new();
239 GAddress
*GAddress_copy(GAddress
*address
);
240 void GAddress_destroy(GAddress
*address
);
242 void GAddress_SetFamily(GAddress
*address
, GAddressType type
);
243 GAddressType
GAddress_GetFamily(GAddress
*address
);
245 /* The use of any of the next functions will set the address family to
246 * the specific one. For example if you use GAddress_INET_SetHostName,
247 * address family will be implicitly set to AF_INET.
250 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
);
251 GSocketError
GAddress_INET_SetBroadcastAddress(GAddress
*address
);
252 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
);
253 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
254 unsigned long hostaddr
);
255 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
256 const char *protocol
);
257 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
);
259 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
,
261 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
);
262 unsigned short GAddress_INET_GetPort(GAddress
*address
);
264 GSocketError
_GAddress_translate_from(GAddress
*address
,
265 struct sockaddr
*addr
, int len
);
266 GSocketError
_GAddress_translate_to (GAddress
*address
,
267 struct sockaddr
**addr
, int *len
);
268 GSocketError
_GAddress_Init_INET(GAddress
*address
);
272 GSocketError
GAddress_INET6_SetHostName(GAddress
*address
, const char *hostname
);
273 GSocketError
GAddress_INET6_SetAnyAddress(GAddress
*address
);
274 GSocketError
GAddress_INET6_SetHostAddress(GAddress
*address
,
275 struct in6_addr hostaddr
);
276 GSocketError
GAddress_INET6_SetPortName(GAddress
*address
, const char *port
,
277 const char *protocol
);
278 GSocketError
GAddress_INET6_SetPort(GAddress
*address
, unsigned short port
);
280 GSocketError
GAddress_INET6_GetHostName(GAddress
*address
, char *hostname
,
282 GSocketError
GAddress_INET6_GetHostAddress(GAddress
*address
,struct in6_addr
*hostaddr
);
283 unsigned short GAddress_INET6_GetPort(GAddress
*address
);
287 // these functions are available under all platforms but only implemented under
288 // Unix ones, elsewhere they just return GSOCK_INVADDR
289 GSocketError
_GAddress_Init_UNIX(GAddress
*address
);
290 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
);
291 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
);
293 // standard linux headers produce many warnings when used with icc
294 #if defined(__INTELC__) && defined(__LINUX__)
295 inline void wxFD_ZERO(fd_set
*fds
)
297 #pragma warning(push)
298 #pragma warning(disable:593)
303 inline void wxFD_SET(int fd
, fd_set
*fds
)
305 #pragma warning(push, 1)
306 #pragma warning(disable:1469)
311 inline bool wxFD_ISSET(int fd
, fd_set
*fds
)
313 #pragma warning(push, 1)
314 #pragma warning(disable:1469)
315 return FD_ISSET(fd
, fds
);
318 inline bool wxFD_CLR(int fd
, fd_set
*fds
)
320 #pragma warning(push, 1)
321 #pragma warning(disable:1469)
322 return FD_CLR(fd
, fds
);
326 #define wxFD_ZERO(fds) FD_ZERO(fds)
327 #define wxFD_SET(fd, fds) FD_SET(fd, fds)
328 #define wxFD_ISSET(fd, fds) FD_ISSET(fd, fds)
329 #define wxFD_CLR(fd, fds) FD_CLR(fd, fds)
330 #endif // __INTELC__/!__INTELC__
332 // this is for Windows where configure doesn't define this
334 #define SOCKOPTLEN_T int
337 #endif /* wxUSE_SOCKETS */
339 #endif /* _WX_GSOCKET_H_ */