Netty protobuf 和 google csharp proto 使用

发布于 2024-11-05 11:59:53 字数 1280 浏览 2 评论 0原文

我想将 C# 客户端应用程序与 Java 服务器应用程序进行通信。 Java 使用带有 protobuf 管道编码器的 Netty 框架。

我的原型文件: 导入“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管道编解码器:

p.addLast("frameDecoder", new ProtobufVarint32FrameDecoder());
p.addLast("protobufDecoder", new   ProtobufDecoder(LoginProtos.Credential.getDefaultInstance()));

    p.addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender());
    p.addLast("protobufEncoder", new ProtobufEncoder());

那么我如何将消息发送到java服务器。 c# 代码:

stream = client.GetStream();
sReader = new StreamReader(stream);
sWriter = new StreamWriter(stream);

Credential.Builder builder = Credential.CreateBuilder();

 builder.SetEmail("[email protected]").SetPassword("12356");

 sWriter.Write(builder.Build().ToByteArray());

 sWriter.Flush();

感谢您的帮助,并对我的英语表示抱歉。

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 can 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("[email protected]").SetPassword("12356");

 sWriter.Write(builder.Build().ToByteArray());

 sWriter.Flush();

Thanks for help and sorry for my english.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盛夏尉蓝 2024-11-12 11:59:53

可能已经太晚了,但我感觉很仁慈,所以如果有人有类似的问题:

关于框架

Netty 只是帮助您设置服务器的一般样板代码(任何 一种服务器,因为 Netty 是“传输无关的”,所以你可以实现它以使用 TCP/IP、UDP 等)。 没有预定义的网络协议,您必须遵守该协议才能与服务器(使用 Netty 运行)进行通信。有效定义您的协议的是您的管道。因此,从客户端的角度来看,是否使用 Netty 服务器端并不重要。

关于您的问题

在您的管道中(假设这是您的服务器管道),您已经定义了 protobuf 编码器和解码器。他们要做的是:

  • 服务器 ->客户端:假设服务器发送的是 protobuf MessageLite 对象,将其解码为字节并通过线路发送这些字节
  • 客户端 ->服务器:假设客户端发送的是一系列可以解码为protobuf对象的字节,如果可能的话就解码它

所以如果你使用Java客户端来测试你的服务器并且你的服务器工作,那么你就是您的服务器基本完成了。您现在需要做的就是使用 C# 库将对象转换为 protobuf 消息,连接到 Java 服务器并发送字节。

将 C# 中的对象转换为 protobuf 消息应该很简单:使用 第三个中的 C# protobuf 库之一protobuf 项目页面上的 party 工具 可以执行此操作。连接到服务器并发送 protobuf 消息字节是您必须自己实现的事情。从本质上讲,它应该不会比创建到服务器端点的套接字、将 protobuf 消息序列化为字节并通过套接字将这些字节发送到服务器难多少。希望这能回答您的问题。

总结:Netty 是您用来实现服务器的Java 框架。 Google protobuf 是您用来定义消息(或者更确切地说合约)的框架。此时,客户端与服务器通信的唯一前提条件是客户端发送 protobuf 消息。 Netty 与客户端无关。

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.

甜尕妞 2024-11-12 11:59:53

这可能也太晚了,但对于遇到此问题的任何人来说,希望它有所帮助。

正如 protobuf-csharp-port 的文档中提到的,

如果您想要一个可以生成 Java 代码和 C# 代码的 .proto 文件,您只需要 Java 选项和 C# 选项。它们根本不冲突。

https://code.google.com/p/protobuf-csharp-port /wiki/GettingStarted

您需要做的是确保 csharp 导入由您的 java 构建工具解释,反之亦然。因此,您可以将 csharp_options.proto 文件包含在您的 java 项目中;该文件附带 protobuf-csharp-port 的源。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文