📄 mbtocis.cpp
字号:
#include "CisPub.hpp"#include "Com_lzser.h"#include "Com_wzser.h"/******************************************************************************* CisServer.bin的功能说明** 多进程管理** 接收从柜台发来的业务*****************************************************************************/#ifndef BOOL#endif#ifndef TRUE #define TRUE 1#endif//全局数据tagMyGlobe *myglobe = new tagMyGlobe;ComSocket_lzSer *comRecvSocket;BepsPub sBepsPub;SybServer m_clsSybServer;BOOL g_bSendConnect = FALSE;int StrTrimChar( char *in_data ,char c){ int i,j = 0; char tmp[1024]; memset(tmp,0,sizeof(tmp)); for(i = 0;i < strlen(in_data);i++) { if(in_data[i] != c) tmp[j++] = in_data[i]; } strcpy( in_data , tmp); return TRUE;}/////////////////////////////////// 主函数int main(int argc, char* argv[]){ //显示其版本号 if(argc == 2 && (memcmp(argv[1], "-v", 2)==0||memcmp(argv[1], "-V", 2)==0)) { printf("System verison is: Direct CIS CisInterworkMb MbToCis V002R003C010\n"); exit(0); } //使主进程成为守护进程 pid_t main_pid =0; main_pid = fork(); if(0!= main_pid) { exit(0); } printf("System verison is: Direct CIS CisInterworkMb MbToCis V002R003C010\n"); printf("System init ......\n"); //调用初始全局数据函数 int iRetval; ComSocket_lzSer comRecv; comRecvSocket = &comRecv; if(!InitGlobeData()) { printf( "[CISInterworkMB.bin] 初始全局失败,程序退出" ); exit (-1); } //通讯插件开始工作 iRetval = comRecvSocket->Open(); if(iRetval != 0) { printf( "[CISInterworkMB.bin]打开接收通讯端口失败, 程序退出"); exit(-1); } //连接数据库 char Password[20]; memset( Password , 0 , sizeof( Password )); strcpy( Password , getenv("PASSWORD")); char DBUserName[10]; memset( DBUserName , 0 , sizeof(DBUserName)); strcpy( DBUserName , getenv("DBUSERNAME")); StrTrimChar(DBUserName,' '); if( strlen( DBUserName ) == 0 ) { strcpy( DBUserName , "sa"); } printf("DBUserName = [%s]\n", DBUserName); int ret = connectToDB( "SYBASE" , DBUserName , Password , "DIRWAYSDB"); if( ret != 0 ) { printf( "[CISInterworkMB.bin]连接数据库失败, 程序退出"); exit(-1); } //初始化本地成员 char sResDesc[MAX_MBFE_BUFFER]; char RecvBuf[MAXBUFF]; int RecvBufLen = 0, SendBufLen = 0; char TrNo[6+1]; int RetCode = 0; char RetMsg[100],Result[MAXBUFF]; while(1) //开始服务 { //侦听接收客户端的连接 if(0 == comRecvSocket->Service()) { filelog(CIS_INTER_MB, "\n[MbToCis]Success accept client IP=[%s]\n", comRecvSocket->Mac_IP); printf("Success accept recv client IP=[%s]\n", comRecvSocket->Mac_IP); } #if 0 FD_ZERO(&rfds); FD_SET(comRecvSocket->connfd, &rfds); ret = select(FD_SETSIZE,(fd_set*)&rfds, NULL, NULL, &timeout); if (ret < 0) { timeout.tv_sec = 0; timeout.tv_usec = 100; } else { timeout.tv_sec = 0; timeout.tv_usec = 0; }#endif //验证如果套接有消息则接收消息 // if (FD_ISSET(comRecvSocket->connfd, &rfds))// { memset( RecvBuf , 0 , sizeof( RecvBuf )); memset( RetMsg , 0 , sizeof( RetMsg )); memset( Result , 0 , sizeof( Result )); memset( sResDesc , 0 , sizeof( sResDesc )); //收取信息 可以处理阻塞 iRetval = comRecvSocket->Recv(RecvBuf, &RecvBufLen, &RetCode, RetMsg); if ( iRetval != 0 && iRetval !=2 ) { filelog(CIS_INTER_MB, "\n[MbToCis]Disconnect from client and close socket!\n"); printf("Disconnect from client and close socket!\n"); //客户端已断开,重新初始化客户端的套接字,准备新的连接 comRecvSocket->msocket_id = -1; comRecvSocket->Close(); continue; } //非法消息不处理 if( iRetval == 2) { memset( RecvBuf , 0 , sizeof( RecvBuf )); filelog(CIS_INTER_MB, "\n[MbToCis]Invalid message and discare not process!\n"); printf( "Invalid message and discare not process!\n"); continue; } if(strlen(RecvBuf) > 0 ) { RecvBuf[RecvBufLen] = '\0'; printf("Recv = [%d][%s]\n", strlen(RecvBuf),RecvBuf); filelog(CIS_INTER_MB, "\n[MbToCis]RecvBuff is:[%d][%s]\n", strlen(RecvBuf),RecvBuf); //获取消息的标识 memset( TrNo , 0 , sizeof( TrNo )); memcpy( TrNo , RecvBuf , 6); char Tmp[1024*10],CmtNo[4]; memset( Tmp ,0 , sizeof( Tmp )); memset( CmtNo ,0 , sizeof( CmtNo )); memcpy( CmtNo , RecvBuf + 3 , 3 ); printf("当前交易码 = [%s]\n", TrNo); //add by limh 0526 格式化成标准格式 begin if( strncmp( CmtNo , "100" , 3) == 0||strncmp( CmtNo , "101",3) == 0) { if(0!=sBepsPub.DataChange(CmtNo, RecvBuf, Tmp, MB)) { printf("TAG格式转换成FIXED格式失败\n"); continue; } }else{ strcpy( Tmp , RecvBuf); } printf("DataChange =Before [%s]\n", RecvBuf); printf("DataChange =After [%s]\n", Tmp); //modify by limh 0528 返回内容签名增加返回码 char TransRetMsg[250]; memset( TransRetMsg , 0 , sizeof( TransRetMsg)); ret = sBepsPub.CisServer( TrNo , Tmp, Result, sResDesc); if( ret <= 0 ) { filelog(CIS_INTER_MB, "\n[MbToCis]交易处理失败[%s]\n",sResDesc); printf("交易处理失败[%s]\n",sResDesc); sprintf( TransRetMsg , "001%s",sResDesc); // continue; }else{ sprintf( TransRetMsg , "000%s",sResDesc); } //SendBufLen = strlen(sResDesc); SendBufLen = strlen(TransRetMsg); //返回处理结果 printf("\r\n TransRetMsg=[%s] SendBufLen=[%d]",TransRetMsg,SendBufLen); iRetval = comRecvSocket->Send(TransRetMsg,0,SendBufLen, &RetCode,RetMsg); if ( iRetval != 0 ) { filelog(CIS_INTER_MB, "\n[MbToCis]Send data failed and close socket!\n"); printf("Send data failed and close socket!\n"); comRecvSocket->msocket_id = -1; comRecvSocket->Close(); continue; } } // } sleep(1); }}//子进程的处理函数//设置SOCKET为非阻塞方式//int flag = fcntl(pMsSoket->connfd, F_GETFL, 0);//fcntl(pMsSoket->connfd, F_SETFL, O_NONBLOCK|flag);/*void ChildProcess(){ filelog(CIS_INTER_MB, "\n[MbToCis]Create process and accept client!\n"); //开始子进程的具体处理 ComSocket_wzSer *pMsSoket = NULL; pMsSoket = new ComSocket_wzSer; if(NULL == pMsSoket) { errlog( "[CisServer.bin] 分配空间失败!"); exit(-1); } //保存主进程当时接收的客户端的IP和套接字 pMsSoket->connfd = comSocket->connfd; memcpy(pMsSoket->clientIp, comSocket->clientIp, strlen(comSocket->clientIp)); int fd = pMsSoket->connfd; memcpy(sBepsPub.m_UserIpAddr, comSocket->clientIp, strlen(comSocket->clientIp)); fd_set rfds; timeval timeout; timeout.tv_sec = 0; timeout.tv_usec = 0; pid_t pid = getpid(); pid_t pgrp = getpgrp(); pid_t ppid = getppid(); pid_t pgid = getpgid(pid); printf("\nNew process info:pid=[%06d],gprp=[%06d],ppid=[%06d],pgid[%06d]\n", pid, pgrp, ppid, pgid); printf("Process ClientIP=[%s] Socket=[%d]\n", pMsSoket->clientIp, pMsSoket->connfd); char sResDesc[MAX_MBFE_BUFFER]; char SendBuf[10]; char *RecvBuf = 0; int RecvBufLen = 0, SendBufLen = 0; int Lock_Ret = 0; CURR_INF curr_inf; CONF_VAL *conf; int accflag, ret; char TrNo[7]; int RetCode = 0, iRetval = 0; char *RetMsg = 0,*Result = 0; char sysValue[21]; char resStr[100]; char Password[20]; memset(sysValue, 0, 21); memset(resStr, 0, 100); accflag = 1; memset( Password , 0 , sizeof(Password)); sprintf( Password , "%s", getenv("PASSWORD")); ret = connectToDB( "SYBASE" , "sa", Password ,"DIRWAYSDB"); if( ret != 0 ) { delete pMsSoket; exit(-1); } if(FALSE==sBepsPub.GetSysCtlPar(NUM_CONNECT, sysValue, resStr)) { printf("%s\n", resStr); delete pMsSoket; exit(-1); } filelog(CIS_INTER_MB, "\n[MbToCis]sysValue=[%s] sysConfig=[%s]\n", sysValue, myglobe->chiProcCount); if(atoi(sysValue)>atoi(myglobe->chiProcCount)) { pMsSoket->SentHeartMsg("000009NOCONNECT"); pMsSoket->Close(); disConnectToDB(); filelog(CIS_INTER_MB, "\n[MbToCis]Connect count is max and disconnect client=[%s]\n", pMsSoket->clientIp); exit(-1); } sBepsPub.UpdateNumClient(ADD_CONNECT); int Xflag = 0; ret = sBepsPub.CheckSerail(&Xflag); if( Xflag != 0) { printf("system not permission exit\n"); exit(0); } printf("System start.....\n"); RecvBuf = ( char *)malloc(MAXBUFF*sizeof(char)); Result = ( char *)malloc(MAXBUFF*sizeof(char)); RetMsg = ( char *)malloc(100*sizeof(char)); conf = (CONF_VAL *)malloc( sizeof(CONF_VAL)); //配置的初始化 printf("System init ......\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -