📄 mesglib.c
字号:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#define MesgLib
#include "mesglib.h"
struct mesg_buf {
long mesg_type; /* message type */
u_char mesg_text[MAXDATALEN]; /* message text */
} rMesgBuf;
extern int errno;
/*
* Initialize the input message queue.
*/
int initMessages( long nMesgKey, int nMesgType )
{
int nMesgMd;
/*
* If initial the message queues failed,
* interrupt the running of program.
*/
if ( (nMesgMd = msgget(nMesgKey, 0666|IPC_CREAT))<0 ) {
return MECreate;
}
if ( nMesgType>=0 ) {
rMesgBuf.mesg_type = nMesgType;
while ( msgrcv( nMesgMd, (void *)&rMesgBuf,
MAXDATALEN, nMesgType, IPC_NOWAIT )>0 )
rMesgBuf.mesg_type = nMesgType;
}
return (nMesgMd);
} /* end initInputMessages() */
int clearMessages( int nMd, int nMesgType )
{
if ( nMd<0 )
return MEInvalidID;
if ( nMesgType<0 )
return MEInvalidArgu;
rMesgBuf.mesg_type = nMesgType;
while ( msgrcv( nMd, (void *)&rMesgBuf,
MAXDATALEN, nMesgType, IPC_NOWAIT )>0 )
rMesgBuf.mesg_type = nMesgType;
return 0;
} /* end clearInputMessages() */
/*
* Send a record to message queue.
* idtype : the type which we send messages to.
* message : the contents of messages.
* length : length of contents.
* sendmod : the mode of sending.
*/
int sendMessages( int fd, long idtype, u_char *message, int length, int sendmod )
{
int nReturn;
if ( fd<0 )
return MEInvalidID;
rMesgBuf.mesg_type = idtype;
if ( length>MAXDATALEN )
length = MAXDATALEN;
memcpy( rMesgBuf.mesg_text, message, length );
rMesgBuf.mesg_text[length] = 0x00;
if ( sendmod==NoDelay )
nReturn=msgsnd(fd,(void *)&rMesgBuf,length,IPC_NOWAIT);
else if ( sendmod==BlockDelay )
nReturn=msgsnd(fd,(void *)&rMesgBuf,length,0);
else
return MEInvalidArgu;
if ( nReturn!=0 ) {
/* return MESend; */
return nReturn;
}
return (0);
} /* end sendMessages() */
int nAlarmReturn;
void interrupt( void )
{
nAlarmReturn = 1;
} /* end interrupt() */
/*
* receive a record from message queue.
* idtype : the type which we receive messages from.
* message : the contents of messages.
* sendmod : the time of waiting, -1: No wait, -2: block.
*/
int recvMessages(int fd, long *idtype, u_char *message, int sendmod)
{
int length, maxlen;
int nOldTime;
long nFirstTime, nSecondTime;
void (*fOldFunc)();
if ( fd<0 )
return MEInvalidID;
maxlen = MAXDATALEN;
rMesgBuf.mesg_type = (*idtype);
if ( sendmod==NoDelay ) {
length = msgrcv( fd, (void *)&rMesgBuf,
maxlen, *idtype, IPC_NOWAIT );
} else if ( sendmod==BlockDelay ) {
length=msgrcv(fd,(void*)&rMesgBuf,maxlen,*idtype,0);
} else if ( sendmod>0 ) {
nAlarmReturn = 0;
fOldFunc = signal( SIGALRM, (void (*)())interrupt );
nOldTime = alarm( sendmod );
nFirstTime = time(NULL);
length=msgrcv(fd,(void*)&rMesgBuf,maxlen,*idtype,0);
if ( nAlarmReturn==1 )
length = -1;
nSecondTime = time(NULL);
if ( nOldTime>0 ) {
if ( nSecondTime-nFirstTime<nOldTime ) {
nOldTime -= nSecondTime-nFirstTime;
} else {
nOldTime = 1;
}
signal( SIGALRM, fOldFunc );
alarm( nOldTime );
} else {
alarm( 0 );
}
} else {
return MEInvalidArgu;
}
if ( length<0 )
return MEReceive;
*idtype = rMesgBuf.mesg_type;
memcpy( message, rMesgBuf.mesg_text, length );
return (length);
} /* end sendMessages() */
int initMsgQueSize(int nMsgId,int nMsgSize)
{
struct msqid_ds buf;
if (msgctl ( nMsgId,IPC_STAT,&buf ) != 0 )
return -1;
buf.msg_qbytes = nMsgSize;
if (msgctl( nMsgId,IPC_SET,&buf ) != 0 )
return -2;
if (msgctl ( nMsgId,IPC_STAT,&buf ) != 0 )
return -3;
if ( buf.msg_qbytes == nMsgSize )
return 0;
return -4;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -