我目前正在尝试使用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));
在服务器端,我可以选择各种boost::asio::async_read*功能。我想使用JsonCpp解析接收到的数据。在研究JsonCpp API(http://jsoncpp.sourceforge.net/class_json_1_1_reader.html)时,我发现Reader可以std::string在char数组或std::istream从boost::asio::streambuf传递给函数的a数组的顶部进行操作。
boost::asio::async_read*
std::string
std::istream
boost::asio::streambuf
关键是,据我所知,不一定要一次传输全部内容,因此我需要某种确认缓冲区是否包含足够的数据以使用JsonCpp处理整个文档。如何确保缓冲区包含足够的数据?
这是应用程序级协议的领域
要么
Content-Length: 12346\r\n
async_read_until
ASIO Http服务器示例包含一个很好的模式,用于解析您可以使用的HTTP请求/标头。假设您的解析器可以检测出完整性,只是“软失败”,直到出现所有信息为止。
void connection::handle_read(const boost::system::error_code& e, std::size_t bytes_transferred) { if (!e) { boost::tribool result; boost::tie(result, boost::tuples::ignore) = request_parser_.parse( request_, buffer_.data(), buffer_.data() + bytes_transferred); if (result) { request_handler_.handle_request(request_, reply_); boost::asio::async_write(socket_, reply_.to_buffers(), boost::bind(&connection::handle_write, shared_from_this(), boost::asio::placeholders::error)); } else if (!result) { reply_ = reply::stock_reply(reply::bad_request); boost::asio::async_write(socket_, reply_.to_buffers(), boost::bind(&connection::handle_write, shared_from_this(), boost::asio::placeholders::error)); } else { socket_.async_read_some(boost::asio::buffer(buffer_), boost::bind(&connection::handle_read, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } } else if (e != boost::asio::error::operation_aborted) { connection_manager_.stop(shared_from_this()); } }