C++ Protobuf implementation in C#

I created the Protobuf files in C++ mentioned below. I want to use exact the same in Protobuf-net. It works fine with normal Protobuf classes, but I can't get it working using extensions in "MainMessage". Can Someone please give me some Code examples?

Here are my C++ .proto files:

File - Messageheader.proto:
message MessageHeader{
required int32 Id = 1;
}

File - MainMessage.proto:
import "Messageheader.proto";

message MainMessage{
required MessageHeader Header = 1;
extensions 100 to 199;
}



File - MessageBool.proto:
import "Messageheader.proto";
import "MainMessage.proto";

message MessageBool{
required bool Successfull = 1;
optional string Information = 2;
    extend MainMessage{
        optional MessageBool Bool = 100;
    }
}

File - MessageLogin.proto:
import "Messageheader.proto";
import "MainMessage.proto";

message MessageLogin{
required string Username = 1;
required string Password = 2;
    extend MainMessage{
        optional MessageLogin Login = 101;
    }
}

The C# program will be the client and a C++ program will be the Server. So the implementation of protobuf should work together.

EDIT: I tried to send a MainMessage which contains a MessageLogin from the C# Client to the C++ Server. The server can deserialize the message kind of successfull, because the header id can be read correct. But the contained MessageLogin is just empty.

You asked for my current attempt and here it is:

[deleted old and wrong sourcecode. Now I have generated .cs files, which are similar to the cpp ones]

So what do I have to correct, so that the C# protobuf messages are equal to the C++ ones?

EDIT2: That semms to have worked, but I still can't read the extension Login message. Current approach for serializing a message:

 //Create the Message
 MainMessage message = new MainMessage
        {
            Header = new MessageHeader
            {
                Id = 1,
            },

        };
        MessageLogin login = new MessageLogin
        {
            Username = username,
            Password = password,
        };
        Extensible.AppendValue<MessageLogin>(message, 101, login);

        string str = ProtoZmq.Serialize(message);


//another file:
public static string Serialize(object protobufMessage)
    {
        string str = "";

        using (MemoryStream stream = new MemoryStream())
        {
            Serializer.Serialize(stream, protobufMessage);
            str = Encoding.ASCII.GetString(stream.GetBuffer());
            //str = Convert.ToBase64String(stream.GetBuffer());
        }

        return str;
    }

And this is how I deserialize it in C++:

    zmq::message_t request;

    // Wait for next request from client
    socket.recv(&request,0);
    std::string rpl = std::string(static_cast<char*>(request.data()), request.size() - 1);
    MainMessage message;
    message.ParseFromString(rpl.data());
    MessageLogin login = message.GetExtension(MessageLogin::Login);
    std::string user = login.username();
    std::string pw = login.password();
    std::cout << message.header().id() << std::endl;
    std::cout << user << std::endl;
    std::cout << pw << std::endl;

The C++ Server always tell me that message has no extension and so user and pw are empty.

链接地址: http://www.djcxy.com/p/42558.html

上一篇: Googles ProtoBuf在C ++上与Protobuf聊天

下一篇: 在C#中使用C ++ Protobuf实现