home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / MFCINC.PAK / AFXSOCK.H < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  9.9 KB  |  356 lines

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