1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/private/socket.h
3 // Purpose: wxSocketImpl and related declarations
4 // Authors: Guilhem Lavaux, Vadim Zeitlin
6 // Copyright: (c) 1997 Guilhem Lavaux
7 // (c) 2008 Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 Brief overview of different socket classes:
14 - wxSocketBase is the public class representing a socket ("Base" here
15 refers to the fact that wxSocketClient and wxSocketServer are derived
16 from it and predates the convention of using "Base" for common base
17 classes for platform-specific classes in wxWidgets) with implementation
18 common to all platforms and forwarding methods whose implementation
19 differs between platforms to wxSocketImpl which it contains.
21 - wxSocketImpl is actually just an abstract base class having only code
22 common to all platforms, the concrete implementation classes derive from
23 it and are created by wxSocketImpl::Create().
25 - Some socket operations have different implementations in console-mode and
26 GUI applications. wxSocketManager class exists to abstract this in such
27 way that console applications (using wxBase) don't depend on wxNet. An
28 object of this class is made available via wxApp and GUI applications set
29 up a different kind of global socket manager from console ones.
31 TODO: it looks like wxSocketManager could be eliminated by providing
32 methods for registering/unregistering sockets directly in
36 #ifndef _WX_PRIVATE_SOCKET_H_
37 #define _WX_PRIVATE_SOCKET_H_
43 #include "wx/socket.h"
44 #include "wx/private/sckaddr.h"
49 Including sys/types.h under Cygwin results in the warnings about "fd_set
50 having been defined in sys/types.h" when winsock.h is included later and
51 doesn't seem to be necessary anyhow. It's not needed under Mac neither.
53 #if !defined(__WXMAC__) && !defined(__WXMSW__) && !defined(__WXWINCE__)
54 #include <sys/types.h>
61 // include the header defining timeval: under Windows this struct is used only
62 // with sockets so we need to include winsock.h which we do via windows.h
64 #include "wx/msw/wrapwin.h"
66 #include <sys/time.h> // for timeval
69 // these definitions are for MSW when we don't use configure, otherwise these
70 // symbols are defined by configure
72 #define WX_SOCKLEN_T int
76 #define SOCKOPTLEN_T int
79 // define some symbols which winsock.h defines but traditional BSD headers
81 #ifndef INVALID_SOCKET
82 #define INVALID_SOCKET (-1)
86 #define SOCKET_ERROR (-1)
89 typedef int wxSocketEventFlags
;
94 Class providing hooks abstracting the differences between console and GUI
95 applications for socket code.
97 We also have different implementations of this class for different platforms
98 allowing us to keep more things in the common code but the main reason for
99 its existence is that we want the same socket code work differently
100 depending on whether it's used from a console or a GUI program. This is
101 achieved by implementing the virtual methods of this class differently in
102 the objects returned by wxConsoleAppTraits::GetSocketManager() and the same
103 method in wxGUIAppTraits.
105 class wxSocketManager
108 // set the manager to use, we don't take ownership of it
110 // this should be called before creating the first wxSocket object,
111 // otherwise the manager returned by wxAppTraits::GetSocketManager() will
113 static void Set(wxSocketManager
*manager
);
115 // return the manager to use
117 // this initializes the manager at first use
118 static wxSocketManager
*Get()
126 // called before the first wxSocket is created and should do the
127 // initializations needed in order to use the network
129 // return true if initialized successfully; if this returns false sockets
130 // can't be used at all
131 virtual bool OnInit() = 0;
133 // undo the initializations of OnInit()
134 virtual void OnExit() = 0;
137 // create the socket implementation object matching this manager
138 virtual wxSocketImpl
*CreateSocket(wxSocketBase
& wxsocket
) = 0;
140 // these functions enable or disable monitoring of the given socket for the
141 // specified events inside the currently running event loop (but notice
142 // that both BSD and Winsock implementations actually use socket->m_server
143 // value to determine what exactly should be monitored so it needs to be
144 // set before calling these functions)
146 // the default event value is used just for the convenience of wxMSW
147 // implementation which doesn't use this parameter anyhow, it doesn't make
148 // sense to pass wxSOCKET_LOST for the Unix implementation which does use
150 virtual void Install_Callback(wxSocketImpl
*socket
,
151 wxSocketNotify event
= wxSOCKET_LOST
) = 0;
152 virtual void Uninstall_Callback(wxSocketImpl
*socket
,
153 wxSocketNotify event
= wxSOCKET_LOST
) = 0;
155 virtual ~wxSocketManager() { }
158 // get the manager to use if we don't have it yet
161 static wxSocketManager
*ms_manager
;
165 Base class for all socket implementations providing functionality common to
166 BSD and Winsock sockets.
168 Objects of this class are not created directly but only via the factory
169 function wxSocketManager::CreateSocket().
174 virtual ~wxSocketImpl();
176 // set various socket properties: all of those can only be called before
177 // creating the socket
178 void SetTimeout(unsigned long millisec
);
179 void SetReusable() { m_reusable
= true; }
180 void SetBroadcast() { m_broadcast
= true; }
181 void DontDoBind() { m_dobind
= false; }
182 void SetInitialSocketBuffers(int recv
, int send
)
184 m_initialRecvBufferSize
= recv
;
185 m_initialSendBufferSize
= send
;
188 wxSocketError
SetLocal(const wxSockAddressImpl
& address
);
189 wxSocketError
SetPeer(const wxSockAddressImpl
& address
);
194 bool IsServer() const { return m_server
; }
196 const wxSockAddressImpl
& GetLocal(); // non const as may update m_local
197 const wxSockAddressImpl
& GetPeer() const { return m_peer
; }
199 wxSocketError
GetError() const { return m_error
; }
200 bool IsOk() const { return m_error
== wxSOCKET_NOERROR
; }
202 // get the error code corresponding to the last operation
203 virtual wxSocketError
GetLastError() const = 0;
206 // creating/closing the socket
207 // --------------------------
209 // notice that SetLocal() must be called before creating the socket using
210 // any of the functions below
212 // all of Create() functions return wxSOCKET_NOERROR if the operation
213 // completed successfully or one of:
214 // wxSOCKET_INVSOCK - the socket is in use.
215 // wxSOCKET_INVADDR - the local (server) or peer (client) address has not
217 // wxSOCKET_IOERR - any other error.
219 // create a socket listening on the local address specified by SetLocal()
220 // (notice that DontDoBind() is ignored by this function)
221 wxSocketError
CreateServer();
223 // create a socket connected to the peer address specified by SetPeer()
224 // (notice that DontDoBind() is ignored by this function)
226 // this function may return wxSOCKET_WOULDBLOCK in addition to the return
227 // values listed above if wait is false
228 wxSocketError
CreateClient(bool wait
);
230 // create (and bind unless DontDoBind() had been called) an UDP socket
231 // associated with the given local address
232 wxSocketError
CreateUDP();
234 // may be called whether the socket was created or not, calls DoClose() if
235 // it was indeed created
238 // shuts down the writing end of the socket and closes it, this is a more
239 // graceful way to close
241 // does nothing if the socket wasn't created
248 // basic IO, work for both TCP and UDP sockets
250 // return the number of bytes read/written (possibly 0) or -1 on error
251 int Read(void *buffer
, int size
);
252 int Write(const void *buffer
, int size
);
254 // basically a wrapper for select(): returns the condition of the socket,
255 // blocking for not longer than timeout if it is specified (otherwise just
256 // poll without blocking at all)
258 // flags defines what kind of conditions we're interested in, the return
259 // value is composed of a (possibly empty) subset of the bits set in flags
260 wxSocketEventFlags
Select(wxSocketEventFlags flags
,
261 const timeval
*timeout
= NULL
);
263 // convenient wrapper calling Select() with our default timeout
264 wxSocketEventFlags
SelectWithTimeout(wxSocketEventFlags flags
)
266 return Select(flags
, &m_timeout
);
269 // just a wrapper for accept(): it is called to create a new wxSocketImpl
270 // corresponding to a new server connection represented by the given
271 // wxSocketBase, returns NULL on error (including immediately if there are
272 // no pending connections as our sockets are non-blocking)
273 wxSocketImpl
*Accept(wxSocketBase
& wxsocket
);
279 // notify m_wxsocket about the given socket event by calling its (inaptly
280 // named) OnRequest() method
281 void NotifyOnStateChange(wxSocketNotify event
);
283 // called after reading/writing the data from/to the socket and should
284 // enable back the wxSOCKET_INPUT/OUTPUT_FLAG notifications if they were
285 // turned off when this data was first detected
286 virtual void ReenableEvents(wxSocketEventFlags flags
) = 0;
288 // TODO: make these fields protected and provide accessors for those of
289 // them that wxSocketBase really needs
293 int m_initialRecvBufferSize
;
294 int m_initialSendBufferSize
;
296 wxSockAddressImpl m_local
,
298 wxSocketError m_error
;
306 struct timeval m_timeout
;
309 wxSocketImpl(wxSocketBase
& wxsocket
);
311 // true if we're a listening stream socket
315 // called by Close() if we have a valid m_fd
316 virtual void DoClose() = 0;
318 // put this socket into non-blocking mode and enable monitoring this socket
319 // as part of the event loop
320 virtual void UnblockAndRegisterWithEventLoop() = 0;
322 // check that the socket wasn't created yet and that the given address
323 // (either m_local or m_peer depending on the socket kind) is valid and
324 // set m_error and return false if this is not the case
325 bool PreCreateCheck(const wxSockAddressImpl
& addr
);
327 // set the given socket option: this just wraps setsockopt(SOL_SOCKET)
328 int SetSocketOption(int optname
, int optval
)
330 // although modern Unix systems use "const void *" for the 4th
331 // parameter here, old systems and Winsock still use "const char *"
332 return setsockopt(m_fd
, SOL_SOCKET
, optname
,
333 reinterpret_cast<const char *>(&optval
),
337 // set the given socket option to true value: this is an even simpler
338 // wrapper for setsockopt(SOL_SOCKET) for boolean options
339 int EnableSocketOption(int optname
)
341 return SetSocketOption(optname
, 1);
344 // apply the options to the (just created) socket and register it with the
345 // event loop by calling UnblockAndRegisterWithEventLoop()
348 // update local address after binding/connecting
349 wxSocketError
UpdateLocalAddress();
351 // functions used to implement Read/Write()
352 int RecvStream(void *buffer
, int size
);
353 int RecvDgram(void *buffer
, int size
);
354 int SendStream(const void *buffer
, int size
);
355 int SendDgram(const void *buffer
, int size
);
358 // set in ctor and never changed except that it's reset to NULL when the
359 // socket is shut down
360 wxSocketBase
*m_wxsocket
;
362 wxDECLARE_NO_COPY_CLASS(wxSocketImpl
);
365 #if defined(__WINDOWS__)
366 #include "wx/msw/private/sockmsw.h"
368 #include "wx/unix/private/sockunix.h"
371 #endif /* wxUSE_SOCKETS */
373 #endif /* _WX_PRIVATE_SOCKET_H_ */