在C ++中解析协议缓冲区

查尼

我想将一些协议缓冲区写入套接字,然后从客户端读取回去。事情不起作用,所以我在对服务器进行编码后立即在服务器本身中编写了一些解码部分。您能否看一下下面的代码,然后告诉我我做错了什么?

(我必须使用arraystream和编码流,以便可以编写定界符)

int bytes_written = tData.ByteSize() + sizeof(google::protobuf::uint32);
google::protobuf::uint8 buffer[bytes_written];
memset(buffer, '\0', bytes_written);
google::protobuf::io::ArrayOutputStream aos(buffer,bytes_written);
google::protobuf::io::CodedOutputStream *coded_output = new google::protobuf::io::CodedOutputStream(&aos);
google::protobuf::uint32 size_  = tData.ByteSize();
coded_output->WriteVarint32(size_);

tData.SerializeToCodedStream(coded_output);

int sent_bytes = 0;
std::cout << buffer << std::endl;
if ( (sent_bytes = send(liveConnections.at(i), buffer, bytes_written, MSG_NOSIGNAL)) == -1 )
    liveConnections.erase(liveConnections.begin() + i);
else
    std::cout << "sent "  << sent_bytes << " bytes to " << i << std::endl;

delete coded_output;



////////////////


google::protobuf::uint8 __buffer[sizeof(google::protobuf::uint32)];
memset(__buffer, '\0', sizeof(google::protobuf::uint32));
memcpy (__buffer, buffer, sizeof(google::protobuf::uint32));

google::protobuf::uint32 __size = 0;

google::protobuf::io::ArrayInputStream ais(__buffer,sizeof(google::protobuf::uint32));
google::protobuf::io::CodedInputStream coded_input(&ais);
coded_input. ReadVarint32(&__size);
std::cout <<" size of payload is "<<__size << std::endl;

google::protobuf::uint8 databuffer[__size];
memset(databuffer, '\0', __size);
memcpy (databuffer, buffer+sizeof(google::protobuf::uint32), __size);    

std::cout << "databuffs " << "size " << __size << "  "<< databuffer << std::endl;
google::protobuf::io::ArrayInputStream array_input(databuffer,__size);
google::protobuf::io::CodedInputStream _coded_input(&array_input);
data_model::terminal_data* tData = new data_model::terminal_data();
if (!tData->ParseFromCodedStream(&_coded_input))
{
    std::cout << "data could not be parsed" << std::endl;     
}
else
{
    std::cout <<" SYMBOL --" << tData->symbol_name() << std::endl;
}
delete tData;

程序输出:

size of payload is 55
databuffs size 55  C109"056*    BANKNIFTY0���20140915@�J    145406340
data could not be parsed
C109"056*   BANKNIFTY0���20140915@�J    145406340
伊戈尔·坦德尼克(Igor Tandetnik)

WriteVarint32不一定写入4个字节,ReadVarint32也不读取4个字节。如“可变长度编码”中一样,“ Var”代表“变量”。

编码时,您写出大小(可能只有一个字节),然后写出原型。解码时,您读取大小,然后前进四个字节,然后读取原型。因此,您是从错误的偏移量开始解析。

使用CurrentPosition()afterReadVarint32来计算大小指示器消耗了多少字节。前进该字节数。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用C ++ API解析协议缓冲区中的错误

来自分类Dev

在C#中解析原始协议缓冲区字节流

来自分类Dev

协议缓冲区C ++中的异常处理

来自分类Dev

C ++中的Google协议缓冲区:从现有结构创建消息

来自分类Dev

从C ++中的协议缓冲区获取所有字段名称?

来自分类Dev

C ++ Google协议缓冲区打开http套接字

来自分类Dev

C ++类应包含协议缓冲区消息还是从协议缓冲区消息构造/填充

来自分类Dev

C ++中的缓冲区大小

来自分类Dev

C ++中的位缓冲区

来自分类Dev

C中的缓冲区溢出与获取

来自分类Dev

在C#中同步缓冲区

来自分类Dev

C中自发的缓冲区溢出

来自分类Dev

C ++中的Tensorflow:成功读取LSTM编码器解码器模型的协议缓冲区

来自分类Dev

c + +和DX11中的动态常量缓冲区或动态顶点缓冲区

来自分类Dev

您将如何限制缓冲区大小并返回c ++中的缓冲区限制?

来自分类Dev

无法使用Google协议缓冲区从C#返回相同的double到C ++,然后返回

来自分类Dev

在C中将协议缓冲区与JSON相互转换,而无需生成C代码

来自分类Dev

C中循环缓冲区中的反向遍历

来自分类Dev

C ++刷新缓冲区

来自分类Dev

C缓冲区指针

来自分类Dev

缓冲区溢出 C

来自分类Dev

C#Xamarin表单System.IO.IOException:重解析点缓冲区中存在的标记无效

来自分类Dev

用于构建C ++ Google协议缓冲区项目的Makefile

来自分类Dev

用于构建C ++ Google协议缓冲区项目的Makefile

来自分类Dev

Google协议缓冲区messag到字节数组C ++

来自分类Dev

VS2008下的协议缓冲区错误C2059

来自分类Dev

尝试在项目中使用Google协议缓冲区C ++链接错误

来自分类Dev

C ++读取缓冲区中的整个文件

来自分类Dev

缓冲区数组在C中的for循环中溢出

Related 相关文章

热门标签

归档