簡體   English   中英

在循環中提升asio問題

[英]boost asio issue within the loop

有人可以幫我解決下面的問題嗎?

  1. 我試圖在while循環中調用async_send。 數據被正確地發送到服務器,但是根本沒有調用onSend處理程序...如果我不使用while循環一切正常(數據被發送和接收,所有處理程序都被調用)

  2. 如果我們在服務器的回答之前發送一些msgs,我的代碼是否會正常工作?

這是TCP客戶端代碼

class TCPClient
{
    public:
        static const size_t maxBufLen = 100;
        static const size_t MAX_INPUT_SIZE = 10;

        TCPClient(boost::asio::io_service& IO_Service, tcp::resolver::iterator EndPointIter);
        void close();

    private:
        boost::asio::io_service& m_IOService;
        tcp::socket m_Socket;

        char recieveBuffer[maxBufLen];

        void promptTxMsgLoop();
        void onConnect(const boost::system::error_code& ErrorCode, tcp::resolver::iterator EndPointIter);
        void onReceive(const boost::system::error_code& ErrorCode);
        void onSend(const boost::system::error_code& ErrorCode);
        void doClose();
};


TCPClient::TCPClient(boost::asio::io_service& IO_Service, tcp::resolver::iterator EndPointIter)
: m_IOService(IO_Service), m_Socket(IO_Service)
{
    tcp::endpoint EndPoint = *EndPointIter;
    recieveBuffer[0] = '\0';

    m_Socket.async_connect(EndPoint,
        boost::bind(&TCPClient::onConnect, this, boost::asio::placeholders::error, ++EndPointIter));
}


void TCPClient::onConnect(const boost::system::error_code& ErrorCode, tcp::resolver::iterator EndPointIter)
{
    if (ErrorCode == 0)
    {
        this->promptTxMsgLoop();
    }
    else if (EndPointIter != tcp::resolver::iterator())
    {
        cout << "m_Socket.close();!" << endl;
        m_Socket.close();
        tcp::endpoint EndPoint = *EndPointIter;

        m_Socket.async_connect(EndPoint, 
            boost::bind(&TCPClient::onConnect, this, boost::asio::placeholders::error, ++EndPointIter));
    }
}

void TCPClient::promptTxMsgLoop()
{
    recieveBuffer[0] = '\0';
    while (true)
    {
        cout << "> " ;
        string tmp;
        cin >> tmp;

        cout << "Entered: " << tmp << endl;
        tmp += "\0";

        if (tmp.length() < MAX_INPUT_SIZE-1)
        {
            try
            {
                //lock untill buffer is emty
                while (strlen(recieveBuffer) > 1)
                {

                }
                //onSend handler is never is called inside while loop 
                m_Socket.async_send(boost::asio::buffer(tmp.c_str(),tmp.length()+1),
                    boost::bind(&TCPClient::onSend, this, boost::asio::placeholders::error));
            }
            catch(exception &e)
            {
                cerr << "Cannot add msg to send queue... " << e.what() << endl;
            }
        }
        else
            cout << "Error: input string is too long. Max length is " << MAX_INPUT_SIZE-1 << endl;
    }
}

void TCPClient::onSend(const boost::system::error_code& ErrorCode)
{
    cout << "Msg has been sent..." << endl;

    if (strlen(recieveBuffer) > 1)
        cout << "ERROR: recieveBuffer in not epmty. Data is overritten!" << endl;

    if (!ErrorCode)
    {
        m_Socket.async_receive(boost::asio::buffer(recieveBuffer, TCPClient::maxBufLen),
            boost::bind(&TCPClient::onReceive, this, boost::asio::placeholders::error));
    }
    else
    {
        cout << "onSend closing" << endl;
        cout << "ERROR! onSend..." << ErrorCode << endl;
        doClose();
    }
}

void TCPClient::onReceive(const boost::system::error_code& ErrorCode)
{
    cout << "Msg has been received..." << endl;

    if (ErrorCode == 0)
    {
        cout << recieveBuffer << endl;
        cout << "msg length: " << strlen(recieveBuffer) << endl;

        //unlock buffer
        recieveBuffer[0] = '\0';
    } 
    else 
    {
        cout << "ERROR! onReceive..." << ErrorCode << endl;
        doClose();
    }
}

void TCPClient::doClose()
{
    m_Socket.close();
}

int main()
{
    try 
    {
        boost::asio::io_service IO_Service;
        tcp::resolver Resolver(IO_Service);
        tcp::resolver::query Query("127.0.0.1", "1");
        tcp::resolver::iterator EndPointIterator = Resolver.resolve(Query);
        TCPClient Client(IO_Service, EndPointIterator);
        boost::thread ClientThread(boost::bind(&boost::asio::io_service::run, &IO_Service));
        ClientThread.join();
        Client.close();
    } 
    catch (exception& e)
    {
        cerr << e.what() << endl;
    }

    cout << "\nClosing";
    getch();

}

在'onConnect'完成處理程序中,您調用執行無限while循環的promptTxMsgLoop ,因此您實際上永遠不會讓io_service繼續其工作 - 因此將不再調用完成處理程序。

此外,您多次調用async_send ,而不等待前一個async_send的comletion處理程序,這也是不正確的。

請參閱asio文檔以找出正確的使用模式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM