Netty protobuf and google csharp proto usage
I want to communicate C# client application with Java server application. Java use Netty framework with protobuf pipeline enconder.
My Proto file: import "google/protobuf/csharp_options.proto";
option (google.protobuf.csharp_file_options).namespace = "ChatClient.LoginProtocol";
option (google.protobuf.csharp_file_options).umbrella_classname = "LoginProtocol";
option optimize_for = SPEED;
message Credential {
required string email = 1;
required string password = 2;
}
Netty pipeline codecs:
p.addLast("frameDecoder", new ProtobufVarint32FrameDecoder());
p.addLast("protobufDecoder", new ProtobufDecoder(LoginProtos.Credential.getDefaultInstance()));
p.addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender());
p.addLast("protobufEncoder", new ProtobufEncoder());
So how ca开发者_StackOverflown I send message to java Server. c# code:
stream = client.GetStream();
sReader = new StreamReader(stream);
sWriter = new StreamWriter(stream);
Credential.Builder builder = Credential.CreateBuilder();
builder.SetEmail("xxx@gmail.com").SetPassword("12356");
sWriter.Write(builder.Build().ToByteArray());
sWriter.Flush();
Thanks for help and sorry for my english.
It's probably already too late, but I'm feeling benevolent, so in case anyone has a similar question:
About the framework
Netty simply helps you with the general boiler plate code that goes into setting up a server (any kind of server, since Netty is "transport agnostic", so you could implement it to use TCP/IP, UDP, etc.). There is no pre-defined network protocol to which you must adhere to communicate with your server (that is running using Netty). What effectively defines your protocol is your pipeline. Therefore, from the client's point of view, whether you're using Netty server-side is irrelevant.
About your problem
In your pipeline (assuming that's your server pipeline) you've defined the protobuf encoders and decorders. What they'll do is:
- Server -> Client: assume that what the server is sending is a protobuf
MessageLite
object, decode it to bytes and send these bytes over the wire - Client -> Server: assume that what the client is sending is a series of bytes that can be decoded to a protobuf object, and decode it if possible
So if you've used a Java client to test your server and your server works, then you're pretty much done with your server. All you need to do now, is use a C# library that'll convert objects to protobuf messages, connect to your Java server and send the bytes.
Converting the objects in C# to protobuf messages should be straightforward: use one of the C# protobuf libraries from third party tools on the protobuf project page to do this. Connecting to your server, and sending the protobuf message bytes is something you're going to have to implement yourself. In essence it shouldn't be much harder than creating a socket to your server endpoint, serializing the protobuf message to bytes and sending these bytes via the socket to the server. Hopefully this answers your question.
To summarize: Netty is the Java framework you have used to implement the server. Google protobuf is the framework you have used to define your message(s) (or rather contract(s)). At this point, the one and only precondition for a client to communicate with your server is that the client sends protobuf messages. Netty is irrelevant for the client.
This is probably late too but for anyone with this issue, hope it helps.
As mentioned in the documents for protobuf-csharp-port,
If you want a single .proto file which can generate Java code and C# code, you just need the Java options and the C# options. They don't conflict at all.
https://code.google.com/p/protobuf-csharp-port/wiki/GettingStarted
What you would need to do is make sure the csharp imports are interpreted by your java build tool or vice versa. So, you could include the csharp_options.proto file in your java project; this file comes with the sources of protobuf-csharp-port.
精彩评论