- 论坛徽章:
- 0
|
/*============================
发送消息
==============================*/
int MsgSend(int aiMsgKeyID, /*消息队列的msgid,<=0表示要重启*/
long alReceiver, /*本消息的接收者*/
long alSender, /*发送者*/
int aiCommand, /*消息命令*/
const char *aszMsg, /*消息内容*/
int aiFlagBlock) /*0非阻塞,1阻塞*/
{
strMsgType strMsg;
int iMsgKeyID = 0;
int iFlag = 0;
memset(&strMsg,0x0,sizeof(strMsgType));
strMsg.iCommand = aiCommand;
strMsg.lReceiver = alReceiver;
strMsg.lSender = alSender;
strncpy(strMsg.szMsg,aszMsg,MSGLEN);
if (aiFlagBlock == 0) iFlag =IPC_NOWAIT;
else iFlag = 0;
if (aiMsgKeyID <= 0) /*需要重新取得MSGID*/
iMsgKeyID = msgget(MSGKEY,0666);
else iMsgKeyID = aiMsgKeyID;
if (iMsgKeyID< 0) return -1;
return msgsnd(iMsgKeyID,(struct msgbuf *)(&strMsg),sizeof(strMsgType),iFlag);
}
/*============================
接收消息
==============================*/
int MsgRcv(int aiMsgKeyID, /*消息队列的msgid,<=0表示要重启*/
long alReceiver, /*入参,本消息的接收者*/
int aiFlagBlock, /*0非阻塞,1阻塞*/
strMsgType *astrMsg) /*出参,消息内容*/
{
int iMsgKeyID = 0;
int iFlag = 0;
if (aiFlagBlock == 0) iFlag =IPC_NOWAIT;
else iFlag = 0;
if (aiMsgKeyID <= 0) /*需要重新取得MSGID*/
iMsgKeyID = msgget(MSGKEY,0666);
else iMsgKeyID = aiMsgKeyID;
if (iMsgKeyID< 0) return -1;
int nret =0;
nret = msgrcv(iMsgKeyID,(struct msgbuf *)(astrMsg),sizeof(strMsgType),alReceiver,iFlag);
LOGTRACE("iMsgKeyID=%d,nret======%d",iMsgKeyID,nret);
if (nret<=0)
return -1;
return 1;
}
int pf_GetOpsInfo(int aiMsgKeyID, /*消息队列的msgid,<=0需要重取*/
long alServiceID, /*SERVICE编号,<=0表示要重取*/
double adOps, /*临时表流水号*/
double adAcctID,
strCliShmType *apCliShm) /*出参,保存在共享内存中的信息*/
{
long lThisSrv,lSender;
char szMessage[MSGLEN + 1],szTemp[7];
int iSerial = 0,iCommand = 0;
strMsgType strMsg;
strCliShmType *pTempCliShm;
if (alServiceID <= 0) lThisSrv = (long)getpid();
else lThisSrv = alServiceID;
if (lThisSrv < MSGTYPESRV) lThisSrv += MSGTYPESRV;
sprintf(szMessage,"%12.0f%12.0f",adOps,adAcctID);
LOGTRACE("szMessage 1 = %s",szMessage);
if(MsgSend(aiMsgKeyID,MSGTYPECTL,lThisSrv,CMDOPSINF,szMessage,1)<0)
return -1;
LOGTRACE("szMessage 2 = %s",szMessage);
if(MsgRcv(aiMsgKeyID,lThisSrv,1,&strMsg)<0) return -1;
LOGTRACE("strMsg.szMsg 3 = %s",strMsg.szMsg);
strcpy(szMessage,strMsg.szMsg);
strncpy(szTemp,szMessage,6);
szTemp[6]='\0';
iSerial = atoi(szTemp);
if (iSerial == -1 || atof(szMessage+6) == -1)
return -1;
memcpy(apCliShm,&(strMsg.strShm),sizeof(strCliShmType));
return 0;
} |
|