开发者

Protocol design

I have a client server program to communicate the pc with an android phone using usb, based on http://www.anothem.net/archives/2开发者_开发知识库010/10/15/android-usb-connection-to-pc/

Now I need to design a protocol that facilitates information exchange from the pc to the phone. How should I go about this ? Should we use XML for this? I was looking at google protocol buffer.. Is that the right direction ?

Thanks Caroline


I've been using protocol buffers on Android. I avoided it for a long time, reasoning that it was overkill. That was before I took half a day to try it out.

Here were my results, after spending an afternoon on it. At first it increased my APK size from 2.89megs to 3.1 megs. I consider that inconsequential. Then I found that I was able to delete code I had all over the place, where I had been doing parsing manually. I was able to delete code that required the data to come in in a specific sequence.

Then I was able to completely delete a few classes I had in my application, whose purpose was to serve as temporary, lightweight information-only "model" classes that represented data coming to or from the data stream. In short, it started making things way easier and smaller, and more reliable.

Protocol Buffers may well be not the best thing for your situation. But I do recommend that you take a few hours and try it out. That way you'll be making your decision to leave it behind from a position of strength and knowledge.

Be sure to use the lite version of protocol buffers. The .jar file is 160k, but the amount of functionality it brings is huge. I'll be using it all the time from here on out.

I do have some concerns about dynamic memory allocation / garbage collection when using it in a game context long term. But for now the serialization happens infrequently enough that it's a non-issue for me.

Another bonus: I have some Python code that generates data files that the application reads. That python code processes some XML and then generates binary files. I think I'll be able to completely eliminate that code by using protocol buffer's text mode, then using protoc directly to create binary files.


You need first to design the higher layer of your protocol. Xml and protocol buffers have to do with how the data are formatted. Depending on the data you need to exchange they may or not be suitable. From what you way in your comment, it seems that you want to develop something like a remote control for your PC. In that case both XML and Google Protocol Buffers will be an overdesign. Simply create a text protocol, allocate a byte or two for the command type and some bytes for the data body.

Have a look at the AT commands structure. The structure of an HDLC frame could also give you some ideas. This has some things like error checking, which you don't need, but other than that a simple text protocol will use similar fields.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜