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


// SendThread.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "HWCmppAPI.h" 
#include "SendThread.h" 
#include "DataConvert.h" 
 
#include "define.h" 
 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
 
 
 
extern void GfWriteLog(CString str); 
 
///////////////////////////////////////////////////////////////////////////// 
// CSendThread 
 
IMPLEMENT_DYNCREATE(CSendThread, CWinThread) 
 
CSendThread::CSendThread() 
{ 
	m_bIfRun = FALSE; 
	this->m_bAutoDelete = TRUE; 
	SetIfShowMsg(FALSE); 
	m_bInit = FALSE; 
} 
 
CSendThread::~CSendThread() 
{ 
	m_bIfRun = FALSE; 
} 
 
BOOL CSendThread::InitInstance() 
{ 
	// TODO:  perform and per-thread initialization here 
	::CoInitialize(NULL); 
	return TRUE; 
} 
 
int CSendThread::ExitInstance() 
{ 
	// TODO:  perform any per-thread cleanup here 
	CoUninitialize(); 
//	::PostThreadMessage(this->m_nThreadID,WM_QUIT,0,0); 
//	::Sleep(50); 
	return CWinThread::ExitInstance(); 
} 
 
BEGIN_MESSAGE_MAP(CSendThread, CWinThread) 
	//{{AFX_MSG_MAP(CSendThread) 
		// NOTE - the ClassWizard will add and remove mapping macros here. 
	//}}AFX_MSG_MAP 
	ON_THREAD_MESSAGE(WM_USER+500,OnSendLoop) 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CSendThread message handlers 
 
BOOL CSendThread::Init(int nID, CString strQueueUp, CString strQueueDown, char * chIcpId, CHWCmppAPIDlg* pParent) 
{ 
	if(m_bInit == TRUE) 
	{ 
		return TRUE; 
	} 
	this->m_nID = nID; 
	if(!m_queueRW.InitInstance(strQueueUp, strQueueDown)){ 
		return FALSE; 
	} 
	if(chIcpId){ 
		strcpy(m_chIcpId, chIcpId); 
	} 
	else{ 
		return FALSE; 
	} 
	if(pParent) 
	{ 
		m_pParent = (CHWCmppAPIDlg*)pParent; 
		SetIfShowMsg(this->m_pParent->m_bIsShowMsg); 
	} 
	else{ 
		return FALSE; 
	} 
	m_bInit = TRUE; 
	return TRUE; 
} 
 
void CSendThread::Start() 
{ 
	m_bIfRun = TRUE; 
	::PostThreadMessage(this->m_nThreadID,WM_USER+500,0,0); 
} 
 
void CSendThread::Stop() 
{ 
	m_bIfRun = FALSE; 
} 
 
LRESULT CSendThread::OnSendLoop(WPARAM w,LPARAM l) 
{ 
	CString strLable = ""; 
	_variant_t vBody; 
	vBody.Clear(); 
	long lPri = 0; 
	int nRet = -1; 
	BOOL bSendSuccess = TRUE;  
 
	while(m_bIfRun) 
	{ 
/*		//test 
		this->m_pParent->PostMessage(WM_USER+600, 1, 1);//MT++, RC++ 
		ShowMsg("OnSendLoop!!!");	 
*/ 
		if(m_queueRW.ReadQueue(strLable,vBody,lPri,1)) 
		{ 
//			GfWriteLog("InitLable:"+strLable); 
			__int32 nMsgID = (__int32)CLable::GetMsgId(strLable); 
			CString strGateID; 
			 
			int MsgFormat = CLable::GetMsgFormat(strLable); 
//			if( ::CLable::GetFormatType(MsgFormat) == 0 )//0-text, 1-Binary, x-expand。 Linbo Comment 2002-11-21 11:25:50 
			if( ::CLable::GetCodeType(strLable) == 0 )//0-text, 1-Binary, x-expand。 Linbo Comment 2002-11-21 11:25:50 
			{ 
				_bstr_t bstrBody(vBody); 
				int msglenbstr=bstrBody.length()*2;	  //长度大	unicode长度 
				 
				CString strBody; 
				strBody=(const char*)bstrBody; 
				int msglenstr=strBody.GetLength();	  //不同长度 ascii长度 
				 
				if(msglenbstr==2*msglenstr) 
				{//纯英文 
					nRet = SendSM(1, 1, CLable::GetSrcId(strLable), CLable::GetDestId(strLable), CLable::GetFeePhone(strLable), 
						(char*)(const char*)strBody, msglenstr, 
						0, MsgFormat, CLable::GetFeetype(strLable),CLable::GetFeecode(strLable), 
						CLable::GetSevId(strLable), nMsgID, lPri, strGateID);		 
				} 
				else 
				{//含中文或纯中文 
					if(msglenstr >140) 
					{ 
					//	strBody = (const wchar_t *)bstrBody; 
						strBody.Left(140); 
						msglenbstr  = 140; 
					} 
					nRet = SendSM(1, 1, CLable::GetSrcId(strLable), CLable::GetDestId(strLable), CLable::GetFeePhone(strLable), 
						(char*)(const char*)strBody, msglenstr,  
						0, MsgFormat, CLable::GetFeetype(strLable),CLable::GetFeecode(strLable), 
						CLable::GetSevId(strLable), nMsgID, lPri, strGateID); 
				} 
				bSendSuccess = !nRet;	 
			} 
			 
			else //if(Body.vt==(VT_ARRAY|VT_UI1))	//binary 
			{ 
				char BinaryBuf[1500] = {0}; 
				_bstr_t bstrBody(vBody); 
				CString strBody = (const char*)bstrBody; 
				long BodySize = strBody.GetLength()/2; 
				 
				memcpy(BinaryBuf,(LPCSTR)strBody,strBody.GetLength()); 
				 
				if(CVariantBufConvert::ConvertToBinary((char*)BinaryBuf, BodySize, (char*)BinaryBuf)) 
				{			 
					int packnum=BodySize/140; 
					if(BodySize%140) 
					{ 
						packnum++; 
					} 
					 
					for(int i=1;i<=packnum;i++) 
					{				 
						nRet = SendSM(packnum, i, CLable::GetSrcId(strLable), CLable::GetDestId(strLable), CLable::GetFeePhone(strLable), 
							BinaryBuf+(i-1)*140, (i==packnum ? (packnum==1?BodySize:BodySize%140) : (packnum==1?BodySize:140)), 
							1, MsgFormat, CLable::GetFeetype(strLable),CLable::GetFeecode(strLable), 
							CLable::GetSevId(strLable), nMsgID, lPri, strGateID); 
						if(nRet != 0) 
						{ 
							bSendSuccess = FALSE; 
						} 
					} 
				}			 
			} 
			if(bSendSuccess == TRUE){ 
				this->m_pParent->PostMessage(WM_USER+600, 1, 1);//MT++, RC++ 
			} 
			else 
			{ 
				this->m_pParent->PostMessage(WM_USER+600, 1, 0);//MT++, RC=RC 
			} 
			strLable.Format("TYPE:Receipt;MsgId:%d;GateId:%s;Status:%d;",nMsgID, strGateID, (int)(!bSendSuccess));			 
			vBody.SetString(""); 
			m_queueRW.WriteQueue(strLable,vBody, lPri); 
			strLable.Format("TYPE:Receipt;MsgId:%d;GateId:%s;Status:%d;",nMsgID, strGateID, nRet); 
			ShowMsg(strLable);			 
			 
		} 
		else{ 
			::Sleep(100); 
		} 
		::Sleep(10);//每次处理完后休息 
	} 
	return 0;  
} 
 
int CSendThread::SendSM(BYTE byPKTotal, BYTE byPKNum, CString strSrc, CString strDest, CString strFeePhone, char *chMsg, int nMsgLen, BYTE byType, int nFormat, int nFeeType, int nFeeCode, CString strSevID, __int32 nMsgID, long nPriority, CString &strGateID) 
{ 
//移至最开始,要完成长度检查、切分、内容为空的检查 
 
	int nTemp = strSrc.GetLength(); 
	if(nTemp == 0 || nTemp >=21){ 
		return -2; 
	}	 
	if(nFeeType<0||nFeeType>5) 
		return -3; 
	if(nFeeCode<0||nFeeCode>1000) 
		return -4; 
	if(strSevID.GetLength()>10) 
		return -5; 
 
	int  nRet = -1; 
	BYTE	chGateID[23]; 
	memset(chGateID, 0, sizeof(chGateID)); 
	__int64	nGateID = 0; 
	unsigned char chFCS; 
	unsigned long lSM_ID; 
	CString strFeeType; 
	CString strFeeCode; 
 
	strFeeType.Format("%d", nFeeType); 
	if(strFeeType.GetLength() == 1){ 
		strFeeType = "0"+strFeeType; 
	} 
	strFeeCode.Format("%d", nFeeCode); 
 
	if(byType == 0)//text 
	{ 
		try 
		{ 
			nRet = SubmitAExEx(/*OrgTON*/2, /*OrgNPI*/1, /*OrgAddr*/strSrc, 
			/*DestTON*/2, /*DestNPI*/1, /*LPCTSTR DestAddr*/strDest,  
			/*unsigned char PRI*//*(int)(nPriority >0)*/1, /*unsigned char RD*/0, /*unsigned char RP*/0,  
			/*unsigned char SRR*//*(nFeeType==3)? 2:1*/1, /*unsigned char MR*/0, /*!!!!unsigned char DCS*/nFormat,  
			/*unsigned char PID!!!协议类型*/0, /*LPCTSTR Schedule*/NULL, /*LPCTSTR Expire*/NULL,  
			/*unsigned long Default_ID*/0, /*unsigned char UDHI!!!!表示UserData是否是结构*/0, /*unsigned long UDLen*/nMsgLen, 
			/*LPCTSTR UserData*/chMsg, /*LPCTSTR ServiceSubType*/strSevID,  
			/*unsigned char* byMsgID*/chGateID, /*unsigned char byPKTotal*//*byPKTotal*/1,  
			/*unsigned char byPKNumber*//*byPKNum*/1, /*unsigned char byMsglevel*/0,  
			/*unsigned char byFeeUserType*/3, /*LPCTSTR sSPID*/m_chIcpId, /*LPCTSTR sFeeType*/strFeeType,  
			/*LPCTSTR sFeeAddr*/strFeePhone, /*LPCTSTR sFeeCode*/strFeeCode,/*unsigned char byUserNum*/1,  
			/*LPCTSTR sDestAddrs*/strDest,	/*unsigned long* SM_ID*/&lSM_ID, /*unsigned char* FCS*/&chFCS);		 
		} 
		catch(...) 
		{ 
		} 
	} 
	else if(byType == 1)//binary 
	{ 
		BYTE	byPID = 0; 
		BYTE	byUDHI = 1;//40; 
		BYTE	byDCS = nFormat; 
//		this->BinPro(nFormat, byPID, byUDHI, byDCS); 
		try 
		{ 
			nRet = SubmitAExEx(/*OrgTON*/2, /*OrgNPI*/1, /*OrgAddr*/strSrc, 
			/*DestTON*/2, /*DestNPI*/1, /*LPCTSTR DestAddr*/strDest,  
			/*unsigned char PRI*//*(int)(nPriority >0)*/1, /*unsigned char RD*/0, /*unsigned char RP*/0,  
			/*unsigned char SRR*//*nFeeType == 3 ? 2:1*/1, /*unsigned char MR*/0, /*!!!!unsigned char DCS*/byDCS,  
			/*unsigned char PID!!!协议类型*/byPID, /*LPCTSTR Schedule*/NULL, /*LPCTSTR Expire*/NULL,  
			/*unsigned long Default_ID*/0, /*unsigned char UDHI!!!!表示UserData是否是结构*/byUDHI, /*unsigned long UDLen*/nMsgLen, 
			/*LPCTSTR UserData*/chMsg, /*LPCTSTR ServiceSubType*/strSevID,  
			/*unsigned char* byMsgID*/chGateID, /*unsigned char byPKTotal*/byPKTotal,  
			/*unsigned char byPKNumber*/byPKNum, /*unsigned char byMsglevel*/0,  
			/*unsigned char byFeeUserType*/3, /*LPCTSTR sSPID*/m_chIcpId, /*LPCTSTR sFeeType*/strFeeType,  
			/*LPCTSTR sFeeAddr*/strFeePhone, /*LPCTSTR sFeeCode*/strFeeCode,/*unsigned char byUserNum*/1,  
			/*LPCTSTR sDestAddrs*/strDest,	/*unsigned long* SM_ID*/&lSM_ID, /*unsigned char* FCS*/&chFCS); 
		} 
		catch(...) 
		{ 
		} 
	} 
	else 
	{ 
		nRet =-2; 
	} 
 
	if(nRet == 0) 
	{ 
	//	strGateID = chGateID; 
		CDataConvert::BYTE8ToDeintGateid(chGateID, strGateID); 
	} 
 
	else 
	//if(nRet != 0) 
	{ 
		CString strSend; 
		strSend.Format("\nSendSM Ret:%d,MsgID:%d,SevID:%s, FeeType:%s, FeeCode:%s, GateID:%s, Dest:%s, Src:%s,FeePhone:%s, Body:%s, MsgLen:%d, Format:%d,SM_ID:%d, FCS:%d,SPID:%s", 
			nRet,nMsgID,strSevID,strFeeType, strFeeCode, strGateID, strDest, strSrc,	strFeePhone,chMsg,nMsgLen,nFormat, lSM_ID, chFCS, m_chIcpId); 
		::GfWriteLog(strSend); 
	}	 
 
	return nRet; 
} 
 
void CSendThread::BinPro(int nFormat, BYTE &byPID, BYTE &byUDHI, BYTE &byDCS) 
{ 
	switch(nFormat) 
	{ 
//		packSubmit.msg_mode = packSubmit.msg_mode |1;//要求状态报告 
	case 2: 
		{ 
			break; 
		} 
	case 3: 
		{ 
			break; 
		} 
	case 4:	//Nokia 
		{ 
			break; 
		} 
	case 5: 
		{ 
			break; 
		} 
	case 8://Nokia  --> binary format 
		{ 
			break; 
		} 
	case 9://Siemens -->  
		{ 
			byPID = 0; 
			byUDHI = 0; 
			byDCS = 245;//0xF5; 
			break; 
		} 
	case 10://Motorola ringtone --> ASCII FORMAT 
		{ 
			byPID = 0; 
			byUDHI = 1;//40; 
			byDCS = 4; 
			break; 
		} 
	case 11://Motorola big picture, EMS picture, EMS ringtone, 松下 picture. --> binary format, TP_UDHI=1. 
		{ 
			byPID = 0; 
			byUDHI = 1; 
			byDCS = 4; 
			break; 
		} 
	case 12://Alcatel	picture and ringtone.  -- TP_UDHI=1, TP_PID=7D, TP_DCS=0XF5. 
		{		 
			byPID = 125;//0x7d 
			byUDHI = 1;//40; 
			byDCS = 245;//0xf5 
		} 
	default: 
		{ 
			byPID = 0; 
			byUDHI = 40; 
			byDCS = 4; 
			break; 
		} 
	} 
 
} 
 
void CSendThread::SetIfShowMsg(BOOL bIsSHow) 
{ 
	m_criticalIsShowMsg.Lock(); 
 	this->m_bIsShowMsg = bIsSHow; 
	m_criticalIsShowMsg.Unlock(); 
} 
BOOL CSendThread::GetIfShowMsg() 
{ 
	m_criticalIsShowMsg.Lock(); 
	BOOL bIsShow = m_bIsShowMsg; 
	m_criticalIsShowMsg.Unlock(); 
	return bIsShow; 
} 
 
void CSendThread::ShowMsg(const CString &strMsg) 
{ 
 	if(this->GetIfShowMsg()) 
	{ 
		m_strMsg = strMsg; 
		m_pParent->SendMessage(WM_USER+605, 1, m_nID);		 
	} 
} 
 
void CSendThread::QuitThread() 
{ 
	m_bIfRun = FALSE; 
	::PostThreadMessage(this->m_nThreadID,WM_QUIT,0,0); 
}