0

It takes about one second of time to read a request through async_read with large serialized jpeg pictures (about 2-3 MB). The reading works correctly and quickly on images up to 1 MB, but then hard delays begin, possibly related to the allocation of request_parser

boost::optional<boost::beast::http::request_parser<boost::beast::http::string_body>> request_pars_;
boost::beast::http::request<boost::beast::http::string_body> request_;
boost::beast::http::responce<boost::beast::http::string_body> resounce_;
void Connection::read() {
    request_pars_.emplace();
    request_pars_->body_limit(10485760); // 10 MB
    boost::beast::http::async_read(socket_, buffer_, request_pars_,
        [self = shared_from_this()](boost::system::error_code err, size_t bytes_transferred) {
            boost::ignore_unused(bytes_transferred);
            self->readHandle(err);
        });
}

void Connection::readHandle(boost::system::error_code err) {
    request_ = request_pars_->release();

    response_process_.do_request();

    response_ = response_process_.get_response();

    writeResponse();
}
void Connection::writeResponse() {
    response_.set("keep-alive", true);
    boost::beast::http::async_write(socket_, response_,
        [self = shared_from_this()](boost::system::error_code err, size_t bytes_transferred) {
            if (err) {
                self->socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_send, err);
            } else {
                self->read();
            }
        });
}

A very large amount of time passes from the call of the Connection::read function to the Connection::readHandle function

Pup_Sik
  • 1
  • 1
  • Have you used chunked transfer and buffer_body? If your goal is to stream to a file, use `file_body`. You could also `reserve()` on the string body to rule out buffer issues. The capacity of `buffer_` (not shown) is also relevant. – sehe Dec 27 '21 at 15:49
  • Note that I just remembered you need to use `message_parser<>` instead of `http::read` on `message` directly.You can scan my answers for examples – sehe Dec 27 '21 at 18:58

0 Answers0