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

📄 sgipsend.cpp

📁 联通接收发送新程序
💻 CPP
字号:
#include "SgipSend.h"
#include "SgipMain.h"
#include "SGIPApi.h"
#include "error.h"

const char   DEFAULT_PROCFILE[]	=	"..\\config\\cmicp.ini" ;
const char   DEFAULT_PROCNAME[]	=	"SgipInit" ;
const int MAX_RETRY_TIMES = 3; 

const unsigned char LOGIN_MT = 1 ;
const unsigned char LOGIN_MO = 2 ;
const unsigned char ONCOMMOND_MODE = 0 ;
const unsigned char MONTH_MODE = 3 ;
const unsigned char MSG_LEVEL_NORMAL = 5 ;
const unsigned char MSG_LEVEL_URGENCY = 8 ;
const unsigned char NEED_REPLY = 1 ;
const unsigned char NOT_NEED_REPLY = 2 ;
const unsigned char SMC_MODE = 3 ;
const unsigned char MSG_GBK = 15 ;
const int SPACE_TIME = 18000 ;
const int AT_TIME = 180 ;

int ISMG_DisConnect(int ISMG_Socket)
{
	if(ISMG_Socket)
	{
#ifdef WIN32
		if(closesocket(ISMG_Socket)!=0)
#else
		if(close(ISMG_Socket) != 0)
#endif
				return CLOSE_SOCKET_FAILED;
	}
	else
		return SOCKET_INEXISTENCE;

	return 0;
}

CSgipSend::CSgipSend()
{
	SetProcPara( DEFAULT_PROCFILE,DEFAULT_PROCNAME ) ;
}

void CSgipSend::SetProcPara( const char *sProcfileName,const char *sProcName )
{
	m_pcProfile = new Profile(sProcfileName) ;

	strcpy( m_sProcName,sProcName ) ;
	ReadInitFile( ) ;

	delete m_pcProfile ;
}

void CSgipSend::ReadInitFile( )
{
	char sValue[ 20 ] ;
//get SrcTermID
	GetProcValue( "SpCode" , m_sSpCode ) ;
//get ICP ID
	GetProcValue( "IcpID" , m_sIcpId ) ;
//get ServerPort
	GetProcValue( "ServerPort" , sValue ) ;
	m_nServerPort = atoi( sValue ) ;

	GetProcValue( "ServerIP",m_sServerIP ) ;

  	GetProcValue( "Account",m_sAccount );
   
	GetProcValue( "Password",m_sPassword );
 
	GetProcValue( "NodeID",sValue );
    m_nNodeID= atol( sValue ) ;
}

void CSgipSend::GetProcValue( char *sEntry,char *sValue )
{
	if ( 0 != m_pcProfile->Read( m_sProcName, sEntry, sValue ) )
	{
		g_clLog.Panic( 0, "Unable to get %s from profile !",sEntry ) ;
	}
}

//return   0   success
//       other fail
int CSgipSend::SMG_Connect( )
{
	int nRet ;

    while ( connect_SMG(&mt_socket, m_sServerIP, m_nServerPort ) < 0 )
	{
		g_clLog.Info(0, "Fail to connect to SMG !") ;
		Sleep( 3000 ) ;
	}

	SGIP_MsgBody bind;

    initialize_msgbody(&bind, SGIP_BIND);
	strcpy(bind.SGIP_Bind.Login_Name, m_sAccount );
	strcpy(bind.SGIP_Bind.Login_Passwd, m_sPassword );
	bind.SGIP_Bind.Login_Type = LOGIN_MT ;

    nRet = sendmsg_withoutseq(mt_socket, &bind, SGIP_BIND, m_nNodeID );
	if ( nRet != 0)
	{
	    g_clLog.Error( 0 , "Connect to ISMG :Send bind request failed : %d !", nRet) ;
		return nRet ;
	}

	SGIP_MsgHead head;
	nRet = recv_msghead(mt_socket, &head) ;
	if ( nRet != 0)
	{
	    g_clLog.Error( 0 , "Connect to ISMG :recv bind_resp head failed : %d !", nRet) ;
		return nRet ;
  	}

	// Bind Resp
	SGIP_MsgBody bind_resp;
	if(recv_msgbody(mt_socket, head.Total_Length - sizeof(head), &bind_resp, head.Command_ID) == 0 
		&& bind_resp.SGIP_Bind_Resp.Result == 0)
	{
		nRet = 0 ;
	}
	else
	{
		g_clLog.Error( 0 , "Connect to ISMG :recv bind_resp response failed!: %d ", bind_resp.SGIP_Bind_Resp.Result ) ;
		nRet = bind_resp.SGIP_Bind_Resp.Result;
	}

	return nRet ;
}
//return   0   success
//       other fail
int CSgipSend::SMG_DisConnect( )
{
	int nRet ;

	SGIP_MsgHead head;
    nRet = sendmsg_withoutseq(mt_socket, NULL, SGIP_UNBIND, m_nNodeID );
	if ( nRet != 0 )
	{
		g_clLog.Error( 0 , "SMG_DisConnect() :Send Unbind fail : %d !", nRet ) ;
		return nRet ;
	}

	nRet = recv_msghead(mt_socket, &head) ;
	if ( nRet != 0)
	{
		g_clLog.Error( 0 , "SMG_DisConnect() :Recv UnbindResp head fail : %d !", nRet ) ;
		return nRet ;
	}

	nRet = ISMG_DisConnect( mt_socket );
	if ( nRet != 0 )
         g_clLog.Error(0, "Fail to DisConnect to ISMG ,mt_socket = %d !",mt_socket);

	return  nRet ;
}

int CSgipSend::SubmitMsg( vector<StruDispense> &vstDispWait )
{
	int nRet ;

	if ( vstDispWait.size() > 1 )
		nRet = SubmitBatchMsg( vstDispWait ) ;
	else
		nRet = SubmitSingleMsg( vstDispWait[0] ) ;

	return nRet ;
}

const char SEND_OK[] = "60K" ;
const char SEND_WAIT[] = "60W" ;
const char SEND_FAIL[] = "60F" ;

int CSgipSend::SubmitSingleMsg( StruDispense &stDispSingle )
{
	string sSpCode ;
	string sChargeNumber = "" ;
	string sUserNumber = "" ;

	char sFeeCode[ 6+1 ] ;
	char sGivenValue[ 6+1 ] ;
	unsigned char ucMTFlag ;
	unsigned char ucMsgLevel ;
	unsigned char ucReportFlag ;
	unsigned char ucTP_pid = 0 ;
	unsigned char ucTP_udhi = 1 ;
	unsigned char ucMsgCode = MSG_GBK ;
	unsigned char ucMsgType = 0 ;
	unsigned char ucUserCount = 1 ;
	char sExpireTime[16+1];
	char sScheduleTime[16+1];
	
	char sMsgID[ 30 ] ;

	if ( strlen(stDispSingle.sp_code) <= strlen(m_sSpCode) )
		sSpCode = m_sSpCode ;
	else
		sSpCode = stDispSingle.sp_code  ;

	if ( 0 != strncmp( stDispSingle.acct_nbr,"86",2) )
		sChargeNumber = "86" ;
	if ( 0 != strncmp( stDispSingle.dest_nbr,"86",2) )
		sUserNumber = "86" ;

	sChargeNumber += stDispSingle.acct_nbr ;
	sUserNumber += stDispSingle.dest_nbr ;

	sprintf( sFeeCode,"%06d",atoi(stDispSingle.fee_code) ) ;
	strcpy( sGivenValue,"000000" ) ;
	
	if ( stDispSingle.information_id == -2 )
	{
		ucMsgLevel = MSG_LEVEL_URGENCY ;
		ucMTFlag = MONTH_MODE ;
		ucReportFlag = SMC_MODE ;
	}
	else
	{
		ucMsgLevel = MSG_LEVEL_NORMAL ;
		ucMTFlag = ONCOMMOND_MODE ;
		ucReportFlag = NOT_NEED_REPLY ;
	}

	GetValidSmppTime( sExpireTime ) ;
	strcpy( sScheduleTime,"" ) ;

	int nRet = SGIPSubmit(  sSpCode.c_str(),
							sChargeNumber.c_str(),
							ucUserCount,
							sUserNumber.c_str(),
							m_sIcpId,
							stDispSingle.service_code,
							(unsigned char)atoi(stDispSingle.fee_type),
							sFeeCode,
							sGivenValue,
							0,
							ucMTFlag,
							ucMsgLevel,
							sExpireTime,
							sScheduleTime,
							ucReportFlag,
							ucTP_pid,
							ucTP_udhi,
							ucMsgCode,
							ucMsgType,
							strlen( stDispSingle.msg_content ),
							stDispSingle.msg_content,
							sMsgID ) ;

	if ( nRet == 0 )
	{
		strcpy( stDispSingle.msg_id,sMsgID ) ;
		strcpy( stDispSingle.gateway_state, SEND_OK ) ;
		
		g_clLog.Info( 0, "Success to Send Msg to %s !", stDispSingle.dest_nbr ) ;
	}
	else
	{
		g_clLog.Error(0, "Fail to Call SubmitSingleMsg() %s, errno = %d !",stDispSingle.dest_nbr,nRet );
		
		if ( NeedResend( nRet ) )
		{
			stDispSingle.try_times ++ ;

			if ( stDispSingle.try_times <= MAX_RETRY_TIMES )
			{
				strcpy( stDispSingle.gateway_state, SEND_WAIT ) ;
				//hyl 20031220
				SetAtTime( stDispSingle.at_time ) ;
			}
			else
				strcpy( stDispSingle.gateway_state, SEND_FAIL ) ;
		}
		else
			strcpy( stDispSingle.gateway_state, SEND_FAIL ) ;
	}

	return nRet ;
}

int CSgipSend::SubmitBatchMsg( vector<StruDispense> &vstDispBatch  )
{
	string sSpCode = "" ;
	string sChargeNumber = "" ;
	string sUserNumber = "" ;

	char sFeeCode[ 6+1 ] ;
	char sGivenValue[ 6+1 ] ;
	unsigned char ucMTFlag ;
	unsigned char ucMsgLevel ;
	unsigned char ucReportFlag ;
	unsigned char ucTP_pid = 0 ;
	unsigned char ucTP_udhi = 1 ;
	unsigned char ucMsgCode = MSG_GBK ;
	unsigned char ucMsgType = 0 ;
	unsigned char ucUserCount ;
	char sExpireTime[16+1];
	char sScheduleTime[16+1];
	
	char sMsgID[ 30 ] ;
    StruDispense &stDispense = vstDispBatch[0] ;

    ucUserCount = vstDispBatch.size() ;
	if ( 0 != strncmp( stDispense.dest_nbr,"86",2 ) )
		sUserNumber += "86" ;
	sUserNumber += stDispense.dest_nbr ;
	for ( int i=1; i<ucUserCount ; i++ )
	{
		sUserNumber += "," ;

		if ( 0 != strncmp( vstDispBatch[i].dest_nbr,"86",2 ) )
			sUserNumber += "86" ;
		sUserNumber += vstDispBatch[i].dest_nbr ;
	}

	if ( strlen(stDispense.sp_code) <= strlen(m_sSpCode) )
		sSpCode = m_sSpCode ;
	else
		sSpCode = stDispense.sp_code ;

	sprintf( sFeeCode,"%06d",atoi(stDispense.fee_code) ) ;
	strcpy( sGivenValue,"000000" ) ;
	
	if ( stDispense.information_id == -2 )
	{
		ucMsgLevel = MSG_LEVEL_URGENCY ;
		ucMTFlag = MONTH_MODE ;
		ucReportFlag = SMC_MODE ;
	}
	else
	{
		ucMsgLevel = MSG_LEVEL_NORMAL ;
		ucMTFlag = ONCOMMOND_MODE ;
		ucReportFlag = NOT_NEED_REPLY ;
	}

	GetValidSmppTime( sExpireTime ) ;
	strcpy( sScheduleTime,"" ) ;

	int nRet = SGIPSubmit(  (char*)sSpCode.c_str(),
							"",
							ucUserCount,
							(char*)sUserNumber.c_str() ,
							m_sIcpId,
							stDispense.service_code,
							(unsigned char)atoi(stDispense.fee_type),
							sFeeCode,
							sGivenValue,
							0,
							ucMTFlag,
							ucMsgLevel,
							sExpireTime,
							sScheduleTime,
							ucReportFlag,
							ucTP_pid,
							ucTP_udhi,
							ucMsgCode,
							ucMsgType,
							strlen( stDispense.msg_content ),
							stDispense.msg_content,
							sMsgID ) ;

	//log and set state
	for ( i=0;i<ucUserCount;i++)
	{
		if ( nRet != 0 )
		{//send fail
			strcpy( vstDispBatch[i].gateway_state,SEND_FAIL ) ;
		}
		else
		{//send success
			strcpy( vstDispBatch[i].gateway_state, SEND_OK ) ;
		}
	}

	if ( nRet != 0 )
	{
		g_clLog.Error(0, "Fail to call CMPPAPI::CMPPSendBatch2() ,ErrCode = %d !" ,nRet ) ;
	}
	else
	{
		g_clLog.Info(0, "Success to Send Msg to %s !",sUserNumber.c_str() );
	}

	return nRet ;
}

int CSgipSend::SGIPSubmit( 	const char *SPNumber,
							const char *ChargeNumber,
							unsigned char UserCount,
							const char *UserNumber,
							char *CorpId,
							char *ServiceType,
							unsigned char FeeType,
							char *FeeValue,
							char *GivenValue,
							unsigned char AgentFlag,
							unsigned char MorelatetoMTFlag,
							unsigned char Priority,
							char *ExpireTime,
							char *ScheduleTime,
							unsigned char ReportFlag,
							unsigned char TP_pid,
							unsigned char TP_udhi,
							unsigned char MessageCoding,
							unsigned char MessageType,
							unsigned long MessageLength,
							char *MessageContent,
							char *MsgID,
							char *Reserve /*= ""*/ )
{
	SGIP_MsgBody submit;

	initialize_msgbody(&submit, SGIP_SUBMIT);

	strcpy( submit.SGIP_Submit.SPNumber, SPNumber );
	strcpy( submit.SGIP_Submit.ChargeNumber, ChargeNumber );
    submit.SGIP_Submit.UserCount = UserCount;
    strcpy( submit.SGIP_Submit.UserNumber, UserNumber );
    strcpy( submit.SGIP_Submit.CorpId, CorpId );

	strcpy( submit.SGIP_Submit.ServiceType, ServiceType);
    submit.SGIP_Submit.FeeType = FeeType ;
	strcpy( submit.SGIP_Submit.FeeValue, FeeValue );
	strcpy( submit.SGIP_Submit.GivenValue, GivenValue);
	submit.SGIP_Submit.AgentFlag = AgentFlag;
	submit.SGIP_Submit.MorelatetoMTFlag = MorelatetoMTFlag ;
	submit.SGIP_Submit.Priority = Priority ;

	strcpy(submit.SGIP_Submit.ExpireTime, ExpireTime);
	strcpy(submit.SGIP_Submit.ScheduleTime, ScheduleTime );
	submit.SGIP_Submit.ReportFlag = ReportFlag ;
	submit.SGIP_Submit.TP_pid = TP_pid;
	submit.SGIP_Submit.TP_udhi = TP_udhi;
	submit.SGIP_Submit.MessageCoding = MessageCoding;
	submit.SGIP_Submit.MessageType = MessageType;
	submit.SGIP_Submit.MessageLength = MessageLength;
	
	strcpy(submit.SGIP_Submit.MessageContent, MessageContent );
	strcpy(submit.SGIP_Submit.Reserve, Reserve );

    int nRet = sendmsg_withoutseq( mt_socket, &submit, SGIP_SUBMIT, m_nNodeID );
	if (  nRet != 0 )
	{
		g_clLog.Info( 0 ,"Connect to ISMG  again ! " );
		ISMG_DisConnect( mt_socket );
		nRet = SMG_Connect() ;
		if ( nRet != 0 )
		{
			g_clLog.Error( 0 , "Connect to ISMG  again fail : %d !", nRet ) ;
			return nRet ;
		}
		else
		{
			nRet = sendmsg_withoutseq( mt_socket, &submit, SGIP_SUBMIT, m_nNodeID );
			if ( nRet != 0 )
			{
				g_clLog.Error( 0 , "Send message submit failed : %d !", nRet ) ;
				return nRet ;
			}
		}
	}
	
	SGIP_MsgHead head;

	nRet = recv_msghead(mt_socket, &head);
	if ( nRet != 0 )
	{
		g_clLog.Error( 0 , "Receive message submit_resp header failed : %d !", nRet ) ;
		return nRet ;
	}
	
	sprintf( MsgID,"%u%u%u",head.Sequence_ID[0],head.Sequence_ID[1],head.Sequence_ID[2] ) ;
	
	//recv head success
	SGIP_MsgBody submit_resp;
	submit_resp.SGIP_Submit_Resp.Result = XW_SEND_ERROR ; //clear
	if(recv_msgbody(mt_socket, head.Total_Length - sizeof(head), &submit_resp, head.Command_ID) == 0 
		&& submit_resp.SGIP_Submit_Resp.Result == 0)
	{//recv body success
		nRet = 0 ;
	}
	else
	{//recv body fail
		nRet = (int)submit_resp.SGIP_Submit_Resp.Result;
		g_clLog.DeBug ( 0 ,"recv submit response fail : %d ", nRet ) ;
	}

	return nRet ;
}

bool CSgipSend::NeedResend( int error_code )
{
	switch(error_code)
	{	
	case SEND_DATA_FAILED:
	case CONNECT_ISGM_FAILED:
	case XW_SEND_ERROR:
 		 return(true);
	default:
		return(false);
	}
}

//return: YYMMDDHHMMSS032+ 
void CSgipSend::GetValidSmppTime( char* ptime  )
{
	OTime oTime ; 
	oTime.GetLocalTime() ;
  
    OTime time_begin( oTime.GetDate() , oTime.GetTime() ) ;
    OTimeSpan oduration( 0 , SPACE_TIME );
    OTime &time_end = time_begin + oduration;
    
	sprintf( ptime,"%s%s032+",time_end.GetDate() + 2,time_end.GetTime() ) ;
}

void CSgipSend::SetAtTime( char *ptime )
{
	OTime clTime ; 
	clTime.GetLocalTime() ;
    OTime time_begin( clTime.GetDate() , clTime.GetTime() ) ;
    OTimeSpan oduration( 0 , AT_TIME );
    OTime &time_end = time_begin + oduration;
	sprintf( ptime,"%s%s",time_end.GetDate(),time_end.GetTime() ) ;
}


⌨️ 快捷键说明

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