[Linux]解决msgsnd <Invalid argument>错误

结构:

typedef struct _msgque_{
long msgtype;
int8_t *msgbuf;
}msgque;

一、创建:

course_data_msgid = r_msg_create_u(msgid_data_key + cond->roomid);


if(course_data_msgid < 0) {
nslog(NS_ERROR, "r_msg_create course_data_msgid is failed!");
goto EXIT;
}


ret = set_msgquen_info(course_data_msgid , 8192);
if(ret == -1) {
nslog(NS_ERROR, "live mode: set msgqueue fail");
return OPERATION_ERR;
}
int qbyte = 0;
int qnum = 0;
get_msgquen_info(course_data_msgid, &qbyte, &qnum);
nslog(NS_WARN, "qbyte:[%d] qnum:[%d]", qbyte, qnum);

二、发送:

   
static int record_send_msg(const unsigned int msg_fd,const unsigned int steam_type,const char* pdata,const Media_Info_t stream)
{
if( msg_fd <= 0 || steam_type < 0 ||NULL == pdata || stream.data_len <= 0){
nslog(NS_ERROR,"steam_type=%d --errno=%d,msg_fd=%d-pdata=%p,data_len=%d\n",steam_type,msg_fd,pdata,stream.data_len);
return -1;
}

int ret=0;
msgque msg;
parse_data_t*  msg_payload =(unsigned char*)malloc(sizeof(parse_data_t));
if(NULL ==msg_payload  ){
nslog(NS_ERROR,"NULL ==  msg_payload !\n");
return -1;
}
msg_payload->data = (unsigned char*)malloc(stream.data_len);
if(NULL ==  msg_payload->data  ){
nslog(NS_ERROR,"NULL ==  msg_payload->data  !\n");
free(msg_payload);
msg_payload=NULL;
return -1;
}
msg_payload->data_len = stream.data_len;

r_memset(msg_payload->data, 0, sizeof(stream.data_len));
r_memset(&msg, 0, sizeof(msgque));
memcpy(msg_payload->data,pdata,stream.data_len);
msg_payload->width = stream.width;
msg_payload->height = stream.height;
msg_payload->data_type = stream.data_type;
msg_payload->flags = stream.Iframe;
msg_payload->time_tick = stream.time_tick;
msg_payload->sindex = stream.sindex;
msg.msgtype = msg_payload->sindex+1 ;
msg.msgbuf = msg_payload;

ret = r_msg_send(msg_fd,&msg, sizeof(msgque)-sizeof(long) ,IPC_NOWAIT);
if(ret <0){
if(0 == msg_payload->sindex ){
nslog(NS_ERROR,"msg.msgbuf=%p\n",msg.msgbuf);
}
nslog(NS_ERROR,"sindex=%d -moive=%d,fd=%d--errno=%d,<%s>\n",msg_payload->sindex ,stream.moive_res,msg_fd, errno,strerror(errno));
free( msg_payload->data);
msg_payload->data=NULL;
free(msg_payload);
msg_payload=NULL;
}
return ret;
}

三、接收:



static int get_media_bufInfo(mp4_record_t *mr, parse_data_t *buf_info)
{
msgque msgp;
parse_data_t *pd = NULL;


r_memset(&msgp, 0, sizeof(msgque));
if(0 > r_msg_recv(mr->course_data_fd, &msgp, sizeof(msgque)-sizeof(long), mr->record_info.media_msg_type, IPC_NOWAIT)) {
if( 0 == mr->record_info.media_msg_type){
nslog(NS_ERROR,"course_data_fd=%d\n",mr->course_data_fd);
}
return -1;
}
r_memcpy(buf_info, msgp.msgbuf, sizeof(parse_data_t));
r_free(msgp.msgbuf);
return 0;
}

反复出现发送报错:Invalid argument,后来在查消息队列的文档时,发现msgpue的第一个参数msgtype必须大于0,而sindex是从零值开始。顺便解决此问题

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页