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.

234 lines
6.5 KiB

  1. /*
  2. * asynconn.h
  3. *
  4. * Purpose:
  5. * prototypes for the async connection class
  6. *
  7. * Owner:
  8. * EricAn
  9. *
  10. * History:
  11. * Apr 96: Created.
  12. *
  13. * Copyright (C) Microsoft Corp. 1996
  14. */
  15. #ifndef __ASYNCONN_H__
  16. #define __ASYNCONN_H__
  17. #include "thorsspi.h"
  18. interface ILogFile;
  19. typedef struct tagRECVBUFQ * PRECVBUFQ;
  20. typedef struct tagRECVBUFQ {
  21. PRECVBUFQ pNext;
  22. int cbLen;
  23. char szBuf[1];
  24. } RECVBUFQ;
  25. // this is structure for server's ignorable errors
  26. typedef struct _SRVIGNORABLEERROR
  27. {
  28. TCHAR *pchServerName;
  29. HRESULT hrError;
  30. struct _SRVIGNORABLEERROR * pLeft;
  31. struct _SRVIGNORABLEERROR * pRight;
  32. } SRVIGNORABLEERROR, *LPSRVIGNORABLEERROR;
  33. void FreeSrvErr(LPSRVIGNORABLEERROR pSrvErr);
  34. // This interface takes the place of any call to AtheMessageBox in asynconn.cpp. The
  35. // user of the CAsyncConn class must provide an implementation of this interface.
  36. interface IAsyncConnPrompt : IUnknown
  37. {
  38. // This method works very much like MessageBox. It is expected that the implementor
  39. // of this method will display a MessageBox. For example:
  40. //
  41. // int CPOP3AsyncConnPrompt::OnPrompt(HRESULT hrError,
  42. // LPCTSTR pszText,
  43. // LPCTSTR pszCaption,
  44. // UINT uType)
  45. // {
  46. // Assert(pszText && pszCaption);
  47. // return MessageBox(m_hwnd, pszText, pszCaption, uType);
  48. // }
  49. //
  50. // The user can compare against the hrError if they want to display their own
  51. // error messages for the corresponding HRESULT.
  52. //
  53. virtual int OnPrompt(HRESULT hrError, LPCTSTR pszText, LPCTSTR pszCaption, UINT uType) PURE;
  54. };
  55. typedef enum {
  56. AS_DISCONNECTED,
  57. AS_RECONNECTING,
  58. AS_LOOKUPINPROG,
  59. AS_LOOKUPDONE,
  60. AS_CONNECTING,
  61. AS_CONNECTED,
  62. AS_HANDSHAKING,
  63. } ASYNCSTATE;
  64. typedef enum {
  65. AE_NONE,
  66. AE_LOOKUPDONE,
  67. AE_CONNECTDONE,
  68. AE_RECV,
  69. AE_SENDDONE,
  70. AE_CLOSE,
  71. AE_WRITE,
  72. AE_TIMEOUT
  73. } ASYNCEVENT;
  74. interface IAsyncConnCB : IUnknown
  75. {
  76. virtual void OnNotify(ASYNCSTATE asOld, ASYNCSTATE asNew, ASYNCEVENT ae) = 0;
  77. };
  78. class CAsyncConnNotify : public IAsyncConnCB
  79. {
  80. public:
  81. CAsyncConnNotify(HWND hwnd, UINT msg)
  82. {
  83. m_hwnd = hwnd;
  84. m_msg = msg;
  85. m_cRef = 1;
  86. }
  87. STDMETHODIMP QueryInterface(REFIID riid, LPVOID *ppv)
  88. {
  89. return E_NOTIMPL;
  90. }
  91. STDMETHODIMP_(ULONG) AddRef(void)
  92. {
  93. return ++m_cRef;
  94. }
  95. STDMETHODIMP_(ULONG) Release(void)
  96. {
  97. if (--m_cRef == 0)
  98. {
  99. delete this;
  100. return 0;
  101. }
  102. return m_cRef;
  103. }
  104. virtual void OnNotify(ASYNCSTATE asOld, ASYNCSTATE asNew, ASYNCEVENT ae)
  105. {
  106. #ifndef WIN16
  107. PostMessage(m_hwnd, m_msg, MAKEWPARAM(asOld, asNew), (LPARAM)ae);
  108. #else
  109. PostMessage(m_hwnd, m_msg, (WPARAM)ae, MAKELPARAM(asOld, asNew));
  110. #endif // !WIN16
  111. }
  112. private:
  113. ULONG m_cRef;
  114. HWND m_hwnd;
  115. UINT m_msg;
  116. };
  117. class CAsyncConn
  118. {
  119. public:
  120. CAsyncConn(ILogFile *pLogFile, IAsyncConnCB *pCB, IAsyncConnPrompt *pPrompt);
  121. ~CAsyncConn();
  122. // ISocketCB methods
  123. virtual ULONG AddRef(void);
  124. virtual ULONG Release(void);
  125. virtual void OnNotify(UINT msg, WPARAM wParam, LPARAM lParam);
  126. HRESULT HrInit(char *szServer, int iDefaultPort, BOOL fSecure = FALSE, DWORD dwTimeout=0);
  127. HRESULT Connect();
  128. HRESULT Close();
  129. HRESULT ReadLine(char **ppszBuf, int *pcbRead);
  130. HRESULT ReadLines(char **ppszBuf, int *pcbRead, int *pcLines);
  131. HRESULT ReadBytes(char **ppszBuf, int cbBytesWanted, int *pcbRead);
  132. HRESULT SendBytes(const char *pszBuf, int cbBuf, int *pcbSent, BOOL fStuffDots=FALSE, CHAR *pchPrev=NULL);
  133. HRESULT SendStream(LPSTREAM pStream, int *pcbSent, BOOL fStuffDots=FALSE);
  134. #ifdef WIN16
  135. #ifdef GetLastError
  136. #undef GetLastError
  137. #endif
  138. #endif // WIN16
  139. int GetLastError() { return m_iLastError; }
  140. int GetConnectStatusString();
  141. ULONG UlGetSendByteCount(VOID);
  142. void StartWatchDog(void);
  143. void StopWatchDog(void);
  144. void OnWatchDogTimer(void);
  145. HRESULT SetWindow(void);
  146. HRESULT ResetWindow(void);
  147. HRESULT TryNextSecurityPkg();
  148. private:
  149. void ChangeState(ASYNCSTATE asNew, ASYNCEVENT ae);
  150. HRESULT HrStuffDots(CHAR *pchPrev, LPSTR pszIn, INT cbIn, LPSTR *ppszOut, INT *pcbOut);
  151. HRESULT AsyncConnect();
  152. HRESULT OnLookupDone(int iLastError);
  153. HRESULT OnConnect();
  154. HRESULT OnClose(ASYNCSTATE asNew);
  155. HRESULT OnRead();
  156. HRESULT OnDataAvail(LPSTR szRecv, int iRecv, BOOL fIncomplete);
  157. HRESULT OnWrite();
  158. HRESULT IReadLines(char **ppszBuf, int *pcbRead, int *pcLines, BOOL fOne);
  159. HRESULT ReadAllBytes(char **ppszBuf, int *pcbRead);
  160. HRESULT OnSSLError();
  161. HRESULT OnRecvHandshakeData();
  162. void CleanUp();
  163. void EnterPausedState();
  164. void LeavePausedState();
  165. HWND CreateWnd();
  166. static LRESULT CALLBACK SockWndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp);
  167. private:
  168. ULONG m_cRef;
  169. CHAR m_chPrev;
  170. BOOL m_fStuffDots;
  171. ULONG m_cbSent;
  172. SOCKET m_sock;
  173. BOOL m_fLookup;
  174. ASYNCSTATE m_state;
  175. SOCKADDR_IN m_sa;
  176. BOOL m_fCachedAddr;
  177. BOOL m_fRedoLookup;
  178. LPSTR m_pszServer;
  179. u_short m_iDefaultPort;
  180. int m_iLastError;
  181. CRITICAL_SECTION m_cs;
  182. ILogFile * m_pLogFile;
  183. IAsyncConnCB * m_pCB;
  184. IAsyncConnPrompt *m_pPrompt;
  185. DWORD m_cbQueued;
  186. char * m_lpbQueued;
  187. char * m_lpbQueueCur;
  188. LPSTREAM m_pStream;
  189. PRECVBUFQ m_pRecvHead;
  190. PRECVBUFQ m_pRecvTail;
  191. int m_iRecvOffset;
  192. BOOL m_fNeedRecvNotify;
  193. HWND m_hwnd;
  194. BOOL m_fNegotiateSecure;
  195. BOOL m_fSecure;
  196. CtxtHandle m_hContext;
  197. int m_iCurSecPkg;
  198. LPSTR m_pbExtra;
  199. int m_cbExtra;
  200. // For Timeout Handling
  201. DWORD m_dwLastActivity;
  202. DWORD m_dwTimeout;
  203. UINT_PTR m_uiTimer;
  204. #ifdef DEBUG
  205. int m_cLock;
  206. #endif
  207. BOOL m_fPaused;
  208. DWORD m_dwEventMask;
  209. };
  210. #endif // __ASYNCONN_H__