home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / include / afxsock.h < prev    next >
C/C++ Source or Header  |  1998-06-16  |  10KB  |  351 lines

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