asio :: streambuf无法与std :: istream一起使用

问题描述

data1打印“ hello,world”,data2为零。

似乎istream读取功能不起作用,有什么好的解决方法吗?

我认为memcpy不是一个好方法 为什么data2为nil?

请让我知道。谢谢。

class Content : public std::istream {
public:
    Content(asio::streambuf& streambuf) noexcept
        : std::istream(&streambuf_),streambuf_(streambuf) {

    }

    ~Content() {

    }

public:
    std::size_t size() noexcept {
        return streambuf_.size();
    }

    std::string string() {
          std::string str;
          auto size = streambuf_.size();
          str.resize(size);

          read(&str[0],static_cast<std::streamsize>(size));

          return str;
    }

private:
    asio::streambuf& streambuf_;
};

int main() {
    logger_init();

    asio::streambuf streambuf;
    std::ostream out(&streambuf);

    out << "hello,world" << std::endl;
    char* data = (char*)calloc(1,sizeof(char) * streambuf.size());
    memcpy(data,asio::buffer_cast<const void*>(streambuf.data()),streambuf.size());

    LOG(INFO) << "data1: " << data;
    
    Content content(streambuf);
    LOG(INFO) << "data2: " << content.string();

    return 0;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)