jameshd
作者jameshd·2010-12-13 17:48
技术总监·胜科金仕达数据系统(中国)有限公司

msgrcv 函数,获取队列

字数 2972阅读 2824评论 0赞 0
int getqueue2(char *name,char *data,int datasize,int *type,struct msqid_ds *info)
{
 struct mymsgbuf msgbuf;
 int len,offset;
 int i,j;
 int handle,fh;
 char buf[255];
 handle=openqueue(name);
 if (handle == -1)
  return -1;
 msgctl(handle,IPC_STAT,info);
 for (i=0; i<60; i++)
 {
  len=msgrcv(handle,&msgbuf,MAXMSG,*type,IPC_NOWAIT);
  if (len < 0)
  {
   printf("getqueue %s msgrcv errno(%d)n",name,errno);
   sprintf(buf,"/tmp/ksqtmp/%s.ksf",name);
   fh=open(buf,O_RDWR,S_IREAD|S_IWRITE);
   if (fh == -1)
   {
    sleep(1);
    continue;
   }
   else
   {
    close(fh);
    break;
   }
  }
  *type=msgbuf.mtype;
  if (len == sizeof(int))
  {
   memcpy((char *)&len,msgbuf.mtext,len);
   sprintf(buf,"/tmp/ksqtmp/%s.ksf",name);
   fh=open(buf,O_RDWR,S_IREAD|S_IWRITE);
   if (fh == -1)
    return -2;
   for (j=0; j<60; j++)
   {
    lseek(fh,len,SEEK_SET);
    if (read(fh,&offset,sizeof(int)) != sizeof(int))
     continue;
    if (read(fh,data,offset) != offset)
     continue;
    close(fh);
    len=len+offset+sizeof(int);
    if (putqueue(name,(char *)&len,sizeof(int),1,NOWAIT_WRITE,NULL,NULL) < 0)
     return -3;
    return offset;
   }
   close(fh);
   return -4;
  }
  if (len > datasize)
  {
#ifdef VIEWMSG
    printf("getqueue2 %s imput bufferlen is too shoot %d %dn",name,len,datasize);
#endif
   memcpy(data,msgbuf.mtext,datasize);
   return len;
  }
  else
  {
   memcpy(data,msgbuf.mtext,len);
#ifdef VIEWMSG
    printf("getqueue2 %s return datalen %d successn",name,len);
#endif
   return len;
  }
 }
 sprintf(buf,"/tmp/ksqtmp/%s.ksf",name);
 fh=open(buf,O_RDWR,S_IREAD|S_IWRITE);
 if (fh == -1)
  return -5;
 for (j=0; j<60; j++)
 {
  lseek(fh,0,SEEK_SET);
  if (read(fh,&offset,sizeof(int)) != sizeof(int))
   continue;
  if (read(fh,data,offset) != offset)
   continue;
  close(fh);
  len=offset+sizeof(int);
  if (putqueue(name,(char *)&len,sizeof(int),1,NOWAIT_WRITE,NULL,NULL) < 0)
   return -6;
  return offset;
 }
 close(fh);
 return -7;
}

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

0

添加新评论0 条评论

Ctrl+Enter 发表

作者其他文章

相关问题

相关资料

X社区推广