📄 scomm_cups.c
字号:
*************************************************/
int SndandRcvCUPS(char *szAddress, char * buffer,int buflen,int * srlen)
{
key_t msgskey,msgrkey,msgctrlkey;
char sinifile[100],cfgbuf[100];
int msgsid,msgrid,msgctrlid;
int timeout,t0,ilen,ilen2,iret;
char sFklsh[11];
TMSG Smsg,Rmsg;
CTLMSG Sctlmsg;
if(getenv("GAPSETCDIR")==NULL)
snprintf(sinifile,sizeof(sinifile),"%s/etc/%s",getenv("HOME"),COMMCLIINI);
else snprintf(sinifile,sizeof(sinifile),"%s/%s",getenv("GAPSETCDIR"),COMMCLIINI);
memset(cfgbuf,0,sizeof(cfgbuf));
if(ExGetCfgItem(sinifile,"CUPS","SNDMSGKEY",cfgbuf,sizeof(cfgbuf))<0)
{
LOG(LM_DEBUG,Fmtmsg("银联处理[%s]配置文件配置项[%s]错误",sinifile,"SNDMSGKEY"),"")
return -3;
}
msgskey=atoi(cfgbuf);
memset(cfgbuf,0,sizeof(cfgbuf));
if(ExGetCfgItem(sinifile,"CUPS","RCVMSGKEY",cfgbuf,sizeof(cfgbuf))<0)
{
LOG(LM_DEBUG,Fmtmsg("银联处理[%s]配置文件配置项[%s]错误",sinifile,"RCVMSGKEY"),"")
return -3;
}
msgrkey=atoi(cfgbuf);
memset(cfgbuf,0,sizeof(cfgbuf));
if(ExGetCfgItem(sinifile,"CUPS","CTRLMSGKEY",cfgbuf,sizeof(cfgbuf))<0)
{
LOG(LM_DEBUG,Fmtmsg("银联处理[%s]配置文件配置项[%s]错误",sinifile,"CTRLMSGKEY"),"")
return -3;
}
msgctrlkey=atoi(cfgbuf);
memset(cfgbuf,0,sizeof(cfgbuf));
if(ExGetCfgItem(sinifile,"CUPS","TIMEOUT",cfgbuf,sizeof(cfgbuf))<0)
{
LOG(LM_DEBUG,Fmtmsg("银联处理[%s]配置文件配置项[%s]错误",sinifile,"TIMEOUT"),"")
return -3;
}
timeout=atoi(cfgbuf);
if((msgsid=msgget(msgskey,0666))<0)
{
LOG(LM_DEBUG,Fmtmsg("银联处理通讯服务未启动"),"")
return -3;
}
if((msgrid=msgget(msgrkey,0666))<0)
{
LOG(LM_DEBUG,Fmtmsg("银联处理通讯服务未启动"),"")
return -3;
}
if((msgctrlid=msgget(msgctrlkey,0666))<0)
{
LOG(LM_DEBUG,Fmtmsg("银联处理通讯服务未启动"),"")
return -3;
}
t0 = time(NULL);
LOG(LM_DEBUG,Fmtmsg("发卡系统接出报文[%s]",buffer),"")
memset(&Smsg, 0, sizeof(TMSG));
memcpy(Smsg.mtext,buffer,*srlen);
/*8583*//* if (Smsg.mtext[0]== ')')
{
Smsg.mtype=getmsgkey(buffer+41);
}
else
{
memset(sFklsh, 0, sizeof(sFklsh));
memcpy(sFklsh, Smsg.mtext+28, 6);
Smsg.mtype=atoi(sFklsh);
} */
Smsg.mtype=atol(szAddress);
/*写控制队列*/
memset(&Sctlmsg,0,sizeof(Sctlmsg));
Sctlmsg.mtype=Smsg.mtype;
Sctlmsg.msglen=*srlen;
Sctlmsg.pid=getpid();
Sctlmsg.msgtime=time(NULL);
Sctlmsg.stat=MSGSTAT_SENDWAIT;
if( (iret=msgsnd(msgctrlid,&Sctlmsg,sizeof(Sctlmsg)-sizeof(long),IPC_NOWAIT))!=0)
{
LOG(LM_DEBUG,Fmtmsg("Send err %d %d %d %d %s",msgctrlid,Smsg.mtype,iret,errno,strerror(errno)),"")
return -1;
}
/*写发送队列*/
if( (iret=msgsnd(msgsid,&Smsg,*srlen,IPC_NOWAIT))!=0)
{
LOG(LM_DEBUG,Fmtmsg("Send err %d %d %d %d %s",msgsid,Smsg.mtype,iret,errno,strerror(errno)),"")
ilen=msgrcv(msgctrlid,&Sctlmsg,sizeof(Sctlmsg),Smsg.mtype,IPC_NOWAIT);
return -1;
}
LOG(LM_DEBUG,Fmtmsg("sendlen=%d,mtype=%d",*srlen,Smsg.mtype),buffer)
memset(&Rmsg, 0, sizeof(TMSG));
memset(&Sctlmsg,0,sizeof(Sctlmsg));
iTimeout=0;
signal(SIGALRM, (void (*)())Bitalarm);
alarm(timeout);
while( iTimeout == 0)
{
ilen=msgrcv(msgrid,&Rmsg,sizeof(Rmsg.mtext),Smsg.mtype,IPC_NOWAIT);
if (ilen > 0)
break;
// sleep(1);
}
alarm(0);
LOG(LM_DEBUG,Fmtmsg("ilen=%d",ilen),buffer)
/*while( iTimeout <= timeout)
{
ilen=msgrcv(msgrid,&Rmsg,sizeof(Rmsg.mtext),Smsg.mtype,IPC_NOWAIT);
if (ilen > 0)
break;
// sleep(1);
iTimeout++;
}
*/
if(ilen>0)
{
/*消息获取成功检查消息*/
ilen2=msgrcv(msgctrlid,&Sctlmsg,sizeof(Sctlmsg),Smsg.mtype,IPC_NOWAIT);
if ( ilen2>0 )
{
if (Sctlmsg.stat!=MSGSTAT_RECVSUCC)
{/*消息状态不符*/
LOG(LM_DEBUG,Fmtmsg("Comm err %d %d %d %d %s",msgsid,Smsg.mtype,Sctlmsg.stat,errno,strerror(errno)),"")
_WriteAbortmsg(Smsg.mtype,Smsg.mtext,ilen,Sctlmsg);
return -2;
}
if (time(NULL)-Sctlmsg.msgtime >MAXMSGLIVETIME )
{/*消息时间超时*/
LOG(LM_DEBUG,Fmtmsg("Comm err 消息超时 %d",time(NULL)-Sctlmsg.msgtime),"")
_WriteAbortmsg(Smsg.mtype,Smsg.mtext,ilen,Sctlmsg);
return -2;
}
}
else
{
LOG(LM_DEBUG,Fmtmsg("获取控制消息失败 %d %d %d %d %s",msgsid,Smsg.mtype,iret,errno,strerror(errno)),"")
_WriteAbortmsg(Smsg.mtype,Smsg.mtext,ilen,Sctlmsg);
return -2;
}
if(ilen>buflen)
ilen=buflen;
memset(buffer, 0, sizeof(buffer));
memcpy(buffer,Rmsg.mtext,ilen);
*srlen=ilen;
}
if ( ilen<0 )
{
/*失败处理*/
LOG(LM_DEBUG,Fmtmsg("Recv err %d %d %d %d %s",msgrid,Smsg.mtype,iret,errno,strerror(errno)),"")
ilen2=msgrcv(msgctrlid,&Sctlmsg,sizeof(Sctlmsg),Smsg.mtype,IPC_NOWAIT);
if ( ilen2>0 )
{
if (Sctlmsg.stat==MSGSTAT_RECVSUCC)
{/*已有应答,读掉应答以免阻塞*/
ilen=msgrcv(msgrid,&Rmsg,sizeof(Rmsg.mtext),Smsg.mtype,IPC_NOWAIT);
if ( ilen >0 )
_WriteAbortmsg(Rmsg.mtype,Rmsg.mtext,ilen,Sctlmsg);
return -2;
}
else if ( Sctlmsg.stat==MSGSTAT_SENDWAIT )
{/*请求未发送,读掉请求以免阻塞*/
ilen=msgrcv(msgsid,&Rmsg,sizeof(Rmsg.mtext),Smsg.mtype,IPC_NOWAIT|MSG_NOERROR);
return -1;
}
else
{
return -1;
}
}
else
{
LOG(LM_DEBUG,Fmtmsg("获取控制消息失败 %d %d %d %d %s",msgctrlid,Smsg.mtype,iret,errno,strerror(errno)),"")
ilen=msgrcv(msgsid,&Smsg,sizeof(Smsg.mtext),Smsg.mtype,IPC_NOWAIT|MSG_NOERROR);
return -2;
}
}
return 0;
}
int _WriteAbortmsg(int mtype,char *msgtext,int msglen,CTLMSG ctrlmsg)
{
char sinifile[100],cfgbuf[100],sHead[1024];
char sAmsgfile[100];
int iAmsgfhandle;
if(getenv("GAPSETCDIR")==NULL)
snprintf(sinifile,sizeof(sinifile),"%s/etc/%s",getenv("HOME"),COMMCLIINI);
else snprintf(sinifile,sizeof(sinifile),"%s/%s",getenv("GAPSETCDIR"),COMMCLIINI);
memset(cfgbuf,0,sizeof(cfgbuf));
if(ExGetCfgItem(sinifile,"CUPS","AMSGFILE",cfgbuf,sizeof(cfgbuf))<0)
{
LOG(LM_DEBUG,Fmtmsg("银联写异常信息失败:获取[%s]配置文件配置项[%s]错误",sinifile,"AMSGFILE"),"")
return -1;
}
if( cfgbuf[0]!='/')
snprintf(sAmsgfile,sizeof(sAmsgfile),"%s/%s",getenv("HOME"),cfgbuf);
else
strcpy(sAmsgfile,cfgbuf);
if ( (iAmsgfhandle = open(sAmsgfile,O_WRONLY|O_APPEND|O_CREAT,0666))< 0 )
{
LOG(LM_DEBUG,Fmtmsg("银联写异常信息失败[%s],%d,%s",sAmsgfile,errno,strerror(errno)),"")
return -2;
}
sprintf(sHead,"----mtype=%d,msglen=%d,stat=%d\n[",mtype,msglen,ctrlmsg.stat);
write(iAmsgfhandle, sHead, strlen(sHead)) ;
write(iAmsgfhandle, msgtext, msglen) ;
write(iAmsgfhandle, "]\n",2);
close(iAmsgfhandle);
return 0;
}
/******************************************************
功能:提取8583中系统跟踪号作为消息队列的键值,该函数仅
适用于银联8583
参数:通讯缓冲
返回:包文跟踪号
*******************************************************/
int getmsgkey(char * buffer)
{
char gzh[7];
unsigned char b[2];
int fdlen[11]={8,0,6,12,12,12,10,8,8,8};
int ilen=0,j=0,i;
char tmp[3];
memset(tmp,0,sizeof(tmp));
ilen+=4;
memcpy(b,buffer+ilen,2);
ilen+=8;
for(i=1;i<11;i++)
{
if((b[j]&0x80)>0)
{
if(fdlen[i-1]==0)
{/*LLVAR*/
memcpy(tmp,buffer+ilen,2);
ilen+=(2+atoi(tmp));
}
else ilen+=fdlen[i-1];
}
b[j]=b[j]<<1;
if(i%8==0) j++;
}
memset(gzh,0,sizeof(gzh));
memcpy(gzh,buffer+ilen,6);
return atoi(gzh);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -