SkipLastField 调用了结束组标签.GoogleProtobuf(C#和C++)

SkipLastField called on an end-group tag. GoogleProtobuf (C# and C++)

本文关键字:C++ GoogleProtobuf 标签 调用 结束 SkipLastField      更新时间:2023-10-16

我正在尝试使用Google proto在C++中序列化消息,通过TCP套接字发送它,最后在C#客户端中接收并解析它。

我在 C# 谷歌原型解析器上不断收到以下异常:

SkipLastField 调用了结束组标记,指示缺少相应的起始组

我在发送前和接收后都打印了序列化输入流,它在两端完美匹配。

C# 侦听器/反序列化程序

 namespace NetStreamHandler
{
    public class Server
    {
        public static void ReceiveData()
        {
            var ipAddress = IPAddress.Parse("127.0.0.1");
            var listener = new TcpListener(ipAddress, 20511);
            listener.Start();
            listener.BeginAcceptSocket(MessageReceived, listener);
        }
        private static void MessageReceived(IAsyncResult result)
        {
            var server = (TcpListener)result.AsyncState;
            using (var client = server.EndAcceptTcpClient(result))
            using (var stream = client.GetStream())
            {
                try
                {
                    var cod = new CodedInputStream(stream);
                    var packet = Serialize.Parser.ParseFrom(cod);
                    Console.WriteLine($"Message: {packet.ToString()}");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Exception Caught at: {ex.Message}n");
                }
            }
        }
    }
}

C++发送/序列化

int bCount;
Serialized::Serialize pLoad;
pLoad.set_command("Test");
pLoad.set_id(10);
pLoad.set_num(3);
pLoad.set_packettype(1);
pLoad.set_ret("Test");
printf("Size after serializing: %d bytesn", pLoad.ByteSize());
auto size = pLoad.ByteSize();
auto packet = new char[size];
ArrayOutputStream ArrayOut(packet, size);
auto coutput = new CodedOutputStream(&ArrayOut);
coutput->WriteVarint32(pLoad.ByteSize());
pLoad.SerializeToCodedStream(coutput);
auto sConnect = SetupSocket();
if (!sConnect) { printf("Socket failuren"); goto DONE; }
std::cout << "Buffer:" << byte_2_str(packet, size) << std::endl;

bCount = send(sConnect, packet, size, 0);
if(bCount == SOCKET_ERROR)
{
    fprintf(stderr, "Server: send() failed: error %d: %sn", WSAGetLastError(), DecodeError(WSAGetLastError()));
    goto DONE;
}
else if (bCount == -1) {
    fprintf(stderr, "Error sending data %dn", errno);
    goto DONE;
}
else {
    printf("Sent bytes %dn", bCount);
}
DONE:
closesocket(sConnect);
printf("All done..n");

您只为绑定到 CodedOutputStream 的 ArrayOut 中使用的缓冲区分配了有效负载大小字节。但是您尝试编写更多内容(有效负载大小 + (Varint32) 的大小)。猜测您需要有足够的缓冲区来存储具有消息长度的有效负载。

我使用 Poco 框架。下面是 c++ 示例代码:

void writeDelimitedTo(const google::protobuf::MessageLite& message, Poco::Net::StreamSocket& socket) {
    int totalMsgSize = message.ByteSize()+4; // Variant32 length 1-4
    char *buf = new char [totalMsgSize];
    ArrayOutputStream aos(buf, totalMsgSize);
    CodedOutputStream coded_output(&aos);
    coded_output.WriteVarint32(message.ByteSize());
    message.SerializeToCodedStream(&coded_output);
    socket.sendBytes(buf, coded_output.ByteCount());
    delete buf;
}

当然,c#部分只是使用System.Io.Stream作为clientStream。 ProtoMsgStructRequest和ProtoMsgStructResponse是ProtoBuf消息:

using (var pbReader = new Google.Protobuf.CodedInputStream(clientStream, true))
{
    var inRequest = new ProtoMsgStructRequest();
    pbReader.ReadMessage(inRequest);
    {
        var outResponse = new ProtoMsgStructResponse();
        using (var pbWriter = new Google.Protobuf.CodedOutputStream(clientStream, true))
        {
            pbWriter.WriteMessage(outResponse);
        }
    }
}