📄 demo.c
字号:
/*************************************************************************** Copyright : 2001-2002, ASPIRE TECHNOLOGIES (SHENZHEN) LTD. Program ID : demo.c Description : API示例程序 Version : 1.6 ***************************************************************************/#include "demo.h"#include <signal.h>const int MaxSendPkgNum = 10000; //发送Submit包数 const int TimeOut = 10; //接收包超时const int nShowPkg = 1; //是否显示包内容 static RecCmppApi grecCmppApi;static int gnExitAllThread = 0;#ifdef WIN32int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *pProcAddr,const void *arg){ DWORD dw; *thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)pProcAddr, (LPVOID)arg, 0, &dw); if( *thread != NULL) return 0; return -1;}int pthread_join(pthread_t thread,void *value_ptr){ WaitForSingleObject( thread, INFINITE ); CloseHandle(thread); return 0;}#endifvoid vMarkSign(){#ifndef WIN32 signal(SIGTERM,SIG_IGN); signal(SIGHUP,SIG_IGN); signal(SIGPIPE,SIG_IGN); signal(SIGALRM,SIG_IGN); signal(SIGURG,SIG_IGN); signal(SIGCONT,SIG_IGN); signal(SIGCHLD,SIG_IGN); signal(SIGTTIN,SIG_IGN); signal(SIGTTOU,SIG_IGN); signal(SIGIO,SIG_IGN);#endif}int InitWSA(){#ifdef WIN32 WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 2, 2 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { return -1; } if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 ) { WSACleanup( ); return -1; }#endif return 0;}void CleanWSA(){#ifdef WIN32 WSACleanup();#endif}//打印回执内容void vPrintReceipt(recCmppReceiptBody rCmppReceiptBody){ printf("\tCMPP RECEIPT CONTENT:\n" "\t\tMsgId =%02x %02x %02x %02x %02x %02x %02x %02x\n" "\t\tchStat =%s\n " "\t\tSubmitTime =%s\n " "\t\tDoneTime =%s\n " "\t\tDestTerminalId =%s\n " "\t\tSmscSequenceId =%d\n\n ", rCmppReceiptBody.uchMsgId[0], rCmppReceiptBody.uchMsgId[1], rCmppReceiptBody.uchMsgId[2], rCmppReceiptBody.uchMsgId[3], rCmppReceiptBody.uchMsgId[4], rCmppReceiptBody.uchMsgId[5], rCmppReceiptBody.uchMsgId[6], rCmppReceiptBody.uchMsgId[7], rCmppReceiptBody.chStat, rCmppReceiptBody.chSubmitTime, rCmppReceiptBody.chDoneTime, rCmppReceiptBody.chDestTerminalId, rCmppReceiptBody.unSmscSequence ); }//打印Deliver包内容void vPrintDeliver(recCmppDeliver rCmppDeliver){ printf("\tCMPP Deliver CONTENT:\n" "\t\tMsgId =%s\n" "\t\tDestId =%s\n" "\t\tServid =%s\n" "\t\tTpPid =0x%2x\n" "\t\tTpUdhi =0x%2x\n" "\t\tMsgFmt =0x%2x\n" "\t\tSrcTermId =%s\n" "\t\tSrcTermIdType =0x%2x\n" "\t\tRegisterDel =0x%2x\n" "\t\tMsg_Len =0x%2x\n" "\t\tMsgContent =%s\n" "\t\tLinkId =%s\n", rCmppDeliver.uchMsgId, rCmppDeliver.chDestId, rCmppDeliver.chServiceId, rCmppDeliver.ucTpPid, rCmppDeliver.ucTpUdhi, rCmppDeliver.ucMsgFmt, rCmppDeliver.chSrcTerminalId, rCmppDeliver.ucSrcTerminalType, rCmppDeliver.ucRegisterDelivery, rCmppDeliver.ucMsgLength, rCmppDeliver.chMsgContent, rCmppDeliver.chLinkId); }//重新初始化API接口int nReInitInterface(){ int nRetry; int nRet; nRet =0 ; nRetry = 0; if(grecCmppApi.nInterfaceFlag == INITING || grecCmppApi.nInterfaceFlag != EXIT) { return 0; } grecCmppApi.nInterfaceFlag = INITING; while(nRetry < 30 ) { printf("\tReInitInterface,retry time <%d>\n\n",nRetry); nRet = nHInitInterface("cmppapi.ini"); if(API_OK == nRet) { grecCmppApi.nInterfaceFlag = INITED; return 0; } nRetry++; sleep(1); } grecCmppApi.nInterfaceFlag = EXIT; return -1; }//发包线程函数void *vSendPkg(){ int nSendPacketNum; int nRet; char sLastTermId[100]; char sMsgId[21]; char chResult; recSubmit rSubmit; //初始化 nSendPacketNum = 0; chResult = 0; //发包主循环 while(1) { //Ctrl+C退出 if(gnExitAllThread) { printf("\tsend thread exit by Ctrl+c !\n\n"); return NULL; } //判断是否发送完毕 if(nSendPacketNum > MaxSendPkgNum) { printf("\tsend thread exit normally!\n\n"); return NULL; } //连接是否可用 if(grecCmppApi.nInterfaceFlag != INITED ) { usleep(10); continue; } memset(sMsgId, 0 , sizeof(sMsgId)); memset( &rSubmit, 0, sizeof( recSubmit ) ); //strcpy( rSubmit.sInMsgId, "12345678" ); //strcpy( rSubmit.sFeeTerminalId, "13900010001" ); strcpy( rSubmit.sFeeType, MacFeeType_Free ); rSubmit.nFeeCode = 0; strcpy( rSubmit.sServiceId, "QXYG" ); rSubmit.ucTpPid = 0 ; rSubmit.ucTpUdhi = 0; strcpy( rSubmit.sSrcId, "222222" ); rSubmit.ucPkTotal = 1; rSubmit.ucPkNumber = 1; rSubmit.ucFeeTerminalType = 0; // strcpy(rSubmit.sFeeTerminalId, "13910000001"); // strcpy(rSubmit.sLinkId, "12345678901234567890"); nRet = nHSubmit( &rSubmit, "13530300001", 0, MSG_FORMAT_GB, //unsigned char ucMsgFmt, 10, //const unsigned char ucMsgLength, "1234567890", //const char *sMsgContent, sMsgId, // unsigned char *sMsgId, &chResult, sLastTermId ); //如果发送成功 if( API_OK == nRet ) { printf("\tsend submit success!count=[%d]\n\n",nSendPacketNum); if(chResult != 0) { printf("\tsubmit result =%d\n\n",chResult); } } //如果返回的协议错误 else if(nRet == API_ERR_RESP) { printf("\tsubmit fail ,please check to ensure nHsubmit parameter is correct!the count is [%d]\n\n",nSendPacketNum); } //如果发送超时 else if(nRet == API_ERR_TIMEOUT) { printf("\tsend the message timeout ,the count is [%d]\n\n",nSendPacketNum); grecCmppApi.nSendFailNum++; } else { //打印错误信息 printf("\tError:%s,the count is [%d]\n\n",sGetErrorDetail(),nSendPacketNum); grecCmppApi.nSendFailNum++; //退出接口 nRet = nHExitInterface(); if(nRet < 0 ) { printf("\tExitInterface Fail!\n\n"); return NULL; } //重新初始化 grecCmppApi.nInterfaceFlag = EXIT; nRet = nReInitInterface(); if(nRet < 0 ) { printf("\tnReInitInterface Fail, send thread Exit!\n\n"); return NULL; } } nSendPacketNum++; } return NULL;}//收包线程函数void *vRecvPkg(void *nNum){ int nReceiptOrNot; int nRet; int nSequence; time_t timetCurrent; time_t timetRecvLast; recCmppDeliver myDeliver; recCmppReceiptBody myReceiptBody; //初始化 nReceiptOrNot = 0; nRet = 0; nSequence = 0; time(&timetCurrent); time(&timetRecvLast); //收包主循环 while(1) { time(&timetCurrent); //超时没有收到包退出线程 if( timetCurrent - timetRecvLast > TimeOut) { printf("\trecv thread exit by time out!\n\n"); return NULL; } //Ctrl+C退出 if(gnExitAllThread) { printf("\trecv thread exit by Ctrl+c !\n\n"); return NULL; } if(grecCmppApi.nInterfaceFlag != INITED ) { usleep(10); continue; } //收包 memset(&myReceiptBody , 0 , sizeof(myReceiptBody)); memset(&myDeliver , 0 , sizeof(myDeliver)); //接收Deliver nRet = nHGetDeliver(&nReceiptOrNot, &nSequence, &myDeliver, &myReceiptBody, 10 ); //如果没有包 if(nRet == API_NOCELL) { usleep(10); } //如果收包成功 else if( nRet == API_OK) { printf("\tReceive a delivery packet\n\n"); grecCmppApi.nRecvDeliverNum++; //打印Deliver包 if(nShowPkg) { vPrintDeliver(myDeliver); } //如果是回执 if( nReceiptOrNot == 1) { grecCmppApi.nRecvReceiptNum++; //打印回执包 if(nShowPkg) { vPrintReceipt(myReceiptBody); } } //记录时间 time(&timetRecvLast); } //如果收包失败 else { printf("\tget deliver return %d\n\n",nRet); usleep(10); } } return NULL;}//设置线程退出标志void *vKillThread(){ gnExitAllThread = 1; printf("\tset thread exit flag succ!\n\n"); return NULL;}//主函数int main(int argc ,char *argv[]){ int nRet; //初始化 nRet = 0; vMarkSign(); InitWSA(); memset(&grecCmppApi,0 , sizeof(grecCmppApi)); //初始化API接口 nRet = nHInitInterface( "cmppapi.ini"); if(0 > nRet) { printf("\tInit Fail ,Return Code = %d\n\n",nRet); printf( "error reason = %s\n", sGetErrorReason( nRet ) ); return -1; } grecCmppApi.nInterfaceFlag = INITED; //启动线程 nRet = pthread_create((void*)&grecCmppApi.nSendThreadId,NULL, vSendPkg,NULL); if(nRet != 0) { printf("\tFail to create send packet thread \n\n"); return -1; } else { printf("\tSucc to create send packet thread \n\n"); } nRet = pthread_create((void *)&grecCmppApi.nRecvThreadId,NULL, vRecvPkg,NULL); if(nRet != 0) { printf("\tFail to create receive packet thread \n\n"); return -1; } else { printf("\tSucc to create receive packet thread \n\n"); } //中断处理函数 signal(SIGINT,(void *)vKillThread); //等待线程结束 nRet = pthread_join(grecCmppApi.nSendThreadId,NULL); if(nRet != 0) { printf("\tFail to join send packet thread\n\n"); } nRet = pthread_join(grecCmppApi.nRecvThreadId,NULL); if(nRet != 0) { printf("\tFail to join receive packet thread \n\n"); } //退出API接口 nHExitInterface(); //显示结果 printf("\n\n\t================Statistic Result:===================\n\n"); printf("\tReceive Deliver Packet Num = %d\n" "\tReceive Receipt Packet Num = %d\n" "\tSend fail Num = %d\n\n", grecCmppApi.nRecvDeliverNum, grecCmppApi.nRecvReceiptNum, grecCmppApi.nSendFailNum ); printf("please input any key to end ......!\n\n"); scanf("!"); //结束处理 CleanWSA(); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -