PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/windows/include/wx/private/socket.h

http://streetview-explorer.googlecode.com/
C++ Header | 378 lines | 139 code | 74 blank | 165 comment | 4 complexity | 456723e33754a18ed290b6c9c059ca14 MD5 | raw file
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/private/socket.h
  3. // Purpose: wxSocketImpl and related declarations
  4. // Authors: Guilhem Lavaux, Vadim Zeitlin
  5. // Created: April 1997
  6. // RCS-ID: $Id$
  7. // Copyright: (c) 1997 Guilhem Lavaux
  8. // (c) 2008 Vadim Zeitlin
  9. // Licence: wxWindows licence
  10. /////////////////////////////////////////////////////////////////////////////
  11. /*
  12. Brief overview of different socket classes:
  13. - wxSocketBase is the public class representing a socket ("Base" here
  14. refers to the fact that wxSocketClient and wxSocketServer are derived
  15. from it and predates the convention of using "Base" for common base
  16. classes for platform-specific classes in wxWidgets) with implementation
  17. common to all platforms and forwarding methods whose implementation
  18. differs between platforms to wxSocketImpl which it contains.
  19. - wxSocketImpl is actually just an abstract base class having only code
  20. common to all platforms, the concrete implementation classes derive from
  21. it and are created by wxSocketImpl::Create().
  22. - Some socket operations have different implementations in console-mode and
  23. GUI applications. wxSocketManager class exists to abstract this in such
  24. way that console applications (using wxBase) don't depend on wxNet. An
  25. object of this class is made available via wxApp and GUI applications set
  26. up a different kind of global socket manager from console ones.
  27. TODO: it looks like wxSocketManager could be eliminated by providing
  28. methods for registering/unregistering sockets directly in
  29. wxEventLoop.
  30. */
  31. #ifndef _WX_PRIVATE_SOCKET_H_
  32. #define _WX_PRIVATE_SOCKET_H_
  33. #include "wx/defs.h"
  34. #if wxUSE_SOCKETS
  35. #include "wx/socket.h"
  36. #include "wx/private/sckaddr.h"
  37. #include <stddef.h>
  38. /*
  39. Including sys/types.h under Cygwin results in the warnings about "fd_set
  40. having been defined in sys/types.h" when winsock.h is included later and
  41. doesn't seem to be necessary anyhow. It's not needed under Mac neither.
  42. */
  43. #if !defined(__WXMAC__) && !defined(__CYGWIN__) && !defined(__WXWINCE__)
  44. #include <sys/types.h>
  45. #endif
  46. #ifdef __WXWINCE__
  47. #include <stdlib.h>
  48. #endif
  49. // include the header defining timeval: under Windows this struct is used only
  50. // with sockets so we need to include winsock.h which we do via windows.h
  51. #ifdef __WXMSW__
  52. #include "wx/msw/wrapwin.h"
  53. #else
  54. #include <sys/time.h> // for timeval
  55. #endif
  56. // these definitions are for MSW when we don't use configure, otherwise these
  57. // symbols are defined by configure
  58. #ifndef WX_SOCKLEN_T
  59. #define WX_SOCKLEN_T int
  60. #endif
  61. #ifndef SOCKOPTLEN_T
  62. #define SOCKOPTLEN_T int
  63. #endif
  64. // define some symbols which winsock.h defines but traditional BSD headers
  65. // don't
  66. #ifndef __WXMSW__
  67. #define SOCKET int
  68. #endif
  69. #ifndef INVALID_SOCKET
  70. #define INVALID_SOCKET (-1)
  71. #endif
  72. #ifndef SOCKET_ERROR
  73. #define SOCKET_ERROR (-1)
  74. #endif
  75. typedef int wxSocketEventFlags;
  76. class wxSocketImpl;
  77. /*
  78. Class providing hooks abstracting the differences between console and GUI
  79. applications for socket code.
  80. We also have different implementations of this class for different platforms
  81. allowing us to keep more things in the common code but the main reason for
  82. its existence is that we want the same socket code work differently
  83. depending on whether it's used from a console or a GUI program. This is
  84. achieved by implementing the virtual methods of this class differently in
  85. the objects returned by wxConsoleAppTraits::GetSocketManager() and the same
  86. method in wxGUIAppTraits.
  87. */
  88. class wxSocketManager
  89. {
  90. public:
  91. // set the manager to use, we don't take ownership of it
  92. //
  93. // this should be called before creating the first wxSocket object,
  94. // otherwise the manager returned by wxAppTraits::GetSocketManager() will
  95. // be used
  96. static void Set(wxSocketManager *manager);
  97. // return the manager to use
  98. //
  99. // this initializes the manager at first use
  100. static wxSocketManager *Get()
  101. {
  102. if ( !ms_manager )
  103. Init();
  104. return ms_manager;
  105. }
  106. // called before the first wxSocket is created and should do the
  107. // initializations needed in order to use the network
  108. //
  109. // return true if initialized successfully; if this returns false sockets
  110. // can't be used at all
  111. virtual bool OnInit() = 0;
  112. // undo the initializations of OnInit()
  113. virtual void OnExit() = 0;
  114. // create the socket implementation object matching this manager
  115. virtual wxSocketImpl *CreateSocket(wxSocketBase& wxsocket) = 0;
  116. // these functions enable or disable monitoring of the given socket for the
  117. // specified events inside the currently running event loop (but notice
  118. // that both BSD and Winsock implementations actually use socket->m_server
  119. // value to determine what exactly should be monitored so it needs to be
  120. // set before calling these functions)
  121. //
  122. // the default event value is used just for the convenience of wxMSW
  123. // implementation which doesn't use this parameter anyhow, it doesn't make
  124. // sense to pass wxSOCKET_LOST for the Unix implementation which does use
  125. // this parameter
  126. virtual void Install_Callback(wxSocketImpl *socket,
  127. wxSocketNotify event = wxSOCKET_LOST) = 0;
  128. virtual void Uninstall_Callback(wxSocketImpl *socket,
  129. wxSocketNotify event = wxSOCKET_LOST) = 0;
  130. virtual ~wxSocketManager() { }
  131. private:
  132. // get the manager to use if we don't have it yet
  133. static void Init();
  134. static wxSocketManager *ms_manager;
  135. };
  136. /*
  137. Base class for all socket implementations providing functionality common to
  138. BSD and Winsock sockets.
  139. Objects of this class are not created directly but only via the factory
  140. function wxSocketManager::CreateSocket().
  141. */
  142. class wxSocketImpl
  143. {
  144. public:
  145. virtual ~wxSocketImpl();
  146. // set various socket properties: all of those can only be called before
  147. // creating the socket
  148. void SetTimeout(unsigned long millisec);
  149. void SetReusable() { m_reusable = true; }
  150. void SetBroadcast() { m_broadcast = true; }
  151. void DontDoBind() { m_dobind = false; }
  152. void SetInitialSocketBuffers(int recv, int send)
  153. {
  154. m_initialRecvBufferSize = recv;
  155. m_initialSendBufferSize = send;
  156. }
  157. wxSocketError SetLocal(const wxSockAddressImpl& address);
  158. wxSocketError SetPeer(const wxSockAddressImpl& address);
  159. // accessors
  160. // ---------
  161. bool IsServer() const { return m_server; }
  162. const wxSockAddressImpl& GetLocal(); // non const as may update m_local
  163. const wxSockAddressImpl& GetPeer() const { return m_peer; }
  164. wxSocketError GetError() const { return m_error; }
  165. bool IsOk() const { return m_error == wxSOCKET_NOERROR; }
  166. // get the error code corresponding to the last operation
  167. virtual wxSocketError GetLastError() const = 0;
  168. // creating/closing the socket
  169. // --------------------------
  170. // notice that SetLocal() must be called before creating the socket using
  171. // any of the functions below
  172. //
  173. // all of Create() functions return wxSOCKET_NOERROR if the operation
  174. // completed successfully or one of:
  175. // wxSOCKET_INVSOCK - the socket is in use.
  176. // wxSOCKET_INVADDR - the local (server) or peer (client) address has not
  177. // been set.
  178. // wxSOCKET_IOERR - any other error.
  179. // create a socket listening on the local address specified by SetLocal()
  180. // (notice that DontDoBind() is ignored by this function)
  181. wxSocketError CreateServer();
  182. // create a socket connected to the peer address specified by SetPeer()
  183. // (notice that DontDoBind() is ignored by this function)
  184. //
  185. // this function may return wxSOCKET_WOULDBLOCK in addition to the return
  186. // values listed above if wait is false
  187. wxSocketError CreateClient(bool wait);
  188. // create (and bind unless DontDoBind() had been called) an UDP socket
  189. // associated with the given local address
  190. wxSocketError CreateUDP();
  191. // may be called whether the socket was created or not, calls DoClose() if
  192. // it was indeed created
  193. void Close();
  194. // shuts down the writing end of the socket and closes it, this is a more
  195. // graceful way to close
  196. //
  197. // does nothing if the socket wasn't created
  198. void Shutdown();
  199. // IO operations
  200. // -------------
  201. // basic IO, work for both TCP and UDP sockets
  202. //
  203. // return the number of bytes read/written (possibly 0) or -1 on error
  204. int Read(void *buffer, int size);
  205. int Write(const void *buffer, int size);
  206. // basically a wrapper for select(): returns the condition of the socket,
  207. // blocking for not longer than timeout if it is specified (otherwise just
  208. // poll without blocking at all)
  209. //
  210. // flags defines what kind of conditions we're interested in, the return
  211. // value is composed of a (possibly empty) subset of the bits set in flags
  212. wxSocketEventFlags Select(wxSocketEventFlags flags,
  213. const timeval *timeout = NULL);
  214. // convenient wrapper calling Select() with our default timeout
  215. wxSocketEventFlags SelectWithTimeout(wxSocketEventFlags flags)
  216. {
  217. return Select(flags, &m_timeout);
  218. }
  219. // just a wrapper for accept(): it is called to create a new wxSocketImpl
  220. // corresponding to a new server connection represented by the given
  221. // wxSocketBase, returns NULL on error (including immediately if there are
  222. // no pending connections as our sockets are non-blocking)
  223. wxSocketImpl *Accept(wxSocketBase& wxsocket);
  224. // notifications
  225. // -------------
  226. // notify m_wxsocket about the given socket event by calling its (inaptly
  227. // named) OnRequest() method
  228. void NotifyOnStateChange(wxSocketNotify event);
  229. // called after reading/writing the data from/to the socket and should
  230. // enable back the wxSOCKET_INPUT/OUTPUT_FLAG notifications if they were
  231. // turned off when this data was first detected
  232. virtual void ReenableEvents(wxSocketEventFlags flags) = 0;
  233. // TODO: make these fields protected and provide accessors for those of
  234. // them that wxSocketBase really needs
  235. //protected:
  236. SOCKET m_fd;
  237. int m_initialRecvBufferSize;
  238. int m_initialSendBufferSize;
  239. wxSockAddressImpl m_local,
  240. m_peer;
  241. wxSocketError m_error;
  242. bool m_stream;
  243. bool m_establishing;
  244. bool m_reusable;
  245. bool m_broadcast;
  246. bool m_dobind;
  247. struct timeval m_timeout;
  248. protected:
  249. wxSocketImpl(wxSocketBase& wxsocket);
  250. // true if we're a listening stream socket
  251. bool m_server;
  252. private:
  253. // called by Close() if we have a valid m_fd
  254. virtual void DoClose() = 0;
  255. // put this socket into non-blocking mode and enable monitoring this socket
  256. // as part of the event loop
  257. virtual void UnblockAndRegisterWithEventLoop() = 0;
  258. // check that the socket wasn't created yet and that the given address
  259. // (either m_local or m_peer depending on the socket kind) is valid and
  260. // set m_error and return false if this is not the case
  261. bool PreCreateCheck(const wxSockAddressImpl& addr);
  262. // set the given socket option: this just wraps setsockopt(SOL_SOCKET)
  263. int SetSocketOption(int optname, int optval)
  264. {
  265. // although modern Unix systems use "const void *" for the 4th
  266. // parameter here, old systems and Winsock still use "const char *"
  267. return setsockopt(m_fd, SOL_SOCKET, optname,
  268. reinterpret_cast<const char *>(&optval),
  269. sizeof(optval));
  270. }
  271. // set the given socket option to true value: this is an even simpler
  272. // wrapper for setsockopt(SOL_SOCKET) for boolean options
  273. int EnableSocketOption(int optname)
  274. {
  275. return SetSocketOption(optname, 1);
  276. }
  277. // apply the options to the (just created) socket and register it with the
  278. // event loop by calling UnblockAndRegisterWithEventLoop()
  279. void PostCreation();
  280. // update local address after binding/connecting
  281. wxSocketError UpdateLocalAddress();
  282. // functions used to implement Read/Write()
  283. int RecvStream(void *buffer, int size);
  284. int RecvDgram(void *buffer, int size);
  285. int SendStream(const void *buffer, int size);
  286. int SendDgram(const void *buffer, int size);
  287. // set in ctor and never changed except that it's reset to NULL when the
  288. // socket is shut down
  289. wxSocketBase *m_wxsocket;
  290. wxDECLARE_NO_COPY_CLASS(wxSocketImpl);
  291. };
  292. #if defined(__WXMSW__)
  293. #include "wx/msw/private/sockmsw.h"
  294. #else
  295. #include "wx/unix/private/sockunix.h"
  296. #endif
  297. #endif /* wxUSE_SOCKETS */
  298. #endif /* _WX_PRIVATE_SOCKET_H_ */