有人有过将 cocoaasyncsocket 与 google protobuf 一起使用的经验吗?我想使用 varint 来分隔帧,使用基于 netty 的客户端/服务器组合非常简单,但在使用 cocoaasync 读取时,我没有看到解码初始 varint 的简单方法。
最佳答案
在 C++ 方面,您必须使用 ReadVarint32() 的组合。和 VarintSize32()做这样的事情:
char buf[4];
buf = some_api_call(); /* _Copy/peak_ up to 4 bytes off the wire */
CodedInputStream cos(buf, sizeof(buf) - 1);
uint32_t frame_sz;
if (!cos.ReadInputStream(&frame_sz)) {
// Unable to read the frame size
return false;
}
frame_sz 应该有一个有效值,该值将告诉您需要读取多少字节。
uint32_t consumed_bytes = CodedOutputStream::VarintSize32(frame_sz);
现在您知道生成frame_sz消耗了多少字节。
我会将这些调用包装在一个我 extern“C”的库中,并从您的应用程序链接。
关于iphone - 使用 cocoaasyncsocket 框架读取 varint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5797193/