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.

307 lines
9.0 KiB

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