www.pudn.com > 200311301927570.rar > HWCmppAPIDlg.cpp


// HWCmppAPIDlg.cpp : implementation file 
// 
 
 
#include "stdafx.h" 
#include "HWCmppAPI.h" 
#include "HWCmppAPIDlg.h" 
#include "DataConvert.h" 
 
#include "Export.h" 
#include "lable.h" 
#include "VariantBufConvert.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
CString g_strLog = "ErrorLog.txt"; 
CString g_strSystemLog = "SystemLog.txt"; 
HANDLE	g_hLogFile = NULL; 
HANDLE	g_hSysLogFile = NULL; 
void GfWriteLog(CString str) 
{ 
	if( WaitForSingleObject(g_hLogFile, 100) != WAIT_OBJECT_0 ){ 
		return; 
	} 
	CFile f;	 
	if(f.Open(g_strLog,CFile::modeWrite|CFile::modeNoTruncate | CFile::modeCreate   ) ) 
	{ 
		if(f.GetLength() >= 5000000) 
		{ 
			f.SetLength(0); 
		} 
		CTime t = CTime::GetCurrentTime(); 
		CString strTime = t.Format(" %Y-%m-%d %H:%M'%S\r\n"); 
		str += strTime; 
		f.SeekToEnd(); 
		f.Write(str.GetBuffer(0),str.GetLength()); 
		f.Close(); 
	} 
	else{ 
		TRACE("JXMT.txt  file open error!\n"); 
	} 
	ReleaseMutex(g_hLogFile); 
 
} 
 
void GfWriteSysLog(CString str) 
{ 
	if( WaitForSingleObject(g_hSysLogFile, 100) != WAIT_OBJECT_0 ){ 
		return; 
	} 
	CFile f;	 
	if(f.Open(g_strSystemLog, CFile::modeWrite|CFile::modeNoTruncate | CFile::modeCreate   ) ) 
	{ 
		if(f.GetLength() >= 500000) 
		{ 
			f.SetLength(0); 
		} 
		CTime t = CTime::GetCurrentTime(); 
		CString strTime = t.Format(" %Y-%m-%d %H:%M'%S\r\n"); 
		str += strTime; 
		f.SeekToEnd(); 
		f.Write(str.GetBuffer(0),str.GetLength()); 
		f.Close(); 
	} 
 
	ReleaseMutex(g_hSysLogFile); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	//{{AFX_DATA(CAboutDlg) 
	enum { IDD = IDD_ABOUTBOX }; 
	//}}AFX_DATA 
 
	// ClassWizard generated virtual function overrides 
	//{{AFX_VIRTUAL(CAboutDlg) 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
	//}}AFX_VIRTUAL 
 
// Implementation 
protected: 
	//{{AFX_MSG(CAboutDlg) 
	//}}AFX_MSG 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
	//{{AFX_DATA_INIT(CAboutDlg) 
	//}}AFX_DATA_INIT 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAboutDlg) 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
		// No message handlers 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CHWCmppAPIDlg dialog 
 
CHWCmppAPIDlg::CHWCmppAPIDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CHWCmppAPIDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CHWCmppAPIDlg) 
	m_strMONum = _T(""); 
	m_strMTNum = _T(""); 
	m_strRTNum = _T(""); 
	m_strRPNum = _T(""); 
	m_strQueueIn = _T(""); 
	m_strQueueOut = _T(""); 
	m_strMsgCont = _T(""); 
	m_bIsShowMsg = FALSE; 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
 
	m_bySendFailNum = 0; 
	m_bIsLog = FALSE; 
	m_bIsStart = FALSE; 
	m_bIsRestarting = FALSE; 
//	m_hSendHandle = NULL; 
//	m_hRecvHandle = NULL; 
 
	m_hmtxMsgContent = CreateMutex(NULL, FALSE, NULL); 
	m_hmtxIsShowMsg = CreateMutex(NULL, FALSE, NULL); 
 
	m_nSendThreadNum = 1; 
	m_nRecvThreadNum = 1; 
 
	for(int i=0;i<1000;i++){ 
//		m_hSend[i] = NULL; 
		m_pSendThread[i] = NULL; 
	} 
	for(i=0;i<10;i++){ 
//		m_hRecv[i] = NULL; 
		m_pRecvThread[i] = NULL; 
	} 
 
	m_nMONum=0; 
	m_nMTNum=0; 
	m_nRTNum=0; 
	m_nRPNum=0; 
	char chPath[_MAX_PATH] = ""; 
	GetModuleFileName(NULL, chPath, _MAX_PATH); 
	strcpy(strrchr(chPath, '\\'), "\\HWCMPPAPI.ini");	 
	m_strIniFilename = chPath; 
	LoadParam(); 
	m_strConnectPara.Format("%s %d 5000", m_chServerIP, m_nServerPort); //"ip port delaytime" 
} 
CHWCmppAPIDlg::~CHWCmppAPIDlg() 
{ 
	if(!IFExitInterface()){		 
		AfxMessageBox("SMEIDLL.dll没有完全卸载,请从任务管理器强制退出该程序!"); 
	} 
//	KillTimer(RestartTimer); 
} 
void CHWCmppAPIDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CHWCmppAPIDlg) 
	DDX_Text(pDX, IDC_STATIC_MO, m_strMONum); 
	DDX_Text(pDX, IDC_STATIC_MT, m_strMTNum); 
	DDX_Text(pDX, IDC_STATIC_RT, m_strRTNum); 
	DDX_Text(pDX, IDC_STATIC_RP, m_strRPNum); 
	DDX_Text(pDX, IDC_EDIT_QUEUE_UP, m_strQueueIn); 
	DDX_Text(pDX, IDC_EDIT_QUEUE_DOWN, m_strQueueOut); 
	DDX_Text(pDX, IDC_EDIT_MSG, m_strMsgCont); 
	DDX_Check(pDX, IDC_CHECK_SHOW_MSG, m_bIsShowMsg); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CHWCmppAPIDlg, CDialog) 
	//{{AFX_MSG_MAP(CHWCmppAPIDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(ID_CONNECT, OnStart) 
	ON_WM_TIMER() 
	ON_BN_CLICKED(ID_DisConnect, OnStop) 
	ON_WM_CLOSE() 
	ON_BN_CLICKED(IDC_BUTTON_INIT, OnButtonInit) 
	ON_BN_CLICKED(IDC_CHECK_SHOW_MSG, OnCheckShowMsg) 
	//}}AFX_MSG_MAP 
	ON_MESSAGE(WM_USER+600,IncreaseSendProcessNum) 
	ON_MESSAGE(WM_USER+601,IncreaseRecvProcessNum) 
	ON_MESSAGE(WM_USER+605,ShowMessage)// 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CHWCmppAPIDlg message handlers 
BOOL CHWCmppAPIDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// Add "About..." menu item to system menu. 
 
	// IDM_ABOUTBOX must be in the system command range. 
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
	ASSERT(IDM_ABOUTBOX < 0xF000); 
 
	CMenu* pSysMenu = GetSystemMenu(FALSE); 
	if (pSysMenu != NULL) 
	{ 
		CString strAboutMenu; 
		strAboutMenu.LoadString(IDS_ABOUTBOX); 
		if (!strAboutMenu.IsEmpty()) 
		{ 
			pSysMenu->AppendMenu(MF_SEPARATOR); 
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
		} 
	} 
 
	// Set the icon for this dialog.  The framework does this automatically 
	//  when the application's main window is not a dialog 
	SetIcon(m_hIcon, TRUE);			// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
	 
	// TODO: Add extra initialization here 
 
	CString strTitle; 
	char chBuf[50]; 
	this->GetWindowText(chBuf, 50); 
	strTitle = chBuf; 
	strTitle += "   启动时间:"; 
	CTime tm = CTime::GetCurrentTime(); 
	strTitle += tm.Format("%Y-%m-%d %H:%M"); 
	this->SetWindowText(strTitle); 
 
 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CHWCmppAPIDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		CDialog::OnSysCommand(nID, lParam); 
	} 
} 
 
// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 
 
void CHWCmppAPIDlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
		// Center icon in client rectangle 
		int cxIcon = GetSystemMetrics(SM_CXICON); 
		int cyIcon = GetSystemMetrics(SM_CYICON); 
		CRect rect; 
		GetClientRect(&rect); 
		int x = (rect.Width() - cxIcon + 1) / 2; 
		int y = (rect.Height() - cyIcon + 1) / 2; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CHWCmppAPIDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CHWCmppAPIDlg::LoadParam() 
{ 
	char buf[_MAX_PATH]; 
	memset (buf, 0, sizeof(buf)); 
	GetPrivateProfileString("Queue", "InQueue", "DIRECT=OS:Airlink\\private$\\p20up",buf, _MAX_PATH, m_strIniFilename); 
	m_strQueueIn = buf; 
	GetPrivateProfileString("Queue", "OutQueue", "DIRECT=OS:Airlink\\private$\\p20down", buf, _MAX_PATH, m_strIniFilename); 
	m_strQueueOut = buf; 
	GetPrivateProfileString("Interface", "ShowCheck", "1", buf, 10, m_strIniFilename); 
	m_bIsShowMsg = (BOOL)atoi(buf);	 
 
	GetPrivateProfileString("CMPP", "ServerIP", "", m_chServerIP, sizeof(m_chServerIP), m_strIniFilename); 
	GetPrivateProfileString("CMPP", "ServerPort", "7890", buf, 20, m_strIniFilename); 
	m_nServerPort = atoi(buf); 
	GetPrivateProfileString("CMPP", "SystemID", "", m_chIcpId, sizeof(m_chIcpId), m_strIniFilename); 
	GetPrivateProfileString("CMPP", "Password", "", m_chIcpAuth, sizeof(m_chIcpAuth), m_strIniFilename); 
	GetPrivateProfileString("CMPP", "SUBMITWINDOW", "1",			 buf		,	10, m_strIniFilename); 
	m_nSubmitWindow = atoi(buf); 
 
	GetPrivateProfileString("Config", "SendTimer", "10",			 buf		,	10, m_strIniFilename); 
	m_nSendTimer = atoi(buf); 
	GetPrivateProfileString("Config", "RecvTimer", "10",			 buf		,	10, m_strIniFilename); 
	m_nRecvTimer = atoi(buf); 
	GetPrivateProfileString("Config", "ActiveTestTimer", "60000",			 buf		,	10, m_strIniFilename); 
	m_nActiveTestTimer = atoi(buf); 
	GetPrivateProfileString("Config", "ReLoginTimer", "20000",			 buf		,	10, m_strIniFilename); 
	m_nReLoginTimer = atoi(buf);	 
	GetPrivateProfileString("Config", "RestartTimer", "720000000",			 buf		,	10, m_strIniFilename);//小于一小时则关闭此功能 
	m_nRestartTimer = atoi(buf); 
 
	GetPrivateProfileString("Config", "SendThreadNum", "1",			 buf		,	10, m_strIniFilename);//小于一小时则关闭此功能 
	m_nSendThreadNum = atoi(buf); 
	if(m_nSendThreadNum > 1000){ 
		AfxMessageBox("发送线程不能超过1000!最好小于100,现在将置为20!"); 
		m_nSendThreadNum =20; 
	} 
	GetPrivateProfileString("Config", "RecvThreadNum", "1",			 buf		,	10, m_strIniFilename);//小于一小时则关闭此功能 
	m_nRecvThreadNum = atoi(buf); 
	if(m_nRecvThreadNum > 10){ 
		AfxMessageBox("接收线程不能超过10!将置为1!"); 
		m_nRecvThreadNum =1; 
	} 
	GetPrivateProfileString("Debug", "SystenFilename", "SysLog.txt",			 buf		,	10, m_strIniFilename);//小于一小时则关闭此功能 
	::g_strSystemLog = buf; 
} 
 
void CHWCmppAPIDlg::SaveParam() 
{ 
	char buf[32]; 
 
	WritePrivateProfileString("Queue", "InQueue", m_strQueueIn, m_strIniFilename); 
	WritePrivateProfileString("Queue", "OutQueue", m_strQueueOut, m_strIniFilename); 
	if(m_bIsShowMsg == TRUE){ 
		WritePrivateProfileString("Interface", "ShowCheck", "1",m_strIniFilename); 
	} 
	else{ 
		WritePrivateProfileString("Interface", "ShowCheck", "0", m_strIniFilename); 
	} 
 
	WritePrivateProfileString("CMPP", "ServerIP",		m_chServerIP,	 m_strIniFilename); 
	itoa(m_nServerPort, buf,10); 
	WritePrivateProfileString("CMPP", "ServerPort",		buf,			 m_strIniFilename); 
	WritePrivateProfileString("CMPP", "SystemID",			m_chIcpId,		 m_strIniFilename); 
	WritePrivateProfileString("CMPP", "Password",		m_chIcpAuth,	 m_strIniFilename); 
	itoa(m_nSubmitWindow, buf,10); 
	WritePrivateProfileString("CMPP", "SUBMITWINDOW",	buf ,			 m_strIniFilename); 
	 
	itoa(m_nSendTimer, buf,10); 
	WritePrivateProfileString("Config", "SendTimer",	buf ,			 m_strIniFilename); 
	itoa(m_nRecvTimer , buf,10); 
	WritePrivateProfileString("Config", "RecvTimer",	buf ,			 m_strIniFilename); 
	itoa(m_nActiveTestTimer, buf,10); 
	WritePrivateProfileString("Config", "ActiveTestTimer",	buf ,			 m_strIniFilename); 
	itoa(m_nReLoginTimer, buf,10); 
	WritePrivateProfileString("Config", "ReLoginTimer",	buf ,			 m_strIniFilename); 
	itoa(m_nRestartTimer, buf,10); 
	WritePrivateProfileString("Config", "RestartTimer",	buf ,			 m_strIniFilename); 
 
	itoa(m_nSendThreadNum, buf,10); 
	WritePrivateProfileString("Config", "SendThreadNum",	buf ,			 m_strIniFilename); 
	itoa(m_nRecvThreadNum, buf,10); 
	WritePrivateProfileString("Config", "RecvThreadNum",	buf ,			 m_strIniFilename); 
 
	WritePrivateProfileString("Debug", "SystenFilename",::g_strSystemLog, m_strIniFilename);//小于一小时则关闭此功能 
} 
 
 
void CHWCmppAPIDlg::UpdateInterface() 
{ 
	this->m_strMONum.Format("%d", this->m_nMONum); 
	this->m_strMTNum.Format("%d", this->m_nMTNum); 
	this->m_strRTNum.Format("%d", this->m_nRTNum); 
	this->m_strRPNum.Format("%d", this->m_nRPNum); 
	UpdateData(FALSE); 
} 
 
void CHWCmppAPIDlg::OnClose()  
{ 
	// TODO: Add your message handler code here and/or call default 
	SaveParam();	 
	CDialog::OnClose(); 
} 
 
 
void CHWCmppAPIDlg::OnButtonInit()  
{ 
	// TODO: Add your control notification handler code here 
    UpdateData(TRUE); 
	//初始化接口 
/*	CString sConnectPara; 
	sConnectPara.Format("%s %d 5000", m_chServerIP, m_nServerPort); //"ip port delaytime" 
	if(IFInitInterface(5, 1, sConnectPara) == TRUE) 
	{ 
		ShowMsg("初始化套接字成功!"); 
		GetDlgItem(IDC_BUTTON_INIT)->EnableWindow(FALSE);	 
	} 
	else 
	{ 
		ShowMsg("初始化套接字失败!"); 
	} 
*/ 
} 
 
 
void CHWCmppAPIDlg::OnTimer(UINT nIDEvent)  
{ 
	// TODO: Add your message handler code here and/or call default 
	switch(nIDEvent) 
	{ 
/*	case SendTimer: 
		{ 
		for(int	i=0; iRestart()"); 
			this->Restart(); 
			break; 
		} 
	case ReLoginTimer: 
		{ 
			::GfWriteSysLog("ReLoginTimer:-->OnStart()"); 
			this->OnStart();		 
			break; 
		} 
	case ReLogoutTimer: 
		{ 
			GfWriteSysLog("ReLogoutTimer:-->Logout()"); 
			this->LogoutISMG(); 
			break; 
		} 
 
	default: 
		break; 
	} 
	CDialog::OnTimer(nIDEvent); 
} 
 
 
void CHWCmppAPIDlg::IncreaseSendProcessNum(WPARAM w,LPARAM l)  
{ 
	if(int(w) == 1) 
	{ 
		InterlockedIncrement((long*)&this->m_nMTNum); 
		this->m_strMTNum.Format("%d", this->m_nMTNum); 
	} 
	if(int(l) == 1) 
	{ 
		m_bySendFailNum = 0; 
		InterlockedIncrement((long*)&this->m_nRTNum); 
		this->m_strRTNum.Format("%d", this->m_nRTNum); 
	} 
 
	if(int(w)==1 && int(l) == 0)//Send Fail 
	{ 
		this->m_bySendFailNum ++; 
		if(m_bySendFailNum >= 10) 
		{ 
			m_bySendFailNum = 0; 
			if(m_bIsRestarting == FALSE) 
			{ 
				m_bIsRestarting = TRUE; 
				this->ShowMsg("发送失败连续超过10次,将自动重启!"); 
				::GfWriteSysLog("发送失败连续超过10次,将自动重启!"); 
				this->Restart(); 
			} 
		} 
	} 
	UpdateData(FALSE); 
} 
 
void CHWCmppAPIDlg::IncreaseRecvProcessNum(WPARAM w,LPARAM l)  
{ 
	if(int(w) == 1) 
	{ 
		InterlockedIncrement((long*)&this->m_nMONum); 
		this->m_strMONum.Format("%d", this->m_nMONum); 
	} 
	if(int(l) == 1){ 
		InterlockedIncrement((long*)&this->m_nRPNum); 
		this->m_strRPNum.Format("%d", this->m_nRPNum); 
	} 
	UpdateData(FALSE); 
} 
 
void CHWCmppAPIDlg::ShowMsg(CString strMsg) 
{ 
	m_strMsg = strMsg; 
	this->SendMessage(WM_USER+605, 2, 0); 
} 
 
void CHWCmppAPIDlg::ShowMessage(WPARAM w,LPARAM l)  
{ 
	if((int)w == 1)//send thread 
	{ 
		CString strMsg = this->m_pSendThread[l]->m_strMsg; 
		if(m_strMsgCont.GetLength()>10000){ 
			m_strMsgCont.Empty(); 
		} 
		m_strMsgCont = strMsg+"\r\n"+m_strMsgCont; 
		UpdateData(FALSE); 
	 
	} 
	else if((int)w == 0)//recv thread 
	{ 
		CString strMsg = this->m_pRecvThread[l]->m_strMsg; 
		if(m_strMsgCont.GetLength()>10000){ 
			m_strMsgCont.Empty(); 
		} 
		m_strMsgCont = strMsg+"\r\n"+m_strMsgCont; 
		UpdateData(FALSE); 
	} 
	else if((int)w == 2)//Dialog thread 
	{ 
		CString strMsg = this->m_strMsg; 
		if(m_strMsgCont.GetLength()>10000){ 
			m_strMsgCont.Empty(); 
		} 
		m_strMsgCont = strMsg+"\r\n"+m_strMsgCont; 
		UpdateData(FALSE); 
	} 
} 
//断开重连 
void CHWCmppAPIDlg::Restart() 
{ 
	ShowMsg("Restart()..."); 
	::GfWriteSysLog("Restart()....."); 
	this->OnStop(); 
	this->ShowMsg("Wait for 30 second......"); 
	UpdateData(FALSE); 
	::Sleep(5000); 
//	this->LogoutISMG(); 
	this->m_bIsLog = FALSE; 
	OnStart(); 
} 
 
BYTE CHWCmppAPIDlg::LoginISMG() 
{ 
	if(m_bIsLog == TRUE){//已经登陆 
		return 2; 
	}	 
 
	GfWriteSysLog("将要初始化套接字、并登陆!"); 
	try 
	{ 
		KillTimer(ReLoginTimer); 
		ShowMsg(m_strConnectPara); 
		if(IFInitInterface(5, 1, m_strConnectPara) == TRUE) 
		{ 
			ShowMsg("初始化套接字成功!"); 
			GfWriteSysLog("初始化套接字成功!"); 
		} 
		else 
		{ 
			SetTimer(ReLoginTimer, this->m_nReLoginTimer, NULL); 
			ShowMsg("初始化套接字失败!"); 
			GfWriteSysLog("初始化套接字失败!"); 
			return 2; 
		} 
		 
 
		if(Login_R(m_chIcpId, m_chIcpAuth) == TRUE)//登陆成功 
		{ 
			m_bIsLog = TRUE; 
			 
			this->OnStart();//启动收发线程 
			m_bIsRestarting = FALSE; 
			 
			ShowMsg("登陆网关成功!"); 
			::GfWriteSysLog("LoginISMG():登陆网关成功!"); 
			return 0; 
		} 
 
		else//登陆失败 
		{ 
			SetTimer(ReLoginTimer, this->m_nReLoginTimer, NULL); 
 
			CString strMsg; 
			strMsg.Format("User:%s, Pasw:%s", m_chIcpId, m_chIcpAuth); 
			ShowMsg("登陆失败!正在重连..."+strMsg); 
			::GfWriteSysLog("LoginISMG():登陆失败!启动重连定时..."); 
			return 1; 
		} 
	} 
	catch(...) 
	{ 
		SetTimer(ReLoginTimer, this->m_nReLoginTimer, NULL); 
		GfWriteSysLog("初始化套接字、登陆失败!, 设置定时器自动重启。"); 
		return 5; 
	} 
} 
 
BYTE CHWCmppAPIDlg::LogoutISMG() 
{ 
	if(m_bIsLog != TRUE){//没登陆返回 
		return 2; 
	} 
 
	KillTimer(ReLogoutTimer);		 
	GfWriteSysLog("LogoutISMG(): 准备注销"); 
	if(Logout()) 
	{ 
		m_bIsLog = FALSE;	 
	//	this->OnStop();//停止收发线程 
 
		ShowMsg("通信组件注销成功!!可以重新连接或退出程序。");	 
		GfWriteSysLog("通信组件注销成功!!可以重新连接或退出程序。"); 
		return 0; 
	} 
 
	else  
	{ 
	//	SetTimer(ReLogoutTimer, 30000, NULL); 
 
	//	ShowMsg("通信组件注销失败!启动自动注销,1分钟后重试..."); 
		GfWriteSysLog("通信组件注销失败!"); 
		return 1; 
	} 
	 
} 
 
void CHWCmppAPIDlg::OnStart() 
{ 
	UpdateData(); 
	if(m_bIsLog == FALSE) 
	{ 
		if(LoginISMG() != 0) 
		{ 
			return; 
		} 
	} 
 
 
	//创建发送、接收线程对象 
	CString str;	 
	for(int i=0;im_nRecvThreadNum;i++) 
	{ 
		if(!m_pRecvThread[i]) 
		{ 
			m_pRecvThread[i] = (CRecvThread*)AfxBeginThread(RUNTIME_CLASS(CRecvThread),THREAD_PRIORITY_NORMAL); 
			str.Format("创建接收线程 %2d  OK!", i+1); 
			ShowMsg(str); 
			GfWriteSysLog(str); 
		} 
		if(m_pRecvThread[i]) 
		{ 
			if(m_pRecvThread[i]->m_bIfRun == TRUE){ 
				continue; 
			} 
			if(m_pRecvThread[i]->Init(i, this->m_strQueueIn, this->m_strQueueOut, this)) 
			{ 
				::Sleep(10); 
				m_pRecvThread[i]->Start(); 
			} 
			else 
			{ 
				CString str; 
				str.Format("创建接收线程%2d 失败,请检查队列配置!", i+1); 
				ShowMsg(str); 
			} 
		} 
	} 
 
	for(i=0;im_nSendThreadNum;i++) 
	{ 
		if(!m_pSendThread[i]) 
		{ 
			m_pSendThread[i] = (CSendThread*)AfxBeginThread(RUNTIME_CLASS(CSendThread),THREAD_PRIORITY_NORMAL); 
			str.Format("创建发送线程 %2d  OK!", i+1); 
			ShowMsg(str); 
			GfWriteSysLog(str); 
		} 
		if(m_pSendThread[i]) 
		{ 
			if(m_pSendThread[i]->m_bIfRun == TRUE){ 
				continue; 
			} 
			if(m_pSendThread[i]->Init(i,this->m_strQueueIn, this->m_strQueueOut, this->m_chIcpId, this)) 
			{ 
				::Sleep(10); 
				m_pSendThread[i]->Start(); 
			} 
			else 
			{ 
				CString str; 
				str.Format("创建发送线程%2d 失败,请检查队列配置", i+1); 
				ShowMsg(str); 
			} 
		} 
	} 
	str.Format("OnStart(): 启动收发线程!"); 
	ShowMsg(str); 
	GfWriteSysLog(str); 
 
	m_bIsStart = TRUE; 
	SetTimer(RefreshTimer, 30000, NULL); 
	SetTimer(RestartTimer, this->m_nRestartTimer, NULL); 
} 
 
//停止读写线程 
void CHWCmppAPIDlg::OnStop()  
{ 
	// TODO: Add your control notification handler code here 
	if(m_bIsStart != TRUE){//若没有启动读写线程,返回 
		return; 
	} 
 
	//杀死读写线程 
	for(int i=0;im_nSendThreadNum;i++) 
	{ 
		if(m_pSendThread[i]!= NULL) 
		{ 
			m_pSendThread[i]->Stop(); 
	//		::Sleep(100); 
	//		m_pSendThread[i]->QuitThread(); 
	//		m_pSendThread[i] = NULL; 
		} 
	} 
 
	for(i=0;im_nRecvThreadNum;i++) 
	{ 
		if(m_pRecvThread[i]!= NULL) 
		{ 
			m_pRecvThread[i]->Stop(); 
	//		::Sleep(100); 
	//		m_pRecvThread[i]->QuitThread(); 
	//		m_pRecvThread[i] = NULL;			 
		} 
	} 
 
	CString str; 
	str.Format("停止收发线程!"); 
	ShowMsg("OnStop(): 停止收发线程!"); 
	::GfWriteSysLog("OnStop():停止收发线程!"); 
	UpdateData(FALSE); 
 
	::Sleep(1000); 
	m_bIsStart = FALSE; 
	//定时器处理Logout 
//	KillTimer(RefreshTimer); 
} 
 
void CHWCmppAPIDlg::OnCheckShowMsg()  
{ 
	UpdateData(); 
	BOOL bIsShow = this->m_bIsShowMsg; 
 
	for(int i=0;im_nRecvThreadNum;i++) 
	{ 
		if(m_pRecvThread[i]) 
		{ 
			m_pRecvThread[i]->SetIfShowMsg(bIsShow); 
		} 
 
	} 
	for(i=0;im_nSendThreadNum;i++) 
	{ 
		if(m_pSendThread[i]) 
		{ 
			m_pSendThread[i]->SetIfShowMsg(bIsShow); 
		} 
	} 
} 
 
BOOL CHWCmppAPIDlg::DestroyWindow()  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	for(int i=0;im_nRecvThreadNum;i++) 
	{ 
		if(m_pRecvThread[i]) 
		{ 
			m_pRecvThread[i]->PostThreadMessage(WM_QUIT,0,0); 
		} 
 
	} 
	for(i=0;im_nSendThreadNum;i++) 
	{ 
		if(m_pSendThread[i]) 
		{ 
			m_pSendThread[i]->PostThreadMessage(WM_QUIT,0,0); 
		} 
	} 
	LogoutISMG(); 
	return CDialog::DestroyWindow(); 
}