RSS Feed

Posts Tagged ‘boost’

  1. boost::asio::async_read 的一些记录

    十二月 30, 2009 by bborn

    boost中的asio是一个非常好用的网络库
    不过可以参考的资料比较少
    大部分都是靠阅读自带的几个example来理解
    这里记录一下这两天的几个心得

    boost::asio::async_read_until(socket_, response_, “\r\n\r\n”, boost::bind(&shoutcast::handle_read_headers, this, boost::asio::placeholders::error));

    async_read_until, 一直读,直到读到指定的内容.但是了,并不是刚刚读到就停止,一般缓冲区里会有更多的内容,
    所以我们需要读出我们需要的,剩下的留下就好
    官方是这么说的”After a successful async_read_until operation, the streambuf may contain additional data beyond the delimiter. An application will typically leave that data in the streambuf for a subsequent async_read_until operation to examine.”

    boost::asio::async_read(socket_, response_, boost::asio::transfer_at_least(512), boost::bind(&shoutcast::handle_read_content, this, boost::asio::placeholders::error));

    async_read 可以选择使用transfer_at_least 这个CompletionCondition
    async_read 读入的缓冲区可以这样
    boost::array buffer_;
    boost::asio::buffer(buffer_);来构造
    也可以直接用字符数组来构造
    char _buf[512] = {0};
    boost::asio::buffer(_buf, 512);
    也可以用流,更像tcp的方式
    使用boost::asio::streambuf response_;
    它继承自 std::streambuf
    一般这样使用
    std::istream _stream(&response_);
    这里要注意的是 basic_istream::get
    如果我们指定get的个数count,那么取出的个数是count-1
    msdn是这样说的”The fourth function extracts up to _Count – 1 elements and stores them in the array beginning at _Str.”