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

📄 smrecv.cpp

📁 联通接收发送新程序
💻 CPP
字号:
#include <process.h>
#include <signal.h>
#include "windows.h"

#ifdef _NEWTEST
#include "RequestWait.h"
#include "SysMidMap.h"
#include "MidRequest.h"
#include "StateReport.h"
#else
#include "RequestWait.h"
#include "DispenseReport.h"
#endif

#include "Sgip.h"
#include "log.hpp"
#include "profile.hpp"
#include "DequePool.h"
#include "SetDayLog.h"

static volatile sig_atomic_t nRunFlag = 1;	//used for stop program
static void SignalHandler(int sig);

const int THREAD_NUM = 5 ;				//recv thread number
HANDLE  hRecvThread[ THREAD_NUM ] ;		//recv thread handle
DWORD   nRecvThreadID[ THREAD_NUM ] ;	//recv thread ID
HANDLE  hProcThread ;
DWORD   nProcThreadID ;

CDequePool<int>	g_cClientPool ;
CDequePool<StruDeliver> g_cDeliverPool ;

char	m_sProcName[ 40 ] ;
char	m_sDispLogName[ 40 ] ;
char	m_sDispReportName[ 40 ] ;
char    m_DBServer[ 40 ] ;

LogFile g_clLog;
Profile g_clProfile;
SetDayLog  m_clSetDayLog;

//COMMENT:	the handler for processing system signal, now just SIGINT
//INPUT:	sig -- catched system signal
//OUTPUT:	none
static void SignalHandler( int sig )
{
	switch ( sig )
	{
	case SIGINT:
		nRunFlag = 0;
		break ;
	default:
		break ;
	}
}

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

void Init()
{
	char sValue[ LINE_LEN+1 ] ;
	char sLogName[128] ;
	
	//set procedure name of log
	g_clLog.SetProcName( m_sProcName ) ;

	//log in database
	GetProcValue( "database_server",m_DBServer ) ;
	Login( m_DBServer ) ;
	//open log
	GetProcValue("standart_output_level",sValue );
	g_clLog.SetTermLevel( (OUTPUT_LEVEL)atoi(sValue) );
	
	GetProcValue("log_output_level",sValue);
	g_clLog.SetLogLevel((OUTPUT_LEVEL)atoi(sValue));

	GetProcValue( "log_file_name",sValue ) ;
	m_clSetDayLog.SetDayLogName( sValue , sLogName ) ;
	g_clLog.Open( sLogName ) ;

	GetProcValue( "dispense_log_name",m_sDispLogName ) ;
	GetProcValue( "dispense_report_name",m_sDispReportName ) ;

	//init WSAStartup
	WSADATA wsaData;
	WORD wVersionRequested;
   
	wVersionRequested = MAKEWORD(2, 2); 
	if ( WSAStartup(wVersionRequested, &wsaData) != 0 )
	{
		exit(0);
	}
}

void Exit()
{
	WSACleanup() ;

	XW_Logout() ;
 	g_clLog.Close() ;
}

CSgipRecv g_cSgipRecv; 

static DWORD WINAPI RecvThread( LPVOID pVoid )
{
	int  moSocket ;
	StruDeliver stDeliver ;
	int command_id ;
	
	int goon = 1 ,ret ;
	while ( goon )
	{
		while ( false == g_cClientPool.GetData( moSocket ) )
		{
			Sleep( 300 ) ;
		}

		g_clLog.DeBug( 0  ,"---Recv a Client Connect,socket = %d !", moSocket );
		
		while ( true )
		{
			ret = g_cSgipRecv.GetDeliverMsg( moSocket , stDeliver ,command_id ) ;
			if( ret != 0 )
			{
				if ( 0 != closesocket( moSocket ) ) 
				   g_clLog.DeBug( 0  ,"close socket %d failed !", moSocket );
				break ;
			}
			
			if( command_id == SGIP_DELIVER || command_id == SGIP_REPORT  )
			{	
				g_cDeliverPool.PutData( stDeliver ) ;						
			}
		}

	}//end while(goon)
	
	return 0;
}

void StartRecvhread()
{
	for ( int i=0; i<THREAD_NUM ; i++ )
	{
		hRecvThread[i] = CreateThread(NULL,
										0,
										RecvThread,
									    0,
										0,
										&nRecvThreadID[i]);
		if( NULL == hRecvThread[i] )
		{
			g_clLog.Panic( 0, "Fail to start %d RecvThread !",i+1 ) ;
		} 
	}
}

static DWORD WINAPI ProcessDeliver( LPVOID pVoid  )
{
	StruDeliver   stDeliver ;

#ifdef _NEWTEST
	CRequestWait    cRequestWait ;
	CSysMidMap		cSysMidMap ;
	CMidRequest     cMidRequest ;
	CStateReport    cStateReport ;
#else
	CRequestWait    cRequestWait ;
	CDispenseReport cDispenseReport ;
	CDispenseReport.SetTbName( m_sDispReportName,m_sDispLogName) ;
#endif

	short nOpen = 1 ;
	int goon = 1 ;
	while ( goon )
	{ 
	   while ( false == g_cDeliverPool.GetData( stDeliver ) )
	   {
		   Sleep( 300 ) ;
	   }
#ifdef _NEWTEST
	   if ( stDeliver.bReport )
	   {//report
		   g_clLog.Info( 0, "R : %s %s %s %u !",stDeliver.stReport.dest_nbr,
			                              stDeliver.stReport.message_id,
										  stDeliver.stReport.state,
										  stDeliver.stReport.ErrorCode ) ;
		   if ( (nOpen=cStateReport.SetState( stDeliver.stReport )) < 0 )
		   {
			   //g_clLog.Error( 0,"Fail to CStateReport::SetState() ! " ) ;
			   g_clLog.Error( 0,"Fail to CStateReport::SetState() : %s ! ",GetMsg() ) ;
		   }
	   }
	   else
	   {//deliver
		   g_clLog.Info( 0, "R: %s %s%s %s!",stDeliver.stRequest.acct_nbr,
			                              stDeliver.stRequest.sp_code,
										  stDeliver.stRequest.inf_type_id,
										  stDeliver.stRequest.msg_content ) ;
		   /*if ( !cMidRequest.InsertRequest( stDeliver.stRequest ) )
				g_clLog.Error( 0,"Fail to CMidRequest::InsertRequest() ! " ) ;
		   */
		   if ( (nOpen=cSysMidMap.QuerySysMidMap( stDeliver.stRequest.inf_type_id )) > 0 )
		   {
				if ( (nOpen=cRequestWait.InsertRequest( stDeliver.stRequest )) < 0 )
					g_clLog.Error( 0, "Fail to Call CRequestWait::InsertRequest() !" ) ;
		   }
		   else
		   {
				if ( (nOpen=cMidRequest.InsertRequest( stDeliver.stRequest )) < 0 )
					g_clLog.Error( 0,"Fail to CMidRequest::InsertRequest() ! " ) ;
		   }
	   }
	   stDeliver.Clear() ;
#else
	   if ( stDeliver.bReport )
	   {//report
		   g_clLog.Info( 0, "R : %s %s %s %u !",stDeliver.stReport.dest_nbr,
			                              stDeliver.stReport.message_id,
										  stDeliver.stReport.state,
										  stDeliver.stReport.ErrorCode ) ;
		   if ( (nOpen=cDispenseReport.SetReport( stDeliver.stReport )) < 0 )
			   g_clLog.Error( 0,"Fail to CDispenseReport::SetReport() ! " ) ;
	   }
	   else
	   {//deliver
		   g_clLog.Info( 0, "R: %s %s%s %s!",stDeliver.stRequest.acct_nbr,
			                              stDeliver.stRequest.sp_code,
										  stDeliver.stRequest.inf_type_id,
										  stDeliver.stRequest.msg_content ) ;
		   
		   if ( (nOpen=cRequestWait.InsertRequest( stDeliver.stRequest )) < 0 )
			   g_clLog.Error( 0, "Fail to Call CRequestWait::InsertRequest() !" ) ;
	   }
	   stDeliver.Clear() ;
#endif
	   	if ( nOpen < 0 )
		{
			Sleep( 1000 ) ;
			g_clLog.Info( 0, "Connect again......!!!" ) ;
			XW_Logout() ;
			Login( m_DBServer ) ;
			nOpen = 1 ;
		}
	}
	
	return 0 ;
}

void StartProcThread()
{
	hProcThread = CreateThread(NULL, 0, ProcessDeliver, 0, 0, &nProcThreadID);
    if ( NULL == hProcThread )
    {
          g_clLog.Panic( 0, "Fail to start Process Deliver thread !" ) ;
    }  
}

int MainLoop()
{
	// Begin Listen
	g_cSgipRecv.BeginListen() ;

	int nWaitTime = 0 ;
	bool bListenAgain = false ;
	int  nRet = 0;

	while ( true )
	{
		nRet = g_cSgipRecv.Accept( ) ;
		
		g_clLog.DeBug(0, "---Accept return ret = %d !",nRet ) ;

		if ( nRet > 0 )
		{//recv
			g_cClientPool.PutData( nRet ) ;
			nWaitTime = 0 ;
		}
		else if ( nRet == 0 )
		{//
			nWaitTime++;
			if (nWaitTime >= 900 )
			{
				bListenAgain = true ;
				g_clLog.Info(0, "Listen again for TimeOut !");
			}
			m_clSetDayLog.SetNextLog();
		}
		else
		{//error
			bListenAgain = true ;
			g_clLog.Error(0, "Listen again for Error !");
		}

		if ( bListenAgain )
		{
			bListenAgain = false ;
			nWaitTime = 0 ;
			g_cSgipRecv.EndListen();
			g_cSgipRecv.BeginListen() ;
		}
	}//end while
		
	return 0 ;
}

int main(int argc, char **argv)
{
	//catch SIGINT(CTRL+C) signal to process it by myself
	signal(SIGINT, SignalHandler);

 	strcpy( m_sProcName,"smrecv" ) ;
	switch ( argc )
	{
	case 2:
		//used for reading profile
		strcpy( m_sProcName,argv[1] ) ;
		break ;
	case 3:
		//used for reading profile
		strcpy( m_sProcName,argv[1] ) ;
		g_clProfile.SetFileName(argv[2]);
		break;
	}

	Init() ;
	g_clLog.Info(0, "Program \"%s.exe\" start !", m_sProcName);
	
	StartRecvhread() ;
	StartProcThread() ;
	
	MainLoop() ;
	
	Exit() ;

	return 0 ;
}

⌨️ 快捷键说明

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