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


// RecvThread.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "HWCmppAPI.h" 
#include "RecvThread.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); 
 
///////////////////////////////////////////////////////////////////////////// 
// CRecvThread 
 
IMPLEMENT_DYNCREATE(CRecvThread, CWinThread) 
 
CRecvThread::CRecvThread() 
{ 
	m_bIfRun = FALSE; 
	this->m_bAutoDelete = TRUE; 
	SetIfShowMsg(FALSE); 
	m_bInit = FALSE; 
} 
 
CRecvThread::~CRecvThread() 
{ 
	m_bIfRun = FALSE; 
 
} 
 
BOOL CRecvThread::InitInstance() 
{ 
	// TODO:  perform and per-thread initialization here 
	::CoInitialize(NULL); 
	return TRUE; 
} 
 
int CRecvThread::ExitInstance() 
{ 
	// TODO:  perform any per-thread cleanup here 
	CoUninitialize(); 
//	::Sleep(50); 
//	::PostThreadMessage(this->m_nThreadID,WM_QUIT,0,0); 
	return CWinThread::ExitInstance(); 
} 
 
BEGIN_MESSAGE_MAP(CRecvThread, CWinThread) 
	//{{AFX_MSG_MAP(CRecvThread) 
		// NOTE - the ClassWizard will add and remove mapping macros here. 
	//}}AFX_MSG_MAP 
	ON_THREAD_MESSAGE(WM_USER+505,OnRecvLoop) 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CRecvThread message handlers 
 
void CRecvThread::Start() 
{ 
	m_bIfRun = TRUE; 
	::PostThreadMessage(this->m_nThreadID,WM_USER+505,0,0); 
//	this->m_pParent->ShowMsg("Recv thread start!"); 
} 
 
void CRecvThread::Stop() 
{ 
	m_bIfRun = FALSE; 
//	::PostThreadMessage(this->m_nThreadID,WM_QUIT,0,0); 
} 
 
BOOL CRecvThread::Init(int nID,CString strQueueUp, CString strQueueDown, CHWCmppAPIDlg *pParent) 
{ 
	if(m_bInit == TRUE) 
	{ 
		return TRUE; 
	} 
	m_nID= nID; 
	if(!m_queueRW.InitInstance(strQueueUp, strQueueDown)){ 
		return FALSE; 
	} 
	if(pParent) 
	{ 
		this->m_pParent = pParent; 
		SetIfShowMsg(this->m_pParent->m_bIsShowMsg); 
	} 
	else{ 
		return FALSE; 
	} 
	m_bInit = TRUE; 
	return TRUE; 
} 
LRESULT CRecvThread::OnRecvLoop(WPARAM w,LPARAM l) 
{ 
 
	CString strLable=""; 
	CString strBody=""; 
	 
	ULONG ulsm_ID; 
	unsigned char byDestTON, byDestNPI, byOrgTON, byOrgNPI, byPRI, byPID, byDCS; 
	char sDestAddr[MAX_ADDR_LEN]; 
	char sOrgAddr[MAX_ADDR_LEN]; 
	char sTimeStamp[MAX_TIMESTAMP_LEN]; 
	char sUserData[MAX_UD_LEN]; 
	unsigned char MsgID[23]; 
	char sSubmitdate[MAX_SUBMIT_TIME_LEN+1]; 
	char sDonedate[MAX_DONE_TIME_LEN+1]; 
	char sStatus[MAX_STATE_LEN+1]; 
	int iRet; 
	unsigned char byRP; 
	unsigned char byUDHI; 
	unsigned long ulUDLen; 
	unsigned char byStatusReport = 0; 
	while(m_bIfRun) 
	{ 
/*		//test 
		this->m_pParent->PostMessage(WM_USER+601, 1, 1);//MT++, RC++ 
		ShowMsg("OnRecvLoop!!!fdjfldjfdjfldjf");	 
*/ 
		if(HasDeliverMessage(100) != 0){ 
			::Sleep(100); 
			continue; 
		} 
		memset(sDestAddr, 0, MAX_ADDR_LEN); 
		memset(sOrgAddr, 0, MAX_ADDR_LEN); 
		memset(sTimeStamp, 0, MAX_TIMESTAMP_LEN); 
		memset(sUserData, 0, MAX_UD_LEN); 
		memset(MsgID, 0, 23); 
		memset(sSubmitdate, 0, MAX_SUBMIT_TIME_LEN+1); 
		memset(sDonedate, 0, MAX_DONE_TIME_LEN+1); 
		memset(sStatus, 0, MAX_STATE_LEN+1); 
 
		try 
		{ 
			iRet = GetDeliverSMExExEx(&ulsm_ID, &byDestTON, &byDestNPI, sDestAddr,    
				&byOrgTON,&byOrgNPI, sOrgAddr, &byPRI, &byRP, &byUDHI, &byPID, 
				&byDCS,sTimeStamp, &ulUDLen, sUserData, &byStatusReport, MsgID,  
				sSubmitdate, sDonedate, sStatus); 
		} 
		catch(...) 
		{ 
		} 
 
		if(iRet == 0) 
		{ 
			if(byStatusReport == 1)// report  
			{ 
				strBody = ""; 
				CString strGateID; 
				CDataConvert::BYTE8ToDeintGateid(MsgID, strGateID); 
 
				if(strcmp(sStatus, "DELIVRD") == 0)//report ok 
				{ 
					strLable.Format("TYPE:Report;GateId:%s;Status:%d", strGateID, 0); 
					 
					if(!m_queueRW.WriteQueue(strLable, strBody, 0)) 
					{ 
						ShowMsg("Report 0 WriteQueue Failed"); 
						::GfWriteLog("\nReport 0 WriteQueue Failed");	 
						GfWriteLog(strLable);//Linbo 2003-1-23 22:39:45 						 
					} 
					//this->m_nRPNum++;				 
					//InterlockedIncrement((long*)&m_nRPNum); 
					this->m_pParent->PostMessage(WM_USER+601, 0,1); 
				} 
				else//report fail 
				{ 
					strLable.Format("TYPE:Report;GateId:%s;Status:%d", strGateID, 1); 
					 
					if(!m_queueRW.WriteQueue(strLable, strBody, 1)) 
					{ 
						ShowMsg("Report 1 WriteQueue Failed"); 
						::GfWriteLog("\nReport 1 WriteQueue Failed");	 
					//	GfWriteLog(strLable);//Linbo 2003-1-23 22:39:45  
					} 
					strLable.Format("TYPE:Report;GateId:%s;Status:%s", strGateID, sStatus); 
					GfWriteLog(strLable);//Linbo 2003-1-23 22:39:45  
				}	 
				ShowMsg(strLable); 
			} 
 
			else// Mo 
			{ 
				CString strTemp = sOrgAddr; 
				CString strSrc = strTemp.Right(11);			 
				strLable.Format("TYPE:MO;SRC:%s;DEST:%s;", strSrc,sDestAddr); 
				strBody.Format("%s", sUserData); 
 
				if(!m_queueRW.WriteQueue(strLable, strBody, (long)4)) 
				{ 
					ShowMsg("MO WriteQueue Failed"); 
					::GfWriteLog("\nMO WriteQueue Failed");	 
					//Linbo Add 2003-1-23 20:48:31 --> 
					//Comments: ... 
					::GfWriteLog("Lable:"+strLable); 
					::GfWriteLog("Body:"+strBody); 
					//Linbo Add 2003-1-23 20:48:31 <-- 
				} 
				//InterlockedIncrement((long*)&m_nMONum); 
				//this->m_nMONum++; 
				this->m_pParent->PostMessage(WM_USER+601,1, 0); 
				ShowMsg(strLable+" "+strBody);				 
			} 
		} 
		else 
		{ 
			::Sleep(10); 
//			::GfWriteLog("Recv Data failed."); 
		} 
		::Sleep(100);//每次处理完后休息 
	} 
	return 0; 
} 
 
void CRecvThread::SetIfShowMsg(BOOL bIsSHow) 
{ 
	m_criticalIsShowMsg.Lock(); 
 	this->m_bIsShowMsg = bIsSHow; 
	m_criticalIsShowMsg.Unlock(); 
} 
BOOL CRecvThread::GetIfShowMsg() 
{ 
	m_criticalIsShowMsg.Lock(); 
	BOOL bIsShow = m_bIsShowMsg; 
	m_criticalIsShowMsg.Unlock(); 
	return bIsShow; 
} 
 
void CRecvThread::ShowMsg(const CString &strMsg) 
{ 
 	if(this->GetIfShowMsg()) 
	{ 
		m_strMsg = strMsg; 
		m_pParent->SendMessage(WM_USER+605, 0, m_nID);	 
	} 
} 
 
void CRecvThread::QuitThread() 
{ 
	m_bIfRun = FALSE; 
	::PostThreadMessage(this->m_nThreadID,WM_QUIT,0,0); 
}