📄 hwcmppapidlg.cpp
字号:
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; i<m_nSubmitWindow; i++)
{
// FecthMsgSend();
}
break;
}
case RecvTimer:
// if(HasDeliverMessage(5)){
// RecvMsgToQueue();
// }
break;
*/
case RefreshTimer:
{
int ret;
CMMPActiveTest(ret);
break;
}
case RestartTimer:
{
::GfWriteSysLog("RestartTimer:-->Restart()");
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;i<this->m_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;i<this->m_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;i<this->m_nSendThreadNum;i++)
{
if(m_pSendThread[i]!= NULL)
{
m_pSendThread[i]->Stop();
// ::Sleep(100);
// m_pSendThread[i]->QuitThread();
// m_pSendThread[i] = NULL;
}
}
for(i=0;i<this->m_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;i<this->m_nRecvThreadNum;i++)
{
if(m_pRecvThread[i])
{
m_pRecvThread[i]->SetIfShowMsg(bIsShow);
}
}
for(i=0;i<this->m_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;i<this->m_nRecvThreadNum;i++)
{
if(m_pRecvThread[i])
{
m_pRecvThread[i]->PostThreadMessage(WM_QUIT,0,0);
}
}
for(i=0;i<this->m_nSendThreadNum;i++)
{
if(m_pSendThread[i])
{
m_pSendThread[i]->PostThreadMessage(WM_QUIT,0,0);
}
}
LogoutISMG();
return CDialog::DestroyWindow();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -