使用boost :: asio发送大文件 [英] Send large files using boost::asio

查看:246
本文介绍了使用boost :: asio发送大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试从http服务器向一些客户端发送大文件(~4GB)。我的服务器实现基于Boosts HTTPServer3示例 HTTPServer3

问题是,文件首先完全加载到内存中然后发送给客户端。

我想读大约5-10mb的块并在读取下一个块之前发送它们,而不是将整个文件读入一个字符串(rep.content)。



这就是我的代码的样子:



Hi,

I'm trying to send large files (~4GB) from a http server to some clients. My server implementation is based on Boosts HTTPServer3 Example HTTPServer3.
The problem is, that the files are first completely loaded into memory and then send to the Client.
I would like to read chunks of about 5-10mb and send them before reading the next chunk, instead of reading the whole file into one string (rep.content).

Thats what my code looks like:

// Open the file to send back.
std::ifstream is(full_path_no_params.c_str(), std::ios::in | std::ios::binary);
if (!is)
{
    rep = reply::stock_reply(reply::not_found);
    std::cout << "File not found\n";
    return;
}
// Fill out the reply to be sent to the client.
rep.status = reply::ok;
// add content of file to reply
char buf[512];
while (is.read(buf, sizeof(buf)).gcount() > 0)
    rep.content.append(buf, is.gcount());
rep.headers.resize(2);
rep.headers[0].name = "Content-Length";
rep.headers[0].value = boost::lexical_cast<std::string>(rep.content.size());
rep.headers[1].name = "Content-Type";
rep.headers[1].value = mime_types::extension_to_type(extension);





使用boost :: asio :: async_write发送数据





The data is send using boost::asio::async_write

boost::asio::async_write(socket_, reply_.to_buffers(),strand_.wrap(
    boost::bind(&connection::handle_write, shared_from_this(),
    boost::asio::placeholders::error)));





和reply_的定义。 to_buffers()





and the definition of reply_.to_buffers()

std::vector<boost::asio::const_buffer> reply::to_buffers()
{
    std::vector<boost::asio::const_buffer> buffers;
    buffers.push_back(status_strings::to_buffer(status));
    for (std::size_t i = 0; i < headers.size(); ++i)
    {
        header& h = headers[i];
        buffers.push_back(boost::asio::buffer(h.name));
        buffers.push_back(boost::asio::buffer(misc_strings::name_value_separator));
        buffers.push_back(boost::asio::buffer(h.value));
        buffers.push_back(boost::asio::buffer(misc_strings::crlf));
    }
    buffers.push_back(boost::asio::buffer(misc_strings::crlf));
    buffers.push_back(boost::asio::buffer(content));
    return buffers;
}





任何想法怎么做?

我可以使用像升级一样的东西:: asio :: streambuf?



Greets Chris



Any ideas how to do that??
May I use somesthing like boost::asio::streambuf?

Greets Chris

推荐答案

看看这个在stackoverflow上回答



考虑将数据拆分为网络数据包大小倍数的较小块,以最小化发送半空数据包。 1024的倍数可能最合适。
Take a look at this answer at stackoverflow.

Consider splitting the data into smaller chunks in network packet size multiples to minize sending half empty packets. Multiples of 1024 may fit best.


这篇关于使用boost :: asio发送大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆