📄 thread1.cpp
字号:
// Thread1.cpp : implementation file
//
#include "stdafx.h"
#include "MyThread.h"
#include "Thread1.h"
#include <afxmt.h>
#include "smgpapi.h"
#include "smgpdef.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//_ConnectionPtr m_connection;
/////////////////////////////////////////////////////////////////////////////
// Thread1
IMPLEMENT_DYNCREATE(Thread1, CWinThread)
Thread1::Thread1()
{
m_stop_state=false;
m_active1=false;
m_pDlg=NULL;
}
Thread1::~Thread1()
{
}
BOOL Thread1::InitInstance()
{
// TODO: perform and per-thread initialization here
return TRUE;
}
int Thread1::ExitInstance()
{
// TODO: perform any per-thread cleanup here
return CWinThread::ExitInstance();
}
BEGIN_MESSAGE_MAP(Thread1, CWinThread)
//{{AFX_MSG_MAP(Thread1)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// Thread1 message handlers
void Thread1::GetOwner(CMyThreadDlg *m_pOwner,Thread3* pThread3)
{
m_pDlg=m_pOwner;
m_thread3=pThread3;
}
int Thread1::Run()
{
CString phone,content,srcPhone,chargetermid,command,buffer,service_id,service,feeType,fixedFee,feeCode,nowtime;
FILE*file;
CString result;
CTime t;
/* Semd Msg Msg Id */
char g_sMsgID[11];
/* Send Msg Error Num */
int g_nErrorCode = 0xff,nNeedReply=0,dberr=0;
char sValidTime[18]; /* 17 bytes */
char sAtTime[18];
char f_name[30];
int m_second=0;
char time1[20];
int msgtype=0,contentType=0,saveType=0,isdbConnect=0,send_speed=0;
memset(sValidTime,0,18);
memset(sAtTime,0,18);
try
{
m_recordset.CreateInstance("ADODB.Recordset");
//other_recordset.CreateInstance("ADODB.Recordset");
}
catch(_com_error e)
{
m_pDlg->AddString("Thread1创建数据集失败!");
return FALSE;
}
//////////////////////
int res=-1;
isdbConnect=1;
while(!m_active1)
//while(!m_stop_state)
{
m_pDlg->g_cs.Lock();
m_active1=m_pDlg->m_active;
m_pDlg->g_cs.Unlock();
if(!m_stop_state) Sleep(1000);
else break;
}
m_pDlg->AddString("Thread1 开始");
switch(m_pDlg->send_speed)
{
case 1:
send_speed=70;//14/s
break;
case 2:
send_speed=100;//10/s
break;
case 3:
send_speed=170;//6/s
break;
default:
send_speed=70;//14/s
break;
}
while(!m_stop_state)
{
try
{
//m_recordset=m_pDlg->m_connection->Execute("SELECT convert(varchar(20),getdate()) nowtime,datepart(second,getdate()) m_second ",&RecordsAffected,adCmdText);
hr=m_recordset->Open("SELECT convert(varchar(20),getdate()) nowtime,datepart(second,getdate()) m_second ", _variant_t((IDispatch*)m_pDlg->m_connection,true),adOpenStatic,adLockOptimistic,adCmdText);
if(!m_recordset->adoEOF)//取时间
{
nowtime.Empty();
m_second=0;
if(m_recordset->GetCollect("nowtime").vt == VT_NULL) {m_recordset->Close();continue;}
nowtime=m_recordset->GetCollect("nowtime").bstrVal;
if(m_recordset->GetCollect("m_second").vt != VT_NULL) m_second=m_recordset->GetCollect("m_second").lVal;
m_recordset->Close();
}else{m_recordset->Close();continue;}
if(isdbConnect==1)
{
m_pDlg->g_cs.Lock();
m_pDlg->db_active=true;//数据库连接正常
m_pDlg->g_cs.Unlock();
isdbConnect=0;
}
m_pDlg->g_cs.Lock();
m_active1=m_pDlg->m_active;//网络连接
m_pDlg->g_cs.Unlock();
if(!m_active1){Sleep(5000);continue;}
command.Empty();
command.Format("SELECT *FROM T_gwdx_send_xlt where time< dateadd(second,%d,convert(datetime,'%s'))",m_second,nowtime);
hr=m_recordset->Open(command.GetBuffer(0), _variant_t((IDispatch*)m_pDlg->m_connection,true),adOpenStatic,adLockOptimistic,adCmdText);
//m_recordset=m_pDlg->m_connection->Execute(command.GetBuffer(0),&RecordsAffected,adCmdText);
if(!m_recordset->adoEOF)/// m_pDlg->AddString("读取数据ok");
{
dberr=0;
while(!m_recordset->adoEOF)
{
Sleep(send_speed);
if(m_recordset->GetCollect("nNeedReply").vt != VT_NULL)nNeedReply=m_recordset->GetCollect("nNeedReply").lVal;
if(m_recordset->GetCollect("msgtype").vt != VT_NULL)msgtype=m_recordset->GetCollect("msgtype").lVal;
if(m_recordset->GetCollect("contentType").vt != VT_NULL)contentType=m_recordset->GetCollect("contentType").lVal;
service_id.Empty();
if(m_recordset->GetCollect("service_id").vt != VT_NULL)service_id=m_recordset->GetCollect("service_id").bstrVal;
service.Empty();
if(m_recordset->GetCollect("service").vt != VT_NULL)service=m_recordset->GetCollect("service").bstrVal;
feeType.Empty();
if(m_recordset->GetCollect("feetype").vt != VT_NULL)feeType=m_recordset->GetCollect("feetype").bstrVal;
fixedFee.Empty();
if(m_recordset->GetCollect("fixedFee").vt != VT_NULL)fixedFee=m_recordset->GetCollect("fixedFee").bstrVal;
feeCode.Empty();
if(m_recordset->GetCollect("feeCode").vt != VT_NULL) feeCode=m_recordset->GetCollect("feeCode").bstrVal;
srcPhone.Empty();
if(m_recordset->GetCollect("srctermid").vt != VT_NULL)srcPhone=m_recordset->GetCollect("srctermid").bstrVal;
phone.Empty();
if(m_recordset->GetCollect("desttermid").vt != VT_NULL)phone=m_recordset->GetCollect("desttermid").bstrVal;
chargetermid.Empty();
if(m_recordset->GetCollect("chargetermid").vt != VT_NULL)chargetermid=m_recordset->GetCollect("chargetermid").bstrVal;
content.Empty();
if(m_recordset->GetCollect("msgcontent").vt != VT_NULL)content=m_recordset->GetCollect("msgcontent").bstrVal;
if((srcPhone.Mid(0,4)!="0898")&&(srcPhone.Mid(0,3)!="118"))srcPhone="118"+srcPhone;
if(service_id==""&&service==""&&feeType==""&&fixedFee==""&&feeCode==""&&srcPhone==""&&phone==""&&chargetermid==""&&content=="")
{
dberr++;
if(dberr>10) break;
}
/////////
int i=0;
while(i<4)
{
g_nErrorCode=0;
memset(g_sMsgID,0,11);
if(srcPhone==""||phone==""||chargetermid=="") res=10;
else
{
nNeedReply=1;
msgtype=6;
res=SMGPSendSingle( nNeedReply,SUBMIT_PRIORITY_ABOVE_NORMAL,service_id.GetBuffer(0),MSG_FORMAT_GB,feeType.GetBuffer(0),
feeCode.GetBuffer(0),fixedFee.GetBuffer(0),sValidTime,sAtTime,chargetermid.GetBuffer(0),phone.GetBuffer(0),
srcPhone.GetBuffer(0),content.GetLength(),content.GetBuffer(0),g_sMsgID,&g_nErrorCode,msgtype);
}
//res=0;
if(res!=0)
{
if(g_nErrorCode!=20&&g_nErrorCode!=99&&g_nErrorCode!=1&&g_nErrorCode!=8)
//if(g_nErrorCode!=20&&g_nErrorCode!=99&&g_nErrorCode!=1&&g_nErrorCode!=8&&g_nErrorCode!=128)
{
i=3;
if((g_nErrorCode==21)&&(!m_stop_state)){i=0;Sleep(60000);}//登录失败
if((g_nErrorCode==51)&&(feeType=="04"))//余额不足
{
command.Empty();
command.Format("update T_gwdx_subscribe set subTime=dateadd(day,5,getdate()) where srcPhone='%s' and service_id+'BY'='%s'",chargetermid,service_id);
m_pDlg->m_connection->Execute(command.GetBuffer(0),&RecordsAffected,adCmdText);
}
}
else
{
CString result;
result.Format("Thread1 发送失败! Err=%d,res=%d",g_nErrorCode,res);
m_pDlg->AddString((char*)result.GetBuffer(0));
m_pDlg->g_cs.Lock();
m_pDlg->m_active=false;
m_pDlg->g_cs.Unlock();
Sleep(10000);
}
}
else
{
t = CTime::GetCurrentTime();
memset(time1,0,20);
sprintf(time1,"%d%02d%02d%02d%02d%02d",t.GetYear(),t.GetMonth(),t.GetDay(),t.GetHour(),t.GetMinute(),t.GetSecond());
if(contentType==1)
{
command.Empty();
command.Format("insert into T_gwdx_smsTime values('%s','%s','%s','0',getdate())",phone,srcPhone,service_id);
m_pDlg->m_connection->Execute(command.GetBuffer(0),&RecordsAffected,adCmdText);
}
if(contentType==2)
{
command.Empty();
command.Format("insert into T_gwdx_HttpGW values('%s','%s',null,'%s','http://192.168.60.17/sms_new/process/contentTypeIs2.asp','%s','%s',getdate(),'1')",srcPhone,phone,service_id,content,time1);
m_pDlg->m_connection->Execute(command.GetBuffer(0),&RecordsAffected,adCmdText);
}
memset(f_name,0,30);
sprintf(f_name,"log/1Thread%d%02d%02dok.txt",t.GetYear(),t.GetMonth(),t.GetDay());
buffer.Empty();
buffer.Format("%s,%s,%s,%s,%s,%s,%s,%s,%s\n",time1,feeType,fixedFee,feeCode,service_id,chargetermid,srcPhone,phone,content);
if((file=fopen(f_name,"a"))==NULL) break;
fwrite(buffer.GetBuffer(0),sizeof(char),buffer.GetLength(),file);
fclose(file);
break;
}
i++;
if(i>=4)
{
t = CTime::GetCurrentTime();
memset(time1,0,20);
sprintf(time1,"%d%02d%02d%02d%02d%02d",t.GetYear(),t.GetMonth(),t.GetDay(),t.GetHour(),t.GetMinute(),t.GetSecond());
memset(f_name,0,30);
sprintf(f_name,"log/1Thread%d%02d%02dfail.txt",t.GetYear(),t.GetMonth(),t.GetDay());
buffer.Empty();
buffer.Format("%s,%s,%s,%s,%s,%s,%s,%s,%s,%d\n",time1,feeType,fixedFee,feeCode,service_id,chargetermid,srcPhone,phone,content,g_nErrorCode);
if((file=fopen(f_name,"a"))==NULL) break;
fwrite(buffer.GetBuffer(0),sizeof(char),buffer.GetLength(),file);
fclose(file);
}
}//while i
m_recordset->MoveNext();
}//end while !m_recordset->adoEOF
m_recordset->Close();
command.Empty();
command.Format("delete from T_gwdx_send_xlt where time< dateadd(second,%d,convert(datetime,'%s'))",m_second,nowtime);
hr=m_recordset->Open(command.GetBuffer(0), _variant_t((IDispatch*)m_pDlg->m_connection,true),adOpenStatic,adLockOptimistic,adCmdText);
//m_pDlg->m_connection->Execute(command.GetBuffer(0),&RecordsAffected,adCmdText);
Sleep(100);
}//有数据
else
{
m_recordset->Close();
Sleep(5000);
}
}//end try
catch(_com_error e)
{
m_pDlg->AddString("Thread1操作数据库失败!");
m_pDlg->g_cs.Lock();
m_pDlg->db_active=false;
m_pDlg->g_cs.Unlock();
isdbConnect=1;
Sleep(1000);
if(m_pDlg->m_connection->State)m_pDlg->m_connection->Close();
if(m_recordset->State)m_recordset->Close();
Sleep(4000);
m_pDlg->ConnectDB();
}
}//线程while
PreStop();
m_pDlg->AddString("thread1退出");
return 0;
}
BOOL Thread1::PreStop()
{
try
{
if (m_pDlg->m_connection->State) m_pDlg->m_connection->Close();
if(m_recordset->State)m_recordset->Close();
m_pDlg->m_connection=NULL;
m_recordset=NULL;
m_pDlg->AddString("Thread1关闭数据库");
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("thread1关闭数据库失败!\r\n错误信息:%s",e.ErrorMessage());
m_pDlg->AddString(errormessage.GetBuffer(0));
return false;
}
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -