开发者

Platform independent protobuf socket protocol - what message will I receive?

I am preparing to write a platform independent socket protocol. After some initial research protobuf seems the way to go. I am new to protobuf and I can't seem to figure out one specific issue.

My requirements are:

  • Completely platform independent client;

  • C# server;

  • Async message communication;

  • Work from a .proto file (so 开发者_Python百科no inference from existing classes);

  • Must be able to send messages without the server/client knowing up front which message type to expect.

I have already found the (De)SerializeWithLengthPrefix methods, and they are a start. What I can't figure out is how to receive a message if I do not know the type up front.

I have already seen that protobuf-net supports inheritance and can detect the type of the message, so for protobuf-net, inheriting all messages from a common base type would work. However, this is not platform independent and I am looking for a platform independent way of doing this.

So my question is: how do I send my messages so that any client can deserialize them without knowing the type up front?


If the requirement is to work with multiple messages of different types:

Just associate a unique number with each different type of message, and use as a prefix to the message; you can do this trivially with the optional integer parameter to the overloaded SerializeWithLengthPrefix method. The client would have to pre-process this prefix (or it can be handled within protobuf-net by Serializer.NonGeneric, which has a deserialization method that provides a callback for obtaining the type from this prefix number).


If the requirement is to work with completely unknown data:

Given you requirements, I suspect Jon's port would be a better fit; since your emphasis is on platform independence and .proto, and not on inference (where protobuf-net excels) or other .NET specific extensions / utilities.

Note that while a .proto can be compiled (via protoc) to a regular protobuf stream, working with entirely foreign data at the receiver is... irregular. It can probably be done by treating everything as extensions or by working with the coded streams, but...


Edit following discussion in comments:

A simple schema here could be simply:

message BaseMessage {
    optional SomeMessage someMessage = 1;
    optional SomeOtherMessage someOtherMessage = 2;
    optional SomeThirdMessage someThirdMessage = 3;
}
message SomeMessage {...}
message SomeOtherMessage {...}
message SomeThirdMessage {...}

(you could optionally add a discriminator, if that helps)

This is actually essentially how protobuf-net treats inheritance, but is easily represented from other clients, and handles everything like lengths of sub-messages automatically.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜