c++ boost库下的beast单元库关于先发包头再发包体

#include 
#include 

using body_type = boost::beast::http::string_body;
using request_serializer = boost::beast::http::serializer;
using stream_type = boost::asio::ip::tcp::socket;

void send_header(stream_type& strm, request_serializer& ser);
void send_rest(stream_type& strm, request_serializer& ser);

// 首先,发送标题
void send_header(stream_type& strm, request_serializer& ser)
{
    boost::beast::http::async_write_header(strm, ser, 
        [&](boost::beast::error_code ec, std::size_t)
        {
            if (!ec)
            {
                send_rest(strm, ser);
            }
            else
            {
                // handle error
            }
        });
}

// 如果标头发送成功,则发送其余消息
void send_rest(stream_type& strm, request_serializer& ser)
{
    boost::beast::http::async_write(strm, ser, 
        [](boost::beast::error_code ec, std::size_t)
        {
            if (!ec)
            {
                // 成功- ser 的生命周期现在可以结束
            }
            else
            {
                // handle error
            }
        });
}

你可能感兴趣的:(c++,boost,c++)