boost::asio学习 - buffer篇
时间:2010-10-11 来源:edwardlost
boost::asio::streambuf
使用read_until() 和 async_read_until()读取line-based(使用"\r\n"或者其它自定义字符序列作为行记录边界)数据时需要boost::asio::streambuf来缓存读取到的数据。

1 class http_connection
2 {
3 ...
4
5 void start()
6 {
7 boost::asio::async_read_until(socket_, data_, "\r\n",
8 boost::bind(&http_connection::handle_request_line, this, _1));
9 }
10
11 void handle_request_line(boost::system::error_code ec)
12 {
13 if (!ec)
14 {
15 std::string method, uri, version;
16 char sp1, sp2, cr, lf;
17 std::istream is(&data_);
18 is.unsetf(std::ios_base::skipws);
19 is >> method >> sp1 >> uri >> sp2 >> version >> cr >> lf;
20 ...
21 }
22 }
23
24 ...
25
26 boost::asio::ip::tcp::socket socket_;
27 boost::asio::streambuf data_;
28 };
相关阅读 更多 +