为 protobuf 的 java 实现调用 parseFrom 时程序挂起

发布于 2024-08-31 12:08:33 字数 1458 浏览 4 评论 0原文

我有一条消息(假设它称为 OuterMessage)

message OuterMessage {
    optional int64 id = 1;
    optional InnerMessage inner = 2;
}

和一条内部消息

message InnerMessage {
    optional string type = 1;
    optioanl int64 id = 2;
}

现在,当我像 OuterMessage.parseFrom(buffer) 这样在 OuterMessage 上调用 parseFrom 时,该方法永远不会返回,也不会抛出任何异常。但如果我这样做 InnerMessage.parseFrom(buffer) 它会返回一个空的 InnerMessage 实例。

我正在使用 protobuf-net 序列化消息。知道是什么原因导致这个问题吗?

更新: 我检查了调试器控制台,并抛出了以下异常:

Caused by: java.lang.IllegalArgumentException: Invalid embedded descriptor for "KeyValuePackageResponse.proto".
        at com.google.protobuf.Descriptors$FileDescriptor.internalBuildGeneratedFileFrom(Descriptors.java:286)
        at com.myproj.protobuf.KeyValuePackageResponseProtocol.<clinit>(KeyValuePackageResponseProtocol.java:538)
        ... 15 more
Caused by: com.google.protobuf.Descriptors$DescriptorValidationException: KeyValuePackageResponse.proto: Dependencies passed to FileDescriptor.buildFrom() don't match those listed in the FileDescriptorProto.
        at com.google.protobuf.Descriptors$FileDescriptor.buildFrom(Descriptors.java:231)
        at com.google.protobuf.Descriptors$FileDescriptor.internalBuildGeneratedFileFrom(Descriptors.java:284)

我检查了 java 和 C# 的 .proto 文件。此消息 KeyValuePackageResponseProtocol 也用于许多其他消息,这些消息似乎初始化得很好。为了解决这个问题我应该考虑什么具体的事情?

I have a message (say its called OuterMessage)

message OuterMessage {
    optional int64 id = 1;
    optional InnerMessage inner = 2;
}

and an inner message

message InnerMessage {
    optional string type = 1;
    optioanl int64 id = 2;
}

Now when I call parseFrom on OuterMessage like OuterMessage.parseFrom(buffer) the method never returns and nor does it throw any exception. But if I do InnerMessage.parseFrom(buffer) it returns an empty InnerMessage instance.

I am serializing the message using protobuf-net. Any idea what might be causing the issue?

UPDATE:
I checked the debugger console and there was following exception thrown:

Caused by: java.lang.IllegalArgumentException: Invalid embedded descriptor for "KeyValuePackageResponse.proto".
        at com.google.protobuf.Descriptors$FileDescriptor.internalBuildGeneratedFileFrom(Descriptors.java:286)
        at com.myproj.protobuf.KeyValuePackageResponseProtocol.<clinit>(KeyValuePackageResponseProtocol.java:538)
        ... 15 more
Caused by: com.google.protobuf.Descriptors$DescriptorValidationException: KeyValuePackageResponse.proto: Dependencies passed to FileDescriptor.buildFrom() don't match those listed in the FileDescriptorProto.
        at com.google.protobuf.Descriptors$FileDescriptor.buildFrom(Descriptors.java:231)
        at com.google.protobuf.Descriptors$FileDescriptor.internalBuildGeneratedFileFrom(Descriptors.java:284)

I checked the .proto file at its the same for both java and C#. This message KeyValuePackageResponseProtocol is also used in many other messages which seems to be initialized fine. Any specific thing that I should look at to resolve this issue?

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

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

发布评论

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

评论(1

姐不稀罕 2024-09-07 12:08:33

什么是缓冲区?是一条流吗?字节数组?什么?我问的原因是protobuf(默认情况下)不包含长度标头,因此它读取到流的末尾。如果流保持打开状态(可能是打开的网络套接字),那么它不会知道结束并且会挂起(这可能就是您所看到的)。当然,有一些方法可以在保持流打开的情况下解决这个问题。

如果这不是问题,您能否指示(可能以十六进制)缓冲区内容以及您正在序列化的值?我想弄清楚这是序列化问题还是解析问题......

What is buffer? Is it a stream? a byte-array? What? The reason I ask is that protobuf does not (by default) include length headers, so it reads to the end of the stream. If the stream is left open (perhaps an open network socket) then it won't know to end and will hang (which is possibly what you are seeing). There are ways to get around this while leaving the stream open, of course.

If this isn't the issue, can you indicate (perhaps in hex) the buffer contents, and the values you are serializing? I'd like to figure out whether it is a serialization issue, or a parsing issue...

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