Winsock RIO:RIOReceive立即返回,没有bytesTransferred

时间:2015-02-05 20:20:51

标签: c++ winsock winsock2 winsockets

我遇到了让winsock RIO工作的问题。 似乎每次发布RIOReceive时它都会立即返回0字节,并且我的同伴无法收到消息。

在发布RIOReceive之后,我等待RIODequeCompletion,它立即使用numResults = 1进行deques,但是当我检查RIORESULT结构的bytesTransferred时,它是0.这告诉我,我不是正确设置这个东西,但我找不到文档或示例告诉我还应该做些什么。

互联网上的RIO似乎很少。我查看了MSDNLen Holgate with TheServerFramework,此网站和两个GitHub RIO服务器。

RIOEchoServer和RIOServer_sm9在GitHub上,但我不能发布两个以上的链接(这是我在这个网站上的第一个问题)。

此代码只是为了证明事情。它目前没有设置使用sendCQ,没有很好地处理错误等...

这是准备工作:

void serverRequestThread() {

//init buffers 

//register big buffers
recvBufferMain = rio.RIORegisterBuffer(pRecvBufferMain, bufferSize);
sendBufferMain = rio.RIORegisterBuffer(pSendBufferMain, bufferSize);

if (recvBufferMain == RIO_INVALID_BUFFERID) {
    cout << "RIO_INVALID_BUFFERID" << endl;
}

if (sendBufferMain == RIO_INVALID_BUFFERID) {
    cout << "RIO_INVALID_BUFFERID" << endl;
}

//create recv buffer slice
recvBuffer1.BufferId = recvBufferMain;
recvBuffer1.Offset = 0;
recvBuffer1.Length = 10000;

//create send buffer slice
sendBuffer1.BufferId = sendBufferMain;
sendBuffer1.Offset = 0;
sendBuffer1.Length = 10000;

//completion queue
recvCQ = rio.RIOCreateCompletionQueue(CQsize, NULL);
sendCQ = rio.RIOCreateCompletionQueue(CQsize, NULL);

if (recvCQ == RIO_INVALID_CQ) {
    cout << "RIO_INVALID_CQ" << endl;
}

if (sendCQ == RIO_INVALID_CQ) {
    cout << "RIO_INVALID_CQ" << endl;
}

//start a loop for newly accept'd socket
while (recvCQ != RIO_INVALID_CQ && sendCQ != RIO_INVALID_CQ) {

    //get accept'd socket
    struct sockaddr_in saClient;
    int iClientSize = sizeof(saClient);
    acceptSocket = accept(listenSocket, (SOCKADDR*)&saClient, &iClientSize);

    if (acceptSocket == INVALID_SOCKET) {
        cout << "Invalid socket" << endl;
        printError();
    }

    //register request queue
    requestQueue = rio.RIOCreateRequestQueue(
        acceptSocket,       //socket
        10,                 //max RECVs on queue
        1,                  //max recv buffers, set to 1
        10,                 //max outstanding sends
        1,                  //max send buffers, set to 1
        recvCQ,             //recv queue
        recvCQ,             //send queue
        pOperationContext   //socket context
        );

    if (requestQueue == RIO_INVALID_RQ) {
        cout << "RIO_INVALID_RQ" << endl;
        printError();
    }

我现在发布一个RIOReceive:

    //start a loop to repin recv buffer for socket
    while (acceptSocket != INVALID_SOCKET) {

        //pin a recv buffer to wait on data
        recvSuccess = rio.RIOReceive(
            requestQueue,           //socketQueue
            &recvBuffer1,           //buffer slice
            1,                      //set to 1
            RIO_MSG_WAITALL,                        //flags
            0);                     //requestContext

        if (recvSuccess == false) {
            cout << "RECV ERROR!!!!!!!!\n";
            printError();
        }

        //wait for recv to post in queue

        //std::this_thread::sleep_for(std::chrono::milliseconds(3000));

一旦我调用RIODequeCompletion,它就会返回1:

        numResults = 0;
        while (numResults == 0) numResults = rio.RIODequeueCompletion(recvCQ, recvArray, 10);

        if (numResults == RIO_CORRUPT_CQ) {

            cout << "RIO_CORRUPT_CQ" << endl;

        } else if (numResults == 0) {

            cout << "no messages on queue\n";

        } else if (numResults > 0) {

但是当我检查RIORESULT的bytesTransferred时,它始终为0:

            if (recvArray[0].BytesTransferred > 0) {

                //process results
                if (pRecvBufferMain[0] == 'G') {

                    //set respnose html
                    strcpy(pSendBufferMain, responseHTTP);

                    sendSuccess = rio.RIOSend(
                        requestQueue,   //socketQueue
                        &sendBuffer1,   //buffer slice
                        1,              //set to 1
                        0,              //flags
                        0);             //requestContext

                } else if (pRecvBufferMain[0] == 'P') {

                    //process post 


                } else {
                    //recv'd a bad message

                }

            } //end bytesTransferred if statement

            //reset everything and post another recv

        }//end response if statement

        std::this_thread::sleep_for(std::chrono::milliseconds(100));

    }//end while loop for recv'ing

    std::this_thread::sleep_for(std::chrono::milliseconds(100));

}//end while loop for accept'ing

}// end function

就像我说的那样,我可能没有正确使用RIOReceive,和/或我没有设置我需要的正确套接字选项(现在没有)。

我感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

尝试删除RIO_MSG_WAITALL。可能存在一个错误,您只能获得关闭通知(字节== 0),而不是使用其中的数据完成。无论哪种方式,看看代码是否在没有标志的情况下工作会很有趣。

我的示例服务器和测试是否适用于您的硬件?

答案 1 :(得分:0)

在使用RioReceive和RioNotify以及RioDequeueCompletion时,我遇到了类似的问题,即我的出队完成结果中有零bytesReceived。我还会在出列的完成结果中看到WSA​​EINVAL(无效参数= 10022)的“状态”值,这似乎表明接收调用的WSA错误代码。

我遇到错误的特殊原因是因为我为receiveBuffer分配了内存而我试图将缓冲区指针作为我的缓冲区句柄传递给RioReceive的RIO_BUFFER_SEGMENT而不是传递 RioRegisterBuffer返回的IntPtr

我完全责怪自己使用太多无类型的IntPtrs并丢失类型检查。 :)