通过Akka IO和套接字的协议缓冲区消息 [英] Protocol Buffer Messages via Akka IO and Sockets

查看:65
本文介绍了通过Akka IO和套接字的协议缓冲区消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了此博客文章,我发现它很棒并且很有启发性,它展示了如何对发送到Akka IO套接字服务器的字符串消息进行定长的定帧。我一直在使用此处称为ScalaBuff 的开源库,它在顶部创建了一个漂亮的薄层协议缓冲区对象。

I came across this blog post, which I found awesome and enlightening, showing how to do fixed-length framing of string messages sent to an Akka IO socket server. I have been working with an open source library I found here called ScalaBuff, which creates a nice thin layer on top of protocol buffer objects.

我遇到的麻烦是改编博客作者的(找不到直接与他联系的链接)代码以占用长度( 4字节序列),然后是protobuf字节数组。我担心以后会弄清楚是哪条消息,现在我只想让代码可以处理一条示例消息。

The trouble I'm having is in adapting the blog author's (couldn't find a link to contact him directly) code to take the length (4-byte sequence) and then the protobuf byte array. I can worry about figuring out which message is on the wire later, right now I just want to get the code to work with one sample message.

我的问题是我将Akka IO代码从提取akka ByteStrings转换为能够发送和提取protobuf消息中的原始字节时遇到了麻烦。这是我缺乏使用Akka IO的套接字服务器的症状。我可以往返于protobuf对象的字节表示形式( Zombie Sighting ),但我只是无法从博客中获取样本来处理字节数组而不是字符串。

My issue is that I am having trouble converting the Akka IO code from pulling akka ByteStrings into being able to send and pull the raw bytes from the protobuf message. This is a symptom of my lack of familiarity with socket servers using Akka IO. I can get to and from the byte representation of my protobuf object (a Zombie Sighting), but I just can't get the sample from the blog to work on byte arrays instead of strings.

如果有人对如何从点A(上面提到的博客文章)到点B(一个发送了A的IO套接字客户端)的建议,一些示例代码或一些输入意见protobuf消息到Akka IO套接字服务器..我想我的客户端在工作..也许),那太好了。

If anybody has some advice, some sample code, or some input on how to get from point A (the blog post mentioned above) to point B (an Akka IO socket client that sends a protobuf message to an Akka IO socket server.. I think I have the client working.. maybe), that would be awesome.

推荐答案

尝试一下:

val myByteArray = myByteString.toArray // converts to an Array[Byte]
val myMessage = MyMessage.defaultInstance.mergeFrom(myByteArray)

MyMessage是使用MyMessage.proto模板由ScalaBuff-compiler生成的类。请注意,Google protobuf库有一个单独的ByteString类,请确保不要将两者混用。

MyMessage is the ScalaBuff-compiler-generated class using your MyMessage.proto template. Note that the Google protobuf library has a separate ByteString class, make sure you don't mix the two.

更新:Kevin的问题已经解决,基本上问题是他使用的是Array [Byte]的toString方法,而不是将字节数组包装在新的String()中,后者可以将字节数组正确转换为String,以便在%s .format调用中使用。

UPDATE: Kevin's problem has been solved, basically the problem was the he was using the toString method of an Array[Byte] instead of wrapping the byte array in a new String(), which correctly converts the byte array to a String, to be used in a "%s".format call.

这篇关于通过Akka IO和套接字的协议缓冲区消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆