⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 thread2.cpp

📁 电信短信应用smgp1.3协议网关,中兴开发包
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Thread2.cpp : implementation file
//

#include "stdafx.h"
#include "MyThread.h"
#include "Thread2.h"
#include <afxmt.h>
#include "smgpapi.h"
#include "smgpdef.h"
#include ".\thread2.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define RECV_MSG_TIME_OUT						1 /* Seconds */	
/////////////////////////////////////////////////////////////////////////////
// Thread2

IMPLEMENT_DYNCREATE(Thread2, CWinThread)

Thread2::Thread2()
{
		m_stop_state=false;//线程停止标志
		m_active1=false;
		m_pDlg=NULL;
		m_thread3=NULL;
		m_thread1=NULL;
		m_connection=NULL;
}

Thread2::~Thread2()
{
}

BOOL Thread2::InitInstance()
{
	// TODO:  perform and per-thread initialization here
	return TRUE;
}

int Thread2::ExitInstance()
{
	// TODO:  perform any per-thread cleanup here
	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(Thread2, CWinThread)
	//{{AFX_MSG_MAP(Thread2)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// Thread2 message handlers

void Thread2::GetOwner(CMyThreadDlg *m_pOwner,Thread3* pThread3,Thread1*pThread1)
{
	m_pDlg=m_pOwner;
	m_thread3=pThread3;
	m_thread1=pThread1;
}

int Thread2::Run()
{
	int res=-1,i=0,j=0,k=0,nRetCode=0,isdbConnect=1,rec_command=0;//命令无法匹配时rec_command=0;
	CTime t,t1;
	CTimeSpan ts;
	DeliverResp m_del;
	CString command;
	CString destPhone,content,content1,server,srcPhone,buffer,serviceID,service,feeType,fixedFee,server_table,commandType,dbTemp,content_err,flag,flag_1;
	FILE*file;
	char f_name[30], serviceTemp[23],time1[17],m_http[8];//服务代码
	WdwAdd sybase_command[100];
	memset(sybase_command,0,sizeof(WdwAdd)*100);
	t1 = CTime::GetCurrentTime();
	//while(!m_stop_state)//
	while(!m_active1)
	{
	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("开始Thread2");
	try
	{
			m_recordset.CreateInstance("ADODB.Recordset");
	}
	catch(_com_error e)
 	{
		m_pDlg->AddString("Thread2创建数据集失败!");
 		return FALSE;
 	}

	while(!m_stop_state)
	{
		if(isdbConnect==1)
		{
			m_active1=false;
			m_pDlg->g_cs.Lock();
			m_active1=m_pDlg->db_active;
			m_pDlg->g_cs.Unlock();
			if(!m_active1){Sleep(5000);continue;}
			isdbConnect=0;
		}
		memset(&m_del,0,sizeof(DeliverResp));
		memset(f_name,0,30);
		nRetCode = SMGPDeliver((int)RECV_MSG_TIME_OUT,&m_del);
		if(nRetCode)
		{
			/* You Can Process Error Here */
			Sleep(50);
			continue;
		}
		/* Post Message To Application Thread */
		else 
		{
			t = CTime::GetCurrentTime();
			res=1;
			if(m_del.nIsReport==1)  res=4;
				//Sleep(10);
			if(strcmp(m_del.sSrcTermID,"1186168")==0) res=4;
			if(res==1)
			{
				memset(time1,0,17);
				sprintf(time1,"%d%02d%02d%02d%02d%02d",t.GetYear(),t.GetMonth(),t.GetDay(),t.GetHour(),t.GetMinute(),t.GetSecond());	
				srcPhone.Empty();
				srcPhone=srcPhone+m_del.sSrcTermID;
				destPhone.Empty();
				destPhone=destPhone+m_del.sDestTermID;
				if(destPhone.Mid(0,3).Compare("118")==0) destPhone=destPhone.Mid(3,destPhone.GetLength()-3);
				content.Empty();
				content=content+m_del.sMsgContent;
				content.TrimLeft();
				content.Replace("'","''");
				serviceID.Empty();
				memset(serviceTemp,0,23);
				if(content.GetLength()>20) i=20;else i=content.GetLength();
				memcpy(serviceTemp,content.GetBuffer(0),i);
				j=0;
				for(k=0;k<20;k++) //全角--半角
				{
					if((serviceTemp[k]==-95&&serviceTemp[k+1]==-95)||serviceTemp[k]==0){serviceTemp[k]=0; break;}//161是全角空格
					if(serviceTemp[k]==-93)
					{
						if(serviceTemp[k+1]!=0)serviceTemp[k]=(unsigned char)serviceTemp[k+1]-128;
						j=k+1;
						while(j<20&&serviceTemp[j]!=0) {serviceTemp[j]=serviceTemp[j+1];j++;}
					}
				}
				serviceTemp[10]=0;
				for(k=0;k<10;k++) //服务代码
				{
					if((serviceTemp[k]<'0')||(serviceTemp[k]>'9'))
					if((serviceTemp[k]<'A')||(serviceTemp[k]>'Z'))
					if((serviceTemp[k]<'a')||(serviceTemp[k]>'z'))
					{serviceTemp[k]=0x00;break;}
				}
				memcpy(serviceTemp,_strupr(serviceTemp),strlen(serviceTemp));//服务代码变大写
				try
				{	
					////////////隔一段时间更新命令表
					ts=t-t1;
					if(ts.GetTotalSeconds()>240&&rec_command==0)
					{
						if(m_pDlg->m_connection==NULL){Sleep(5000);continue;}
						hr=m_recordset->Open("select*from T_gwdx_service where server='0' and command is not null and command<>''", _variant_t((IDispatch*)m_pDlg->m_connection,true),adOpenStatic,adLockOptimistic,adCmdText);
						if(!m_recordset->adoEOF) 
							while((!m_recordset->adoEOF)&&(rec_command<99))
							{
								dbTemp.Empty();
								if(m_recordset->GetCollect("destId").vt != VT_NULL) dbTemp=m_recordset->GetCollect("destId").bstrVal;
								command.Empty();
								if(m_recordset->GetCollect("command").vt != VT_NULL) command=m_recordset->GetCollect("command").bstrVal;
								if(dbTemp.GetLength()<11&&command.GetLength()<11)
								{
									memset(&sybase_command[rec_command],0,sizeof(WdwAdd));
									memcpy(sybase_command[rec_command].destId,dbTemp.GetBuffer(0),dbTemp.GetLength());
									memcpy(sybase_command[rec_command].command,command.GetBuffer(0),command.GetLength());
									rec_command++;
								}
								m_recordset->MoveNext();
							}
						memset(&sybase_command[rec_command],0,sizeof(WdwAdd));
						rec_command=1;
						t1=t;
						m_recordset->Close();
					}
				///////////////////
					server.Empty();
					server_table.Empty();
					commandType.Empty();
					serviceID.Empty();
					service.Empty();
					fixedFee.Empty();
					content1.Empty();
					content_err.Empty();						
					i=0;//destId和command
					if(strlen(serviceTemp)==0&&destPhone!="6168")command.Format("select*from T_gwdx_service where destId='%s' and (command is null or command='')",destPhone);
					else command.Format("select*from T_gwdx_service where destId='%s' and command='%s'",destPhone,serviceTemp);
					if(m_pDlg->m_connection==NULL){Sleep(5000);continue;}
					hr=m_recordset->Open(command.GetBuffer(0), _variant_t((IDispatch*)m_pDlg->m_connection,true),adOpenStatic,adLockOptimistic,adCmdText);
					if(!m_recordset->adoEOF) i=1;
					if(strlen(serviceTemp)==0&&destPhone=="6168"&&i==0) i=1;
			//destId!=6168和command=null
					if((i==0)&&(destPhone.Compare("6168")!=0)&&(strlen(serviceTemp)>0))
					{
						m_recordset->Close();
						command.Format("select*from T_gwdx_service where destId='%s' and (command is null or command='')",destPhone);
						hr=m_recordset->Open(command.GetBuffer(0), _variant_t((IDispatch*)m_pDlg->m_connection,true),adOpenStatic,adLockOptimistic,adCmdText);
						if(!m_recordset->adoEOF) {i=2;memset(serviceTemp,0,21);}
					}
			//destId和command(只取字母)
					if(i==0)
					{
						j=strlen(serviceTemp);
						for(k=0;k<10;k++) //服务代码
						{
							if((serviceTemp[k]<'A')||(serviceTemp[k]>'Z'))
							{if(k>0)serviceTemp[k]=0x00;break;}
						}
						//if(strlen(serviceTemp)==j&&j>2)serviceTemp[j-1]=0x00;
						if(strlen(serviceTemp)<j&&strlen(serviceTemp)!=0)
						{
							m_recordset->Close();
							command.Format("select*from T_gwdx_service where destId='%s' and command='%s'",destPhone,serviceTemp);
							hr=m_recordset->Open(command.GetBuffer(0), _variant_t((IDispatch*)m_pDlg->m_connection,true),adOpenStatic,adLockOptimistic,adCmdText);
							if(!m_recordset->adoEOF) i=3;
						}
					}
			//destId和command(只取数字)
					if(i==0)
					{
						j=strlen(serviceTemp);
						for(k=0;k<10;k++) //服务代码
						{
							if((serviceTemp[k]<'0')||(serviceTemp[k]>'9'))
							{if(k>0)serviceTemp[k]=0x00;break;}
						}
						if(strlen(serviceTemp)==j&&j>2)serviceTemp[j-1]=0x00;
						if(strlen(serviceTemp)<j&&strlen(serviceTemp)!=0)
						{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -