Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

350 lines
9.8 KiB

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #ifndef __AFXSOCK_H__
  11. #define __AFXSOCK_H__
  12. #ifdef _AFX_NO_SOCKET_SUPPORT
  13. #error Windows Sockets classes not supported in this library variant.
  14. #endif
  15. #ifndef __AFXWIN_H__
  16. #include <afxwin.h>
  17. #endif
  18. #ifndef _WINSOCKAPI_
  19. #include <winsock.h>
  20. #endif
  21. #ifdef _AFX_MINREBUILD
  22. #pragma component(minrebuild, off)
  23. #endif
  24. #ifndef _AFX_FULLTYPEINFO
  25. #pragma component(mintypeinfo, on)
  26. #endif
  27. #ifndef _AFX_NOFORCE_LIBS
  28. /////////////////////////////////////////////////////////////////////////////
  29. // Win32 libraries
  30. #ifdef _AFXDLL
  31. #if defined(_DEBUG) && !defined(_AFX_MONOLITHIC)
  32. #ifndef _UNICODE
  33. #pragma comment(lib, "mfcn42d.lib")
  34. #else
  35. #pragma comment(lib, "mfcn42ud.lib")
  36. #endif
  37. #endif
  38. #endif
  39. #pragma comment(lib, "wsock32.lib")
  40. #endif //!_AFX_NOFORCE_LIBS
  41. /////////////////////////////////////////////////////////////////////////////
  42. #ifdef _AFX_PACKING
  43. #pragma pack(push, _AFX_PACKING)
  44. #endif
  45. /////////////////////////////////////////////////////////////////////////////
  46. // AFXSOCK - MFC support for Windows Sockets
  47. // Classes declared in this file
  48. // CObject
  49. class CAsyncSocket; // Async Socket implementation and
  50. // base class for Synchronous Socket
  51. class CSocket; // Synchronous Socket
  52. // CFile
  53. class CSocketFile; // Used with CSocket and CArchive for
  54. // streaming objects on sockets.
  55. /////////////////////////////////////////////////////////////////////////////
  56. // AFXDLL support
  57. #undef AFX_DATA
  58. #define AFX_DATA AFX_NET_DATA
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CSocketWnd -- internal use only
  61. // Implementation for sockets notification callbacks.
  62. // Future versions of MFC may or may not include this exact class.
  63. class CSocketWnd : public CWnd
  64. {
  65. // Construction
  66. public:
  67. CSocketWnd();
  68. protected:
  69. //{{AFX_MSG(CSocketWnd)
  70. LRESULT OnSocketNotify(WPARAM wParam, LPARAM lParam);
  71. LRESULT OnSocketDead(WPARAM wParam, LPARAM lParam);
  72. //}}AFX_MSG
  73. DECLARE_MESSAGE_MAP()
  74. };
  75. /////////////////////////////////////////////////////////////////////////////
  76. // CAsyncSocket
  77. class CAsyncSocket : public CObject
  78. {
  79. DECLARE_DYNAMIC(CAsyncSocket);
  80. private:
  81. CAsyncSocket(const CAsyncSocket& rSrc); // no implementation
  82. void operator=(const CAsyncSocket& rSrc); // no implementation
  83. // Construction
  84. public:
  85. CAsyncSocket();
  86. BOOL Create(UINT nSocketPort = 0, int nSocketType=SOCK_STREAM,
  87. long lEvent = FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE,
  88. LPCTSTR lpszSocketAddress = NULL);
  89. // Attributes
  90. public:
  91. SOCKET m_hSocket;
  92. operator SOCKET() const;
  93. BOOL Attach(SOCKET hSocket, long lEvent =
  94. FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE);
  95. SOCKET Detach();
  96. BOOL GetPeerName(CString& rPeerAddress, UINT& rPeerPort);
  97. BOOL GetPeerName(SOCKADDR* lpSockAddr, int* lpSockAddrLen);
  98. BOOL GetSockName(CString& rSocketAddress, UINT& rSocketPort);
  99. BOOL GetSockName(SOCKADDR* lpSockAddr, int* lpSockAddrLen);
  100. BOOL SetSockOpt(int nOptionName, const void* lpOptionValue,
  101. int nOptionLen, int nLevel = SOL_SOCKET);
  102. BOOL GetSockOpt(int nOptionName, void* lpOptionValue,
  103. int* lpOptionLen, int nLevel = SOL_SOCKET);
  104. static CAsyncSocket* PASCAL FromHandle(SOCKET hSocket);
  105. static int PASCAL GetLastError();
  106. // Operations
  107. public:
  108. virtual BOOL Accept(CAsyncSocket& rConnectedSocket,
  109. SOCKADDR* lpSockAddr = NULL, int* lpSockAddrLen = NULL);
  110. BOOL Bind(UINT nSocketPort, LPCTSTR lpszSocketAddress = NULL);
  111. BOOL Bind (const SOCKADDR* lpSockAddr, int nSockAddrLen);
  112. virtual void Close();
  113. BOOL Connect(LPCTSTR lpszHostAddress, UINT nHostPort);
  114. BOOL Connect(const SOCKADDR* lpSockAddr, int nSockAddrLen);
  115. BOOL IOCtl(long lCommand, DWORD* lpArgument);
  116. BOOL Listen(int nConnectionBacklog=5);
  117. virtual int Receive(void* lpBuf, int nBufLen, int nFlags = 0);
  118. int ReceiveFrom(void* lpBuf, int nBufLen,
  119. CString& rSocketAddress, UINT& rSocketPort, int nFlags = 0);
  120. int ReceiveFrom(void* lpBuf, int nBufLen,
  121. SOCKADDR* lpSockAddr, int* lpSockAddrLen, int nFlags = 0);
  122. enum { receives = 0, sends = 1, both = 2 };
  123. BOOL ShutDown(int nHow = sends);
  124. virtual int Send(const void* lpBuf, int nBufLen, int nFlags = 0);
  125. int SendTo(const void* lpBuf, int nBufLen,
  126. UINT nHostPort, LPCTSTR lpszHostAddress = NULL, int nFlags = 0);
  127. int SendTo(const void* lpBuf, int nBufLen,
  128. const SOCKADDR* lpSockAddr, int nSockAddrLen, int nFlags = 0);
  129. BOOL AsyncSelect(long lEvent =
  130. FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE);
  131. // Overridable callbacks
  132. protected:
  133. virtual void OnReceive(int nErrorCode);
  134. virtual void OnSend(int nErrorCode);
  135. virtual void OnOutOfBandData(int nErrorCode);
  136. virtual void OnAccept(int nErrorCode);
  137. virtual void OnConnect(int nErrorCode);
  138. virtual void OnClose(int nErrorCode);
  139. // Implementation
  140. public:
  141. virtual ~CAsyncSocket();
  142. static CAsyncSocket* PASCAL LookupHandle(SOCKET hSocket, BOOL bDead = FALSE);
  143. static void PASCAL AttachHandle(SOCKET hSocket, CAsyncSocket* pSocket, BOOL bDead = FALSE);
  144. static void PASCAL DetachHandle(SOCKET hSocket, BOOL bDead = FALSE);
  145. static void PASCAL KillSocket(SOCKET hSocket, CAsyncSocket* pSocket);
  146. static void PASCAL DoCallBack(WPARAM wParam, LPARAM lParam);
  147. BOOL Socket(int nSocketType=SOCK_STREAM, long lEvent =
  148. FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE,
  149. int nProtocolType = 0, int nAddressFormat = PF_INET);
  150. #ifdef _DEBUG
  151. virtual void AssertValid() const;
  152. virtual void Dump(CDumpContext& dc) const;
  153. #endif
  154. protected:
  155. friend class CSocketWnd;
  156. virtual BOOL ConnectHelper(const SOCKADDR* lpSockAddr, int nSockAddrLen);
  157. virtual int ReceiveFromHelper(void* lpBuf, int nBufLen,
  158. SOCKADDR* lpSockAddr, int* lpSockAddrLen, int nFlags);
  159. virtual int SendToHelper(const void* lpBuf, int nBufLen,
  160. const SOCKADDR* lpSockAddr, int nSockAddrLen, int nFlags);
  161. };
  162. /////////////////////////////////////////////////////////////////////////////
  163. // CSocket
  164. class CSocket : public CAsyncSocket
  165. {
  166. DECLARE_DYNAMIC(CSocket);
  167. private:
  168. CSocket(const CSocket& rSrc); // no implementation
  169. void operator=(const CSocket& rSrc); // no implementation
  170. // Construction
  171. public:
  172. CSocket();
  173. BOOL Create(UINT nSocketPort = 0, int nSocketType=SOCK_STREAM,
  174. LPCTSTR lpszSocketAddress = NULL);
  175. // Attributes
  176. public:
  177. BOOL IsBlocking();
  178. static CSocket* PASCAL FromHandle(SOCKET hSocket);
  179. BOOL Attach(SOCKET hSocket);
  180. // Operations
  181. public:
  182. void CancelBlockingCall();
  183. // Overridable callbacks
  184. protected:
  185. virtual BOOL OnMessagePending();
  186. // Implementation
  187. public:
  188. int m_nTimeOut;
  189. virtual ~CSocket();
  190. static int PASCAL ProcessAuxQueue();
  191. virtual BOOL Accept(CAsyncSocket& rConnectedSocket,
  192. SOCKADDR* lpSockAddr = NULL, int* lpSockAddrLen = NULL);
  193. virtual void Close();
  194. virtual int Receive(void* lpBuf, int nBufLen, int nFlags = 0);
  195. virtual int Send(const void* lpBuf, int nBufLen, int nFlags = 0);
  196. int SendChunk(const void* lpBuf, int nBufLen, int nFlags);
  197. protected:
  198. friend class CSocketWnd;
  199. BOOL* m_pbBlocking;
  200. int m_nConnectError;
  201. virtual BOOL ConnectHelper(const SOCKADDR* lpSockAddr, int nSockAddrLen);
  202. virtual int ReceiveFromHelper(void* lpBuf, int nBufLen,
  203. SOCKADDR* lpSockAddr, int* lpSockAddrLen, int nFlags);
  204. virtual int SendToHelper(const void* lpBuf, int nBufLen,
  205. const SOCKADDR* lpSockAddr, int nSockAddrLen, int nFlags);
  206. static void PASCAL AuxQueueAdd(UINT message, WPARAM wParam, LPARAM lParam);
  207. virtual BOOL PumpMessages(UINT uStopFlag);
  208. #ifdef _DEBUG
  209. virtual void AssertValid() const;
  210. virtual void Dump(CDumpContext& dc) const;
  211. #endif
  212. };
  213. /////////////////////////////////////////////////////////////////////////////
  214. // CSocketFile
  215. class CSocketFile : public CFile
  216. {
  217. DECLARE_DYNAMIC(CSocketFile)
  218. public:
  219. //Constructors
  220. CSocketFile(CSocket* pSocket, BOOL bArchiveCompatible = TRUE);
  221. // Implementation
  222. public:
  223. CSocket* m_pSocket;
  224. BOOL m_bArchiveCompatible;
  225. virtual ~CSocketFile();
  226. #ifdef _DEBUG
  227. virtual void AssertValid() const;
  228. virtual void Dump(CDumpContext& dc) const;
  229. #endif
  230. virtual UINT Read(void* lpBuf, UINT nCount);
  231. virtual void Write(const void* lpBuf, UINT nCount);
  232. virtual void Close();
  233. // Unsupported APIs
  234. virtual BOOL Open(LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError = NULL);
  235. virtual CFile* Duplicate() const;
  236. virtual DWORD GetPosition() const;
  237. virtual LONG Seek(LONG lOff, UINT nFrom);
  238. virtual void SetLength(DWORD dwNewLen);
  239. virtual DWORD GetLength() const;
  240. virtual void LockRange(DWORD dwPos, DWORD dwCount);
  241. virtual void UnlockRange(DWORD dwPos, DWORD dwCount);
  242. virtual void Flush();
  243. virtual void Abort();
  244. };
  245. /////////////////////////////////////////////////////////////////////////////
  246. // Global functions
  247. BOOL AFXAPI AfxSocketInit(WSADATA* lpwsaData = NULL);
  248. void AFXAPI AfxSocketTerm();
  249. /////////////////////////////////////////////////////////////////////////////
  250. // Inline function declarations
  251. #ifdef _AFX_PACKING
  252. #pragma pack(pop)
  253. #endif
  254. #ifdef _AFX_ENABLE_INLINES
  255. #define _AFXSOCK_INLINE AFX_INLINE
  256. #include <afxsock.inl>
  257. #undef _AFXSOCK_INLINE
  258. #endif
  259. #undef AFX_DATA
  260. #define AFX_DATA
  261. #ifdef _AFX_MINREBUILD
  262. #pragma component(minrebuild, on)
  263. #endif
  264. #ifndef _AFX_FULLTYPEINFO
  265. #pragma component(mintypeinfo, off)
  266. #endif
  267. #endif // __AFXSOCK_H__
  268. /////////////////////////////////////////////////////////////////////////////