utility.cpp&&blocksock.h&&ex34a.h&&ex34aDoc.h&&ex34aView.h&&MainFrm.h&&SheetConfig.h&&utility.h

来源:互联网 发布:mes系统编程语言 编辑:程序博客网 时间:2024/05/08 17:31

// utility.cpp -- contains stuff used by more than one thread
#include <stdafx.h>
#include "blocksock.h"
#include "utility.h"

volatile int g_nConnection = 0; // connection number
CString g_strServerName = "localhost"; // used by both winsock and wininet
CString g_strServerIP;
volatile UINT g_nPort = 80;
CString g_strFile = "/custom";

CCallbackInternetSession::CCallbackInternetSession( LPCTSTR pstrAgent, DWORD dwContext,
  DWORD dwAccessType, LPCTSTR pstrProxyName, LPCTSTR pstrProxyBypass, DWORD dwFlags) :
 CInternetSession(pstrAgent, dwContext, dwAccessType, pstrProxyName, pstrProxyBypass, dwFlags)
{
 EnableStatusCallback();
}

void CCallbackInternetSession::OnStatusCallback(DWORD dwContext, DWORD dwInternalStatus,
  LPVOID lpvStatusInformation, DWORD dwStatusInformationLength)
{
 AFX_MANAGE_STATE(AfxGetStaticModuleState());
 int errors[] = {10, 11, 20, 21, 30, 31, 40, 41, 42, 43, 50, 51, 60, 70, 100, 110, 0};
 char* text[] = {
  "Resolving name",        
  "Name resolved",      
  "Connecting to server",
  "Connected to server", 
  "Sending request",     
  "Request sent",        
  "Receiving response",  
  "Response received",   
  "Ctl response received",
  "Prefetch",            
  "Closing connection",  
  "Connection closed",   
  "Handle created",      
  "Handle closing",      
  "Request complete",    
  "Redirect",
  "Unknown" };
 int n;
/*  // demonstrates request cancellation
 if(dwInternalStatus == INTERNET_STATUS_REQUEST_SENT) {
  AfxThrowInternetException(dwContext, 999);
 }
*/
 for(n = 0; errors[n] != 0; n++) {
  if(errors[n] == (int) dwInternalStatus) break;
 }
 g_csStatus.Lock();
   strcpy(g_pchStatus, text[n]);
   if(dwInternalStatus  == INTERNET_STATUS_RESOLVING_NAME ||
    dwInternalStatus == INTERNET_STATUS_NAME_RESOLVED) {
  strcat(g_pchStatus, "-");
  strcat(g_pchStatus, (char*) lpvStatusInformation);
   }
   TRACE("WININET STATUS: %s/n", g_pchStatus);
 g_csStatus.Unlock();
 // frame doesn't need a handler -- message triggers OnIdle, which updates status bar
 ::PostMessage(g_hMainWnd, WM_CALLBACK, 0, 0);
}

void LogInternetException(LPVOID pParam, CInternetException* pe)
{ // pParam holds the HWND for the destination window (in another thread)
 CString strGmt = CTime::GetCurrentTime().FormatGmt("%m/%d/%y %H:%M:% GMT");
 char text1[300], text2[100];
 wsprintf(text1, "CLIENT ERROR: WinInet error #%d -- %s/r/n   ",
  pe->m_dwError, (const char*) strGmt);
 pe->GetErrorMessage(text2, 99);
 strcat(text1, text2);
 if(pe->m_dwError == 12152) {
  strcat(text1, "  URL not found?/r/n");
 }
 ::SendMessage((HWND) pParam, EM_SETSEL, (WPARAM) 65534, 65535);
 ::SendMessage((HWND) pParam, EM_REPLACESEL, (WPARAM) 0, (LPARAM) text1);
}

 

//********************************

// blocksock.h

// needs winsock.h in the precompiled headers

typedef const struct sockaddr* LPCSOCKADDR;

class CBlockingSocketException : public CException
{
 DECLARE_DYNAMIC(CBlockingSocketException)
public:
// Constructor
 CBlockingSocketException(char* pchMessage);

public:
 ~CBlockingSocketException() {}
 virtual BOOL GetErrorMessage(LPTSTR lpstrError, UINT nMaxError,
  PUINT pnHelpContext = NULL);
private:
 int m_nError;
 CString m_strMessage;
};

extern void LogBlockingSocketException(LPVOID pParam, char* pch, CBlockingSocketException* pe);

class CSockAddr : public sockaddr_in {
public:
 // constructors
 CSockAddr()
  { sin_family = AF_INET;
    sin_port = 0;
    sin_addr.s_addr = 0; } // Default
 CSockAddr(const SOCKADDR& sa) { memcpy(this, &sa, sizeof(SOCKADDR)); }
 CSockAddr(const SOCKADDR_IN& sin) { memcpy(this, &sin, sizeof(SOCKADDR_IN)); }
 CSockAddr(const ULONG ulAddr, const USHORT ushPort = 0) // parms are host byte ordered
  { sin_family = AF_INET;
    sin_port = htons(ushPort);
       sin_addr.s_addr = htonl(ulAddr); }
 CSockAddr(const char* pchIP, const USHORT ushPort = 0) // dotted IP addr string
  { sin_family = AF_INET;
    sin_port = htons(ushPort);
    sin_addr.s_addr = inet_addr(pchIP); } // already network byte ordered
 // Return the address in dotted-decimal format
 CString DottedDecimal()
  { return inet_ntoa(sin_addr); } // constructs a new CString object
 // Get port and address (even though they're public)
 USHORT Port() const
  { return ntohs(sin_port); }
 ULONG IPAddr() const
  { return ntohl(sin_addr.s_addr); }
 // operators added for efficiency
 const CSockAddr& operator=(const SOCKADDR& sa)
  { memcpy(this, &sa, sizeof(SOCKADDR));
    return *this; }
 const CSockAddr& operator=(const SOCKADDR_IN& sin)
  { memcpy(this, &sin, sizeof(SOCKADDR_IN));
    return *this; }
 operator SOCKADDR()
  { return *((LPSOCKADDR) this); }
 operator LPSOCKADDR()
  { return (LPSOCKADDR) this; }
 operator LPSOCKADDR_IN()
  { return (LPSOCKADDR_IN) this; }
};

// member functions truly block and must not be used in UI threads
// use this class as an alternative to the MFC CSocket class
class CBlockingSocket : public CObject
{
 DECLARE_DYNAMIC(CBlockingSocket)
public:
 SOCKET m_hSocket;
 CBlockingSocket() { m_hSocket = NULL; }//产生一个没有初始化的对象
 void Cleanup();
 void Create(int nType = SOCK_STREAM);
 void Close();
 void Bind(LPCSOCKADDR psa);
 void Listen();
 void Connect(LPCSOCKADDR psa);
 BOOL Accept(CBlockingSocket& s, LPSOCKADDR psa);
 int Send(const char* pch, const int nSize, const int nSecs);
 int Write(const char* pch, const int nSize, const int nSecs);
 int Receive(char* pch, const int nSize, const int nSecs);
 int SendDatagram(const char* pch, const int nSize, LPCSOCKADDR psa,
  const int nSecs);
 int ReceiveDatagram(char* pch, const int nSize, LPSOCKADDR psa,
  const int nSecs);
 void GetPeerAddr(LPSOCKADDR psa);
 void GetSockAddr(LPSOCKADDR psa);
 static CSockAddr GetHostByName(const char* pchName,
  const USHORT ushPort = 0);
 static const char* GetHostByAddr(LPCSOCKADDR psa);
 operator SOCKET()
  { return m_hSocket; }
};

class CHttpBlockingSocket : public CBlockingSocket
{
public:
 DECLARE_DYNAMIC(CHttpBlockingSocket)
 enum {nSizeRecv = 1000}; // max receive buffer size (> hdr line length)
 CHttpBlockingSocket();
 ~CHttpBlockingSocket();
 int ReadHttpHeaderLine(char* pch, const int nSize, const int nSecs);
 int ReadHttpResponse(char* pch, const int nSize, const int nSecs);
private:
 char* m_pReadBuf; // read buffer
 int m_nReadBuf; // number of bytes in the read buffer
};

 

//***************************

// ex34a.h : main header file for the EX34A application
//

#ifndef __AFXWIN_H__
 #error include 'stdafx.h' before including this file for PCH
#endif

#include "resource.h"       // main symbols

/////////////////////////////////////////////////////////////////////////////
// CEx34aApp:
// See ex34a.cpp for the implementation of this class
//

class CEx34aApp : public CWinApp
{
public:
 CEx34aApp();

// Overrides
 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(CEx34aApp)
 public:
 virtual BOOL InitInstance();
 virtual int ExitInstance();
 virtual BOOL OnIdle(LONG lCount);
 //}}AFX_VIRTUAL

// Implementation

 //{{AFX_MSG(CEx34aApp)
 afx_msg void OnAppAbout();
  // NOTE - the ClassWizard will add and remove member functions here.
  //    DO NOT EDIT what you see in these blocks of generated code !
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};


/////////////////////////////////////////////////////////////////////////////

 

 

 

//******************************************

 

// ex34aDoc.h : interface of the CEx34aDoc class
//
/////////////////////////////////////////////////////////////////////////////

class CEx34aDoc : public CDocument
{
protected: // create from serialization only
 CEx34aDoc();
 DECLARE_DYNCREATE(CEx34aDoc)

// Attributes
public:

// Operations
public:

// Overrides
 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(CEx34aDoc)
 public:
 virtual BOOL OnNewDocument();
 virtual void Serialize(CArchive& ar);
 protected:
 virtual BOOL SaveModified();
 //}}AFX_VIRTUAL

// Implementation
public:
 virtual ~CEx34aDoc();
#ifdef _DEBUG
 virtual void AssertValid() const;
 virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:
 //{{AFX_MSG(CEx34aDoc)
  // NOTE - the ClassWizard will add and remove member functions here.
  //    DO NOT EDIT what you see in these blocks of generated code !
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

 

///**********************

// ex34aView.h : interface of the CEx34aView class
//
/////////////////////////////////////////////////////////////////////////////

class CEx34aView : public CEditView
{
protected: // create from serialization only
 CEx34aView();
 DECLARE_DYNCREATE(CEx34aView)

// Attributes
public:
 CEx34aDoc* GetDocument();

// Operations
public:

// Overrides
 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(CEx34aView)
 public:
 virtual void OnDraw(CDC* pDC);  // overridden to draw this view
 virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
 protected:
 virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
 virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
 virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
 //}}AFX_VIRTUAL

// Implementation
public:
 virtual ~CEx34aView();
#ifdef _DEBUG
 virtual void AssertValid() const;
 virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:
 //{{AFX_MSG(CEx34aView)
 afx_msg void OnInternetStartServer();
 afx_msg void OnUpdateInternetStartServer(CCmdUI* pCmdUI);
 afx_msg void OnInternetRequestSocket();
 afx_msg void OnInternetRequestWininet();
 afx_msg void OnInternetStopServer();
 afx_msg void OnUpdateInternetStopServer(CCmdUI* pCmdUI);
 afx_msg void OnInternetConfiguration();
 afx_msg void OnUpdateInternetConfiguration(CCmdUI* pCmdUI);
 afx_msg void OnEditClearAll();
 afx_msg void OnRequest();
 afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};

#ifndef _DEBUG  // debug version in ex34aView.cpp
inline CEx34aDoc* CEx34aView::GetDocument()
   { return (CEx34aDoc*)m_pDocument; }
#endif

/////////////////////////////////////////////////////////////////////////////

 

 

//*******************************************

// MainFrm.h : interface of the CMainFrame class
//
/////////////////////////////////////////////////////////////////////////////

class CMainFrame : public CFrameWnd
{
protected: // create from serialization only
 CMainFrame();
 DECLARE_DYNCREATE(CMainFrame)

// Attributes
public:

// Operations
public:

// Overrides
 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(CMainFrame)
 virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
 //}}AFX_VIRTUAL

// Implementation
public:
 virtual ~CMainFrame();
#ifdef _DEBUG
 virtual void AssertValid() const;
 virtual void Dump(CDumpContext& dc) const;
#endif

protected:  // control bar embedded members
 CToolBar    m_wndToolBar;
public:
 CStatusBar  m_wndStatusBar;
 CDialogBar m_wndDialogBar;

// Generated message map functions
protected:
 //{{AFX_MSG(CMainFrame)
 afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
 afx_msg void OnUpdateListening(CCmdUI* pCmdUI);
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

 

//******************************

// SheetConfig.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CPageServer dialog

class CPageServer : public CPropertyPage
{
 DECLARE_DYNCREATE(CPageServer)

// Construction
public:
 CPageServer();
 ~CPageServer();

// Dialog Data
 //{{AFX_DATA(CPageServer)
 enum { IDD = IDD_PROPPAGE_SERVER };
 CString m_strDirect;
 UINT m_nPortServer;
 CString m_strDefault;
 //}}AFX_DATA


// Overrides
 // ClassWizard generate virtual function overrides
 //{{AFX_VIRTUAL(CPageServer)
 protected:
 virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
 //}}AFX_VIRTUAL

// Implementation
protected:
 // Generated message map functions
 //{{AFX_MSG(CPageServer)
  // NOTE: the ClassWizard will add member functions here
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()

};
/////////////////////////////////////////////////////////////////////////////
// CPageAdv dialog

class CPageAdv : public CPropertyPage
{
 DECLARE_DYNCREATE(CPageAdv)

// Construction
public:
 CPageAdv();
 ~CPageAdv();

// Dialog Data
 //{{AFX_DATA(CPageAdv)
 enum { IDD = IDD_PROPPAGE_ADV };
 CString m_strIPServer;
 CString m_strIPClient;
 //}}AFX_DATA


// Overrides
 // ClassWizard generate virtual function overrides
 //{{AFX_VIRTUAL(CPageAdv)
 protected:
 virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
 //}}AFX_VIRTUAL

// Implementation
protected:
 // Generated message map functions
 //{{AFX_MSG(CPageAdv)
  // NOTE: the ClassWizard will add member functions here
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()

};

/////////////////////////////////////////////////////////////////////////////
// CPageClient dialog

class CPageClient : public CPropertyPage
{
 DECLARE_DYNCREATE(CPageClient)

// Construction
public:
 CPageClient();
 ~CPageClient();

// Dialog Data
 //{{AFX_DATA(CPageClient)
 enum { IDD = IDD_PROPPAGE_CLIENT };
 CString m_strProxy;
 CString m_strServerIP;
 CString m_strServerName;
 BOOL m_bUseProxy;
 CString m_strFile;
 UINT m_nPort;
 //}}AFX_DATA


// Overrides
 // ClassWizard generate virtual function overrides
 //{{AFX_VIRTUAL(CPageClient)
 protected:
 virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
 //}}AFX_VIRTUAL

// Implementation
protected:
 // Generated message map functions
 //{{AFX_MSG(CPageClient)
  // NOTE: the ClassWizard will add member functions here
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()

};

/////////////////////////////////////////////////////////////////////////////
// CSheetConfig

class CSheetConfig : public CPropertySheet
{
 DECLARE_DYNAMIC(CSheetConfig)

// Construction
public:
 CSheetConfig(UINT nIDCaption, CWnd* pParentWnd = NULL, UINT iSelectPage = 0);
 CSheetConfig(LPCTSTR pszCaption, CWnd* pParentWnd = NULL, UINT iSelectPage = 0);

public:
 CPageAdv m_pageAdv;
 CPageClient m_pageClient;
 CPageServer m_pageServer;
// Overrides
 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(CSheetConfig)
 //}}AFX_VIRTUAL

// Implementation
public:
 virtual ~CSheetConfig();

 // Generated message map functions
protected:
 //{{AFX_MSG(CSheetConfig)
  // NOTE - the ClassWizard will add and remove member functions here.
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

 

 

//****************************************************

// utility.h
#define WM_CALLBACK WM_USER + 5
extern volatile int g_nConnection;
extern CString g_strServerName; // used by both winsock and wininet code
extern CString g_strServerIP; // used by both winsock and wininet code
extern CString g_strFile;
extern char g_pchStatus[];
extern HWND g_hMainWnd;
extern CCriticalSection g_csStatus;
extern CString g_strIPClient;
extern volatile UINT g_nPort;
extern CString g_strProxy;
extern BOOL g_bUseProxy;
extern volatile BOOL g_bListening;
extern CString g_strDirect;
extern CString g_strIPServer;
extern volatile UINT g_nPortServer;
extern CString g_strURL;
extern CString g_strDefault;

extern UINT ClientUrlThreadProc(LPVOID pParam);
extern UINT ServerThreadProc(LPVOID pParam);
extern UINT ClientWinInetThreadProc(LPVOID pParam);
extern UINT ClientSocketThreadProc(LPVOID pParam);

extern void LogInternetException(LPVOID pParam, CInternetException* pe);

class CCallbackInternetSession : public CInternetSession
{
public:
 CCallbackInternetSession( LPCTSTR pstrAgent = NULL, DWORD dwContext = 1,
  DWORD dwAccessType = PRE_CONFIG_INTERNET_ACCESS, LPCTSTR pstrProxyName = NULL,
  LPCTSTR pstrProxyBypass = NULL, DWORD dwFlags = 0 );
protected:
 virtual void OnStatusCallback(DWORD dwContext, DWORD dwInternalStatus,
  LPVOID lpvStatusInformation, DWORD dwStatusInformationLength);
};


原创粉丝点击