📄 fifolib.c
字号:
#define _FIFOLIB_C
#include "fifolib.h"
/*main()
{
int nFd=0;
int nID=0;
char sItem[MaxFifoPckLen] = { 0 } ;
if ( (nFd = initFifo("/tmp/mytry"))<=0 ){
printf("Can not init Fifo , return =%d\n", nFd);
}
printf("set FIFO nodelay, return=%d\n", setFifoNDelay(nFd));
printf("init fifo OK \n");
printf("rcv Fm FIFO =%d\n", rcvFifoPck(nFd,&nID,sItem));
printf("Rcv: ProcID=%d item=%s\n", nID, sItem);
while ( 1 ) {
printf("snd to FIFO =%d\n",
sndFifoLimPck(nFd,1001,"123",3,FIFO_NDELAY));
sleep(1);
}
printf("rcv Fm FIFO =%d\n", rcvFifoPck(nFd,&nID,sItem));
printf("Rcv: ProcID=%d item=%s\n", nID, sItem);
} */
static void emptynil()
{}
static char creatLenFlag( int nLen )
{
unsigned char c;
c = nLen%127;
if ( c == '\0' )
c = '\0';
return c;
}
/*--------------------------------------------------------------*
* Name: isFifoFile *
* Desc: check The "sName" if exist. if not exist, creat *
* else if exist, but isn't a FIFO, then dele it ,creat *
* a FIFO file *
* Retn: -1 if unexist; 0 if exst, else -2; *
*--------------------------------------------------------------*/
int isFifoFile( char sName[] )
{
struct stat rStat;
if ( stat( sName, &rStat ) != 0 )
{
return -1;
}
if ( !S_ISFIFO( rStat.st_mode) )
{
/*
* the name hand exist, but not fifo file. now delete it ,
* then creat a FIFO
*/
if ( unlink(sName) !=0 )
return -2;
return -1;
}
return 0;
}
/*--------------------------------------------------------------*
* Name: initFifo *
* Desc: creat a fifo, return the fifo the identyfier. if the *
* FIFO had exist, only open the fifo and return fildhand *
* Para: sName: fifo name want creat or open. *
* Retn: if failed , return -1, else return a value > 0 *
*--------------------------------------------------------------*/
int initFifo( char sName[] )
{
int nRet;
nRet = isFifoFile(sName);
if ( nRet == -2 )
return -1;
if ( nRet == 0 )
{
return (open(sName,O_RDWR));
}
if ( mknod(sName, S_IFIFO|0666,0 ) != 0 )
return -1;
else
return (open(sName,O_RDWR));
}
/*--------------------------------------------------------------*
* Name : getFifoLength *
* Func : return the file(or fifo) <fd> size in bytes *
*--------------------------------------------------------------*/
int getFifoLength(int nFd)
{
struct stat rBuf;
if (fstat(nFd,&rBuf)==0)
return rBuf.st_size;
else
return -1;
}
/*--------------------------------------------------------------*
* Name: sndFifoPck *
* Func: put a packet to FIFO pointed nFd *
* Para: nFd: Fifo file desc. *
* nProcID: process ID *
* nFlag: 1.send mode(FIFO_DELAY, FIFO_NDELAY ) *
* 2.if nFlag not in send mode, as seconds before *
* return. *
* sItem: string to input to input to FIFO *
* nLen: length of sItem *
* Retn: return 0 when success, return -1 when Paras error, -2 *
* when Fifo had full , -3 when a system error *
*--------------------------------------------------------------*/
int sndFifoPck(int nFd,long nProcID,char sItem[],int nLen,int nFlag)
{
unsigned int nRestTime;
int nArgs;
void (*fSigAlr)();
char sFifoPck[MaxFifoPckLen+FifoPckHead+2] = {0}; /*nProcID(4) nLen(4) */
if ( nFd<0 || nLen>MaxFifoPckLen || nProcID<=0 || nProcID>9999 )
return -1;
sprintf(sFifoPck,"%c%04d%c%04d", PCK_SYN_CHAR, nLen,creatLenFlag(nLen),
nProcID );
memcpy(sFifoPck+FifoPckHead+1, sItem, nLen );
sFifoPck[FifoPckHead+1+nLen] = '\0';
if ( nFlag == FIFO_DELAY )
{
if( write( nFd, sFifoPck, nLen+FifoPckHead+1) != (nLen +FifoPckHead+1))
{
return -3;
}
return 0;
}
else if ( nFlag == FIFO_NDELAY){
if ( fcntl(nFd,F_GETFL,&nArgs) ==-1 )
return -2;
if ( fcntl(nFd,F_SETFL,nArgs|O_NDELAY) ==-1 )
return -3;
if( write( nFd, sFifoPck, nLen+FifoPckHead+1) != (nLen +FifoPckHead+1))
{
fcntl(nFd,F_SETFL,nArgs);
if ( errno == EAGAIN )
return -4;
return -5;
}
fcntl(nFd,F_SETFL,nArgs);
return 0;
}
else {
if( nFlag <= 0 )
nFlag = 1;
fSigAlr = signal(SIGALRM,(void (*)())emptynil );
nRestTime = alarm(nFlag);
nRestTime = ((nRestTime-nFlag) > 0)? (nRestTime-nFlag) : 1;
if( write( nFd, sFifoPck, nLen+FifoPckHead+1) != (nLen +FifoPckHead+1))
return -3;
signal(SIGALRM,fSigAlr);
alarm(nRestTime);
return 0;
}
}
int sndFifoLimPck(int nFd,long nProcID,char sItem[],int nLen,int nFlag)
{
int nFifoLen=0;
if ( nFd <= 0 )
return -11;
nFifoLen = getFifoLength( nFd );
if ( nFifoLen >= MaxFifoLimSize || nFifoLen < 0 )
return -12;
return ( sndFifoPck(nFd, nProcID, sItem, nLen, nFlag) );
}
/*--------------------------------------------------------------*
* Name: rcvFifoPck *
* Desc: read a packet from fifo-hd *
* Inpt: nFd: fifo file description *
* sItem: object string *
* nLen : the length of the item received *
* Ret: if sucesss, return 0 if nothing read, else return the *
* length of the packet read + FifoPckHead.return -1 while*
* any system error occured *
*--------------------------------------------------------------*/
int rcvFifoPck(int nFd,int *nProcID, char sItem[])
{
char sFifoPck[MaxFifoPckLen+1];
int nRet=0;
int nLen = 0;
char cLenFlag=0;
while( (nRet = read(nFd,&cLenFlag,1)) ==1 )
{
if (cLenFlag == PCK_SYN_CHAR)
break;
}
if ( nRet != 1 )
return 0;
if( (nRet = read(nFd,sFifoPck, FifoPckHead)) !=FifoPckHead )
{
return (nRet==0 ? 0 : -1 );
}
sFifoPck[FifoPckHead] = '\0';
(*nProcID) = atoi( sFifoPck+5);
cLenFlag = sFifoPck[4];
sFifoPck[4] = '\0';
nLen = atoi(sFifoPck);
if ( creatLenFlag(nLen) != cLenFlag)
return -1;
if( (nRet=read(nFd,sItem,nLen)) != nLen)
{
return -1;
}
sItem[nLen]='\0';
return (nLen+FifoPckHead);
}
int setFifoNDelay(int nFd)
{
int nArgs;
if ( fcntl(nFd,F_GETFL,&nArgs) == -1 )
return -1;
if ( fcntl(nFd,F_SETFL,nArgs|O_NDELAY) == -1 )
return -1;
return 0;
}
int setFifoDelay(int nFd)
{
int nArgs;
if ( fcntl(nFd,F_GETFL,&nArgs) == -1 )
return -1;
if ( nArgs & O_NDELAY != O_NDELAY )
return 0;
nArgs = (nArgs & (~O_NDELAY) );
if ( fcntl(nFd,F_SETFL,nArgs) == -1 )
return -1;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -