java - 将协议缓冲区编码的消息从 Python 服务器发送到 Java 客户端
问题描述
我正在编写一个使用协议缓冲区对一些数据进行编码的小服务器。
在 Android Client 和 Python Server 之间打开 TCP Socket
Android 客户端发送字符串以作为普通换行符分隔的 utf-8 进行处理。
Python Server 进行一些处理以生成响应,该响应给出一个 Int Arrays 数组:[[int]]。这是在协议缓冲区文件中编码的:
syntax = "proto2";
package tts;
message SentenceContainer {
repeated Sentence sentence = 1;
}
message Sentence {
repeated uint32 phonemeSymbol = 1;
}
它被加载到这个结构中并按如下方式发送......
container = ttsSentences_pb2.SentenceContainer()
for sentence in input_sentences:
phonemes = container.sentence.add()
# Add all the phonemes to the phoneme list
phonemes.phonemeSymbol.extend(processor.text_to_sequence(sentence))
payload = container.SerializeToString()
client.send(payload)
- Android 客户端接收到 Protocol Buffer 编码的消息并尝试解码。
这就是我被困的地方......
# I get the InputStream when the TCP connection is first opened
bufferIn = socket.getInputStream();
TtsSentences.SentenceContainer sentences = TtsSentences.SentenceContainer.parseDelimitedFrom(bufferIn);
收到消息时,客户端会收到此异常:
E/TCP: Server Error
com.google.protobuf.InvalidProtocolBufferException: Protocol message end-group tag did not match expected tag.
at com.google.protobuf.CodedInputStream.checkLastTagWas(CodedInputStream.java:164)
at com.google.protobuf.GeneratedMessageLite.parsePartialDelimitedFrom(GeneratedMessageLite.java:1527)
at com.google.protobuf.GeneratedMessageLite.parseDelimitedFrom(GeneratedMessageLite.java:1496)
at com.tensorspeech.tensorflowtts.TtsSentences$SentenceContainer.parseDelimitedFrom(TtsSentences.java:221)
at com.tensorspeech.tensorflowtts.network.PersistentTcpClient.run(PersistentTcpClient.java:100)
at com.tensorspeech.tensorflowtts.MainActivity.lambda$onCreate$0$MainActivity(MainActivity.java:71)
at com.tensorspeech.tensorflowtts.-$$Lambda$MainActivity$NTUE8bAusaoF3UGkWb7-Jt806BY.run(Unknown Source:2)
at java.lang.Thread.run(Thread.java:919)
我已经知道这个问题是因为协议缓冲区不是自定界的,但我不确定我应该如何正确界定它。我尝试添加换行符client.send(payload + b'\n')
,并将 PB 大小(以字节为单位)添加到 payload 的开头client.send(container.ByteSize().to_bytes(2, 'little') + payload)
,但不知道如何继续。
很遗憾,没有关于如何在 Java 中使用基于 TCP 套接字的协议缓冲区的文档......
解决方案
好的,我解决了这个...
如果您有一个短暂的连接,套接字关闭将表示有效负载的结束,因此不需要额外的逻辑。
就我而言,我有一个长期连接,因此关闭套接字以表示有效负载的结束是行不通的。
使用 Java 客户端和服务器,您可以使用以下方法解决此问题:
MessageLite.writeDelimitedTo(OutputStream)
然后在接收方:
MessageLite.parseDelimitedFrom(InputStream)
.
够简单...
但是在 Python API 中,没有writeDelimitedTo()
函数。因此,我们必须重新创建writeDelimitedTo()
正在做的事情。幸运的是,这很简单。它只是在消息的开头添加一个等于有效负载大小的 _VarintBytes!
client, _ = socket.accept()
payload = your_PB_item.SerializeToString()
size = payload.ByteSize()
client.send(_VarintBytes(size) + payload)
然后在 Java 接收方...
bufferIn = socket.getInputStream();
yourPbItem message;
if ((message = yourPbItem.parseDelimitedFrom(bufferIn)) != null) {
// Do stuff :)
}
这样,您的协议缓冲区库确切地知道要读取多少字节,然后停止关心 InputStream,而不是无限期地坐着听。
推荐阅读
- python - python3标准应用引擎项目中如何访问谷歌云库?
- flutter - 命名路由如何在flutter web中具有URL参数?
- go - 使用客户来源时,Stripe Charges 失败
- hibernate - 为什么 Envers 在阅读 REVINFO 后会删除新记录
- python - DropzoneJS - progressBar 不等待进程结束
- yosys - suprove 求解器的崩溃/问题
- python-sphinx - 是否可以使用 sphinx 使用 :class: 角色链接到特定的 URL?
- docusignapi - 通过docusign rest api中的pdf表单字段转换将签名者分配给Radio组
- probability - 概率事件 - 事件发生的总概率
- database - 包装在数据库事务中的业务逻辑