boost :: asio :: streambuf empty? [英] boost::asio::streambuf empty?
问题描述
我使用boost :: asio :: streambuf来写一个ostream到我的boost socket。
I am using boost::asio::streambuf to write a ostream to my boost socket.
Name name;
name.set_name("platzhirsch");
boost::asio::streambuf b;
std::ostream os(&b);
ZeroCopyOutputStream *raw_output = new OstreamOutputStream(&os);
CodedOutputStream *coded_output = new CodedOutputStream(raw_output);
coded_output->WriteVarint32(name.ByteSize());
name.SerializeToCodedStream(coded_output);
socket.send(b.data());
但是,send返回的size_t是0.我怀疑没有数据发送。也因为客户端套接字抛出可怕的异常。我问,如果有一些奇怪的我的代码。
However, size_t returned by send is 0. I am suspicious that no data is sent at all. Also because the client socket throws horrible exceptions. I am asking, if there is something strange about my code.
换句话说,我可以测试streambuf是否为空或如果写入它的数据真的存在。
In other words, can I test if streambuf is empty or if the data written to it is really there.
推荐答案
boost :: asio没有问题,这是Google protobuf的问题。
It was no problem of boost::asio, it was a problem of Google protobuf.
为了刷新CodedOutputStream,必须删除它们:
In order to flush the CodedOutputStream, they have to be deleted:
ZeroCopyOutputStream *raw_output = new OstreamOutputStream(&os);
CodedOutputStream *coded_output = new CodedOutputStream(raw_output);
coded_output->WriteVarint32(name.ByteSize());
name.SerializeToCodedStream(coded_output);
delete coded_output;
delete raw_output;
socket.send(b.data());
这篇关于boost :: asio :: streambuf empty?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!