2

async_read_until の使用に問題があります。最初は async_receive を使用していましたが、サーバーからの応答が複数のパケットになり、最初のパケットを受信した後に async_receive が読み取りを停止するという問題に遭遇しました。次に、async_read_until を調べて、区切り文字に達するまで読み取ろうとしました。ただし、これは問題のようです。async_read_until を使用するたびに、streambuf オブジェクトは最初の空白までしか移動しないようです。これが私が使用している私のコードです:

bool request_handler::get_recommendation(std::string& content, std::string &returnJson)
{
    //Connect to recommendation engine
    boost::asio::io_service io_service;
    boost::asio::ip::tcp::socket socket(io_service);
    boost::asio::ip::tcp::resolver resolver(io_service);
    boost::asio::ip::tcp::resolver::query query(Settings::get_recommendation_ip(), Settings::get_recommendation_port(), boost::asio::ip::resolver_query_base::numeric_service);
    boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query);
    boost::asio::ip::tcp::endpoint endpoint = *iterator;
    boost::system::error_code ec;
    socket.connect(endpoint, ec);
    if(ec)
    {
        Log::error("Couldn't connect: " + ec.message());
        return false;
    }

    //Create post Message
    std::string request_information = "POST / HTTP/1.1\r\n";
    request_information += "Host: " + Settings::get_recommendation_ip() + "\r\n";
    request_information += "Accept: */*\r\n";
    request_information += "Content-Type:text/plain\r\n";
    request_information += "Content-Length:" + boost::lexical_cast<std::string>(content.size()) + "\r\n";
    request_information += "Connection: close\r\n\r\n";
    request_information += content;

    try
    {        
        boost::shared_ptr<bool> timer1_result(new bool(false));
        boost::shared_ptr<bool> timer2_result(new bool(false));
        boost::shared_ptr<bool> write_result(new bool(false));
        boost::shared_ptr<bool> read_result(new bool(false));

//        boost::array<char,8192> buf;
        boost::asio::streambuf buf;
//        buf.assign(0);

        boost::asio::deadline_timer dt(io_service);
        //Create write timer
        dt.expires_from_now(boost::posix_time::milliseconds(Settings::get_write_timeout()));
        dt.async_wait(boost::bind(&request_handler::set_result, this, timer1_result, _1));
        //Call async write
        boost::asio::async_write(socket, boost::asio::buffer(request_information, request_information.size()), boost::bind(&request_handler::set_result, this, write_result, _1));
        io_service.reset();

        //Run until either the timer finishes or the write completes
        while(io_service.run_one())
        {
            //Write completes
            if(*write_result)
            {
                //Stop the timer
                dt.cancel();
                break;
            }
            //Timer completes
            else if(*timer1_result)
            {
                //Stop the write
                socket.cancel();
                return false;
            }
        }

        boost::asio::deadline_timer dt2(io_service);
        //Create read timer
        dt2.expires_from_now(boost::posix_time::milliseconds(Settings::get_read_timeout()));
        dt2.async_wait(boost::bind(&request_handler::set_result, this, timer2_result, _1));
        //Call async_receive
//        socket.async_receive(boost::asio::buffer(buf), boost::bind(&request_handler::set_result, this, read_result, _1));
        boost::asio::async_read_until(socket, buf, "]", boost::bind(&request_handler::set_result, this, read_result, _1));

        io_service.reset();
        //Run until either the timer finishes or the receive completes
        while(io_service.run_one())
        {
            //Receive completes
            if(*read_result)
            {
                //Stop timer
//                dt2.cancel();
//                break;
            }
            else if(*timer2_result)
            {
                //Stop the write
                socket.cancel();
                break;
            }
        }

        //Put result in return string
//        returnJson = buf.data();
        std::istream is(&buf);
        is >> returnJson;
        std::cout << "***" << returnJson << std::endl;
        return true;
    }catch(std::exception& e)
    {
        return false;
    }

}

void request_handler::set_result(boost::shared_ptr<bool> a, boost::system::error_code ec)
{
    std::cout << ec.message() << std::endl;
    *a = true;   
}

read_until をさらに呼び出す必要があり、パケット全体が一度に読み込まれなかったことが問題であると考えましたが、読み込んだ streambuf からの応答の「HTTP/1.1」よりも多くのデータを取得できません。 . 何が問題なのか手がかりはありますか?

4

1 に答える 1