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: called in the beginning of
148 // the socket initialization
149 virtual bool Init_Socket(GSocket
*socket
) = 0;
151 // called when the socket is being closed
153 // TODO: merge this with Destroy_Socket(), currently 2 separate functions
154 // are needed because Init_Socket() always allocates manager-specific
155 // resources in GSocket and Destroy_Socket() must be called even if
156 // the socket has never been opened, but if the allocation were done
157 // on demand, then Destroy_Socket() could be called from
158 // GSocket::Close() and we wouldn't need Close_Socket() at all
159 virtual void Close_Socket(GSocket
*socket
) = 0;
161 // undo Init_Socket(): called from GSocket dtor
162 virtual void Destroy_Socket(GSocket
*socket
) = 0;
165 // these functions enable or disable monitoring of the given socket for the
166 // specified events inside the currently running event loop (but notice
167 // that both BSD and Winsock implementations actually use socket->m_server
168 // value to determine what exactly should be monitored so it needs to be
169 // set before calling these functions)
170 virtual void Install_Callback(GSocket
*socket
,
171 GSocketEvent event
= GSOCK_MAX_EVENT
) = 0;
172 virtual void Uninstall_Callback(GSocket
*socket
,
173 GSocketEvent event
= GSOCK_MAX_EVENT
) = 0;
175 virtual ~GSocketManager() { }
178 // get the manager to use if we don't have it yet
181 static GSocketManager
*ms_manager
;
185 Base class providing functionality common to BSD and Winsock sockets.
187 TODO: merge this in wxSocket itself, there is no reason to maintain the
188 separation between wxSocket and GSocket.
193 // static factory function: creates the low-level socket associated with
194 // the given wxSocket (and inherits its attributes such as timeout)
195 static GSocket
*Create(wxSocketBase
& wxsocket
);
197 virtual ~GSocketBase();
199 void SetTimeout(unsigned long millisec
);
201 GSocketError
SetLocal(GAddress
*address
);
202 GSocketError
SetPeer(GAddress
*address
);
203 GAddress
*GetLocal();
206 GSocketEventFlags
Select(GSocketEventFlags flags
);
208 virtual GSocket
*WaitConnection(wxSocketBase
& wxsocket
) = 0;
211 virtual void Shutdown();
213 void SetInitialSocketBuffers(int recv
, int send
)
215 m_initialRecvBufferSize
= recv
;
216 m_initialSendBufferSize
= send
;
219 // notify m_wxsocket about the given socket event by calling its (inaptly
220 // named) OnRequest() method
221 void NotifyOnStateChange(GSocketEvent event
);
223 // this is officially SOCKET (unsigned int) under Windows but we don't want
224 // to include winsock.h which defines SOCKET from here so just use int
225 // under all platforms
228 int m_initialRecvBufferSize
;
229 int m_initialSendBufferSize
;
233 GSocketError m_error
;
243 struct timeval m_timeout
;
245 GSocketEventFlags m_detected
;
248 GSocketBase(wxSocketBase
& wxsocket
);
251 // set in ctor and never changed except that it's reset to NULL when the
252 // socket is shut down
253 wxSocketBase
*m_wxsocket
;
255 DECLARE_NO_COPY_CLASS(GSocketBase
)
258 #if defined(__WINDOWS__)
259 #include "wx/msw/gsockmsw.h"
261 #include "wx/unix/gsockunx.h"
264 /* Global initializers */
266 /* GSocket_Init() must be called at the beginning (but after calling
267 * GSocketManager::Set() if a custom manager should be used) */
270 /* GSocket_Cleanup() must be called at the end */
271 void GSocket_Cleanup();
276 // Represents a socket endpoint, i.e. -- in spite of its name -- not an address
277 // but an (address, port) pair
280 struct sockaddr
*m_addr
;
283 GAddressType m_family
;
286 GSocketError m_error
;
289 GAddress
*GAddress_new();
290 GAddress
*GAddress_copy(GAddress
*address
);
291 void GAddress_destroy(GAddress
*address
);
293 void GAddress_SetFamily(GAddress
*address
, GAddressType type
);
294 GAddressType
GAddress_GetFamily(GAddress
*address
);
296 /* The use of any of the next functions will set the address family to
297 * the specific one. For example if you use GAddress_INET_SetHostName,
298 * address family will be implicitly set to AF_INET.
301 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
);
302 GSocketError
GAddress_INET_SetBroadcastAddress(GAddress
*address
);
303 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
);
304 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
305 unsigned long hostaddr
);
306 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
307 const char *protocol
);
308 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
);
310 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
,
312 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
);
313 unsigned short GAddress_INET_GetPort(GAddress
*address
);
315 GSocketError
_GAddress_translate_from(GAddress
*address
,
316 struct sockaddr
*addr
, int len
);
317 GSocketError
_GAddress_translate_to (GAddress
*address
,
318 struct sockaddr
**addr
, int *len
);
319 GSocketError
_GAddress_Init_INET(GAddress
*address
);
323 GSocketError
GAddress_INET6_SetHostName(GAddress
*address
, const char *hostname
);
324 GSocketError
GAddress_INET6_SetAnyAddress(GAddress
*address
);
325 GSocketError
GAddress_INET6_SetHostAddress(GAddress
*address
,
326 struct in6_addr hostaddr
);
327 GSocketError
GAddress_INET6_SetPortName(GAddress
*address
, const char *port
,
328 const char *protocol
);
329 GSocketError
GAddress_INET6_SetPort(GAddress
*address
, unsigned short port
);
331 GSocketError
GAddress_INET6_GetHostName(GAddress
*address
, char *hostname
,
333 GSocketError
GAddress_INET6_GetHostAddress(GAddress
*address
,struct in6_addr
*hostaddr
);
334 unsigned short GAddress_INET6_GetPort(GAddress
*address
);
338 // these functions are available under all platforms but only implemented under
339 // Unix ones, elsewhere they just return GSOCK_INVADDR
340 GSocketError
_GAddress_Init_UNIX(GAddress
*address
);
341 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
);
342 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
);
344 #endif /* wxUSE_SOCKETS */
346 #endif /* _WX_GSOCKET_H_ */