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 */
23 class WXDLLIMPEXP_FWD_NET wxSocketBase
;
28 Including sys/types.h under Cygwin results in the warnings about "fd_set
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.
32 #if !defined(__WXMAC__) && !defined(__CYGWIN__) && !defined(__WXWINCE__)
33 #include <sys/types.h>
40 // include the header defining timeval: under Windows this struct is used only
41 // with sockets so we need to include winsock.h which we do via windows.h
43 #include "wx/msw/wrapwin.h"
45 #include <sys/time.h> // for timeval
77 /* See below for an explanation on how events work.
90 GSOCK_INPUT_FLAG
= 1 << GSOCK_INPUT
,
91 GSOCK_OUTPUT_FLAG
= 1 << GSOCK_OUTPUT
,
92 GSOCK_CONNECTION_FLAG
= 1 << GSOCK_CONNECTION
,
93 GSOCK_LOST_FLAG
= 1 << GSOCK_LOST
96 typedef int GSocketEventFlags
;
101 typedef void (*GSocketCallback
)(GSocket
*socket
, GSocketEvent event
,
105 Class providing hooks abstracting the differences between console and GUI
106 applications for socket code.
108 We also have different implementations of this class for different platforms
109 allowing us to keep more things in the common code but the main reason for
110 its existence is that we want the same socket code work differently
111 depending on whether it's used from a console or a GUI program. This is
112 achieved by implementing the virtual methods of this class differently in
113 the objects returned by wxConsoleAppTraits::GetSocketFunctionsTable() and
114 the same method in wxGUIAppTraits.
119 // set the manager to use, we don't take ownership of it
121 // this should be called before GSocket_Init(), i.e. before the first
122 // wxSocket object is created, otherwise the manager returned by
123 // wxAppTraits::GetSocketManager() will be used
124 static void Set(GSocketManager
*manager
);
126 // return the manager to use
128 // this initializes the manager at first use
129 static GSocketManager
*Get()
137 // called before the first wxSocket is created and should do the
138 // initializations needed in order to use the network
140 // return true if initialized successfully
141 virtual bool OnInit() = 0;
143 // undo the initializations of OnInit()
144 virtual void OnExit() = 0;
147 // do manager-specific socket initializations (and undo it): this is called
148 // in the beginning/end of the socket initialization/destruction
149 virtual bool Init_Socket(GSocket
*socket
) = 0;
150 virtual void Destroy_Socket(GSocket
*socket
) = 0;
152 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
) = 0;
153 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
) = 0;
155 virtual void Enable_Events(GSocket
*socket
) = 0;
156 virtual void Disable_Events(GSocket
*socket
) = 0;
158 virtual ~GSocketManager() { }
161 // get the manager to use if we don't have it yet
164 static GSocketManager
*ms_manager
;
168 Base class providing functionality common to BSD and Winsock sockets.
170 TODO: merge this in wxSocket itself, there is no reason to maintain the
171 separation between wxSocket and GSocket.
176 // static factory function: creates the low-level socket associated with
177 // the given wxSocket (and inherits its attributes such as timeout)
178 static GSocket
*Create(wxSocketBase
& wxsocket
);
180 virtual ~GSocketBase();
182 void SetTimeout(unsigned long millisec
);
184 GSocketError
SetLocal(GAddress
*address
);
185 GSocketError
SetPeer(GAddress
*address
);
186 GAddress
*GetLocal();
189 GSocketEventFlags
Select(GSocketEventFlags flags
);
191 virtual GSocket
*WaitConnection(wxSocketBase
& wxsocket
) = 0;
193 virtual void Close() = 0;
194 virtual void Shutdown();
196 void SetInitialSocketBuffers(int recv
, int send
)
198 m_initialRecvBufferSize
= recv
;
199 m_initialSendBufferSize
= send
;
202 // notify m_wxsocket about the given socket event by calling its (inaptly
203 // named) OnRequest() method
204 void NotifyOnStateChange(GSocketEvent event
);
206 // this is officially SOCKET (unsigned int) under Windows but we don't want
207 // to include winsock.h which defines SOCKET from here so just use int
208 // under all platforms
211 int m_initialRecvBufferSize
;
212 int m_initialSendBufferSize
;
216 GSocketError m_error
;
226 struct timeval m_timeout
;
228 GSocketEventFlags m_detected
;
231 GSocketBase(wxSocketBase
& wxsocket
);
234 // set in ctor and never changed except that it's reset to NULL when the
235 // socket is shut down
236 wxSocketBase
*m_wxsocket
;
238 DECLARE_NO_COPY_CLASS(GSocketBase
)
241 #if defined(__WINDOWS__)
242 #include "wx/msw/gsockmsw.h"
244 #include "wx/unix/gsockunx.h"
247 /* Global initializers */
249 /* GSocket_Init() must be called at the beginning (but after calling
250 * GSocketManager::Set() if a custom manager should be used) */
253 /* GSocket_Cleanup() must be called at the end */
254 void GSocket_Cleanup();
259 // Represents a socket endpoint, i.e. -- in spite of its name -- not an address
260 // but an (address, port) pair
263 struct sockaddr
*m_addr
;
266 GAddressType m_family
;
269 GSocketError m_error
;
272 GAddress
*GAddress_new();
273 GAddress
*GAddress_copy(GAddress
*address
);
274 void GAddress_destroy(GAddress
*address
);
276 void GAddress_SetFamily(GAddress
*address
, GAddressType type
);
277 GAddressType
GAddress_GetFamily(GAddress
*address
);
279 /* The use of any of the next functions will set the address family to
280 * the specific one. For example if you use GAddress_INET_SetHostName,
281 * address family will be implicitly set to AF_INET.
284 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
);
285 GSocketError
GAddress_INET_SetBroadcastAddress(GAddress
*address
);
286 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
);
287 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
288 unsigned long hostaddr
);
289 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
290 const char *protocol
);
291 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
);
293 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
,
295 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
);
296 unsigned short GAddress_INET_GetPort(GAddress
*address
);
298 GSocketError
_GAddress_translate_from(GAddress
*address
,
299 struct sockaddr
*addr
, int len
);
300 GSocketError
_GAddress_translate_to (GAddress
*address
,
301 struct sockaddr
**addr
, int *len
);
302 GSocketError
_GAddress_Init_INET(GAddress
*address
);
306 GSocketError
GAddress_INET6_SetHostName(GAddress
*address
, const char *hostname
);
307 GSocketError
GAddress_INET6_SetAnyAddress(GAddress
*address
);
308 GSocketError
GAddress_INET6_SetHostAddress(GAddress
*address
,
309 struct in6_addr hostaddr
);
310 GSocketError
GAddress_INET6_SetPortName(GAddress
*address
, const char *port
,
311 const char *protocol
);
312 GSocketError
GAddress_INET6_SetPort(GAddress
*address
, unsigned short port
);
314 GSocketError
GAddress_INET6_GetHostName(GAddress
*address
, char *hostname
,
316 GSocketError
GAddress_INET6_GetHostAddress(GAddress
*address
,struct in6_addr
*hostaddr
);
317 unsigned short GAddress_INET6_GetPort(GAddress
*address
);
321 // these functions are available under all platforms but only implemented under
322 // Unix ones, elsewhere they just return GSOCK_INVADDR
323 GSocketError
_GAddress_Init_UNIX(GAddress
*address
);
324 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
);
325 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
);
327 #endif /* wxUSE_SOCKETS */
329 #endif /* _WX_GSOCKET_H_ */