我目前正在尝试使用boost-asio的套接字API通过网络将一些JSON数据从客户端传输到服务器.我的客户基本上这样做:
int from = 1, to = 2;
boost::asio::streambuf buf;
ostream str(&buf);
str << "{"
<< "\"purpose\" : \"request\"" << "," << endl
<< "\"from\" : " << from << "," << endl
<< "\"to\" : " << to << "," << endl
<< "}" << endl;
// Start an asynchronous operation to send the message.
boost::asio::async_write(socket_, buf,
boost::bind(&client::handle_write, this, _1));
Run Code Online (Sandbox Code Playgroud)
在服务器端,我可以选择各种boost::asio::async_read*功能.我想使用JsonCpp来解析收到的数据.研究JsonCpp API(http://jsoncpp.sourceforge.net/class_json_1_1_reader.html)我发现Reader在std::stringa*,char*数组或者std::istream我可以从boost::asio::streambuf传递给函数的操作中运行.
关键是,据我所知,不一定是整个内容一次传输的情况,所以我需要某种确认缓冲区包含足够的数据来使用JsonCpp处理整个文档.如何确保缓冲区包含足够的数据?