1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/private/gsocket.h
3 // Purpose: GSocket implementation
4 // Authors: Guilhem Lavaux, Vadim Zeitlin
7 // Copyright: (c) 1997 Guilhem Lavaux
8 // (c) 2008 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_PRIVATE_GSOCKET_H_
13 #define _WX_PRIVATE_GSOCKET_H_
19 #include "wx/dlimpexp.h" /* for WXDLLIMPEXP_NET */
21 class WXDLLIMPEXP_FWD_NET wxSocketBase
;
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>
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
41 #include "wx/msw/wrapwin.h"
43 #include <sys/time.h> // for timeval
75 /* See below for an explanation on how events work.
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
94 typedef int GSocketEventFlags
;
99 typedef void (*GSocketCallback
)(GSocket
*socket
, GSocketEvent event
,
103 Class providing hooks abstracting the differences between console and GUI
104 applications for socket code.
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.
117 // set the manager to use, we don't take ownership of it
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
);
124 // return the manager to use
126 // this initializes the manager at first use
127 static GSocketManager
*Get()
135 // called before the first wxSocket is created and should do the
136 // initializations needed in order to use the network
138 // return true if initialized successfully
139 virtual bool OnInit() = 0;
141 // undo the initializations of OnInit()
142 virtual void OnExit() = 0;
145 // do manager-specific socket initializations: called in the beginning of
146 // the socket initialization
147 virtual bool Init_Socket(GSocket
*socket
) = 0;
149 // called when the socket is being closed
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;
159 // undo Init_Socket(): called from GSocket dtor
160 virtual void Destroy_Socket(GSocket
*socket
) = 0;
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;
173 virtual ~GSocketManager() { }
176 // get the manager to use if we don't have it yet
179 static GSocketManager
*ms_manager
;
183 Base class providing functionality common to BSD and Winsock sockets.
185 TODO: merge this in wxSocket itself, there is no reason to maintain the
186 separation between wxSocket and GSocket.
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
);
195 virtual ~GSocketBase();
197 void SetTimeout(unsigned long millisec
);
199 GSocketError
SetLocal(GAddress
*address
);
200 GSocketError
SetPeer(GAddress
*address
);
201 GAddress
*GetLocal();
204 GSocketEventFlags
Select(GSocketEventFlags flags
);
206 virtual GSocket
*WaitConnection(wxSocketBase
& wxsocket
) = 0;
209 virtual void Shutdown();
211 void SetInitialSocketBuffers(int recv
, int send
)
213 m_initialRecvBufferSize
= recv
;
214 m_initialSendBufferSize
= send
;
217 // notify m_wxsocket about the given socket event by calling its (inaptly
218 // named) OnRequest() method
219 void NotifyOnStateChange(GSocketEvent event
);
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() { }
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
233 int m_initialRecvBufferSize
;
234 int m_initialSendBufferSize
;
238 GSocketError m_error
;
248 struct timeval m_timeout
;
250 GSocketEventFlags m_detected
;
253 GSocketBase(wxSocketBase
& wxsocket
);
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
;
260 DECLARE_NO_COPY_CLASS(GSocketBase
)
263 #if defined(__WINDOWS__)
264 #include "wx/msw/gsockmsw.h"
266 #include "wx/unix/gsockunx.h"
269 /* Global initializers */
271 /* GSocket_Init() must be called at the beginning (but after calling
272 * GSocketManager::Set() if a custom manager should be used) */
275 /* GSocket_Cleanup() must be called at the end */
276 void GSocket_Cleanup();
281 // Represents a socket endpoint, i.e. -- in spite of its name -- not an address
282 // but an (address, port) pair
285 struct sockaddr
*m_addr
;
288 GAddressType m_family
;
291 GSocketError m_error
;
294 GAddress
*GAddress_new();
295 GAddress
*GAddress_copy(GAddress
*address
);
296 void GAddress_destroy(GAddress
*address
);
298 void GAddress_SetFamily(GAddress
*address
, GAddressType type
);
299 GAddressType
GAddress_GetFamily(GAddress
*address
);
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.
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
);
315 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
,
317 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
);
318 unsigned short GAddress_INET_GetPort(GAddress
*address
);
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
);
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
);
336 GSocketError
GAddress_INET6_GetHostName(GAddress
*address
, char *hostname
,
338 GSocketError
GAddress_INET6_GetHostAddress(GAddress
*address
,struct in6_addr
*hostaddr
);
339 unsigned short GAddress_INET6_GetPort(GAddress
*address
);
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
);
349 #endif /* wxUSE_SOCKETS */
351 #endif /* _WX_PRIVATE_GSOCKET_H_ */