消息队列发送和接收

时间:2014-02-07 09:26:55

标签: ipc vxworks

我在vxworks中有以下程序,这是大程序的一部分

 /* defines */
#define MAX_MESSAGES 100
#define MAX_MESSAGE_LENGTH 50


if ((msgQStudyId = msgQCreate(MAX_MESSAGES,MAX_MESSAGE_LENGTH,MSG_Q_FIFO)) == NULL) {
        printf("msgQCreate in failed\n");
}


CVenkyMsgQWrite *pMsgWrite = new CVenkyMsgQWrite();
    pMsgWrite->OpenWrite();


    CVenkyMsgQRead *pMsgRead = new CVenkyMsgQRead();
    pMsgRead->OpenRead();

class CVenkyMsgQWrite {

public :
    // Constructors and destructors:
    CVenkyMsgQWrite();
    ~CVenkyMsgQWrite();

    RESULT OpenWrite(void);
    void TaskLoop(void);

};


class CVenkyMsgQRead {

public :
    // Constructors and destructors:
    CVenkyMsgQRead();
    ~CVenkyMsgQRead();

    RESULT OpenRead(void);
    void TaskLoop(void);



CVenkyMsgQWrite::CVenkyMsgQWrite() {
    // Open(void);
}

CVenkyMsgQWrite::~CVenkyMsgQWrite() { }

static int WriteTask(const void *envPtr)
{    
    CVenkyMsgQWrite *ctrl = (CVenkyMsgQWrite *)envPtr;
    try
    {
        ctrl->TaskLoop();   // Normal run
        return CI857_ERROR;
    }
    catch(...)
    {
        CI857DBG(printError,("Error in Controller input task\n",0,0,0,0,0,0));      
    }
    return CI857_ERROR;
}

RESULT CVenkyMsgQWrite::OpenWrite(void) {

    RESULT ret = CI857_OK;

    taskSpawn( "VenkyMsgQWrite", 
        78, 
        0,
        4096,  /* stack size */
        WriteTask, 
        this, 2,3,4,5,6,7,8,9,10);
    return ret;
}

void CVenkyMsgQWrite::TaskLoop(void)
{
    STATUS MsgQStatus = OK;

    // MsgQStatus = msgQSend(msgQStudyId, (char*)pPHData, iDataLength, NO_WAIT, MSG_PRI_NORMAL );
    // if(ERROR == MsgQStatus)
    // {
        // m_pILogHandler->Log((cConfigMod|cError),"#%d: Error: SendToConfMsgQ() msgQSend failed with Err %d.",__LINE__,errnoGet());
    // }

    while(1) {

        char message[] = "message";

        /* send message */
        if((msgQSend(msgQStudyId,message,strlen(message), WAIT_FOREVER,     MSG_PRI_NORMAL)) == ERROR) {
            printf("msgQSend in CVenkyMsgQWrite::TaskLoop failed\n");
        }
        else {
            printf("msgQSend in CVenkyMsgQWrite::TaskLoop success\n");
        }
         vosSleepMs( 1000 );
    }



}

CVenkyMsgQRead::CVenkyMsgQRead() {
    // Open(void);
}

CVenkyMsgQRead::~CVenkyMsgQRead() { }

static int ReadTask(const void *envPtr)
{    
    CVenkyMsgQRead *ctrl = (CVenkyMsgQRead *)envPtr;
    try
    {
        ctrl->TaskLoop();   // Normal run
        return CI857_ERROR;
    }
    catch(...)
    {
        CI857DBG(printError,("Error in Controller input task\n",0,0,0,0,0,0));      
    }
    return CI857_ERROR;
}

RESULT CVenkyMsgQRead::OpenRead(void) {

    RESULT ret = CI857_OK;

    taskSpawn( "VenkyMsgQRead", 
        79, 
        0,
        4096,  /* stack size */
        ReadTask, 
        this, 2,3,4,5,6,7,8,9,10);
    return ret;
}

void CVenkyMsgQRead::TaskLoop(void)
{
    STATUS MsgQStatus = OK;

    char msgBuf[MAX_MESSAGE_LENGTH];
    memset(msgBuf, 0x00, sizeof(msgBuf));

    while(1) {

        /* receive message */
        int msgLength = msgQReceive(msgQStudyId, msgBuf, MAX_MESSAGE_LENGTH,  WAIT_FOREVER) ;
        if(msgLength == ERROR) {
            printf("msgQReceive in taskTwo failed\n");  
        }
        else {
            printf(" Length received %d  and string is %s\n", msgLength, msgBuf); 
        }
         vosSleepMs( 5000 );

        memset(msgBuf, 0x00, sizeof(msgBuf));
    }
}

以下是输出。

enter code here> msgQSend在CVenkyMsgQWrite :: TaskLoop成功msgQSend in

  

CVenkyMsgQWrite :: TaskLoop成功msgQSend in   CVenkyMsgQWrite :: TaskLoop成功msgQSend in   CVenkyMsgQWrite :: TaskLoop成功msgQSend in   CVenkyMsgQWrite :: TaskLoop成功长度收到7,字符串是   消息msgQSend在CVenkyMsgQWrite :: TaskLoop成功msgQSend中   CVenkyMsgQWrite :: TaskLoop成功msgQSend in   CVenkyMsgQWrite :: TaskLoop成功msgQSend in   CVenkyMsgQWrite :: TaskLoop成功msgQSend in   CVenkyMsgQWrite :: TaskLoop成功长度收到7,字符串是   消息msgQSend在CVenkyMsgQWrite :: TaskLoop成功msgQSend中   CVenkyMsgQWrite :: TaskLoop成功msgQSend in   CVenkyMsgQWrite :: TaskLoop成功msgQSend in   CVenkyMsgQWrite :: TaskLoop成功msgQSend in   CVenkyMsgQWrite :: TaskLoop成功长度收到7,字符串是   消息msgQSend在CVenkyMsgQWrite :: TaskLoop成功msgQSend中   CVenkyMsgQWrite :: TaskLoop成功msgQSend in   CVenkyMsgQWrite :: TaskLoop成功

我的问题是

  1. 当我们快速发送数据时,任务正在读取,因为我们最多可以有100条长度为50的消息我期待队列中有5条消息,并且收到的长度为35条。但事实并非如此,我们可以从中看到以上输出我们读取长度为7和单个消息。
  2. 我做对了吗?这是预期的行为吗?如果我想读35的长度,我该怎么读?

1 个答案:

答案 0 :(得分:1)

Message Queue接口是基于消息的,它不是像TCP套接字那样的流接口。

您正在使用7字节消息调用msgQSend。每次调用都是一个单独的消息。

因此,每个msgQReceive都会单独收到这些消息。

如果您想接收来自队列的所有消息,请删除您的5秒睡眠声明... msgQReceive将挂起任务,直到有新消息进入。