?? 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; //發(fā)送Submit包數(shù) const int TimeOut = 10; //接收包超時const int nShowPkg = 1; //是否顯示包內(nèi)容 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}//打印回執(zhí)內(nèi)容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包內(nèi)容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; }//發(fā)包線程函數(shù)void *vSendPkg(){ int nSendPacketNum; int nRet; char sLastTermId[100]; char sMsgId[21]; char chResult; recSubmit rSubmit; //初始化 nSendPacketNum = 0; chResult = 0; //發(fā)包主循環(huán) while(1) { //Ctrl+C退出 if(gnExitAllThread) { printf("\tsend thread exit by Ctrl+c !\n\n"); return NULL; } //判斷是否發(fā)送完畢 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 ); //如果發(fā)送成功 if( API_OK == nRet ) { printf("\tsend submit success!count=[%d]\n\n",nSendPacketNum); if(chResult != 0) { printf("\tsubmit result =%d\n\n",chResult); } } //如果返回的協(xié)議錯誤 else if(nRet == API_ERR_RESP) { printf("\tsubmit fail ,please check to ensure nHsubmit parameter is correct!the count is [%d]\n\n",nSendPacketNum); } //如果發(fā)送超時 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;}//收包線程函數(shù)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); //收包主循環(huán) 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); } //如果是回執(zhí) if( nReceiptOrNot == 1) { grecCmppApi.nRecvReceiptNum++; //打印回執(zhí)包 if(nShowPkg) { vPrintReceipt(myReceiptBody); } } //記錄時間 time(&timetRecvLast); } //如果收包失敗 else { printf("\tget deliver return %d\n\n",nRet); usleep(10); } } return NULL;}//設(shè)置線程退出標志void *vKillThread(){ gnExitAllThread = 1; printf("\tset thread exit flag succ!\n\n"); return NULL;}//主函數(shù)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"); } //中斷處理函數(shù) signal(SIGINT,(void *)vKillThread); //等待線程結(jié)束 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(); //顯示結(jié)果 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("!"); //結(jié)束處理 CleanWSA(); return 0;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -