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

📄 sendrecv.c

📁 小灵通系统网关程序
💻 C
字号:
/********************************************************************
ModuleName: 	SMGW Client API Example Program
FileName:		Sendrecv.c 
DESCRIPTION:    Receive And Send Message Multi-Thread Example Program
				Example Just For WIN32 Plat
History:                                                        
Date       	Version			Modifier			 	Activies
2002/03/19	1.0				Zhang Jiebin			Create
********************************************************************/

/********************************************************************
Include Files 
********************************************************************/
#include <windows.h>
#include <stdio.h>

#include "sendrecv.h"

/********************************************************************
Macro Definition
********************************************************************/
#define MSG_DEFAULT_INT							0
#define MSG_DEFAULT_STRING						"default"

#define RECV_MSG_TIME_OUT						1 /* Seconds */	
#define RECEIVE_MSG_FROM_SMGW					1000		

/********************************************************************
Global Var Definition
********************************************************************/
/* SMGPSendSingle Param */
static SMGP_Submit 	g_SMGP_Submit;
/* Semd Msg Msg Id */
static char 		g_sMsgID[10];
/* Send Msg Error Num */
static int			g_nErrorCode = 0xff;
/* Send Msg Config File Dir */
static char 		g_cConfigFileDir[200];
/* About Thread */
static DWORD		g_nApplicationThreadId;
static DWORD		g_nReceiveThreadId;
static HANDLE		g_hApplicationHnd;
static HANDLE		g_hReceiveHnd;
/* SMGPDeliver Output Param */ 
static DeliverResp	g_DeliverResp;

/********************************************************************
Local Function Definition
********************************************************************/
static int ReadSmFile(const char *pSmConfigFile);
static int SendMsgToSMGW(void);
static DWORD WINAPI ApplicationThread(LPVOID);  
static DWORD WINAPI ReceiveThread(LPVOID);  

/********************************************************************
FunctionName:	main 
DESCRIPTION:    Main Function         
Input:			None 
Output:			None
Return:			None
********************************************************************/
void main(void)
{
	/* Local Vars */
	int 	nRetCode;

	/* Call API InitSMGPAPI*/
	/* If Your Config File Not In Current Dir Or Config Dir Please */
	/* All Dir And File Name */	 
	nRetCode = InitSMGPAPI(NULL);
	if(nRetCode)
	{
		printf("InitSMGPAPI Return Error\n");
	}
	
	/* Create Application Thread */
	g_hApplicationHnd = CreateThread(NULL,0,ApplicationThread,NULL,0,&g_nApplicationThreadId);
	if(NULL == g_hApplicationHnd)
	{
		printf("CreateThread Return Error\n");
	}
	
	/* Create Receive Thread */
	g_hReceiveHnd = CreateThread(NULL,0,ReceiveThread,NULL,0,&g_nReceiveThreadId);
	if(NULL == g_hApplicationHnd)
	{
		printf("CreateThread Return Error\n");
	}
	
	Sleep(1000000);

	return;
}

/********************************************************************
FunctionName:	SendMsgToSMGW 
DESCRIPTION:    Send Sinle Msg To SMGW         
Input:			None
Output:			None
Return:			0:Sucess;1:Error
********************************************************************/
static int SendMsgToSMGW(void)
{
	/* Local Vars */
	int nRetCode;
	/* Read Send Msg Param From Config File */
	/* Just A Example You Can Get These Params From Other Method */
	nRetCode = ReadSmFile((char *)SEMD_MSG_CONFIG_FILE);
	if(nRetCode)
	{
		printf("ReadSmFile Return Error\n");
	}
	
	/* Call API Function SMGPSendSingle To Send Msg */ 
	nRetCode = SMGPSendSingle(	g_SMGP_Submit.nNeedReport,
								g_SMGP_Submit.nPriority,
								g_SMGP_Submit.sServerId,
								g_SMGP_Submit.nMsgFormat,
								g_SMGP_Submit.sFeeType,
								g_SMGP_Submit.sFeeCode,
								g_SMGP_Submit.sValidTime,
								g_SMGP_Submit.sAtTime,
								g_SMGP_Submit.sChargeTermId,
								g_SMGP_Submit.sDestTermId,
								g_SMGP_Submit.sReplyPath,
								g_SMGP_Submit.nMsgLength,
								g_SMGP_Submit.sMsgContent,
								g_sMsgID,
								&g_nErrorCode
							 );
	/* If Return Error */
	if(nRetCode)
	{
		printf("SMGPSendSingle Return Error\n");
		printf("The Error Code Is %d",&g_nErrorCode);
		return 1;
	}
	/* Else Return Success */
	else 
	{
		printf("SMGPSendSingle Return Success\n");
		printf("You have sent a short message to SMGW\n");
	}
	return 0;
}

/********************************************************************
FunctionName:	ReadSmFile 
DESCRIPTION:    Read Send Msg Param From Config File         
Input:			pSmConfigFile, Send Msg Param Config File
Output:			None
Return:			0:Sucess;1:Error
********************************************************************/
static int ReadSmFile(const char *pSmConfigFile)
{
	/* Get Current Dir */
	GetCurrentDirectory(200,g_cConfigFileDir);
	#ifdef _WIN32 
	strcat(g_cConfigFileDir,"\\");
	#else
	strcat(g_cConfigFileDir,"/");
	#endif
	strcat(g_cConfigFileDir,pSmConfigFile);

	/* Get Params*/
	g_SMGP_Submit.nNeedReport = GetPrivateProfileInt
								("SendSingle",
								 "nNeedReply",
								 MSG_DEFAULT_INT,
								 g_cConfigFileDir
								);


	g_SMGP_Submit.nPriority	 = GetPrivateProfileInt
								("SendSingle",
								 "nMsgLevel",
								 MSG_DEFAULT_INT,
								 g_cConfigFileDir
								);

	
	GetPrivateProfileString		("SendSingle",
								 "sServiceID",
								 MSG_DEFAULT_STRING,
								 g_SMGP_Submit.sServerId,
								 10,
								 g_cConfigFileDir
								);

				
	g_SMGP_Submit.nMsgFormat = GetPrivateProfileInt
								("SendSingle",
								 "nMsgFormat",
								 MSG_DEFAULT_INT,
								 g_cConfigFileDir
								);
	
	GetPrivateProfileString		("SendSingle",
								 "sFeeType",
								 MSG_DEFAULT_STRING,
								 g_SMGP_Submit.sFeeType,
								 2,
								 g_cConfigFileDir
								);

	GetPrivateProfileString		("SendSingle",
								 "sFeeCode",
								 MSG_DEFAULT_STRING,
								 g_SMGP_Submit.sFeeCode,
								 6,
								 g_cConfigFileDir
								);
								
	GetPrivateProfileString		("SendSingle",
								 "sValidTime",
								 MSG_DEFAULT_STRING,
								 g_SMGP_Submit.sValidTime,
								 17,
								 g_cConfigFileDir
								);

	GetPrivateProfileString		("SendSingle",
								 "sAtTime",
								 MSG_DEFAULT_STRING,
								 g_SMGP_Submit.sAtTime,
								 17,
								 g_cConfigFileDir
								);
	

	GetPrivateProfileString		("SendSingle",
								 "sChargeTermID",
								 MSG_DEFAULT_STRING,
								 g_SMGP_Submit.sChargeTermId,
								 21,
								 g_cConfigFileDir
								);

	GetPrivateProfileString		("SendSingle",
								 "sDestTermID",
								 MSG_DEFAULT_STRING,
								 g_SMGP_Submit.sDestTermId,
								 21,
								 g_cConfigFileDir
								);


	GetPrivateProfileString		("SendSingle",
								 "sReplyPath",
								 MSG_DEFAULT_STRING,
								 g_SMGP_Submit.sReplyPath,
								 21,
								 g_cConfigFileDir
								);

	g_SMGP_Submit.nMsgLength = GetPrivateProfileInt
								("SendSingle",
								 "nMsgLen",
								 MSG_DEFAULT_INT,
								 g_cConfigFileDir
								);
	
	GetPrivateProfileString		("SendSingle",
								 "sMsgContent",
								 MSG_DEFAULT_STRING,
								 g_SMGP_Submit.sMsgContent,
								 252,
								 g_cConfigFileDir
								);
	
	return 0;
}
/********************************************************************
FunctionName:	ReceiveThread 
DESCRIPTION:    Receive Thread         
Input:			LPVOID Which Is NULL
Output:			None
Return:			0:Sucess
********************************************************************/
static DWORD WINAPI ReceiveThread(LPVOID pVoid)
{
	/* Local Vars */
	int 	nRetCode;

	for(;;)
	{
		/* Call SMGPDeliver To Get Message */ 
		nRetCode = SMGPDeliver((int)RECV_MSG_TIME_OUT,&g_DeliverResp);
		if(nRetCode)
		{
			/* You Can Process Error Here */
			printf("SMGPDeliver Return Error,If It Is Time Out,Not Caring For It\n");
			continue;
		}
		/* Post Message To Application Thread */
		else 
		{
			printf("SMGPDeliver Success\n");
			PostThreadMessage	(g_nApplicationThreadId,
								(int)RECEIVE_MSG_FROM_SMGW,
								(WPARAM)&g_DeliverResp,
								(LPARAM)sizeof(g_DeliverResp));
		}
			
	}

}

/********************************************************************
FunctionName:	ApplicationThread 
DESCRIPTION:    Application Thread         
Input:			LPVOID Which Is NULL
Output:			None
Return:			0:Sucess
********************************************************************/
static DWORD WINAPI ApplicationThread(LPVOID pVoid)
{
	/* Local Vars */
	MSG		msg;

	for(;;)
	{
		/* Get Message */
		GetMessage(&msg,NULL,0,0);
		
		/* Process Message */
		switch(msg.message)
		{
			case RECEIVE_MSG_FROM_SMGW:
				/* You Can Put Your Code Here */
				SendMsgToSMGW();
				break;
			default:
				printf("Get Exception Message\n");
				break;
		}
	}
}

/********************************************************************
End Of File
********************************************************************/

⌨️ 快捷键说明

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