我正在尝试实现与java版本兼容的protobuf发送/接收,其首先包含varint32前缀。
我几乎让它工作了,但由于某种原因,某些消息变得局部并且失败了一个断言()。
/receiver.cpp:69: void tcp_connection::handle_read_message(const boost::system::error_code&, size_t): Assertion `line.ParseFromCodedStream(&input)' failed.
semder.pp
boost::asio::streambuf buffer;
std::ostream writer(&buffer);
bool packet_full = false;
uint32_t sent_lines = 0;
{ //new scope for protobuf streams, these flush in dtor
google::protobuf::io::OstreamOutputStream osostream(&writer);
google::protobuf::io::CodedOutputStream output(&osostream);
std::string lines;
while(std::getline(reader, line)) {
lines += line + "\n";
++sent_lines;
if(sent_lines > 100) {
packet_full = true;
break;
}
}
if(!lines.empty()) {
msg->set_text(lines);
const uint32_t size = msg->ByteSize();
output.WriteVarint32(size);
uint8_t* buffer = output.GetDirectBufferForNBytesAndAdvance(size);
if(buffer != 0) {
msg->SerializeWithCachedSizesToArray(buffer);
} else {
msg->SerializeWithCachedSizes(&output);
}
}
if(sent_lines > 0) {
sock.send(buffer.data());
if(!packet_full && !reader.eof()) { //Read ended, and not due to end of file
std::cout << "An error occured" << std::endl;
break;
}
reader.clear(); //clear EOF flag
}
receiver.cpp
这是一个提升asio回调。
成员变量:
boost::asio::ip::tcp::socket socket_;
boost::asio::streambuf buffer_;
代码
void handle_read_message(const boost::system::error_code& error,
size_t bytes_transferred) {
if(!error) {
buffer_.commit(bytes_transferred);
std::istream reader(&buffer_);
google::protobuf::io::IstreamInputStream isistream(&reader);
google::protobuf::io::CodedInputStream input(&isistream);
uint32_t size = 0;
assert(input.ReadVarint32(&size));
auto limit = input.PushLimit(size);
msgs::Line line;
assert(line.ParseFromCodedStream(&input));
assert(input.ConsumedEntireMessage());
input.PopLimit(limit);
start();
} else {
std::cout <<"error during handle_read_message: " << error << std::endl;
}
}
这主要基于https://stackoverflow.com/a/22899712
编辑: 新的接收器版本,reader_现在是一个成员变量:
void handle_read_message(const boost::system::error_code& error,
size_t bytes_transferred) {
std::cout << "handle_read_message(" << bytes_transferred << ")" <<std::endl;
if(!error) {
buffer_.commit(bytes_transferred);
uint32_t size = 0;
google::protobuf::io::IstreamInputStream isistream_(&reader_);
{
google::protobuf::io::CodedInputStream input(&isistream_);
if(!input.ReadVarint32(&size)) {
std::cout << "Failed to read size, waiting for more data" << std::endl;
start();
return;
}
}
std::size_t varint_size = isistream_.ByteCount();
std::cout <<"varintsize: " << varint_size << ", size: " << size << ", have bytes: " << buffer_.size() << std::endl;
if(varint_size + size > buffer_.size()) {
std::cout << "Not enough data received, waiting for more" << std::endl;
start();
return;
}
google::protobuf::io::CodedInputStream input(&isistream_);
auto limit = input.PushLimit(size);
msgs::Line line;
assert(line.ParseFromCodedStream(&input));
std::cout << line.text() << std::endl;
assert(input.ConsumedEntireMessage());
input.PopLimit(limit);
start();
} else {
std::cout <<"error during handle_read_message: " << error << std::endl;
}
}
答案 0 :(得分:4)
如果您在接收端使用异步I / O,则需要确保在开始解析之前确实收到了整个消息。请记住,TCP连接是一个流。只要有可用数据,异步回调就会运行 - 即使它不完整。您可能只获得部分消息,或者您可能会收到整条消息以及下一条消息。这就是为什么首先需要readDelimitedFrom()
的原因:要确定在解析之前需要等待多少字节。
因此,在使用异步I / O时,您需要以不同的方式编写代码。你可以使用这样的策略:
ZeroCopyInputStream
和CodedInputStream
。ReadVarint32()
来读取大小。如果ReadVarint32失败,那么你还没有收到整个大小,所以停下来等待更多的字节。ReadVarint32()
成功,则销毁CodedInputStream
,然后在基础ByteCount()
上调用ZeroCopyInputStream
以找出varint消耗的字节数。(另外:您的sender.cpp代码中似乎缺少右括号。如果原始文件有相同的错误,可能是您在CodedOutputStream刷新之前发送数据。但是我&#39 ;猜测错误不在原文中。)